Commit d06b334b authored by PrabithaPrabhakaran's avatar PrabithaPrabhakaran
Browse files

Add SensorData imp and exp

parent 83e7fafd
......@@ -5,6 +5,7 @@ build/
!**/src/test/**/build/
### IntelliJ IDEA ###
.idea/
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
......@@ -39,4 +40,4 @@ bin/
.vscode/
### Mac OS ###
.DS_Store
\ No newline at end of file
.DS_Store
......@@ -9,10 +9,7 @@ import org.citydb.core.operation.exporter.CityGMLExportException;
import org.citydb.core.query.filter.projection.ProjectionFilter;
import org.citygml4j.ade.energy3.model.core.*;
import org.citygml4j.ade.energy3.model.module.EnergyADEModule;
import org.citygml4j.ade.energy3.model.supportingClasses.AbstractADEFeature;
import org.citygml4j.ade.energy3.model.supportingClasses.AbstractADEFeatureProperty;
import org.citygml4j.ade.energy3.model.supportingClasses.AbstractFeatureWithLifeSpan;
import org.citygml4j.ade.energy3.model.supportingClasses.AbstractFeatureWithLifeSpanProperty;
import org.citygml4j.ade.energy3.model.supportingClasses.*;
import org.citygml4j.model.citygml.core.AbstractCityObject;
import org.citygml4j.model.gml.geometry.primitives.PointProperty;
......@@ -31,6 +28,7 @@ public class CityObjectPropertiesExporter implements ADEExporter {
private final Connection connection;
//private WeatherDataExporter weatherDataExporter;
private ResourceExporter resourceExporter;
private SensorDataExporter sensorDataExporter;
private AbstractADEFeatureExporter adeFeatureExporter;
private AbstractFeatureWithLifeSpanExporter featureWithLifeSpanExporter;
private String module;
......@@ -71,6 +69,12 @@ public class CityObjectPropertiesExporter implements ADEExporter {
cityObject.addGenericApplicationPropertyOfCityObject(property);
}
}
if (projectionFilter.containsProperty("sensorData", module)) {
for (SensorData sensorData : sensorDataExporter.doExport(objectId)) {
SensorDataProperty property = new SensorDataProperty(sensorData);
cityObject.addGenericApplicationPropertyOfCityObject(property);
}
}
if (projectionFilter.containsProperty("abstractAdeFeature", module)) {
for (AbstractADEFeature feature : adeFeatureExporter.doExport(objectId)) {
AbstractADEFeatureProperty property = new AbstractADEFeatureProperty(feature);
......
package de.stuttgart.hft.ade.energy3.exporter;
import de.stuttgart.hft.ade.energy3.schema.ADETable;
import org.citydb.config.geometry.GeometryObject;
import org.citydb.core.ade.exporter.ADEExporter;
import org.citydb.core.ade.exporter.CityGMLExportHelper;
import org.citydb.core.operation.exporter.CityGMLExportException;
import org.citygml4j.ade.energy3.model.supportingClasses.*;
import org.citygml4j.model.gml.geometry.primitives.PointProperty;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
public class SensorDataExporter implements ADEExporter {
private final CityGMLExportHelper helper;
private TimeSeriesExporter timeSeriesExporter;
private PreparedStatement ps;
public SensorDataExporter(Connection connection, CityGMLExportHelper helper, ExportManager manager)
throws CityGMLExportException, SQLException {
this.helper = helper;
String tableName = helper.getTableNameWithSchema(manager.getSchemaMapper().getTableName(ADETable.SENSOR_DATA));
String sql = "SELECT " + getColumnNames() + " FROM " + tableName + " WHERE cityobject_id = ?";
ps = connection.prepareStatement(sql);
timeSeriesExporter = manager.getExporter(TimeSeriesExporter.class);
}
public SensorDataExporter(CityGMLExportHelper helper) {
this.helper = helper;
}
public Collection<SensorData> doExport(long cityObjectId) throws CityGMLExportException, SQLException {
ps.setLong(1, cityObjectId);
try (ResultSet rs = ps.executeQuery()) {
Collection<SensorData> result = new ArrayList<>();
SensorData sensorData = null;
while (rs.next()) {
int id = rs.getInt(1);
int objectClassId = rs.getInt(2);
sensorData = helper.createObject(id, objectClassId, SensorData.class);
String type = rs.getString(3);
String typeCodespace = rs.getString(4);
sensorData.getType().setValue(type);
sensorData.getType().setCodeSpace(typeCodespace);
String valueType = rs.getString(4);
String valueTypeCodespace = rs.getString(5);
sensorData.getValueType().setValue(valueType);
sensorData.getValueType().setCodeSpace(valueTypeCodespace);
Double yearlyValue = rs.getObject(6) != null ? rs.getDouble(6) : null;
String yearlyValueUom = rs.getString(7);
sensorData.getYearlyValue().setValue(yearlyValue);
sensorData.getYearlyValue().setUom(yearlyValueUom);
Object pointObj = rs.getObject("position");
if (pointObj != null) {
GeometryObject point = helper.getDatabaseAdapter().getGeometryConverter().getPoint(pointObj);
if (point != null) {
PointProperty pointProperty = new PointProperty();
// pointProperty.setPoint(point);
sensorData.setPosition(pointProperty);
}
}
}
return result;
}
}
@Override
public void close() throws CityGMLExportException, SQLException {
ps.close();
}
private String getColumnNames() {
return "id, objectclass_id, type, type_codespace, value_type, value_type_codespace, " +
"yearly_value, yearly_value_uom, time_series_id, cityobject_id, position";
}
}
......@@ -30,6 +30,9 @@
//import java.util.ArrayList;
//import java.util.Collection;
//
/*
Not a part of beta 8
*/
//public class WeatherDataExporter implements ADEExporter {
//
// private final CityGMLExportHelper helper;
......@@ -180,4 +183,4 @@
// public void close() throws CityGMLExportException, SQLException {
// ps.close();
// }
//}
\ No newline at end of file
//}
......@@ -121,6 +121,19 @@ public class CityObjectPropertiesImporter implements ADEImporter {
}
}
}
if (properties.contains(SensorDataProperty.class)) {
for (SensorDataProperty propertyElement : properties.getAll(SensorDataProperty.class)) {
SensorData sensorData = propertyElement.getSensorData();
if (sensorData != null) {
helper.importObject(sensorData, ForeignKeys.create().with("cityObjectId", parentId));
propertyElement.unsetSensorData();
} else {
String href = propertyElement.getHref();
if (href != null && href.length() != 0)
helper.logOrThrowUnsupportedXLinkMessage(parent, SensorData.class, href);
}
}
}
}
@Override
......
......@@ -151,7 +151,8 @@ public class ResourceImporter implements ADEImporter {
helper.executeBatch(objectType);
}
private void setAbstractResourceProperties(AbstractResource abstractResource) throws SQLException, CityGMLImportException {
private void setAbstractResourceProperties(AbstractResource abstractResource)
throws SQLException, CityGMLImportException {
if (abstractResource.getStatus() != null) {
ps.setString(7, abstractResource.getStatus().value());
......@@ -172,31 +173,42 @@ public class ResourceImporter implements ADEImporter {
ps.setString(14, abstractResource.getAmount().getUom());
}
if (abstractResource.isSetTimeDependentAmount()){
long timeSeriesId = helper.importObject(abstractResource.getTimeDependentAmount().getAbstractTimeSeries(), null);
ps.setLong(15, timeSeriesId);
if (abstractResource.isSetTimeDependentAmount()) {
long timeSeriesId = helper.importObject(
abstractResource.getTimeDependentAmount().getAbstractTimeSeries(), null);
ps.setLong(35, timeSeriesId);
}
if (abstractResource.isSetTimeDependentCosts()) {
long timeSeriesId = helper.importObject(
abstractResource.getTimeDependentCosts().getAbstractTimeSeries(), null);
ps.setLong(36, timeSeriesId);
}
if (abstractResource.isSetTimeDependentYields()) {
long timeSeriesId = helper.importObject(
abstractResource.getTimeDependentYields().getAbstractTimeSeries(), null);
ps.setLong(37, timeSeriesId);
}
// Handling boolean-to-numeric conversion
if (abstractResource.isSetAmountNormalized()) {
ps.setInt(16, 1); // True -> 1
ps.setBoolean(15, true);
} else {
ps.setInt(16, 0);
ps.setBoolean(15, false);
}
if (abstractResource.getNormalizationValue() != null) {
ps.setDouble(18, abstractResource.getNormalizationValue().getValue());
ps.setString(19, abstractResource.getNormalizationValue().getUom());
if (abstractResource.getNormalizationParameter() != null) {
ps.setString(16, abstractResource.getNormalizationParameter());
}
if (abstractResource.getNormalizationParameter() != null) {
ps.setString(17, abstractResource.getNormalizationParameter());
if (abstractResource.getNormalizationValue() != null) {
ps.setDouble(17, abstractResource.getNormalizationValue().getValue());
ps.setString(18, abstractResource.getNormalizationValue().getUom());
}
if (abstractResource.getCo2Equivalent() != null) {
ps.setDouble(20, abstractResource.getCo2Equivalent().getValue());
ps.setString(21, abstractResource.getCo2Equivalent().getUom());
ps.setDouble(21, abstractResource.getCo2Equivalent().getValue());
ps.setString(22, abstractResource.getCo2Equivalent().getUom());
}
if (abstractResource.getYear() != null) {
......@@ -204,52 +216,64 @@ public class ResourceImporter implements ADEImporter {
}
if (abstractResource.getCostsMoney() != null) {
ps.setDouble(22, abstractResource.getCostsMoney().getValue());
ps.setString(23, abstractResource.getCostsMoney().getUom());
ps.setDouble(23, abstractResource.getCostsMoney().getValue());
ps.setString(24, abstractResource.getCostsMoney().getUom());
}
if (abstractResource.getYieldsMoney() != null) {
ps.setDouble(24, abstractResource.getYieldsMoney().getValue());
ps.setString(25, abstractResource.getYieldsMoney().getUom());
ps.setDouble(25, abstractResource.getYieldsMoney().getValue());
ps.setString(26, abstractResource.getYieldsMoney().getUom());
}
if (abstractResource.getReferencePeriodValue() != null) {
ps.setString(19, abstractResource.getReferencePeriodValue().getValue());
ps.setString(20, abstractResource.getReferencePeriodValue().getCodeSpace());
}
}
private void setResourceToNull() throws SQLException {
ps.setNull(1, Types.BIGINT); // id
ps.setNull(2, Types.INTEGER); // objectclass_id
ps.setNull(3, Types.VARCHAR); // type
ps.setNull(4, Types.VARCHAR); // type_codespace
ps.setNull(5, Types.VARCHAR); // enduse
ps.setNull(6, Types.VARCHAR); // enduse_codespace
ps.setNull(7, Types.VARCHAR); // status
ps.setNull(8, Types.VARCHAR); // operation_type
ps.setNull(9, Types.VARCHAR); // operation_type_codespace
ps.setNull(10, Types.INTEGER); // year
ps.setNull(11, Types.VARCHAR); // amount_type
ps.setNull(12, Types.VARCHAR); // amount_type_codespace
ps.setNull(13, Types.NUMERIC); // amount
ps.setNull(14, Types.VARCHAR); // amount_uom
ps.setNull(15, Types.BIGINT); // time_series_id
ps.setNull(16, Types.NUMERIC); // is_amount_normalized
ps.setNull(17, Types.VARCHAR); // normalization_param
ps.setNull(18, Types.NUMERIC); // normalization_value
ps.setNull(19, Types.VARCHAR); // normalization_value_uom
ps.setNull(20, Types.NUMERIC); // co2_equivalent
ps.setNull(21, Types.VARCHAR); // co2_equivalent_uom
ps.setNull(22, Types.NUMERIC); // costs_money
ps.setNull(23, Types.VARCHAR); // costs_money_uom
ps.setNull(24, Types.NUMERIC); // yields_money
ps.setNull(25, Types.NUMERIC); // yields_money_uom
ps.setNull(26, Types.VARCHAR); // energy_carrier
ps.setNull(27, Types.VARCHAR); // energy_carrier_codespace
ps.setNull(28, Types.NUMERIC); // maximum_load
ps.setNull(29, Types.VARCHAR); // maximum_load_uom
ps.setNull(30, Types.VARCHAR); // source
ps.setNull(31, Types.VARCHAR); // source_codespace
ps.setNull(32, Types.NUMERIC); // is_dangerous
ps.setNull(33, Types.NUMERIC); // is_recyclable
ps.setNull(34, Types.BIGINT); // cityobject_id
ps.setNull(1, Types.BIGINT); // id
ps.setNull(2, Types.INTEGER); // objectclass_id
ps.setNull(3, Types.VARCHAR); // type
ps.setNull(4, Types.VARCHAR); // type_codespace
ps.setNull(5, Types.VARCHAR); // enduse
ps.setNull(6, Types.VARCHAR); // enduse_codespace
ps.setNull(7, Types.VARCHAR); // status
ps.setNull(8, Types.VARCHAR); // operation_type
ps.setNull(9, Types.VARCHAR); // operation_type_codespace
ps.setNull(10, Types.INTEGER); // year
ps.setNull(11, Types.VARCHAR); // amount_type
ps.setNull(12, Types.VARCHAR); // amount_type_codespace
ps.setNull(13, Types.NUMERIC); // amount
ps.setNull(14, Types.VARCHAR); // amount_uom
ps.setNull(15, Types.BOOLEAN); // is_amount_normalized
ps.setNull(16, Types.VARCHAR); // normalization_param
ps.setNull(17, Types.NUMERIC); // normalization_value
ps.setNull(18, Types.VARCHAR); // normalization_value_uom
ps.setNull(19, Types.VARCHAR); // ref_period
ps.setNull(20, Types.VARCHAR); // ref_period_codespace
ps.setNull(21, Types.NUMERIC); // co2_equivalent
ps.setNull(22, Types.VARCHAR); // co2_equivalent_uom
ps.setNull(23, Types.NUMERIC); // costs_money
ps.setNull(24, Types.VARCHAR); // costs_money_uom
ps.setNull(25, Types.NUMERIC); // yields_money
ps.setNull(26, Types.VARCHAR); // yields_money_uom
ps.setNull(27, Types.VARCHAR); // energy_carrier
ps.setNull(28, Types.VARCHAR); // energy_carrier_codespace
ps.setNull(29, Types.NUMERIC); // maximum_load
ps.setNull(30, Types.VARCHAR); // maximum_load_uom
ps.setNull(31, Types.VARCHAR); // source
ps.setNull(32, Types.VARCHAR); // source_codespace
ps.setNull(33, Types.BOOLEAN); // is_dangerous
ps.setNull(34, Types.BOOLEAN); // is_recyclable
ps.setNull(35, Types.BIGINT); // amount_time_series_id
ps.setNull(36, Types.BIGINT); // costs_time_series_id
ps.setNull(37, Types.BIGINT); // yields_time_series_id
ps.setNull(38, Types.BIGINT); // cityobject_id
}
......
package de.stuttgart.hft.ade.energy3.importer;
import de.stuttgart.hft.ade.energy3.schema.ADETable;
import org.citydb.core.ade.importer.ADEImporter;
import org.citydb.core.ade.importer.CityGMLImportHelper;
import org.citydb.core.ade.importer.ForeignKeys;
import org.citydb.core.database.schema.mapping.AbstractObjectType;
import org.citydb.core.operation.importer.CityGMLImportException;
import org.citydb.core.operation.importer.util.GeometryConverter;
import org.citygml4j.ade.energy3.model.supportingClasses.SensorData;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Types;
public class SensorDataImporter implements ADEImporter {
private final Connection connection;
private final CityGMLImportHelper helper;
private GeometryConverter geometryConverter;
private PreparedStatement ps;
private int batchCounter;
public SensorDataImporter(Connection connection, CityGMLImportHelper helper, ImportManager manager) throws CityGMLImportException, SQLException {
this.connection = connection;
this.helper = helper;
ps = connection.prepareStatement("insert into " +
helper.getTableNameWithSchema(manager.getSchemaMapper().getTableName(ADETable.SENSOR_DATA)) + " " +
"(id, objectclass_id, type, type_codespace, value_type, value_type_codespace,yearly_value," +
" yearly_value_uom, time_series_id, cityobject_id, position) " +
"values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
geometryConverter = helper.getGeometryConverter();
}
public void doImport(SensorData sensorData, long objectId, AbstractObjectType<?> objectType, ForeignKeys foreignKeys) throws CityGMLImportException, SQLException {
ps.setLong(1, objectId);
setSensorDataNull();
if (sensorData.isSetType()) {
ps.setString(3, sensorData.getType().getValue());
ps.setString(4, sensorData.getType().getCodeSpace());
}
if (sensorData.isSetValueType()){
ps.setString(5, sensorData.getValueType().getValue());
ps.setString(6, sensorData.getValueType().getCodeSpace());
}
if (sensorData.isSetYearlyValue()) {
ps.setDouble(7, sensorData.getYearlyValue().getValue());
ps.setString(8, sensorData.getYearlyValue().getUom());
}
if (sensorData.isSetPosition()) {
ps.setObject(9, sensorData.getPosition().getPoint());
}
ps.addBatch();
if (++batchCounter == helper.getDatabaseAdapter().getMaxBatchSize())
helper.executeBatch(objectType);
}
private void setSensorDataNull() throws SQLException {
ps.setNull(1, Types.BIGINT); // id
ps.setNull(2, Types.INTEGER); // objectclass_id
ps.setNull(3, Types.VARCHAR); // type
ps.setNull(4, Types.VARCHAR); // type_codespace
ps.setNull(5, Types.VARCHAR); // value_type
ps.setNull(6, Types.VARCHAR); // value_type_codespace
ps.setNull(7, Types.DOUBLE); // yearly_value
ps.setNull(8, Types.VARCHAR); // yearly_value_uom
ps.setNull(9, Types.BIGINT); // time_series_id
ps.setNull(10, Types.BIGINT); // cityobject_id
ps.setNull(11, Types.OTHER); // position (PostGIS geometry)
}
@Override
public void executeBatch() throws CityGMLImportException, SQLException {
if (batchCounter > 0) {
ps.executeBatch();
batchCounter = 0;
}
}
@Override
public void close() throws CityGMLImportException, SQLException {
ps.close();
}
}
......@@ -17,6 +17,9 @@ import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Types;
/*
Not a part of beta 8
*/
public class WeatherDataImporter implements ADEImporter {
private final Connection connection;
......@@ -60,10 +63,10 @@ public class WeatherDataImporter implements ADEImporter {
ps.setString(7, weatherData.getYearlyValue().getUom());
}
if (weatherData.isSetLibraryCode()){
ps.setString(8, weatherData.getLibraryCode().getValue());
ps.setString(9, weatherData.getLibraryCode().getCodeSpace());
}
// if (weatherData.isSetLibraryCode()){
// ps.setString(8, weatherData.getLibraryCode().getValue());
// ps.setString(9, weatherData.getLibraryCode().getCodeSpace());
// }
if (cityObjectId != 0) {
ps.setLong(11, cityObjectId);
......@@ -72,9 +75,9 @@ public class WeatherDataImporter implements ADEImporter {
if (weatherData.isSetPosition()){
GeometryObject geometryObject = null;
ReferencePointProperty referencePoint = weatherData.getPosition();
if (referencePoint != null && referencePoint.isSetValue())
geometryObject = helper.getGeometryConverter().getPoint(referencePoint.getValue());
// ReferencePointProperty referencePoint = weatherData.getPosition();
// if (referencePoint != null && referencePoint.isSetValue())
// geometryObject = helper.getGeometryConverter().getPoint(referencePoint.getValue());
if (geometryObject != null)
ps.setObject(12, helper.getDatabaseAdapter().getGeometryConverter().getDatabaseObject(geometryObject, connection));
else
......@@ -118,4 +121,4 @@ public class WeatherDataImporter implements ADEImporter {
public void close() throws CityGMLImportException, SQLException {
ps.close();
}
}
\ No newline at end of file
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment