29
29
package net .sf .seesea .data .io .postgis ;
30
30
31
31
import java .sql .Connection ;
32
- import java .sql .DriverManager ;
33
32
import java .sql .PreparedStatement ;
34
33
import java .sql .SQLException ;
35
34
import java .sql .Timestamp ;
40
39
import java .util .List ;
41
40
import java .util .Map ;
42
41
42
+ import javax .sql .DataSource ;
43
+
44
+ import org .osgi .service .component .annotations .Component ;
45
+ import org .osgi .service .component .annotations .ConfigurationPolicy ;
46
+ import org .osgi .service .component .annotations .Reference ;
47
+ import org .osgi .service .component .annotations .ReferenceCardinality ;
48
+ import org .osgi .service .component .annotations .ReferencePolicy ;
49
+
43
50
import net .sf .seesea .data .io .IDataWriter ;
44
51
import net .sf .seesea .data .io .WriterException ;
45
52
import net .sf .seesea .model .core .geo .Depth ;
48
55
import net .sf .seesea .model .core .physx .Measurement ;
49
56
50
57
/**
58
+ *
59
+ * This class writes data points to multiple tables
51
60
*
52
61
* Schema to write to is;
53
62
* <pre>
64
73
* </pre>
65
74
*
66
75
*/
76
+ @ Component (configurationPolicy = ConfigurationPolicy .REQUIRE , property = "type=postgis" )
67
77
public class PostInsertGISWriter implements IDataWriter {
68
78
69
- private Connection connection ;
70
79
private List <PreparedStatement > insertStatements ;
71
80
private int batchSizeCounter ;
72
81
private List <String > tableNames ;
73
82
private Map <Integer , Long > counters ;
83
+ private DataSource outputDataSource ;
84
+ private Connection connection ;
74
85
75
- public PostInsertGISWriter (String url , String user , String password , List <String > tables ) throws WriterException {
76
- try {
77
- insertStatements = new ArrayList <PreparedStatement >(tables .size ());
78
- connection = DriverManager .getConnection (url , user , password );
79
- // DriverWrapper.addGISTypes(((PGConnection)connection));
80
- this .tableNames = tables ;
81
- prepareInsertStatement ();
82
- batchSizeCounter = 0 ;
83
- counters = new HashMap <Integer ,Long >();
84
- for (int i = 0 ; i < tables .size (); i ++) {
85
- counters .put (i , 100L );
86
- }
87
- } catch (SQLException e ) {
88
- throw new WriterException ("Could not create PostGIS Writer" , e ); //$NON-NLS-1$
86
+ public void activate (Map <String , Object > properties ) throws SQLException {
87
+ tableNames = (List <String >) properties .get ("outputTables" );
88
+ connection = outputDataSource .getConnection ();
89
+ insertStatements = new ArrayList <PreparedStatement >(tableNames .size ());
90
+ prepareInsertStatement ();
91
+ batchSizeCounter = 0 ;
92
+ counters = new HashMap <Integer ,Long >();
93
+ for (int i = 0 ; i < tableNames .size (); i ++) {
94
+ counters .put (i , 100L );
89
95
}
90
96
}
91
-
92
-
97
+
98
+ public void deactivate (Map <String , Object > properties ) throws SQLException {
99
+ connection .close ();
100
+ }
101
+
93
102
94
103
private void prepareInsertStatement () throws SQLException {
95
- for (int i = 0 ; i < tableNames .size () ; i ++) {
96
- String insert = null ;
97
- if (i == 0 ) {
98
- insert = "INSERT INTO " //$NON-NLS-1$
99
- + tableNames .get (i )
100
- + " (lat, lon , dbs, datasetid, valid, recordingdate, latvar, lonvar, depthvar, the_geom) VALUES (?::numeric(8,6), ?::numeric(11,8), ?::numeric(8,2), ?, ?, ?, ?, ?, ?, ST_SetSRID(ST_MakePoint(?, ?), 4326))" ; //$NON-NLS-1$
101
- } else {
102
- insert = "INSERT INTO " //$NON-NLS-1$
103
- + tableNames .get (i )
104
- + " (lat, lon , dbs, datasetid, valid, recordingdate, the_geom) VALUES (?::numeric(8,6), ?::numeric(11,8), ?::numeric(8,2), ?, ?, ?, ST_SetSRID(ST_MakePoint(?, ?), 4326))" ; //$NON-NLS-1$
104
+ for (int i = 0 ; i < tableNames .size () ; i ++) {
105
+ String insert = null ;
106
+ if (i == 0 ) {
107
+ insert = "INSERT INTO " //$NON-NLS-1$
108
+ + tableNames .get (i )
109
+ + " (lat, lon , dbs, datasetid, valid, recordingdate, latvar, lonvar, depthvar, the_geom) VALUES (?::numeric(8,6), ?::numeric(11,8), ?::numeric(8,2), ?, ?, ?, ?, ?, ?, ST_SetSRID(ST_MakePoint(?, ?), 4326))" ; //$NON-NLS-1$
110
+ } else {
111
+ insert = "INSERT INTO " //$NON-NLS-1$
112
+ + tableNames .get (i )
113
+ + " (lat, lon , dbs, datasetid, valid, recordingdate, the_geom) VALUES (?::numeric(8,6), ?::numeric(11,8), ?::numeric(8,2), ?, ?, ?, ST_SetSRID(ST_MakePoint(?, ?), 4326))" ; //$NON-NLS-1$
114
+ }
115
+ PreparedStatement prepareStatement = connection .prepareStatement (insert );
116
+ insertStatements .add (prepareStatement );
105
117
}
106
- PreparedStatement prepareStatement = connection .prepareStatement (insert );
107
- insertStatements .add (prepareStatement );
108
- }
109
118
}
110
119
111
120
@@ -116,12 +125,9 @@ public void closeOutput() throws WriterException {
116
125
for (PreparedStatement insertStatement : insertStatements ) {
117
126
insertStatement .executeBatch ();
118
127
}
119
- connection .close ();
120
128
} catch (SQLException e ) {
121
129
throw new WriterException (e );
122
- } finally {
123
130
}
124
- connection = null ;
125
131
}
126
132
127
133
@@ -148,7 +154,11 @@ public void write(Collection<Measurement> measurements, boolean valid, long sour
148
154
}
149
155
if (geoPosition != null && depth != null ) {
150
156
if (valid && (float )depth .getDepth () > 0.01 ) {
151
- write (geoPosition .getLatitude ().getDecimalDegree (), geoPosition .getLongitude ().getDecimalDegree (), depth .getDepth (), sourceTrackIdentifier , 0 , 0 , 0 , geoPosition .getTime ());
157
+ double latitude = geoPosition .getLatitude ().getDecimalDegree ();
158
+ double longitude = geoPosition .getLongitude ().getDecimalDegree ();
159
+ double depth2 = depth .getDepth ();
160
+ Date time = geoPosition .getTime ();
161
+ write (latitude , longitude , depth2 , sourceTrackIdentifier , 0 , 0 , 0 , time );
152
162
}
153
163
}
154
164
}
@@ -223,7 +233,15 @@ public void write(double lat, double lon, double depth, long sourceTrackIdentifi
223
233
} catch (SQLException e ) {
224
234
throw new WriterException ("Failed to insert data into table" , e ); //$NON-NLS-1$
225
235
}
236
+ }
237
+
238
+ @ Reference (cardinality = ReferenceCardinality .MANDATORY , policy = ReferencePolicy .DYNAMIC , target = "(db=depth)" )
239
+ public synchronized void bindOutputConnection (DataSource outputDataSource ) {
240
+ this .outputDataSource = outputDataSource ;
241
+ }
226
242
243
+ public synchronized void unbindOutputConnection (DataSource outputDataSource ) {
244
+ this .outputDataSource = null ;
227
245
}
228
246
229
247
}
0 commit comments