Skip to content

Commit 3519151

Browse files
author
cleanerx
committed
F: Proceed in setting up filtering
1 parent bf8298b commit 3519151

25 files changed

+734
-601
lines changed

net.sf.seesea.data.io.postgis/META-INF/MANIFEST.MF

+8-7
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,18 @@ Bundle-ClassPath: .
77
Bundle-Activator: net.sf.seesea.data.io.postgis.PostgisIOActivator
88
Bundle-Vendor: SeeSea
99
Export-Package: net.sf.seesea.data.io.postgis
10-
Require-Bundle: org.eclipse.core.runtime,
11-
net.sf.seesea.data.io,
12-
org.apache.log4j;bundle-version="[1.2.0,2.0.0)",
13-
net.sf.seesea.geometry,
14-
net.sf.seesea.model.core,
10+
Require-Bundle: org.eclipse.core.runtime,
11+
net.sf.seesea.data.io,
12+
org.apache.log4j;bundle-version="[1.2.0,2.0.0)",
13+
net.sf.seesea.geometry,
14+
net.sf.seesea.model.core,
1515
org.postgresql.jdbc41;bundle-version="9.4.1208"
16-
Import-Package: org.osgi.service.component;version="1.2.1",
17-
org.osgi.service.component.annotations,
16+
Import-Package: org.osgi.service.component;version="1.2.1",
17+
org.osgi.service.component.annotations,
1818
org.osgi.service.jdbc;version="[1.0.0,2.0.0)"
1919
Service-Component: OSGI-INF/net.sf.seesea.data.io.postgis.PartitionRetrival.xml,
2020
OSGI-INF/net.sf.seesea.data.io.postgis.PostGISWriterFactory.xml,
21+
OSGI-INF/net.sf.seesea.data.io.postgis.PostInsertGISWriter.xml,
2122
OSGI-INF/net.sf.seesea.data.io.postgis.PostgresDatasourceConfiguration.xml
2223
Bundle-ActivationPolicy: lazy
2324
Bundle-RequiredExecutionEnvironment: JavaSE-1.7

net.sf.seesea.data.io.postgis/OSGI-INF/net.sf.seesea.data.io.postgis.PostGISWriterFactory.xml

+1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@
66
<service>
77
<provide interface="net.sf.seesea.data.io.IWriterFactory"/>
88
</service>
9+
<reference name="Writer" interface="net.sf.seesea.data.io.IDataWriter" cardinality="1..1" policy="dynamic" target="(type=postgis)" bind="bindWriter" unbind="unbindWriter"/>
910
</scr:component>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<!--Generated by DS Annotation Builder [src/net/sf/seesea/data/io/postgis/PostInsertGISWriter.java]-->
3+
<scr:component name="net.sf.seesea.data.io.postgis.PostInsertGISWriter" configuration-policy="require" xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0">
4+
<property name="type" value="postgis"/>
5+
<service>
6+
<provide interface="net.sf.seesea.data.io.IDataWriter"/>
7+
</service>
8+
<reference name="OutputConnection" interface="javax.sql.DataSource" cardinality="1..1" policy="dynamic" target="(db=depth)" bind="bindOutputConnection" unbind="unbindOutputConnection"/>
9+
<implementation class="net.sf.seesea.data.io.postgis.PostInsertGISWriter"/>
10+
</scr:component>

net.sf.seesea.data.io.postgis/src/net/sf/seesea/data/io/postgis/PostGISWriterFactory.java

+14-24
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,10 @@
2828

2929
package net.sf.seesea.data.io.postgis;
3030

31-
import java.util.Arrays;
32-
import java.util.HashMap;
33-
import java.util.Map;
34-
3531
import org.osgi.service.component.annotations.Component;
32+
import org.osgi.service.component.annotations.Reference;
33+
import org.osgi.service.component.annotations.ReferenceCardinality;
34+
import org.osgi.service.component.annotations.ReferencePolicy;
3635

3736
import net.sf.seesea.data.io.IDataWriter;
3837
import net.sf.seesea.data.io.IWriterFactory;
@@ -41,29 +40,20 @@
4140
@Component(property = "type=postgis")
4241
public class PostGISWriterFactory implements IWriterFactory {
4342

44-
public PostGISWriterFactory() throws ClassNotFoundException {
45-
Class.forName ("org.postgresql.Driver"); //$NON-NLS-1$
46-
}
43+
private IDataWriter writer;
4744

4845
@Override
4946
public IDataWriter createWriter() throws WriterException {
50-
Map<String,Object> parameters = new HashMap<String, Object>();
51-
String user = (String) parameters.get("user"); //$NON-NLS-1$
52-
String password = (String) parameters.get("password"); //$NON-NLS-1$
53-
String host = (String) parameters.get("host"); //$NON-NLS-1$
54-
if(host == null || host.isEmpty()) {
55-
host = "localhost"; //$NON-NLS-1$
56-
}
57-
String port = (String) parameters.get("port"); //$NON-NLS-1$
58-
if(port == null || port.isEmpty()) {
59-
port = "5432"; //$NON-NLS-1$
60-
}
61-
String database = (String) parameters.get("outputDatabase"); //$NON-NLS-1$
62-
String table = (String) parameters.get("outputTable"); //$NON-NLS-1$
63-
String[] outputTables = table.split(","); //$NON-NLS-1$
64-
65-
String url = "jdbc:postgresql:" + (host != null ? ("//" + host) + (port != null ? ":" + port : "") + "/" : "") + database; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
66-
return new PostInsertGISWriter(url, user, password,Arrays.asList(outputTables));
47+
return writer;
48+
}
49+
50+
@Reference(cardinality = ReferenceCardinality.MANDATORY, policy = ReferencePolicy.DYNAMIC, target = "(type=postgis)")
51+
public synchronized void bindWriter(IDataWriter writer) {
52+
this.writer = writer;
53+
}
54+
55+
public synchronized void unbindWriter(IDataWriter writer) {
56+
this.writer = null;
6757
}
6858
}
6959

