From 2dc52b4a4b6f2ca3defdeb3e10b8f34f1f38dea9 Mon Sep 17 00:00:00 2001
From: ?? ? <ulysseskao@ximple.com.tw>
Date: Tue, 06 May 2008 18:45:25 +0800
Subject: [PATCH] update for EOFM-75

---
 xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/GeneralDgnConvertJobContext.java |  282 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 281 insertions(+), 1 deletions(-)

diff --git a/xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/GeneralDgnConvertJobContext.java b/xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/GeneralDgnConvertJobContext.java
index a2bf495..715b4f2 100644
--- a/xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/GeneralDgnConvertJobContext.java
+++ b/xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/GeneralDgnConvertJobContext.java
@@ -1,15 +1,90 @@
 package com.ximple.eofms.jobs;
 
 import java.io.File;
+import java.io.IOException;
+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.FeatureWriter;
+import org.geotools.data.Transaction;
+import org.geotools.data.shapefile.ShapefileDataStore;
+import org.geotools.feature.AttributeTypeFactory;
+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.geotools.feature.SimpleFeature;
+
+import com.vividsolutions.jts.geom.Geometry;
+import com.vividsolutions.jts.geom.GeometryFactory;
+
+import com.ximple.eofms.util.DefaultColorTable;
+import com.ximple.io.dgn7.Element;
+import com.ximple.io.dgn7.FrammeAttributeData;
+import com.ximple.io.dgn7.LineElement;
+import com.ximple.io.dgn7.LineStringElement;
+import com.ximple.io.dgn7.TextElement;
+import com.ximple.io.dgn7.UserAttributeData;
 
 public class GeneralDgnConvertJobContext extends AbstractDgnFileJobContext
 {
-    private final static String SHPOUTPATH = "shpout";
+    static final Log logger = LogFactory.getLog(IndexDgnConvertJobContext.class);
+    static final LoggerFacade sLogger = new CommonsLoggingLogger(logger);
+    static final GeometryFactory geometryFactory = new GeometryFactory();
+    static final String SHPOUTPATH = "shpout";
+
     private String dataOut = null;
+
+    private HashMap featuresContext = new HashMap();
+    private HashMap featuresWriterContext = new HashMap();
+
+    private PessimisticMapWrapper txFeaturesContext;
+    private FeatureTypeBuilder typeBuilder = null;
+    private TreeMap<String, FeatureType> featureTypes = new TreeMap<String, FeatureType>();
+
+    private TWD97GeometryConverterDecorator convertDecorator = null;
+    private String featureBaseName = null;
 
     public GeneralDgnConvertJobContext(String dataPath)
     {
         super(dataPath);
+        txFeaturesContext = new PessimisticMapWrapper(featuresContext, sLogger);
+        convertDecorator = new TWD97GeometryConverterDecorator();
+    }
+
+    public void putFeatureCollection(Element element) throws IllegalAttributeException, SchemaException
+    {
+        FeatureType ft = lookupFeatureType(element);
+        if (ft == null)
+        {
+            Feature feature = createFeature(ft, 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 = (ArrayList) txFeaturesContext.get(feature.getFeatureType());
+            arrayList.add(feature);
+        } else
+        {
+            logger.info("Unknown Element :" + element.getType() + ", lv=" + element.getLevelIndex());
+        }
     }
 
     public void startTransaction()
@@ -18,10 +93,79 @@
 
     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()
+    {
+        Iterator it = featuresContext.keySet().iterator();
+
+        try
+        {
+            while (it.hasNext())
+            {
+                FeatureType featureType = (FeatureType) it.next();
+                File sfile = new File(getDataOutPath() + "\\" + featureType.getTypeName());
+                logger.debug("Begin Save shapefile:" + sfile.toURI());
+
+                FeatureWriter writer;
+                if (featuresWriterContext.containsKey(featureType.getTypeName()))
+                {
+                    writer = (FeatureWriter) featuresWriterContext.get(featureType.getTypeName());
+                } else
+                {
+                    ShapefileDataStore shapefileDataStore = new ShapefileDataStore(sfile.toURI().toURL());
+                    shapefileDataStore.createSchema(featureType);
+                    writer = shapefileDataStore.getFeatureWriter(featureType.getTypeName(), Transaction.AUTO_COMMIT);
+                    if (this.featuresWriterContext == null)
+                    {
+                        this.featuresWriterContext = new HashMap();
+                    }
+                    featuresWriterContext.put(featureType.getTypeName(), writer);
+                }
+
+                ArrayList features = (ArrayList) featuresContext.get(featureType);
+                Iterator itFeature = features.iterator();
+                while (itFeature.hasNext())
+                {
+                    Feature feature = (Feature) itFeature.next();
+                    ((SimpleFeature) writer.next()).setAttributes(feature.getAttributes(null));
+                }
+                //writer.close();
+                logger.debug("End Save shapefile:" + sfile.toURI());
+            }
+            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);
+        }
     }
 
     public String getDataOutPath()
@@ -40,4 +184,140 @@
         }
         return dataOut;
     }
