Commit 86bff7d5 authored by PrabithaPrabhakaran's avatar PrabithaPrabhakaran
Browse files

Add importer exporter for resource module

parent 360a3a02
package de.stuttgart.hft.ade.energy3.exporter;
import de.stuttgart.hft.ade.energy3.schema.ADETable;
import org.citydb.core.ade.exporter.ADEExporter;
import org.citydb.core.ade.exporter.CityGMLExportHelper;
import org.citydb.core.database.schema.mapping.ObjectType;
import org.citydb.core.operation.exporter.CityGMLExportException;
import org.citydb.core.query.filter.projection.ProjectionFilter;
import org.citygml4j.ade.energy3.model.core.AbstractResource;
import org.citygml4j.ade.energy3.model.supportingClasses.*;
import org.citygml4j.model.gml.basicTypes.Code;
import org.citygml4j.model.gml.basicTypes.Measure;
import org.sig3d.citygml.energyADE3beta8.ResourceStatusValueType;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.GregorianCalendar;
public class AbstractADEFeatureExporter implements ADEExporter {
private final CityGMLExportHelper helper;
private AbstractADEFeatureExporter abstractADEFeatureExporter;
private PreparedStatement ps;
public AbstractADEFeatureExporter(Connection connection, CityGMLExportHelper helper, ExportManager manager)
throws CityGMLExportException, SQLException {
this.helper = helper;
String tableName = helper.getTableNameWithSchema(manager.getSchemaMapper().getTableName(ADETable.CITYOBJECT));
String sql = "SELECT " + getColumnNames() + " FROM " + tableName + " WHERE cityobject_id = ?";
ps = connection.prepareStatement(sql);
abstractADEFeatureExporter = manager.getExporter(AbstractADEFeatureExporter.class);
}
public Collection<AbstractADEFeature> doExport(long cityObjectId) throws CityGMLExportException, SQLException {
ps.setLong(1, cityObjectId);
try (ResultSet rs = ps.executeQuery()) {
Collection<AbstractADEFeature> result = new ArrayList<>();
while (rs.next()) {
long objectId = rs.getLong(1);
int objectClassId = rs.getInt(2);
if (rs.wasNull()) {
continue;
}
AbstractADEFeature adeFeature = helper.createObject(objectId, objectClassId, AbstractADEFeature.class);
if (adeFeature == null) {
helper.logOrThrowErrorMessage("Failed to instantiate " + helper.getObjectSignature(objectClassId, cityObjectId) + " as ade feature object.");
continue;
}
ObjectType objectType = helper.getObjectType(objectClassId);
ProjectionFilter projectionFilter = helper.getProjectionFilter(objectType);
// Set AbstractADEFeature properties
String identifierValue = rs.getString(2);
String identifierCodespace = rs.getString(3);
if (identifierValue != null || identifierCodespace != null) {
// adeFeature.getExternalReference(); check with uml
}
String valid_from = rs.getString(4);
String valid_to = rs.getString(5);
try {
DatatypeFactory df = DatatypeFactory.newInstance();
XMLGregorianCalendar fromCal = null;
XMLGregorianCalendar toCal = null;
if (valid_from != null) {
GregorianCalendar gc = (GregorianCalendar) javax.xml.bind.DatatypeConverter.parseDate(valid_from);
fromCal = df.newXMLGregorianCalendar(gc);
adeFeature.setCreationDate(fromCal);
}
if (valid_to != null) {
GregorianCalendar gc = (GregorianCalendar) javax.xml.bind.DatatypeConverter.parseDate(valid_to);
toCal = df.newXMLGregorianCalendar(gc);
adeFeature.setTerminationDate(toCal);
}
} catch (Exception e) {
e.printStackTrace();
}
result.add(adeFeature);
}
return result;
}
}
@Override
public void close() throws CityGMLExportException, SQLException {
ps.close();
}
private String getColumnNames() {
return "id, objectclass_id, type, type_codespace, enduse, enduse_codespace, status, operation_type," +
" operation_type_codespace, year, amount_type, amount_type_codespace, amount, " +
"amount_uom, time_series_id, is_amount_normalized, normalization_param, normalization_value, " +
"normalization_value_uom, co2_equivalent, co2_equivalent_uom, costs_money, costs_money_uom, " +
"yields_money, yields_money_uom, energy_carrier, energy_carrier_codespace, maximum_load, " +
"maximum_load_uom, source, source_codespace, is_dangerous, is_recyclable, cityobject_id";
}
}
package de.stuttgart.hft.ade.energy3.exporter;
import de.stuttgart.hft.ade.energy3.schema.ADETable;
import org.citydb.core.ade.exporter.ADEExporter;
import org.citydb.core.ade.exporter.CityGMLExportHelper;
import org.citydb.core.database.schema.mapping.ObjectType;
import org.citydb.core.operation.exporter.CityGMLExportException;
import org.citydb.core.query.filter.projection.ProjectionFilter;
import org.citygml4j.ade.energy3.model.supportingClasses.*;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.GregorianCalendar;
public class AbstractFeatureWithLifeSpanExporter implements ADEExporter {
private final CityGMLExportHelper helper;
private AbstractFeatureWithLifeSpanExporter lifeSpanExporter;
private PreparedStatement ps;
public AbstractFeatureWithLifeSpanExporter(Connection connection, CityGMLExportHelper helper, ExportManager manager)
throws CityGMLExportException, SQLException {
this.helper = helper;
String tableName = helper.getTableNameWithSchema(manager.getSchemaMapper().getTableName(ADETable.CITYOBJECT));
String sql = "SELECT " + getColumnNames() + " FROM " + tableName + " WHERE cityobject_id = ?";
ps = connection.prepareStatement(sql);
lifeSpanExporter = manager.getExporter(AbstractFeatureWithLifeSpanExporter.class);
}
public Collection<AbstractFeatureWithLifeSpan> doExport(long cityObjectId) throws CityGMLExportException, SQLException {
ps.setLong(1, cityObjectId);
try (ResultSet rs = ps.executeQuery()) {
Collection<AbstractFeatureWithLifeSpan> result = new ArrayList<>();
while (rs.next()) {
long objectId = rs.getLong(1);
int objectClassId = rs.getInt(2);
if (rs.wasNull()) {
continue;
}
AbstractFeatureWithLifeSpan lifeSpan = helper.createObject(objectId, objectClassId, AbstractFeatureWithLifeSpan.class);
if (lifeSpan == null) {
helper.logOrThrowErrorMessage("Failed to instantiate " + helper.getObjectSignature(objectClassId, cityObjectId) + " as feature with life span object.");
continue;
}
ObjectType objectType = helper.getObjectType(objectClassId);
ProjectionFilter projectionFilter = helper.getProjectionFilter(objectType);
// Set AbstractFeatureWithLifeSpan properties
String identifier_value = rs.getString(2);
String identifier_codespace = rs.getString(3);
if (identifier_value != null || identifier_codespace != null) {
lifeSpan.getIdentifier().setValue(identifier_value);
lifeSpan.getIdentifier().setCodeSpace(identifier_codespace);
}
String valid_from = rs.getString(4);
String valid_to = rs.getString(5);
try {
DatatypeFactory df = DatatypeFactory.newInstance();
XMLGregorianCalendar fromCal = null;
XMLGregorianCalendar toCal = null;
if (valid_from != null) {
GregorianCalendar gc = (GregorianCalendar) javax.xml.bind.DatatypeConverter.parseDate(valid_from);
fromCal = df.newXMLGregorianCalendar(gc);
lifeSpan.setValidFrom(fromCal);
}
if (valid_to != null) {
GregorianCalendar gc = (GregorianCalendar) javax.xml.bind.DatatypeConverter.parseDate(valid_to);
toCal = df.newXMLGregorianCalendar(gc);
lifeSpan.setValidTo(toCal);
}
} catch (Exception e) {
e.printStackTrace();
}
result.add(lifeSpan);
}
return result;
}
}
@Override
public void close() throws CityGMLExportException, SQLException {
ps.close();
}
private String getColumnNames() {
return "id, objectclass_id, type, type_codespace, enduse, enduse_codespace, status, operation_type," +
" operation_type_codespace, year, amount_type, amount_type_codespace, amount, " +
"amount_uom, time_series_id, is_amount_normalized, normalization_param, normalization_value, " +
"normalization_value_uom, co2_equivalent, co2_equivalent_uom, costs_money, costs_money_uom, " +
"yields_money, yields_money_uom, energy_carrier, energy_carrier_codespace, maximum_load, " +
"maximum_load_uom, source, source_codespace, is_dangerous, is_recyclable, cityobject_id";
}
}
......@@ -9,6 +9,10 @@ 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.model.citygml.core.AbstractCityObject;
import org.citygml4j.model.gml.geometry.primitives.PointProperty;
......@@ -27,6 +31,8 @@ public class CityObjectPropertiesExporter implements ADEExporter {
private final Connection connection;
//private WeatherDataExporter weatherDataExporter;
private ResourceExporter resourceExporter;
private AbstractADEFeatureExporter adeFeatureExporter;
private AbstractFeatureWithLifeSpanExporter featureWithLifeSpanExporter;
private String module;
public CityObjectPropertiesExporter(Connection connection, CityGMLExportHelper helper, ExportManager manager) throws CityGMLExportException, SQLException {
......@@ -65,6 +71,18 @@ public class CityObjectPropertiesExporter implements ADEExporter {
cityObject.addGenericApplicationPropertyOfCityObject(property);
}
}
if (projectionFilter.containsProperty("abstractAdeFeature", module)) {
for (AbstractADEFeature feature : adeFeatureExporter.doExport(objectId)) {
AbstractADEFeatureProperty property = new AbstractADEFeatureProperty(feature);
cityObject.addGenericApplicationPropertyOfCityObject(property);
}
}
if (projectionFilter.containsProperty("abstractAdeFeature", module)) {
for (AbstractFeatureWithLifeSpan lifeSpan : featureWithLifeSpanExporter.doExport(objectId)) {
AbstractFeatureWithLifeSpanProperty property = new AbstractFeatureWithLifeSpanProperty(lifeSpan);
cityObject.addGenericApplicationPropertyOfCityObject(property);
}
}
if (pointObj != null) {
GeometryObject pointObject = helper.getDatabaseAdapter().getGeometryConverter().getPoint(pointObj);
......
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.AbstractADEFeature;
import javax.xml.datatype.XMLGregorianCalendar;
import java.sql.*;
import java.util.Collections;
public class AbstractADEFeatureImporter implements ADEImporter {
private final Connection connection;
private final CityGMLImportHelper helper;
private GeometryConverter geometryConverter;
private PreparedStatement ps;
private int batchCounter;
public AbstractADEFeatureImporter(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.FEATURE_WITH_LIFE_SPAN)) +
"(id, objectclass_id, type, type_codespace, enduse, enduse_codespace, status, " +
"operation_type, operation_type_codespace, year, amount_type, amount_type_codespace, amount, " +
"amount_uom, time_series_id, is_amount_normalized, normalization_param, normalization_value, " +
"normalization_value_uom, co2_equivalent, co2_equivalent_uom, costs_money, costs_money_uom, " +
"yields_money, yields_money_uom, energy_carrier, energy_carrier_codespace, maximum_load, " +
"maximum_load_uom, source, source_codespace, is_dangerous, is_recyclable, cityobject_id) " +
"VALUES (" + String.join(", ", Collections.nCopies(34, "?")) + ")"
);
geometryConverter = helper.getGeometryConverter();
}
public void doImport(AbstractADEFeature abstractADEFeature, long objectId, AbstractObjectType<?> objectType, ForeignKeys foreignKeys) throws CityGMLImportException, SQLException {
setAbstractADEFeatureToNull();
ps.setLong(1, objectId);
ps.setLong(2, objectType.getObjectClassId());
ps.setLong(34, foreignKeys.get("cityObjectId"));
setAbstractADEFeatureProperties(abstractADEFeature);
ps.addBatch();
if (++batchCounter == helper.getDatabaseAdapter().getMaxBatchSize())
helper.executeBatch(objectType);
}
private void setAbstractADEFeatureToNull() throws SQLException {
ps.setNull(1, Types.BIGINT); // id
ps.setNull(2, Types.INTEGER); // objectclass_id
ps.setNull(3, Types.VARCHAR); // Identifier value
ps.setNull(5, Types.TIMESTAMP); // creationDate
ps.setNull(6, Types.TIMESTAMP); // terminationDate
}
private void setAbstractADEFeatureProperties(AbstractADEFeature abstractADEFeature) throws SQLException {
if (abstractADEFeature.getExternalReference() != null) {
// ps.setString(3, abstractADEFeature.getExternalReference());
}
if (abstractADEFeature.getCreationDate()!= null){
XMLGregorianCalendar xmlCal = abstractADEFeature.getCreationDate();
Timestamp timestamp = new Timestamp(xmlCal.toGregorianCalendar().getTimeInMillis());
ps.setTimestamp(5, timestamp);
}
// if (abstractADEFeature.getTerminationDate()!= null){
// XMLGregorianCalendar xmlCal = abstractADEFeature.getTerminationDate()));
// Timestamp timestamp = new Timestamp(xmlCal.toGregorianCalendar().getTimeInMillis());
// ps.setTimestamp(6, timestamp);
// }
}
@Override
public void executeBatch() throws CityGMLImportException, SQLException {
if (batchCounter > 0) {
ps.executeBatch();
batchCounter = 0;
}
}
@Override
public void close() throws CityGMLImportException, SQLException {
ps.close();
}
}
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.*;
import javax.xml.datatype.XMLGregorianCalendar;
import java.sql.*;
import java.util.Collections;
public class AbstractFeatureWithLifeSpanImporter implements ADEImporter {
private final Connection connection;
private final CityGMLImportHelper helper;
private GeometryConverter geometryConverter;
private PreparedStatement ps;
private int batchCounter;
public AbstractFeatureWithLifeSpanImporter(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.FEATURE_WITH_LIFE_SPAN)) +
"(id, objectclass_id, type, type_codespace, enduse, enduse_codespace, status, " +
"operation_type, operation_type_codespace, year, amount_type, amount_type_codespace, amount, " +
"amount_uom, time_series_id, is_amount_normalized, normalization_param, normalization_value, " +
"normalization_value_uom, co2_equivalent, co2_equivalent_uom, costs_money, costs_money_uom, " +
"yields_money, yields_money_uom, energy_carrier, energy_carrier_codespace, maximum_load, " +
"maximum_load_uom, source, source_codespace, is_dangerous, is_recyclable, cityobject_id) " +
"VALUES (" + String.join(", ", Collections.nCopies(34, "?")) + ")"
);
geometryConverter = helper.getGeometryConverter();
}
public void doImport(AbstractFeatureWithLifeSpan abstractFeatureWithLifeSpan, long objectId, AbstractObjectType<?> objectType, ForeignKeys foreignKeys) throws CityGMLImportException, SQLException {
setAbstractFeatureWithLifeSpanToNull();
ps.setLong(1, objectId);
ps.setLong(2, objectType.getObjectClassId());
ps.setLong(34, foreignKeys.get("cityObjectId"));
setAbstractFeatureWithLifeSpanProperties(abstractFeatureWithLifeSpan);
ps.addBatch();
if (++batchCounter == helper.getDatabaseAdapter().getMaxBatchSize())
helper.executeBatch(objectType);
}
private void setAbstractFeatureWithLifeSpanToNull() throws SQLException {
ps.setNull(1, Types.BIGINT); // id
ps.setNull(2, Types.INTEGER); // objectclass_id
ps.setNull(3, Types.VARCHAR); // Identifier value
ps.setNull(4, Types.VARCHAR); // Identifier codespace
ps.setNull(5, Types.TIMESTAMP); // vaildFrom
ps.setNull(6, Types.TIMESTAMP); // vaildTo
}
private void setAbstractFeatureWithLifeSpanProperties(AbstractFeatureWithLifeSpan abstractFeatureWithLifeSpan) throws SQLException {
if (abstractFeatureWithLifeSpan.getIdentifier() != null) {
ps.setString(3, abstractFeatureWithLifeSpan.getIdentifier().getValue());
ps.setString(4, abstractFeatureWithLifeSpan.getIdentifier().getCodeSpace());
}
if (abstractFeatureWithLifeSpan.getValidFrom()!= null){
XMLGregorianCalendar xmlCal = abstractFeatureWithLifeSpan.getValidFrom();
Timestamp timestamp = new Timestamp(xmlCal.toGregorianCalendar().getTimeInMillis());
ps.setTimestamp(5, timestamp);
}
if (abstractFeatureWithLifeSpan.getValidTo()!= null){
XMLGregorianCalendar xmlCal = abstractFeatureWithLifeSpan.getValidTo();
Timestamp timestamp = new Timestamp(xmlCal.toGregorianCalendar().getTimeInMillis());
ps.setTimestamp(6, timestamp);
}
}
@Override
public void executeBatch() throws CityGMLImportException, SQLException {
if (batchCounter > 0) {
ps.executeBatch();
batchCounter = 0;
}
}
@Override
public void close() throws CityGMLImportException, SQLException {
ps.close();
}
}
......@@ -96,6 +96,32 @@ public class CityObjectPropertiesImporter implements ADEImporter {
}
}
}
if(properties.contains(AbstractFeatureWithLifeSpanProperty.class)){
for (AbstractFeatureWithLifeSpanProperty propertyElement : properties.getAll(AbstractFeatureWithLifeSpanProperty.class)) {
AbstractFeatureWithLifeSpan lifeSpan = propertyElement.getAbstractFeatureWithLifeSpan();
if (lifeSpan != null) {
helper.importObject(lifeSpan, ForeignKeys.create().with("cityObjectId", parentId));
propertyElement.unsetAbstractFeatureWithLifeSpan();
} else {
String href = propertyElement.getHref();
if (href != null && href.length() != 0)
helper.logOrThrowUnsupportedXLinkMessage(parent, AbstractFeatureWithLifeSpan.class, href);
}
}
}
if (properties.contains(AbstractADEFeatureProperty.class)){
for (AbstractADEFeatureProperty propertyElement : properties.getAll(AbstractADEFeatureProperty.class)) {
AbstractADEFeature adeFeature = propertyElement.getAbstractADEFeature();
if (adeFeature != null) {
helper.importObject(adeFeature, ForeignKeys.create().with("cityObjectId", parentId));
propertyElement.unsetAbstractADEFeature();
} else {
String href = propertyElement.getHref();
if (href != null && href.length() != 0)
helper.logOrThrowUnsupportedXLinkMessage(parent, AbstractADEFeature.class, href);
}
}
}
}
@Override
......
......@@ -27,6 +27,7 @@ public enum ADETable {
QUALIFIED_ATTRIBUTE(AbstractQualifiedAttributeImporter.class),
REFURBISHMENT_MEASURE(RefurbishmentMeasureImporter.class),
RESOURCE(ResourceImporter.class),
FEATURE_WITH_LIFE_SPAN(AbstractFeatureWithLifeSpanImporter.class),
SCHEDULE(null),
SCHEDULE_COMPONENT(null),
SENSOR_DATA (null),
......
......@@ -85,6 +85,12 @@ public class ObjectMapper implements ADEObjectMapper {
case "QualifiedArea":
objectClassIds.put(QualifiedArea.class, objectClassId);
break;
case "AbstractADEFeature":
objectClassIds.put(AbstractADEFeature.class, objectClassId);
break;
case "AbstractFeatureWithLifeSpan":
objectClassIds.put(AbstractFeatureWithLifeSpan.class, objectClassId);
break;
}
}
}
......
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