net.sf.seesea.data.io.postgis/src/net/sf/seesea/data/io/postgis/PostInsertGISWriter.java

+53-35
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
package net.sf.seesea.data.io.postgis;
3030

3131
import java.sql.Connection;
32-
import java.sql.DriverManager;
3332
import java.sql.PreparedStatement;
3433
import java.sql.SQLException;
3534
import java.sql.Timestamp;
@@ -40,6 +39,14 @@
4039
import java.util.List;
4140
import java.util.Map;
4241

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+
4350
import net.sf.seesea.data.io.IDataWriter;
4451
import net.sf.seesea.data.io.WriterException;
4552
import net.sf.seesea.model.core.geo.Depth;
@@ -48,6 +55,8 @@
4855
import net.sf.seesea.model.core.physx.Measurement;
4956

5057
/**
58+
*
59+
* This class writes data points to multiple tables
5160
*
5261
* Schema to write to is;
5362
* <pre>
@@ -64,48 +73,48 @@
6473
* </pre>
6574
*
6675
*/
76+
@Component(configurationPolicy = ConfigurationPolicy.REQUIRE, property = "type=postgis")
6777
public class PostInsertGISWriter implements IDataWriter {
6878

69-
private Connection connection;
7079
private List<PreparedStatement> insertStatements;
7180
private int batchSizeCounter;
7281
private List<String> tableNames;
7382
private Map<Integer, Long> counters;
83+
private DataSource outputDataSource;
84+
private Connection connection;
7485

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);
8995
}
9096
}
91-
92-
97+
98+
public void deactivate(Map<String, Object> properties) throws SQLException {
99+
connection.close();
100+
}
101+
93102

94103
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);
105117
}
106-
PreparedStatement prepareStatement = connection.prepareStatement(insert);
107-
insertStatements.add(prepareStatement);
108-
}
109118
}
110119

111120

@@ -116,12 +125,9 @@ public void closeOutput() throws WriterException {
116125
for (PreparedStatement insertStatement : insertStatements) {
117126
insertStatement.executeBatch();
118127
}
119-
connection.close();
120128
} catch (SQLException e) {
121129
throw new WriterException(e);
122-
} finally {
123130
}
124-
connection = null;
125131
}
126132

127133

@@ -148,7 +154,11 @@ public void write(Collection<Measurement> measurements, boolean valid, long sour
148154
}
149155
if(geoPosition != null && depth != null) {
150156
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);
152162
}
153163
}
154164
}
@@ -223,7 +233,15 @@ public void write(double lat, double lon, double depth, long sourceTrackIdentifi
223233
} catch (SQLException e) {
224234
throw new WriterException("Failed to insert data into table", e); //$NON-NLS-1$
225235
}
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+
}
226242

243+
public synchronized void unbindOutputConnection(DataSource outputDataSource) {
244+
this.outputDataSource = null;
227245
}
228246

229247
}

net.sf.seesea.data.io.postgis/src/net/sf/seesea/data/io/postgis/PostgresDatasourceConfiguration.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ public void activate(ComponentContext context) {
4141
Properties dbProps = new Properties();
4242
dbProps.put(DataSourceFactory.JDBC_DATABASE_NAME, properties.get("dbname"));
4343
dbProps.put(DataSourceFactory.JDBC_USER, properties.get("user"));
44-
dbProps.put(DataSourceFactory.JDBC_PASSWORD, properties.get("password"));
44+
if(properties.get("password") != null) {
45+
dbProps.put(DataSourceFactory.JDBC_PASSWORD, properties.get("password"));
46+
}
4547
dbProps.put(DataSourceFactory.JDBC_SERVER_NAME, properties.get("server"));
4648
dbProps.put(DataSourceFactory.JDBC_PORT_NUMBER, properties.get("port"));
4749
// dbProps.put("db", properties.get("db"));

net.sf.seesea.data.postprocessing.system.test/META-INF/MANIFEST.MF

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ Require-Bundle: org.eclipse.core.runtime,
1111
org.junit,
1212
net.sf.seesea.content.api,
1313
org.eclipse.equinox.cm;bundle-version="1.1.100",
14-
org.eclipse.equinox.ds;bundle-version="1.4.300"
14+
org.eclipse.equinox.ds;bundle-version="1.4.300",
15+
net.sf.seesea.filter.api;bundle-version="1.0.0"
1516
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
1617
Bundle-ActivationPolicy: lazy
1718
Import-Package: org.osgi.service.jdbc

0 commit comments

Comments
 (0)