Commit 709cd9cd authored by Rushikesh Padsala's avatar Rushikesh Padsala
Browse files

Merge branch 'beta7' into 'master'

Initial commit

See merge request energy-ade/energy-ade-2-3dcitydb!1
parents f37efb20 31a9fd12
package de.stuttgart.hft.ade.energy2.exporter;
import de.stuttgart.hft.ade.energy2.schema.ADETable;
import org.citydb.core.ade.exporter.ADEExporter;
import org.citydb.core.ade.exporter.CityGMLExportHelper;
import org.citydb.core.database.schema.mapping.AbstractType;
import org.citydb.core.database.schema.mapping.ComplexType;
import org.citydb.core.database.schema.mapping.SchemaMapping;
import org.citydb.core.operation.exporter.CityGMLExportException;
import org.citydb.core.query.filter.projection.ProjectionFilter;
import org.citydb.core.registry.ObjectRegistry;
import org.citygml4j.ade.energy.model.core.*;
import org.citygml4j.ade.energy.model.module.EnergyADEModule;
import org.citygml4j.model.gml.basicTypes.Code;
import org.citygml4j.model.gml.basicTypes.Measure;
import org.citygml4j.model.gml.base.StringOrRef;
import java.lang.reflect.InvocationTargetException;
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.HashMap;
import java.util.Map;
public class AbstractQualifiedAttributeExporter implements ADEExporter {
private final PreparedStatement ps;
private final CityGMLExportHelper helper;
private final ExportManager manager;
private final Connection connection;
private WeatherDataExporter weatherDataExporter;
private ResourceExporter resourceExporter;
private String module;
private Map<Integer, Class <? extends AbstractQualifiedAttribute>> attributeClassMap;
public AbstractQualifiedAttributeExporter(Connection connection, CityGMLExportHelper helper, ExportManager manager) throws CityGMLExportException, SQLException {
this.connection = connection;
this.helper = helper;
this.manager = manager;
attributeClassMap = new HashMap<>();
SchemaMapping schemaMapping = ObjectRegistry.getInstance().getSchemaMapping();
ComplexType qualifiedHeightType = schemaMapping.getComplexType("QualifiedHeight", "http://www.citygml.org/ade/energy/2.0");
int objectClassIdOfQualifiedHeight = qualifiedHeightType.getObjectClassId();
attributeClassMap.put(objectClassIdOfQualifiedHeight, QualifiedHeight.class);
ComplexType qualifiedAreaType = schemaMapping.getComplexType("QualifiedArea", "http://www.citygml.org/ade/energy/2.0");
int objectClassIdOfQualifiedArea = qualifiedAreaType.getObjectClassId();
attributeClassMap.put(objectClassIdOfQualifiedArea, QualifiedArea.class);
ComplexType qualifiedVolumeType = schemaMapping.getComplexType("QualifiedVolume", "http://www.citygml.org/ade/energy/2.0");
int objectClassIdOfQualifiedVolume = qualifiedVolumeType.getObjectClassId();
attributeClassMap.put(objectClassIdOfQualifiedVolume, QualifiedVolume.class);
String tableName = helper.getTableNameWithSchema(
manager.getSchemaMapper().getTableName(ADETable.QUALIFIED_ATTRIBUTE)
);
String select = "SELECT id, objectclass_id, type, type_codespace, \"value\", value_uom, description, source, building_id, building_partition_id " +
"FROM " + tableName + " WHERE building_id = ?";
module = EnergyADEModule.v2_0.getNamespaceURI();
ps = connection.prepareStatement(select);
}
public Collection<AbstractQualifiedAttribute> doExport(long objectId) throws SQLException {
ps.setLong(1, objectId);
try (ResultSet rs = ps.executeQuery()) {
Collection<AbstractQualifiedAttribute> result = new ArrayList<>();
while (rs.next()) {
long objectIdFromDB = rs.getLong(1);
int objectClassId = rs.getInt(2);
AbstractQualifiedAttribute attribute = createQualifiedAttribute(objectClassId);
String descriptionValue = rs.getString(7);
if (descriptionValue != null && !descriptionValue.trim().isEmpty()) {
StringOrRef description = new StringOrRef();
description.setValue(descriptionValue);
attribute.setDescription(description);
// System.out.println("Set description for QualifiedAttribute id=" + objectIdFromDB + ": " + descriptionValue);
}
if (rs.getString(8) != null)
attribute.setSource(rs.getString(8));
Measure value = new Measure();
value.setValue(rs.getDouble(5));
value.setUom(rs.getString(6));
attribute.setValue(value);
if (attribute instanceof QualifiedHeight qualifiedHeight) {
Code type = new Code();
type.setValue(rs.getString(3));
type.setCodeSpace(rs.getString(4));
qualifiedHeight.setType(type);
} else if (attribute instanceof QualifiedArea qualifiedArea) {
Code type = new Code();
type.setValue(rs.getString(3));
type.setCodeSpace(rs.getString(4));
qualifiedArea.setType(type);
} else if (attribute instanceof QualifiedVolume qualifiedVolume) {
Code type = new Code();
type.setValue(rs.getString(3));
type.setCodeSpace(rs.getString(4));
qualifiedVolume.setType(type);
}
result.add(attribute);
}
return result;
}
}
private AbstractQualifiedAttribute createQualifiedAttribute(int objectClassId) {
try {
Class<? extends AbstractQualifiedAttribute> aClass = attributeClassMap.get(objectClassId);
return aClass.getDeclaredConstructor().newInstance();
}catch (NoSuchMethodException | InvocationTargetException | InstantiationException | IllegalAccessException exception){
throw new IllegalStateException(exception);
}
}
@Override
public void close() throws CityGMLExportException, SQLException {
ps.close();
}
}
package de.stuttgart.hft.ade.energy2.exporter;
import de.stuttgart.hft.ade.energy2.schema.ADETable;
import net.opengis.gml.CodeType;
import org.citydb.core.ade.exporter.ADEExporter;
import org.citydb.core.ade.exporter.CityGMLExportHelper;
import org.citydb.core.database.schema.mapping.AbstractType;
import org.citydb.core.operation.exporter.CityGMLExportException;
import org.citydb.core.query.filter.projection.ProjectionFilter;
import org.citygml4j.ade.energy.model.core.*;
import org.citygml4j.ade.energy.model.module.EnergyADEModule;
import org.citygml4j.model.citygml.building.AbstractBuilding;
import org.sig3d.citygml.energyADE2beta7.ThermalStatusValueType;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
public class BuildingPropertiesExporter implements ADEExporter {
private final PreparedStatement ps;
private final CityGMLExportHelper helper;
private final ExportManager manager;
private final Connection connection;
private final AbstractQualifiedAttributeExporter abstractQualifiedAttributeExporter;
private final RefurbishmentMeasureExporter refurbishmentMeasureExporter;
private final String module;
public BuildingPropertiesExporter(Connection connection, CityGMLExportHelper helper, ExportManager manager) throws CityGMLExportException, SQLException {
this.connection = connection;
this.helper = helper;
this.manager = manager;
String select = "select id, type, type_codespace, is_protected, constr_weight, constr_weight_codespace, attic_thm_status, basement_thm_status from " +
helper.getTableNameWithSchema(manager.getSchemaMapper().getTableName(ADETable.BUILDING)) +
" where id = ?";
module = EnergyADEModule.v2_0.getNamespaceURI();
ps = connection.prepareStatement(select);
abstractQualifiedAttributeExporter = manager.getExporter(AbstractQualifiedAttributeExporter.class);
refurbishmentMeasureExporter = manager.getExporter(RefurbishmentMeasureExporter.class);
}
public void doExport(AbstractBuilding building, long objectId, AbstractType<?> objectType, ProjectionFilter projectionFilter) throws SQLException, CityGMLExportException {
ps.setLong(1, objectId);
try (ResultSet rs = ps.executeQuery()) {
if (rs.next()) {
// Building type
if (projectionFilter.containsProperty("buildingType", module)) {
String type = rs.getString("type");
String typeCodespace = rs.getString("type_codespace");
if (type != null || typeCodespace != null) {
CodeType bdgType = new CodeType();
if (type != null)
bdgType.setValue(type);
if (typeCodespace != null)
bdgType.setCodeSpace(typeCodespace);
BuildingTypeProperty buildingTypeProperty = new BuildingTypeProperty();
buildingTypeProperty.setValue(bdgType);
building.addGenericApplicationPropertyOfAbstractBuilding(buildingTypeProperty);
}
}
// Is protected
if (projectionFilter.containsProperty("isProtected", module)) {
int isProtected = rs.getInt("is_protected");
boolean wasNull = rs.wasNull();
Boolean isProtectedValue = wasNull ? false : (isProtected == 1);
BuildingIsProtectedProperty buildingIsProtectedProperty = new BuildingIsProtectedProperty();
buildingIsProtectedProperty.setValue(isProtectedValue);
building.addGenericApplicationPropertyOfAbstractBuilding(buildingIsProtectedProperty);
}
// Construction weight
if (projectionFilter.containsProperty("constructionWeight", module)) {
String constrWeightValue = rs.getString("constr_weight");
String constrWeightCodeSpace = rs.getString("constr_weight_codespace");
if (constrWeightValue != null || constrWeightCodeSpace != null) {
CodeType constrWeight = new CodeType();
constrWeight.setValue(constrWeightValue);
constrWeight.setCodeSpace(constrWeightCodeSpace);
BuildingConstructionWeightProperty buildingConstructionWeightProperty = new BuildingConstructionWeightProperty();
buildingConstructionWeightProperty.setValue(constrWeight);
building.addGenericApplicationPropertyOfAbstractBuilding(buildingConstructionWeightProperty);
}
}
// Attic thermal status
if (projectionFilter.containsProperty("atticThermalStatus", module)) {
String atticThmStatusValue = rs.getString("attic_thm_status");
if (atticThmStatusValue != null) {
ThermalStatusValueType thermalStatusValue = ThermalStatusValueType.fromValue(atticThmStatusValue);
BuildingAtticThermalStatusProperty buildingAtticThmStatusProperty = new BuildingAtticThermalStatusProperty();
buildingAtticThmStatusProperty.setValue(thermalStatusValue);
building.addGenericApplicationPropertyOfAbstractBuilding(buildingAtticThmStatusProperty);
}
}
// Basement thermal status
if (projectionFilter.containsProperty("basementThermalStatus", module)) {
String basementThmStatusValue = rs.getString("basement_thm_status");
if (basementThmStatusValue != null) {
ThermalStatusValueType thermalStatusValueType = ThermalStatusValueType.fromValue(basementThmStatusValue);
BuildingBasementThermalStatusProperty buildingBasementThermalStatusProperty = new BuildingBasementThermalStatusProperty();
buildingBasementThermalStatusProperty.setValue(thermalStatusValueType);
building.addGenericApplicationPropertyOfAbstractBuilding(buildingBasementThermalStatusProperty);
}
}
// Refurbishment measures
if (projectionFilter.containsProperty("refurbishmentMeasure", module)) {
Collection<RefurbishmentMeasureProperty> refurbProps = refurbishmentMeasureExporter.doExport(objectId, objectType, projectionFilter);
for (RefurbishmentMeasureProperty refurbProp : refurbProps) {
building.addGenericApplicationPropertyOfAbstractBuilding(refurbProp);
}
}
// Qualified attributes
if (projectionFilter.containsProperty("qualifiedAttribute", module)) {
for (AbstractQualifiedAttribute qualifiedAttribute : abstractQualifiedAttributeExporter.doExport(objectId)) {
AbstractQualifiedAttributeProperty property = new AbstractQualifiedAttributeProperty(qualifiedAttribute);
building.addGenericApplicationPropertyOfAbstractBuilding(property);
}
}
}
}
}
@Override
public void close() throws CityGMLExportException, SQLException {
ps.close();
abstractQualifiedAttributeExporter.close();
refurbishmentMeasureExporter.close();
}
}
\ No newline at end of file
package de.stuttgart.hft.ade.energy2.exporter;
import de.stuttgart.hft.ade.energy2.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.database.schema.mapping.AbstractType;
import org.citydb.core.operation.exporter.CityGMLExportException;
import org.citydb.core.query.filter.projection.ProjectionFilter;
import org.citygml4j.ade.energy.model.core.*;
import org.citygml4j.ade.energy.model.module.EnergyADEModule;
import org.citygml4j.model.citygml.core.AbstractCityObject;
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 CityObjectPropertiesExporter implements ADEExporter {
private final PreparedStatement ps;
private final CityGMLExportHelper helper;
private final ExportManager manager;
private final Connection connection;
private WeatherDataExporter weatherDataExporter;
private ResourceExporter resourceExporter;
private String module;
public CityObjectPropertiesExporter(Connection connection, CityGMLExportHelper helper, ExportManager manager) throws CityGMLExportException, SQLException {
this.connection = connection;
this.helper = helper;
this.manager = manager;
String select = "select id, ref_point from " +
helper.getTableNameWithSchema(manager.getSchemaMapper().getTableName(ADETable.CITYOBJECT)) +
" where id = ?";
module = EnergyADEModule.v2_0.getNamespaceURI();
ps = connection.prepareStatement(select);
weatherDataExporter = manager.getExporter(WeatherDataExporter.class);
resourceExporter = manager.getExporter(ResourceExporter.class);
}
public Collection<AbstractCityObject> doExport(AbstractCityObject cityObject, long objectId, AbstractType<?> objectType, ProjectionFilter projectionFilter) throws SQLException {
ps.setLong(1, objectId);
try (ResultSet rs = ps.executeQuery()) {
Collection<AbstractCityObject> result = new ArrayList<>();
while (rs.next()) {
Object pointObj = rs.getObject(2);
if (projectionFilter.containsProperty("weatherData", module)) {
for (WeatherData weatherData : weatherDataExporter.doExport(objectId)) {
WeatherDataProperty property = new WeatherDataProperty(weatherData);
cityObject.addGenericApplicationPropertyOfCityObject(property);
}
}
if (projectionFilter.containsProperty("resource", module)) {
for (AbstractResource resource : resourceExporter.doExport(objectId)) {
AbstractResourceProperty property = new AbstractResourceProperty(resource);
cityObject.addGenericApplicationPropertyOfCityObject(property);
}
}
if (pointObj != null) {
GeometryObject pointObject = helper.getDatabaseAdapter().getGeometryConverter().getPoint(pointObj);
if (pointObject != null) {
PointProperty point = helper.getGMLConverter().getPointProperty(pointObject , false);
ReferencePointProperty referencePointProperty = new ReferencePointProperty(point);
cityObject.getGenericApplicationPropertyOfCityObject().add(referencePointProperty);
}
}
result.add(cityObject);
}
return result;
} catch (CityGMLExportException e) {
throw new RuntimeException(e);
}
}
@Override
public void close() throws CityGMLExportException, SQLException {
ps.close();
}
}
/*
* 3D City Database - The Open Source CityGML Database
* https://www.3dcitydb.org/
*
* Copyright 2013 - 2024
* Chair of Geoinformatics
* Technical University of Munich, Germany
* https://www.lrg.tum.de/gis/
*
* The 3D City Database is jointly developed with the following
* cooperation partners:
*
* Virtual City Systems, Berlin <https://vc.systems/>
* M.O.S.S. Computer Grafik Systeme GmbH, Taufkirchen <http://www.moss.de/>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.stuttgart.hft.ade.energy2.exporter;
import de.stuttgart.hft.ade.energy2.schema.ADETable;
import de.stuttgart.hft.ade.energy2.schema.ObjectMapper;
import de.stuttgart.hft.ade.energy2.schema.SchemaMapper;
import org.citydb.core.ade.exporter.ADEExportManager;
import org.citydb.core.ade.exporter.ADEExporter;
import org.citydb.core.ade.exporter.CityGMLExportHelper;
import org.citydb.core.database.schema.mapping.AbstractObjectType;
import org.citydb.core.database.schema.mapping.FeatureType;
import org.citydb.core.operation.exporter.CityGMLExportException;
import org.citydb.core.query.filter.projection.ProjectionFilter;
import org.citygml4j.ade.energy.model.core.AbstractResource;
import org.citygml4j.ade.energy.model.core.QualifiedHeight;
import org.citygml4j.ade.energy.model.core.WeatherData;
import org.citygml4j.ade.energy.model.supportingClasses.*;
import org.citygml4j.model.citygml.ade.binding.ADEModelObject;
import org.citygml4j.model.citygml.building.AbstractBuilding;
import org.citygml4j.model.citygml.core.AbstractCityObject;
import org.citygml4j.model.gml.feature.AbstractFeature;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
public class ExportManager implements ADEExportManager {
private final Map<Class<? extends ADEExporter>, ADEExporter> exporters;
private final ObjectMapper objectMapper;
private final SchemaMapper schemaMapper;
private Connection connection;
private CityGMLExportHelper helper;
public ExportManager(ObjectMapper objectMapper, SchemaMapper schemaMapper) {
this.objectMapper = objectMapper;
this.schemaMapper = schemaMapper;
exporters = new HashMap<>();
}
@Override
public void init(Connection connection, CityGMLExportHelper helper) throws CityGMLExportException, SQLException {
this.connection = connection;
this.helper = helper;
}
@Override
public void exportObject(ADEModelObject object, long objectId, AbstractObjectType<?> objectType, ProjectionFilter projectionFilter) throws CityGMLExportException, SQLException {
if (object instanceof WeatherStation)
getExporter(WeatherStationExporter.class).doExport((WeatherStation) object, objectId, objectType, projectionFilter);
else if (object instanceof WeatherData)
getExporter(WeatherDataExporter.class).doExport(objectId);
else if (object instanceof UrbanFunctionArea)
getExporter(UrbanFunctionAreaExporter.class).doExport((UrbanFunctionArea) object, objectId, objectType, projectionFilter);
else if (object instanceof AbstractTimeSeries)
getExporter(TimeSeriesExporter.class).doExport(objectId);
else if (object instanceof AbstractResource)
getExporter(ResourceExporter.class).doExport(objectId);
}
@Override
public void exportGenericApplicationProperties(String adeHookTable, AbstractFeature parent, long parentId, FeatureType parentType, ProjectionFilter projectionFilter) throws CityGMLExportException, SQLException {
if (adeHookTable.equals(schemaMapper.getTableName(ADETable.TIME_SERIES)) && parent instanceof AbstractTimeSeries)
getExporter(TimeSeriesExporter.class).doExport(parentId);
else if (adeHookTable.equals(schemaMapper.getTableName(ADETable.CITYOBJECT)) && parent instanceof AbstractCityObject)
getExporter(CityObjectPropertiesExporter.class).doExport((AbstractCityObject) parent, parentId, parentType, projectionFilter);
else if (adeHookTable.equals(schemaMapper.getTableName(ADETable.RESOURCE)) && parent instanceof AbstractResource)
getExporter(ResourceExporter.class).doExport(parentId);
else if (adeHookTable.equals(schemaMapper.getTableName(ADETable.BUILDING)) && parent instanceof AbstractBuilding)
getExporter(BuildingPropertiesExporter.class).doExport((AbstractBuilding) parent, parentId, parentType, projectionFilter);
}
@Override
public void close() throws CityGMLExportException, SQLException {
for (ADEExporter exporter : exporters.values())
exporter.close();
}
protected ObjectMapper getObjectMapper() {
return objectMapper;
}
protected SchemaMapper getSchemaMapper() {
return schemaMapper;
}
protected <T extends ADEExporter> T getExporter(Class<T> type) throws CityGMLExportException, SQLException {
ADEExporter exporter = exporters.get(type);
if (exporter == null) {
if (type == TimeSeriesExporter.class)
exporter = new TimeSeriesExporter(connection, helper, this);
else if (type == UrbanFunctionAreaExporter.class)
exporter = new UrbanFunctionAreaExporter(connection, helper, this);
else if (type == WeatherStationExporter.class)
exporter = new WeatherStationExporter(connection, helper, this);
else if (type == ResourceExporter.class)
exporter = new ResourceExporter(connection, helper, this);
else if (type == WeatherDataExporter.class)
exporter = new WeatherDataExporter(connection, helper, this);
else if (type == CityObjectPropertiesExporter.class)
exporter = new CityObjectPropertiesExporter(connection, helper, this);
else if (type == BuildingPropertiesExporter.class)
exporter = new BuildingPropertiesExporter(connection, helper, this);
else if (type == AbstractQualifiedAttributeExporter.class)
exporter = new AbstractQualifiedAttributeExporter(connection, helper, this);
else if (type == RefurbishmentMeasureExporter.class)
exporter = new RefurbishmentMeasureExporter(connection, helper, this);
if (exporter == null)
throw new SQLException("Failed to build ADE exporter of type " + type.getName() + ".");
exporters.put(type, exporter);
}
return type.cast(exporter);
}
}
package de.stuttgart.hft.ade.energy2.exporter;
import de.stuttgart.hft.ade.energy2.schema.ADETable;
import org.citydb.core.ade.exporter.ADEExporter;
import org.citydb.core.ade.exporter.CityGMLExportHelper;
import org.citydb.core.database.schema.mapping.AbstractType;
import org.citydb.core.operation.exporter.CityGMLExportException;
import org.citydb.core.query.filter.projection.ProjectionFilter;
import org.citygml4j.ade.energy.model.core.RefurbishmentMeasure;
import org.citygml4j.ade.energy.model.core.RefurbishmentMeasureProperty;
import org.citygml4j.ade.energy.model.module.EnergyADEModule;
import org.citygml4j.model.gml.basicTypes.Code;
import org.citygml4j.model.gml.base.StringOrRef;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import java.sql.Connection;
import java.sql.Date;
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 RefurbishmentMeasureExporter implements ADEExporter {
private final PreparedStatement ps;
private final PreparedStatement cityObjectPs;
private final CityGMLExportHelper helper;
private final ExportManager manager;
private final Connection connection;
private final String module;
public RefurbishmentMeasureExporter(Connection connection, CityGMLExportHelper helper, ExportManager manager) throws CityGMLExportException, SQLException {
this.connection = connection;
this.helper = helper;
this.manager = manager;
String select = "SELECT id, type, type_codespace, start_date, end_date, library_code, library_code_codespace, building_id, building_partition_id FROM " +
helper.getTableNameWithSchema(manager.getSchemaMapper().getTableName(ADETable.REFURBISHMENT_MEASURE)) +
" WHERE building_id = ?";
module = EnergyADEModule.v2_0.getNamespaceURI();
ps = connection.prepareStatement(select);
// Prepare statement for cityobject table
String cityObjectSelect = "SELECT gmlid, description FROM " +
helper.getTableNameWithSchema("cityobject") +
" WHERE id = ?";
cityObjectPs = connection.prepareStatement(cityObjectSelect);
}
public Collection<RefurbishmentMeasureProperty> doExport(long objectId, AbstractType<?> objectType, ProjectionFilter projectionFilter) throws SQLException {
ps.setLong(1, objectId); // Set the building_id parameter
Collection<RefurbishmentMeasureProperty> result = new ArrayList<>();
try (ResultSet rs = ps.executeQuery()) {
while (rs.next()) {
long refurbId = rs.getLong("id");
RefurbishmentMeasure refurbishmentMeasure = new RefurbishmentMeasure();
// Fetch gmlid and description from cityobject
cityObjectPs.setLong(1, refurbId);
try (ResultSet cityObjectRs = cityObjectPs.executeQuery()) {
if (cityObjectRs.next()) {
String gmlId = cityObjectRs.getString("gmlid");
if (!cityObjectRs.wasNull()) {
refurbishmentMeasure.setId(gmlId);
}
String description = cityObjectRs.getString("description");
if (!cityObjectRs.wasNull()) {
StringOrRef descriptionType = new StringOrRef();
descriptionType.setValue(description);
refurbishmentMeasure.setDescription(descriptionType);
}
}
}
// Set type
if (projectionFilter.containsProperty("type", module)) {
String type = rs.getString("type");
String typeCodespace = rs.getString("type_codespace");
if (type != null || typeCodespace != null) {
Code refMeasureType = new Code();
if (type != null) {
refMeasureType.setValue(type);
}
if (typeCodespace != null) {
refMeasureType.setCodeSpace(typeCodespace);
}
refurbishmentMeasure.setType(refMeasureType);
}
}
// Set start date
if (projectionFilter.containsProperty("startDate", module)) {
Date start_date = rs.getDate("start_date");
if (start_date != null) {
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(start_date);
XMLGregorianCalendar xmlCal = DatatypeFactory.newInstance().newXMLGregorianCalendar(cal);
//xmlCal.setTimezone(DatatypeConstants.FIELD_UNDEFINED);
refurbishmentMeasure.setStartDate(xmlCal);
}
}
// Set end date
if (projectionFilter.containsProperty("endDate", module)) {
Date end_date = rs.getDate("end_date");
if (end_date != null) {
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(end_date);
XMLGregorianCalendar xmlCal = DatatypeFactory.newInstance().newXMLGregorianCalendar(cal);
// xmlCal.setTimezone(DatatypeConstants.FIELD_UNDEFINED);
refurbishmentMeasure.setEndDate(xmlCal);
}
}
// Set library code
if (projectionFilter.containsProperty("libraryCode", module)) {
String library_code = rs.getString("library_code");
String library_code_codespace = rs.getString("library_code_codespace");
if (library_code != null || library_code_codespace != null) {
Code library = new Code();
if (library_code != null) {
library.setValue(library_code);
}
if (library_code_codespace != null) {
library.setCodeSpace(library_code_codespace);
}
refurbishmentMeasure.setLibraryCode(library);
}
}
// Wrap in RefurbishmentMeasureProperty
RefurbishmentMeasureProperty refurbProp = new RefurbishmentMeasureProperty();
refurbProp.setRefurbishmentMeasure(refurbishmentMeasure);
result.add(refurbProp);
}
return result;
} catch (DatatypeConfigurationException e) {
throw new SQLException("Failed to convert date: " + e.getMessage(), e);
} catch (SQLException e) {
throw e;
}
}
@Override
public void close() throws CityGMLExportException, SQLException {
ps.close();
cityObjectPs.close();
}
}
\ No newline at end of file
package de.stuttgart.hft.ade.energy2.exporter;
import de.stuttgart.hft.ade.energy2.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.energy.model.core.AbstractResource;
import org.citygml4j.ade.energy.model.supportingClasses.*;
import org.citygml4j.model.gml.basicTypes.Code;
import org.citygml4j.model.gml.basicTypes.Measure;
import org.sig3d.citygml.energyADE2beta7.ResourceStatusValueType;
import java.sql.*;
import java.util.ArrayList;
import java.util.Collection;
public class ResourceExporter implements ADEExporter {
private final CityGMLExportHelper helper;
private TimeSeriesExporter timeSeriesExporter;
private PreparedStatement ps;
public ResourceExporter(Connection connection, CityGMLExportHelper helper, ExportManager manager)
throws CityGMLExportException, SQLException {
this.helper = helper;
String tableName = helper.getTableNameWithSchema(manager.getSchemaMapper().getTableName(ADETable.RESOURCE));
String sql = "SELECT " + getColumnNames() + " FROM " + tableName + " WHERE cityobject_id = ?";
ps = connection.prepareStatement(sql);
timeSeriesExporter = manager.getExporter(TimeSeriesExporter.class);
}
public Collection<AbstractResource> doExport(long cityObjectId) throws CityGMLExportException, SQLException {
ps.setLong(1, cityObjectId);
try (ResultSet rs = ps.executeQuery()) {
Collection<AbstractResource> result = new ArrayList<>();
while (rs.next()) {
long objectId = rs.getLong(1);
int objectClassId = rs.getInt(2);
if (rs.wasNull()) {
continue;
}
AbstractResource resource = helper.createObject(objectId, objectClassId, AbstractResource.class);
if (resource == null) {
helper.logOrThrowErrorMessage("Failed to instantiate " + helper.getObjectSignature(objectClassId, cityObjectId) + " as resource object.");
continue;
}
ObjectType objectType = helper.getObjectType(objectClassId);
ProjectionFilter projectionFilter = helper.getProjectionFilter(objectType);
// Set AbstractResource properties
String statusValue = rs.getString(7);
if (statusValue != null) {
resource.setStatus(ResourceStatusValueType.fromValue(statusValue));
}
String operationValue = rs.getString(8);
String operationCodeSpace = rs.getString(9);
if (operationValue != null || operationCodeSpace != null) {
Code operationType = new Code();
operationType.setValue(operationValue);
operationType.setCodeSpace(operationCodeSpace);
resource.setOperationType(operationType);
}
String amountValue = rs.getString(11);
String amountCodeSpace = rs.getString(12);
if (amountValue != null || amountCodeSpace != null) {
Code amountType = new Code();
amountType.setValue(amountValue);
amountType.setCodeSpace(amountCodeSpace);
resource.setAmountType(amountType);
}
double amountVal = rs.getDouble(13);
if (!rs.wasNull()) {
Measure amount = new Measure();
amount.setValue(amountVal);
String uom = rs.getString(14);
if (uom != null) {
amount.setUom(uom);
}
amount.setParent(resource);
resource.setAmount(amount);
}
int normalized = rs.getInt(16);
if (!rs.wasNull()) {
resource.setAmountNormalized(normalized == 1);
}
String normalizationParam = rs.getString(17);
if (normalizationParam != null) {
resource.setNormalizationParameter(normalizationParam);
}
double normVal = rs.getDouble(18);
if (!rs.wasNull()) {
Measure normalizationValue = new Measure();
normalizationValue.setParent(resource);
normalizationValue.setValue(normVal);
String uom = rs.getString(19);
if (uom != null) {
normalizationValue.setUom(uom);
}
resource.setNormalizationValue(normalizationValue);
}
double co2Val = rs.getDouble(20);
if (!rs.wasNull()) {
Measure co2Equivalent = new Measure();
co2Equivalent.setValue(co2Val);
String uom = rs.getString(21);
if (uom != null) {
co2Equivalent.setUom(uom);
}
resource.setCo2Equivalent(co2Equivalent);
}
int year = rs.getInt(10);
if (!rs.wasNull()) {
resource.setYear(year);
}
double costVal = rs.getDouble(22);
if (!rs.wasNull()) {
Measure costsMoney = new Measure();
costsMoney.setValue(costVal);
String uom = rs.getString(23);
if (uom != null) {
costsMoney.setUom(uom);
}
resource.setCostsMoney(costsMoney);
}
double yieldVal = rs.getDouble(24);
if (!rs.wasNull()) {
Measure yieldsMoney = new Measure();
yieldsMoney.setValue(yieldVal);
String uom = rs.getString(25);
if (uom != null) {
yieldsMoney.setUom(uom);
}
resource.setYieldsMoney(yieldsMoney);
}
long timeSeriesId = rs.getLong(15);
if (timeSeriesId != 0 && timeSeriesExporter != null) {
Collection<AbstractTimeSeries> timeSeries = timeSeriesExporter.doExport(timeSeriesId);
if (timeSeries != null && !timeSeries.isEmpty()) {
AbstractTimeSeries firstSeries = timeSeries.iterator().next();
if (firstSeries != null) {
resource.setTimeDependentAmount(new AbstractTimeSeriesProperty(firstSeries));
}
}
}
if (resource instanceof Energy) {
Energy energy = (Energy) resource;
String energyType = rs.getString(3);
String energyTypeCodeSpace = rs.getString(4);
if (energyType != null || energyTypeCodeSpace != null) {
Code energyTypeValue = new Code();
energyTypeValue.setValue(energyType);
energyTypeValue.setCodeSpace(energyTypeCodeSpace);
energy.setEnergyTypeValue(energyTypeValue);
}
String energyEndUse = rs.getString(5);
String energyEndUseCodeSpace = rs.getString(6);
if (energyEndUse != null || energyEndUseCodeSpace != null) {
Code energyEndUseValue = new Code();
energyEndUseValue.setValue(energyEndUse);
energyEndUseValue.setCodeSpace(energyEndUseCodeSpace);
energy.setEnergyEndUseValue(energyEndUseValue);
}
String energyCarrier = rs.getString(26);
String energyCarrierCodeSpace = rs.getString(27);
if (energyCarrier != null || energyCarrierCodeSpace != null) {
Code energyCarrierValue = new Code();
energyCarrierValue.setValue(energyCarrier);
energyCarrierValue.setCodeSpace(energyCarrierCodeSpace);
energy.setEnergyCarrierValue(energyCarrierValue);
}
double maxLoadVal = rs.getDouble(28);
if (!rs.wasNull()) {
Measure maximumLoad = new Measure();
maximumLoad.setValue(maxLoadVal);
String uom = rs.getString(29);
if (uom != null) {
maximumLoad.setUom(uom);
}
energy.setMaximumLoad(maximumLoad);
}
String energySource = rs.getString(30);
String energySourceCodeSpace = rs.getString(31);
if (energySource != null || energySourceCodeSpace != null) {
Code energySourceValue = new Code();
energySourceValue.setValue(energySource);
energySourceValue.setCodeSpace(energySourceCodeSpace);
energy.setEnergySourceValue(energySourceValue);
}
} else if (resource instanceof Water) {
Water water = (Water) resource;
String waterType = rs.getString(3);
String waterTypeCodeSpace = rs.getString(4);
if (waterType != null || waterTypeCodeSpace != null) {
Code waterTypeValue = new Code();
waterTypeValue.setValue(waterType);
waterTypeValue.setCodeSpace(waterTypeCodeSpace);
water.setWaterTypeValue(waterTypeValue);
}
String waterEndUse = rs.getString(5);
String waterEndUseCodeSpace = rs.getString(6);
if (waterEndUse != null || waterEndUseCodeSpace != null) {
Code waterEndUseValue = new Code();
waterEndUseValue.setValue(waterEndUse);
waterEndUseValue.setCodeSpace(waterEndUseCodeSpace);
water.setWaterEndUseValue(waterEndUseValue);
}
} else if (resource instanceof Waste) {
Waste waste = (Waste) resource;
String wasteType = rs.getString(3);
String wasteTypeCodeSpace = rs.getString(4);
if (wasteType != null || wasteTypeCodeSpace != null) {
Code wasteTypeValue = new Code();
wasteTypeValue.setValue(wasteType);
wasteTypeValue.setCodeSpace(wasteTypeCodeSpace);
waste.setWasteTypeValue(wasteTypeValue);
}
String wasteEndUse = rs.getString(5);
String wasteEndUseCodeSpace = rs.getString(6);
if (wasteEndUse != null || wasteEndUseCodeSpace != null) {
Code wasteEndUseValue = new Code();
wasteEndUseValue.setValue(wasteEndUse);
wasteEndUseValue.setCodeSpace(wasteEndUseCodeSpace);
waste.setWasteEndUseValue(wasteEndUseValue);
}
int isDangerous = rs.getInt(32);
if (!rs.wasNull()) {
waste.setAmountNormalized(isDangerous == 1);
}
int recyclable = rs.getInt(33);
if (!rs.wasNull()) {
waste.setRecyclable(recyclable == 1);
}
} else if (resource instanceof ConstructionMaterial) {
ConstructionMaterial cm = (ConstructionMaterial) resource;
String type = rs.getString(3);
String typeCodeSpace = rs.getString(4);
if (type != null || typeCodeSpace != null) {
Code typeValue = new Code();
typeValue.setValue(type);
typeValue.setCodeSpace(typeCodeSpace);
cm.setConstructionMaterialTypeValue(typeValue);
}
String endUse = rs.getString(5);
String endUseCodeSpace = rs.getString(6);
if (endUse != null || endUseCodeSpace != null) {
Code endUseValue = new Code();
endUseValue.setValue(endUse);
endUseValue.setCodeSpace(endUseCodeSpace);
cm.setConstructionMaterialEndUseValue(endUseValue);
}
} else if (resource instanceof Food) {
Food food = (Food) resource;
String foodType = rs.getString(3);
String foodTypeCodeSpace = rs.getString(4);
if (foodType != null || foodTypeCodeSpace != null) {
Code foodTypeValue = new Code();
foodTypeValue.setValue(foodType);
foodTypeValue.setCodeSpace(foodTypeCodeSpace);
food.setFoodTypeValue(foodTypeValue);
}
String foodEndUse = rs.getString(5);
String foodEndUseCodeSpace = rs.getString(6);
if (foodEndUse != null || foodEndUseCodeSpace != null) {
Code foodEndUseValue = new Code();
foodEndUseValue.setValue(foodEndUse);
foodEndUseValue.setCodeSpace(foodEndUseCodeSpace);
food.setFoodEndUseValue(foodEndUseValue);
}
} else if (resource instanceof OtherResource) {
OtherResource other = (OtherResource) resource;
String otherType = rs.getString(3);
String otherTypeCodeSpace = rs.getString(4);
if (otherType != null || otherTypeCodeSpace != null) {
Code otherTypeValue = new Code();
otherTypeValue.setValue(otherType);
otherTypeValue.setCodeSpace(otherTypeCodeSpace);
other.setOtherResourceTypeValue(otherTypeValue);
}
String otherEndUse = rs.getString(5);
String otherEndUseCS = rs.getString(6);
if (otherEndUse != null || otherEndUseCS != null) {
Code otherEndUseValue = new Code();
otherEndUseValue.setValue(otherEndUse);
otherEndUseValue.setCodeSpace(otherEndUseCS);
other.setOtherResourceEndUseValue(otherEndUseValue);
}
}
result.add(resource);
}
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";
}
}
\ No newline at end of file
package de.stuttgart.hft.ade.energy2.importer;
import de.stuttgart.hft.ade.energy2.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.energy.model.core.CityObjectRelation;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class CityObjectRelationImporter implements ADEImporter {
private final Connection connection;
private final CityGMLImportHelper helper;
private GeometryConverter geometryConverter;
private PreparedStatement ps;
private int batchCounter;
public CityObjectRelationImporter(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.UTL_NTW_CONNECTION)) + " " +
"(id, ng2_cityobject_id, cityobject_id, relation_type, relation_type_codespace) " +
"VALUES (?, ?, ?, ?, ?)");
geometryConverter = helper.getGeometryConverter();
}
public void doImport(CityObjectRelation cityObjectRelation, long objectId, AbstractObjectType<?> objectType, ForeignKeys foreignKeys) throws CityGMLImportException, SQLException {
ps.setLong(1, objectId);
if (cityObjectRelation.isSetRelation()) {
ps.setString(4, cityObjectRelation.getRelationType().getValue());
ps.setString(5, cityObjectRelation.getRelationType().getCodeSpace());
}
//set correctly
ps.setLong(2, objectType.getObjectClassId());
ps.setLong(3, objectType.getObjectClassId());
ps.addBatch();
if (++batchCounter == helper.getDatabaseAdapter().getMaxBatchSize())
helper.executeBatch(objectType);
}
@Override
public void executeBatch() throws CityGMLImportException, SQLException {
if (batchCounter > 0) {
ps.executeBatch();
batchCounter = 0;
}
}
@Override
public void close() throws CityGMLImportException, SQLException {
ps.close();
}
}
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