package com.ximple.eofms.jobs.context.mysql; import java.io.IOException; import java.math.BigDecimal; import java.math.RoundingMode; import java.net.MalformedURLException; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.TreeMap; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.commons.transaction.memory.PessimisticMapWrapper; import org.apache.commons.transaction.util.CommonsLoggingLogger; import org.apache.commons.transaction.util.LoggerFacade; import org.geotools.data.DataStore; import org.geotools.data.FeatureWriter; import org.geotools.data.Transaction; import org.opengis.feature.IllegalAttributeException; import org.geotools.feature.SchemaException; import com.vividsolutions.jts.geom.Geometry; import com.vividsolutions.jts.geom.GeometryFactory; import com.ximple.eofms.util.DefaultColorTable; import com.ximple.eofms.util.EPSG3825GeometryConverterDecorator; import com.ximple.eofms.util.EPSG3826GeometryConverterDecorator; import com.ximple.eofms.util.FeatureTypeBuilderUtil; import com.ximple.eofms.util.GeometryConverterDecorator; import com.ximple.io.dgn7.ArcElement; import com.ximple.io.dgn7.ComplexChainElement; import com.ximple.io.dgn7.Element; import com.ximple.io.dgn7.EllipseElement; import com.ximple.io.dgn7.FrammeAttributeData; import com.ximple.io.dgn7.LineElement; import com.ximple.io.dgn7.LineStringElement; import com.ximple.io.dgn7.ShapeElement; import com.ximple.io.dgn7.TextElement; import com.ximple.io.dgn7.TextNodeElement; import com.ximple.io.dgn7.UserAttributeData; import org.geotools.feature.simple.SimpleFeatureBuilder; import org.geotools.feature.simple.SimpleFeatureTypeBuilder; import org.geotools.geometry.jts.JTSFactoryFinder; import org.opengis.feature.simple.SimpleFeature; import org.opengis.feature.simple.SimpleFeatureType; public class GeneralDgnConvertMySQLJobContext extends AbstractDgnToMySQLJobContext { static final Log logger = LogFactory.getLog(GeneralDgnConvertMySQLJobContext.class); static final LoggerFacade sLogger = new CommonsLoggingLogger(logger); static final GeometryFactory geometryFactory = JTSFactoryFinder.getGeometryFactory(null); private String dataOut = null; private HashMap> featuresContext = new HashMap>(); private HashMap featuresWriterContext = new HashMap(); private PessimisticMapWrapper txFeaturesContext; private TreeMap featureTypes = new TreeMap(); private String featureBaseName = null; private boolean withIndex = false; public GeneralDgnConvertMySQLJobContext(String dataPath, DataStore targetDataStore, boolean profileMode, boolean useTransform) { super(dataPath, targetDataStore, profileMode, useTransform); txFeaturesContext = new PessimisticMapWrapper(featuresContext, sLogger); } public void putFeatureCollection(Element element) throws IllegalAttributeException, SchemaException { SimpleFeatureType ft = lookupFeatureType(element); if (ft != null) { SimpleFeature feature = createFeature(ft, element); if (feature == null) { if (element instanceof TextElement) logger.info("cannot craete feature." + element.toString() + "'" + ((TextElement) element).getText() + "'"); else if (element instanceof ShapeElement) logger.info("cannot craete feature." + element.toString() + "'" + ((ShapeElement) element).getVerticeSize() + "'" + ((ShapeElement) element).getStartPoint()); else if (element instanceof LineStringElement) logger.info("cannot craete feature." + element.toString() + "'" + ((LineStringElement) element).getVerticeSize() + "'" + ((LineStringElement) element).getStartPoint()); else if (element instanceof ArcElement) logger.info("cannot craete feature." + element.toString() + "'" + ((ArcElement) element).getOrigin().toString() + "'" + ((ArcElement) element).getRotationAngle()); return; } if (!txFeaturesContext.containsKey(feature.getFeatureType())) { txFeaturesContext.put(feature.getFeatureType(), new ArrayList()); } ArrayList arrayList = (ArrayList) txFeaturesContext.get(feature.getFeatureType()); arrayList.add(feature); } else { logger.info("Unknown Element :" + element.getType() + ", lv=" + element.getLevelIndex()); } } public void startTransaction() { } public void commitTransaction() { if (!txFeaturesContext.isEmpty()) { logger.debug("Transaction size = " + txFeaturesContext.size()); //txFeaturesContext.commitTransaction(); } else { logger.debug("Transaction is empty."); } if (!featuresContext.isEmpty()) { updateDataStore(); } } public void rollbackTransaction() { //txFeaturesContext.rollbackTransaction(); if (!featuresContext.isEmpty()) { updateDataStore(); } } private void updateDataStore() { if (isProfileMode()) markUpdateTime(); Iterator it = featuresContext.keySet().iterator(); try { while (it.hasNext()) { SimpleFeatureType featureType = (SimpleFeatureType) it.next(); logger.debug("Begin Save into OracleSDO:" + featureType.getTypeName()); FeatureWriter writer; if (featuresWriterContext.containsKey(featureType.getTypeName())) { writer = featuresWriterContext.get(featureType.getTypeName()); } else { if (!isExistFeature(featureType)) { targetDataStore.createSchema(featureType); writer = targetDataStore.getFeatureWriter(featureType.getTypeName(), Transaction.AUTO_COMMIT); } else { writer = targetDataStore.getFeatureWriterAppend(featureType.getTypeName(), Transaction.AUTO_COMMIT); } featuresWriterContext.put(featureType.getTypeName(), writer); } ArrayList features = featuresContext.get(featureType); Iterator itFeature = features.iterator(); while (itFeature.hasNext()) { SimpleFeature feature = (SimpleFeature) itFeature.next(); ((SimpleFeature) writer.next()).setAttributes(feature.getAttributes()); } //writer.close(); logger.debug("End Save into OracleSDO:" + featureType.getTypeName()); } featuresContext.clear(); } catch (MalformedURLException e) { logger.error(e.getMessage(), e); } catch (IllegalAttributeException e) { logger.error(e.getMessage(), e); } catch (IOException e) { logger.error(e.getMessage(), e); } finally { if (isProfileMode()) accumulateUpdateTime(); } } public void closeFeatureWriter() throws IOException { for (FeatureWriter featureWriter : this.featuresWriterContext.values()) { featureWriter.close(); } this.featuresWriterContext.clear(); } public SimpleFeatureType createPointFeatureElement(String featureName) throws SchemaException { if (!featureTypes.containsKey(featureName)) { SimpleFeatureTypeBuilder typeBuilder = FeatureTypeBuilderUtil.createNormalPointFeatureTypeBuilder(featureName); SimpleFeatureType featureType = typeBuilder.buildFeatureType(); featureTypes.put(featureName, featureType); } return featureTypes.get(featureName); } public SimpleFeatureType createLineFeatureElement(String featureName) throws SchemaException { if (!featureTypes.containsKey(featureName)) { SimpleFeatureTypeBuilder typeBuilder = FeatureTypeBuilderUtil.createNormalLineFeatureTypeBuilder(featureName); SimpleFeatureType featureType = typeBuilder.buildFeatureType(); featureTypes.put(featureName, featureType); } return featureTypes.get(featureName); } public SimpleFeatureType createArcFeatureElement(String featureName) throws SchemaException { if (!featureTypes.containsKey(featureName)) { SimpleFeatureTypeBuilder typeBuilder = FeatureTypeBuilderUtil.createNormalArcFeatureTypeBuilder(featureName); SimpleFeatureType featureType = typeBuilder.buildFeatureType(); featureTypes.put(featureName, featureType); } return featureTypes.get(featureName); } public SimpleFeatureType createEllipseFeatureElement(String featureName) throws SchemaException { if (!featureTypes.containsKey(featureName)) { SimpleFeatureTypeBuilder typeBuilder = FeatureTypeBuilderUtil.createNormalEllipseFeatureTypeBuilder(featureName); SimpleFeatureType featureType = typeBuilder.buildFeatureType(); featureTypes.put(featureName, featureType); } return featureTypes.get(featureName); } public SimpleFeature createFeature(SimpleFeatureType featureType, Element element) throws IllegalAttributeException { DefaultColorTable colorTable = (DefaultColorTable) DefaultColorTable.getInstance(); GeometryConverterDecorator convertDecorator = FeatureTypeBuilderUtil.lookupDefaultGeometryConverter(); if (element instanceof TextElement) { TextElement textElement = (TextElement) element; convertDecorator.setConverter(textElement); Geometry geom = convertDecorator.toGeometry(geometryFactory); double angle = textElement.getRotationAngle(); String content = textElement.getText(); angle = BigDecimal.valueOf(angle).setScale(3, RoundingMode.HALF_UP).doubleValue(); if (geom != null) { return SimpleFeatureBuilder.build(featureType, new Object[]{ geom, colorTable.getColorCode(textElement.getColorIndex()), textElement.getFontIndex(), textElement.getJustification(), textElement.getTextHeight(), textElement.getTextWidth(), angle, content }, null); } else { logger.info("geometry is null." + element.toString()); } return null; } else if (element instanceof TextNodeElement) { TextNodeElement textNodeElement = (TextNodeElement) element; convertDecorator.setConverter(textNodeElement); Geometry geom = convertDecorator.toGeometry(geometryFactory); double angle = textNodeElement.getRotationAngle(); angle = BigDecimal.valueOf(angle).setScale(3, RoundingMode.HALF_UP).doubleValue(); String[] texts = textNodeElement.getTextArray(); StringBuilder sb = new StringBuilder(); for (String text : texts) { if (sb.length() != 0) sb.append("\n"); sb.append(text); } if (geom != null) { return SimpleFeatureBuilder.build(featureType, new Object[]{ geom, colorTable.getColorCode(textNodeElement.getColorIndex()), textNodeElement.getFontIndex(), textNodeElement.getJustification(), textNodeElement.getTextNodeHeight(), textNodeElement.getTextNodeLength(), angle, sb.toString() }, null); } else { logger.info("geometry is null." + element.toString()); } return null; } else if (element instanceof ShapeElement) { ShapeElement shapeElement = (ShapeElement) element; convertDecorator.setConverter(shapeElement); Geometry geom = convertDecorator.toGeometry(geometryFactory); if (geom != null) { return SimpleFeatureBuilder.build(featureType, new Object[]{ geom, colorTable.getColorCode(shapeElement.getColorIndex()), shapeElement.getWeight(), shapeElement.getLineStyle() }, null); } else { logger.info("geometry is null." + element.toString()); } return null; } else if (element instanceof LineStringElement) { LineStringElement linestring = (LineStringElement) element; convertDecorator.setConverter(linestring); Geometry geom = convertDecorator.toGeometry(geometryFactory); if (geom != null) { return SimpleFeatureBuilder.build(featureType, new Object[]{ geom, colorTable.getColorCode(linestring.getColorIndex()), linestring.getWeight(), linestring.getLineStyle() }, null); } return null; } else if (element instanceof LineElement) { LineElement line = (LineElement) element; convertDecorator.setConverter(line); Geometry geom = convertDecorator.toGeometry(geometryFactory); if (geom != null) { return SimpleFeatureBuilder.build(featureType, new Object[]{ geom, colorTable.getColorCode(line.getColorIndex()), line.getWeight(), line.getLineStyle() }, null); } return null; } else if (element instanceof ArcElement) { ArcElement arcElement = (ArcElement) element; /* logger.fatal("" + arcElement.getPrimary() + ":" + arcElement.getSecondary() + "-" + arcElement.getStartAngle() + ":" + arcElement.getSweepAngle() + ":" + arcElement.getRotationAngle() + ":" + arcElement.getOrigin()); */ convertDecorator.setConverter(arcElement); Geometry geom = convertDecorator.toGeometry(geometryFactory); if (geom != null) { return SimpleFeatureBuilder.build(featureType, new Object[]{ geom, colorTable.getColorCode(arcElement.getColorIndex()), arcElement.getWeight(), arcElement.getLineStyle() }, null); } return null; } else if (element instanceof EllipseElement) { EllipseElement arcElement = (EllipseElement) element; convertDecorator.setConverter(arcElement); Geometry geom = convertDecorator.toGeometry(geometryFactory); if (geom != null) { return SimpleFeatureBuilder.build(featureType, new Object[]{ geom, colorTable.getColorCode(arcElement.getColorIndex()), arcElement.getWeight(), arcElement.getLineStyle() }, null); } return null; } else if (element instanceof ComplexChainElement) { ComplexChainElement complexChainElement = (ComplexChainElement) element; convertDecorator.setConverter(complexChainElement); Geometry geom = convertDecorator.toGeometry(geometryFactory); if (geom != null) { return SimpleFeatureBuilder.build(featureType, new Object[]{ geom, colorTable.getColorCode(complexChainElement.getColorIndex()), complexChainElement.getWeight(), complexChainElement.getLineStyle() }, null); } return null; } return null; } private String getFeatureBaseName() { if (featureBaseName == null) { String dgnname = getFilename().toLowerCase(); int i = dgnname.lastIndexOf("."); if (i != -1) { dgnname = dgnname.substring(0, i); } featureBaseName = dgnname; } return featureBaseName; } private SimpleFeatureType lookupFeatureType(Element element) throws SchemaException, IllegalAttributeException { String typeName; if (element instanceof TextElement) { typeName = getFeatureBaseName() + "_P"; if (!featureTypes.containsKey(typeName)) { featureTypes.put(typeName, createPointFeatureElement(typeName)); } return featureTypes.get(typeName); } else if (element instanceof TextNodeElement) { typeName = getFeatureBaseName() + "_P"; if (!featureTypes.containsKey(typeName)) { featureTypes.put(typeName, createPointFeatureElement(typeName)); } return featureTypes.get(typeName); } else if (element instanceof LineStringElement) { if (element instanceof ShapeElement) { typeName = getFeatureBaseName() + "_R"; if (!featureTypes.containsKey(typeName)) { featureTypes.put(typeName, createLineFeatureElement(typeName)); } return featureTypes.get(typeName); } else { typeName = getFeatureBaseName() + "_L"; if (!featureTypes.containsKey(typeName)) { featureTypes.put(typeName, createLineFeatureElement(typeName)); } return featureTypes.get(typeName); } } else if (element instanceof LineElement) { typeName = getFeatureBaseName() + "_L"; if (!featureTypes.containsKey(typeName)) { featureTypes.put(typeName, createLineFeatureElement(typeName)); } return featureTypes.get(typeName); } else if (element instanceof ComplexChainElement) { typeName = getFeatureBaseName() + "_ML"; if (!featureTypes.containsKey(typeName)) { featureTypes.put(typeName, createLineFeatureElement(typeName)); } return featureTypes.get(typeName); } else if (element instanceof ArcElement) { typeName = getFeatureBaseName() + "_A"; if (!featureTypes.containsKey(typeName)) { featureTypes.put(typeName, createArcFeatureElement(typeName)); } return featureTypes.get(typeName); } else if (element instanceof EllipseElement) { typeName = getFeatureBaseName() + "_R"; if (!featureTypes.containsKey(typeName)) { featureTypes.put(typeName, createEllipseFeatureElement(typeName)); } return featureTypes.get(typeName); } return null; } 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 boolean isWithIndex() { return withIndex; } public void setWithIndex(boolean withIndex) { this.withIndex = withIndex; } public Log getLogger() { return logger; } }