+
+    public FeatureType createPointFeatureElement(String featureName) throws SchemaException
+    {
+        if (typeBuilder == null)
+        {
+            typeBuilder = FeatureTypeBuilder.newInstance(featureName);
+            typeBuilder.addType(AttributeTypeFactory.newAttributeType("GEOM", Geometry.class));
+            typeBuilder.addType(AttributeTypeFactory.newAttributeType("SYMCOLOR", String.class));
+            typeBuilder.addType(AttributeTypeFactory.newAttributeType("FONT", Integer.class));
+            typeBuilder.addType(AttributeTypeFactory.newAttributeType("JUST", Integer.class));
+            typeBuilder.addType(AttributeTypeFactory.newAttributeType("HEIGHT", Double.class));
+            typeBuilder.addType(AttributeTypeFactory.newAttributeType("WIDTH", Double.class));
+            typeBuilder.addType(AttributeTypeFactory.newAttributeType("ANGLE", Double.class));
+            typeBuilder.addType(AttributeTypeFactory.newAttributeType("CONTEXT", String.class));
+        }
+        return typeBuilder.getFeatureType();
+    }
+
+    public FeatureType createLineFeatureElement(String featureName) throws SchemaException
+    {
+        if (typeBuilder == null)
+        {
+            typeBuilder = FeatureTypeBuilder.newInstance(featureName);
+            typeBuilder.addType(AttributeTypeFactory.newAttributeType("GEOM", Geometry.class));
+            typeBuilder.addType(AttributeTypeFactory.newAttributeType("SYMCOLOR", String.class));
+            typeBuilder.addType(AttributeTypeFactory.newAttributeType("SYMWEIGHT", Integer.class));
+            typeBuilder.addType(AttributeTypeFactory.newAttributeType("SYMSTYLE", Integer.class));
+        }
+        return typeBuilder.getFeatureType();
+    }
+
+    public Feature createFeature(FeatureType featureType, Element element) throws IllegalAttributeException
+    {
+        DefaultColorTable colorTable = (DefaultColorTable) DefaultColorTable.getInstance();
+        if (element instanceof TextElement)
+        {
+            TextElement textElement = (TextElement) element;
+            convertDecorator.setConverter(textElement);
+            Feature feature = featureType.create(new Object[]{
+                    convertDecorator.toGeometry(geometryFactory),
+                    colorTable.getColorCode(textElement.getColorIndex()),
+                    textElement.getFontIndex(),
+                    textElement.getJustification(),
+                    textElement.getTextHeight(),
+                    textElement.getTextWidth(),
+                    textElement.getRotationAngle(),
+                    textElement.getText()
+            });
+            return feature;
+        } else if (element instanceof LineStringElement)
+        {
+            LineStringElement linestring = (LineStringElement) element;
+            convertDecorator.setConverter(linestring);
+            Feature feature = featureType.create(new Object[]{
+                    convertDecorator.toGeometry(geometryFactory),
+                    colorTable.getColorCode(linestring.getColorIndex()),
+                    linestring.getWeight(),
+                    linestring.getLineStyle()
+            });
+            return feature;
+        } else if (element instanceof LineElement)
+        {
+            LineElement line = (LineElement) element;
+            convertDecorator.setConverter(line);
+            Feature feature = featureType.create(new Object[]{
+                    convertDecorator.toGeometry(geometryFactory),
+                    colorTable.getColorCode(line.getColorIndex()),
+                    line.getWeight(),
+                    line.getLineStyle()
+            });
+            return feature;
+        }
+        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 FeatureType 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 LineStringElement)
+        {
+            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);
+        }
+
+        return null;
+    }
+
+    protected FrammeAttributeData getFeatureLinkage(Element element)
+    {
+        if (!element.hasUserAttributeData())
+            return null;
+
+        List<UserAttributeData> usrDatas = element.getUserAttributeData();
+        for (UserAttributeData anUsrData : usrDatas)
+        {
+            if (anUsrData instanceof FrammeAttributeData)
+            {
+                return (FrammeAttributeData) anUsrData;
+            }
+        }
+        return null;
+    }
 }

--
Gitblit v0.0.0-SNAPSHOT