package com.ximple.eofms.jobs.context.postgis; import java.io.IOException; import java.math.BigDecimal; import java.math.RoundingMode; import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; import java.sql.PreparedStatement; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Arrays; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.commons.transaction.util.CommonsLoggingLogger; import org.apache.commons.transaction.util.LoggerFacade; import org.geotools.data.DataStore; import org.geotools.data.Transaction; import org.geotools.feature.Feature; import org.geotools.feature.FeatureType; import org.geotools.feature.FeatureTypeBuilder; import org.geotools.feature.IllegalAttributeException; import org.geotools.feature.SchemaException; import org.postgresql.util.PSQLException; import com.vividsolutions.jts.geom.Coordinate; import com.vividsolutions.jts.geom.Envelope; import com.vividsolutions.jts.geom.Geometry; import com.vividsolutions.jts.geom.GeometryFactory; import com.ximple.eofms.util.DefaultColorTable; import com.ximple.eofms.util.FeatureTypeBuilderUtil; import com.ximple.eofms.util.TPCLIDConverter; import com.ximple.eofms.util.TWD97GeometryConverterDecorator; import com.ximple.eofms.util.TWDDatumConverter; import com.ximple.io.dgn7.Element; import com.ximple.io.dgn7.FrammeAttributeData; import com.ximple.io.dgn7.TextElement; import com.ximple.io.dgn7.UserAttributeData; public class IndexDgnConvertPostGISJobContext extends AbstractDgnToPostGISJobContext { static final Log logger = LogFactory.getLog(IndexDgnConvertPostGISJobContext.class); static final LoggerFacade sLogger = new CommonsLoggingLogger(logger); static final GeometryFactory geometryFactory = new GeometryFactory(); TWD97GeometryConverterDecorator convertDecorator = new TWD97GeometryConverterDecorator(); private HashMap> txFeaturesContext = new HashMap>(); private FeatureTypeBuilder typeBuilderPnt = null; private FeatureTypeBuilder typeBuilderRect = null; private FeatureTypeBuilder typeBuilderSmallRect = null; private FeatureType featureType = null; private FeatureType featureType2 = null; private FeatureType featureType3 = null; private boolean dropTableMode = true; private int accumulate = 0; public IndexDgnConvertPostGISJobContext(String dataPath, DataStore targetDataStore, String targetSchema) { super(dataPath, targetDataStore, targetSchema); } public void putFeatureCollection(Element element) throws IllegalAttributeException, SchemaException { if (!(element instanceof TextElement)) { return; } Feature feature = createFeature((TextElement) element); if (feature == null) { logger.info("cannot craete feature." + element.toString() + "'" + ((TextElement) element).getText() + "'"); return; } if (!txFeaturesContext.containsKey(feature.getFeatureType())) { txFeaturesContext.put(feature.getFeatureType(), new ArrayList()); } ArrayList arrayList = txFeaturesContext.get(feature.getFeatureType()); arrayList.add(feature); feature = createFeature2((TextElement) element); if (feature == null) { logger.info("cannot craete feature2." + element.toString() + "'" + ((TextElement) element).getText() + "'"); return; } if (!txFeaturesContext.containsKey(feature.getFeatureType())) { txFeaturesContext.put(feature.getFeatureType(), new ArrayList()); } arrayList = txFeaturesContext.get(feature.getFeatureType()); arrayList.add(feature); Feature[] features = createFeature3((TextElement) element); if (features == null) { logger.info("cannot craete feature3." + element.toString() + "'" + ((TextElement) element).getText() + "'"); return; } if (!txFeaturesContext.containsKey(features[0].getFeatureType())) { txFeaturesContext.put(features[0].getFeatureType(), new ArrayList()); } arrayList = txFeaturesContext.get(features[0].getFeatureType()); arrayList.addAll(Arrays.asList(features)); accumulate++; if (accumulate > BATCHSIZE) { commitTransaction(); } } public void startTransaction() { } public void commitTransaction() { if (!txFeaturesContext.isEmpty()) { logger.debug("Transaction size = " + txFeaturesContext.size()); } else { logger.debug("Transaction is empty."); } if (!txFeaturesContext.isEmpty()) { updateDataStore(); } } public void rollbackTransaction() { } private void updateDataStore() { Iterator it = txFeaturesContext.keySet().iterator(); try { while (it.hasNext()) { FeatureType featureType = it.next(); logger.debug("Begin Save PostGIS:" + featureType.getTypeName()); String bindingStmt = makePrepareInsertSql(featureType); ArrayList features = txFeaturesContext.get(featureType); Connection conn = getConnection(); boolean autoCommit = conn.getAutoCommit(); conn.setAutoCommit(true); PreparedStatement pstmt = conn.prepareStatement(bindingStmt); for (Feature feature : features) { // currentStmt = feature; // Statement stmt = conn.createStatement(); try { // stmt.execute(feature); bindFeatureParameters(pstmt, feature); pstmt.execute(); } catch (PSQLException e) { if (bindingStmt != null) { logger.error("Execute:" + bindingStmt); } logger.error(e.getServerErrorMessage()); logger.error(e.getMessage(), e); /* } finally { stmt.close(); */ } } /* if ((i % BATCHSIZE) != 0) { stmt.executeBatch(); } stmt.close(); */ pstmt.close(); features.clear(); conn.setAutoCommit(autoCommit); logger.debug("End Save PostGIS:" + featureType.getTypeName()); } accumulate = 0; } catch (PSQLException e) { logger.error(e.getServerErrorMessage()); logger.error(e.getMessage(), e); } catch (SQLException e) { logger.error(e.getMessage(), e); } } public void closeFeatureWriter() throws IOException { txFeaturesContext.clear(); /* for (FeatureWriter featureWriter : this.featuresWriterContext.values()) { featureWriter.close(); } this.featuresWriterContext.clear(); */ } public FeatureType createFeatureElement(String featureName) throws SchemaException { if (typeBuilderRect == null) { typeBuilderRect = FeatureTypeBuilderUtil.createNormalIndexFeatureTypeBuilder(featureName); if (isExistFeature(typeBuilderRect.getFeatureType())) { try { Connection conn = targetDataStore.getConnection(Transaction.AUTO_COMMIT); if (dropTableMode) { try { dropGeometryColumn(conn, featureName, typeBuilderRect.getFeatureType().getDefaultGeometry().getLocalName()); } catch (PSQLException e) { logger.debug(e.getMessage(), e); } try { dropTable(conn, featureName); } catch (PSQLException e) { logger.debug(e.getMessage(), e); } ArrayList schemaTexts = createNewSchemaTexts(typeBuilderRect.getFeatureType()); for (String stmtText : schemaTexts) { Statement stmt = conn.createStatement(); stmt.execute(stmtText); stmt.close(); } } else { deleteTable(conn, featureName); } conn.close(); } catch (IOException e) { logger.warn(e.getMessage(), e); } catch (SQLException e) { logger.warn(e.getMessage(), e); } } else { try { Connection conn = targetDataStore.getConnection(Transaction.AUTO_COMMIT); ArrayList schemaTexts = createNewSchemaTexts(typeBuilderRect.getFeatureType()); for (String stmtText : schemaTexts) { Statement stmt = conn.createStatement(); stmt.execute(stmtText); stmt.close(); } conn.close(); } catch (IOException e) { logger.warn(e.getMessage(), e); } catch (SQLException e) { logger.warn(e.getMessage(), e); } } } return typeBuilderRect.getFeatureType(); } public FeatureType createFeatureElement2(String featureName) throws SchemaException { if (typeBuilderPnt == null) { typeBuilderPnt = FeatureTypeBuilderUtil.createNormalIndexTextFeatureTypeBuilder(featureName); if (isExistFeature(typeBuilderPnt.getFeatureType())) { try { Connection conn = targetDataStore.getConnection(Transaction.AUTO_COMMIT); if (dropTableMode) { dropGeometryColumn(conn, featureName, typeBuilderPnt.getFeatureType().getDefaultGeometry().getLocalName()); dropTable(conn, featureName); ArrayList schemaTexts = createNewSchemaTexts(typeBuilderPnt.getFeatureType()); for (String stmtText : schemaTexts) { Statement stmt = conn.createStatement(); stmt.execute(stmtText); stmt.close(); } } else { deleteTable(conn, featureName); } conn.close(); } catch (IOException e) { logger.warn(e.getMessage(), e); } catch (SQLException e) { logger.warn(e.getMessage(), e); } } else { try { Connection conn = targetDataStore.getConnection(Transaction.AUTO_COMMIT); ArrayList schemaTexts = createNewSchemaTexts(typeBuilderPnt.getFeatureType()); for (String stmtText : schemaTexts) { Statement stmt = conn.createStatement(); stmt.execute(stmtText); stmt.close(); } conn.close(); } catch (IOException e) { logger.warn(e.getMessage(), e); } catch (SQLException e) { logger.warn(e.getMessage(), e); } } } return typeBuilderPnt.getFeatureType(); } public FeatureType createFeatureElement3(String featureName) throws SchemaException { if (typeBuilderSmallRect == null) { typeBuilderSmallRect = FeatureTypeBuilderUtil.createNormalIndexFeatureTypeBuilder(featureName); if (isExistFeature(typeBuilderSmallRect.getFeatureType())) { try { Connection conn = targetDataStore.getConnection(Transaction.AUTO_COMMIT); if (dropTableMode) { try { dropGeometryColumn(conn, featureName, typeBuilderSmallRect.getFeatureType().getDefaultGeometry().getLocalName()); } catch (PSQLException e) { logger.debug(e.getMessage(), e); } try { dropTable(conn, featureName); } catch (PSQLException e) { logger.debug(e.getMessage(), e); } ArrayList schemaTexts = createNewSchemaTexts(typeBuilderSmallRect.getFeatureType()); for (String stmtText : schemaTexts) { Statement stmt = conn.createStatement(); stmt.execute(stmtText); stmt.close(); } } else { deleteTable(conn, featureName); } conn.close(); } catch (IOException e) { logger.warn(e.getMessage(), e); } catch (SQLException e) { logger.warn(e.getMessage(), e); } } else { try { Connection conn = targetDataStore.getConnection(Transaction.AUTO_COMMIT); ArrayList schemaTexts = createNewSchemaTexts(typeBuilderSmallRect.getFeatureType()); for (String stmtText : schemaTexts) { Statement stmt = conn.createStatement(); stmt.execute(stmtText); stmt.close(); } conn.close(); } catch (IOException e) { logger.warn(e.getMessage(), e); } catch (SQLException e) { logger.warn(e.getMessage(), e); } } } return typeBuilderSmallRect.getFeatureType(); } public Feature createFeature(FeatureType featureType, Element element) throws IllegalAttributeException { DefaultColorTable colorTable = (DefaultColorTable) DefaultColorTable.getInstance(); if (element instanceof TextElement) { TextElement textElement = (TextElement) element; String tpclid = textElement.getText(); Envelope extent = TPCLIDConverter.convertTpclIdToEnvelope(tpclid); Geometry geom = geometryFactory.createPolygon(geometryFactory.createLinearRing(new Coordinate[] { TWDDatumConverter.fromTM2ToTWD97(new Coordinate(extent.getMinX(), extent.getMinY())), TWDDatumConverter.fromTM2ToTWD97(new Coordinate(extent.getMaxX(), extent.getMinY())), TWDDatumConverter.fromTM2ToTWD97(new Coordinate(extent.getMaxX(), extent.getMaxY())), TWDDatumConverter.fromTM2ToTWD97(new Coordinate(extent.getMinX(), extent.getMaxY())), TWDDatumConverter.fromTM2ToTWD97(new Coordinate(extent.getMinX(), extent.getMinY())), }), null); return featureType.create(new Object[]{ geom, extent.getMinX(), extent.getMinY(), extent.getMaxX(), extent.getMaxY(), tpclid, colorTable.getColorCode(textElement.getColorIndex()), textElement.getWeight(), textElement.getLineStyle() }); } return null; } public Feature createFeature2(FeatureType featureType, Element element) throws IllegalAttributeException { DefaultColorTable colorTable = (DefaultColorTable) DefaultColorTable.getInstance(); if (element instanceof TextElement) { Feature feature = null; TextElement txtElement = (TextElement) element; double angle = txtElement.getRotationAngle(); angle = BigDecimal.valueOf(angle).setScale(3, RoundingMode.HALF_UP).doubleValue(); convertDecorator.setConverter(txtElement); Geometry gobj = convertDecorator.toGeometry(geometryFactory); if (gobj != null) feature = featureType.create(new Object[]{ gobj, colorTable.getColorCode(txtElement.getColorIndex()), txtElement.getWeight(), txtElement.getLineStyle(), txtElement.getJustification(), txtElement.getTextHeight(), txtElement.getTextWidth(), angle, txtElement.getText() }); return feature; } return null; } public Feature[] createFeature3(FeatureType featureType, Element element) throws IllegalAttributeException { DefaultColorTable colorTable = (DefaultColorTable) DefaultColorTable.getInstance(); if (element instanceof TextElement) { TextElement textElement = (TextElement) element; String tpclid = textElement.getText(); Feature[] result = new Feature[4]; Envelope extent = TPCLIDConverter.convertTpclIdToEnvelope(tpclid); for (int i = 0; i < 4; i++) { char mapSubId = TPCLIDConverter.intToAscii(65 + i); int dx = (i % 2) * TPCLIDConverter.SX600; int dy = (i / 2) * TPCLIDConverter.SY600; Geometry geom = geometryFactory.createPolygon(geometryFactory.createLinearRing(new Coordinate[] { TWDDatumConverter.fromTM2ToTWD97(new Coordinate( extent.getMinX() + dx, extent.getMaxY() - TPCLIDConverter.SY600 - dy)), TWDDatumConverter.fromTM2ToTWD97(new Coordinate( extent.getMinX() + TPCLIDConverter.SX600 + dx, extent.getMaxY() - TPCLIDConverter.SY600 - dy)), TWDDatumConverter.fromTM2ToTWD97(new Coordinate( extent.getMinX() + TPCLIDConverter.SX600 + dx, extent.getMaxY() - dy)), TWDDatumConverter.fromTM2ToTWD97(new Coordinate( extent.getMinX() + dx, extent.getMaxY() - dy)), TWDDatumConverter.fromTM2ToTWD97(new Coordinate( extent.getMinX() + dx, extent.getMaxY() - TPCLIDConverter.SY600 - dy)), }), null); result[i] = featureType.create(new Object[]{ geom, extent.getMinX(), extent.getMinY(), extent.getMaxX(), extent.getMaxY(), tpclid + mapSubId, colorTable.getColorCode(textElement.getColorIndex()), textElement.getWeight(), textElement.getLineStyle() }); } return result; } return null; } private Feature createFeature(TextElement element) throws SchemaException, IllegalAttributeException { if (featureType == null) { String dgnname = getFilename().toLowerCase(); int i = dgnname.lastIndexOf("."); if (i != -1) { dgnname = dgnname.substring(0, i); } featureType = createFeatureElement(dgnname.toLowerCase()); } return createFeature(featureType, element); } private Feature createFeature2(TextElement element) throws SchemaException, IllegalAttributeException { if (featureType2 == null) { String dgnname = getFilename().toLowerCase(); int i = dgnname.lastIndexOf("."); if (i != -1) { dgnname = dgnname.substring(0, i); } dgnname = dgnname + "_p"; featureType2 = createFeatureElement2(dgnname.toLowerCase()); } return createFeature2(featureType2, element); } private Feature[] createFeature3(TextElement element) throws SchemaException, IllegalAttributeException { if (featureType3 == null) { String dgnname = getFilename().toLowerCase(); int i = dgnname.lastIndexOf("."); if (i != -1) { dgnname = dgnname.substring(0, i); } dgnname = dgnname + "_s"; featureType3 = createFeatureElement3(dgnname.toLowerCase()); } return createFeature3(featureType3, element); } protected FrammeAttributeData getFeatureLinkage(Element element) { if (!element.hasUserAttributeData()) return null; List usrDatas = element.getUserAttributeData(); for (UserAttributeData anUsrData : usrDatas) { if (anUsrData instanceof FrammeAttributeData) { return (FrammeAttributeData) anUsrData; } } return null; } public Log getLogger() { return logger; } public boolean isDropTableMode() { return dropTableMode; } public void setDropTableMode(boolean dropTableMode) { this.dropTableMode = dropTableMode; } public void clearOutputDatabase() { } }