From e74264d50398db0c9f69761291e2891823f3c450 Mon Sep 17 00:00:00 2001
From: ?? ? <ulysseskao@ximple.com.tw>
Date: Fri, 05 Sep 2008 18:32:01 +0800
Subject: [PATCH] update for EOFM-157

---
 xdgnjobs/ximple-elmparser/src/main/java/com/ximple/eofms/XElementParser.java  |  106 +++++++++++++++++++++++++++++++++-
 xdgnjobs/ximple-elmparser/src/main/java/com/ximple/eofms/XElementFetcher.java |   17 +++--
 2 files changed, 112 insertions(+), 11 deletions(-)

diff --git a/xdgnjobs/ximple-elmparser/src/main/java/com/ximple/eofms/XElementFetcher.java b/xdgnjobs/ximple-elmparser/src/main/java/com/ximple/eofms/XElementFetcher.java
index 5e6ec32..f9f9cc7 100644
--- a/xdgnjobs/ximple-elmparser/src/main/java/com/ximple/eofms/XElementFetcher.java
+++ b/xdgnjobs/ximple-elmparser/src/main/java/com/ximple/eofms/XElementFetcher.java
@@ -1,5 +1,6 @@
 package com.ximple.eofms;
 
+import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.UnsupportedEncodingException;
@@ -38,15 +39,15 @@
 import oracle.sql.BLOB;
 
 import com.ximple.eofms.filter.ElementDispatcher;
-import com.ximple.eofms.jobs.context.postgis.FeatureDgnConvertPostGISJobContext;
 import com.ximple.eofms.jobs.OracleElementLogger;
+import com.ximple.eofms.jobs.context.postgis.FeatureDgnConvertPostGISJobContext;
+import com.ximple.io.dgn7.ArcElement;
+import com.ximple.io.dgn7.ComplexChainElement;
 import com.ximple.io.dgn7.ComplexElement;
 import com.ximple.io.dgn7.Dgn7fileException;
 import com.ximple.io.dgn7.Element;
 import com.ximple.io.dgn7.ElementType;
 import com.ximple.io.dgn7.IElementHandler;
-import com.ximple.io.dgn7.ArcElement;
-import com.ximple.io.dgn7.ComplexChainElement;
 import com.ximple.util.PrintfFormat;
 
 public class XElementFetcher implements Runnable
@@ -61,7 +62,6 @@
     private static final String ORAUSER = "ORAUSER";
     private static final String ORAPASS = "ORAPASS";
     private static final String ORGSCHEMA = "ORGSCHEMA";
-    private static final String DATAPATH = "DATAPATH";
 
     private static final int FETCHSIZE = 30;
     private static final int COMMITSIZE = 100;
@@ -114,7 +114,7 @@
     private void initializeDataConfig()
     {
         dataConfig = new HashMap<String, String>();
-        dataConfig.put(DATAPATH, "G:\\Temp\\JobData\\tctpc\\elmout");
+        dataConfig.put(XElementParser.ROOTDATAPATH, XElementParser.DEFAULT_DATAPATH);
         dataConfig.put(ORAHOST, "192.168.11.200");
         dataConfig.put(ORAINST, "tctpc");
         dataConfig.put(ORAPORT, "1521");
@@ -166,7 +166,7 @@
             ArrayList<String> schemas = getSchemaNames();
             for (String schema : schemas)
             {
-                executeFetchElement(dataStore, schema, dataConfig.get(DATAPATH));
+                executeFetchElement(dataStore, schema, getOutputPath());
             }
         } catch (SQLException e)
         {
@@ -175,6 +175,11 @@
         }
     }
 
+    private String getOutputPath()
+    {
+        return dataConfig.get(XElementParser.ROOTDATAPATH) + File.separator + XElementParser.DEFAULT_DGNOUTPATH;
+    }
+
     protected OracleDataStore createSourceDataStore()
     {
         if (!driverFound)
diff --git a/xdgnjobs/ximple-elmparser/src/main/java/com/ximple/eofms/XElementParser.java b/xdgnjobs/ximple-elmparser/src/main/java/com/ximple/eofms/XElementParser.java
index a26d5ee..ffecfd9 100644
--- a/xdgnjobs/ximple-elmparser/src/main/java/com/ximple/eofms/XElementParser.java
+++ b/xdgnjobs/ximple-elmparser/src/main/java/com/ximple/eofms/XElementParser.java
@@ -9,6 +9,7 @@
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.nio.channels.FileChannel;
+import java.nio.charset.Charset;
 import java.util.HashMap;
 import java.util.List;
 
@@ -18,6 +19,15 @@
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.geotools.feature.Feature;
+import org.geotools.feature.FeatureType;
+import org.geotools.feature.SimpleFeature;
+import org.geotools.feature.IllegalAttributeException;
+import org.geotools.feature.FeatureCollection;
+import org.geotools.feature.FeatureCollections;
+import org.geotools.data.FeatureWriter;
+import org.geotools.data.Transaction;
+import org.geotools.data.FeatureStore;
+import org.geotools.data.shapefile.ShapefileDataStore;
 import org.xml.sax.SAXException;
 
 import com.vividsolutions.jts.geom.GeometryFactory;
@@ -25,6 +35,7 @@
 import com.ximple.eofms.filter.AbstractFLinkageDispatchableFilter;
 import com.ximple.eofms.filter.ElementDispatcher;
 import com.ximple.eofms.jobs.context.postgis.FeatureDgnConvertPostGISJobContext;
+import com.ximple.eofms.util.FeatureTypeBuilderUtil;
 import com.ximple.io.dgn7.ComplexElement;
 import com.ximple.io.dgn7.Dgn7fileException;
 import com.ximple.io.dgn7.Dgn7fileReader;
@@ -39,6 +50,14 @@
     static final GeometryFactory geometryFactory = new GeometryFactory();
 
     static final boolean isCompactMode = true;
+    static final boolean postOutMode = true;
+
+    protected static final String ROOTDATAPATH = "DATAPATH";
+
+    protected static final String DEFAULT_DATAPATH = "G:\\Temp\\JobData\\tctpc";
+    protected static final String DEFAULT_DGNINPATH = "elmin"; 
+    protected static final String DEFAULT_DGNOUTPATH = "elmout";
+    protected static final String DEFAULT_SHPOUTPATH = "out";
 
     private HashMap<String, String> dataConfig;
     private ElementDispatcher elementDispatcher;
@@ -58,9 +77,11 @@
     private void initializeDataConfig()
     {
         dataConfig = new HashMap<String, String>();
-        dataConfig.put("DATAPATH", "G:\\Temp\\JobData\\tctpc\\elmout");
+        dataConfig.put(ROOTDATAPATH, DEFAULT_DATAPATH);
         
         elementDispatcher = createElementDispatcher();
+
+        FeatureTypeBuilderUtil.setNotAllowNull(true);
     }
 
     private ElementDispatcher createElementDispatcher()
@@ -97,7 +118,8 @@
 
     public void run()
     {
-        File dataDir = new File(dataConfig.get("DATAPATH"));
+        File rootDir = new File(dataConfig.get(ROOTDATAPATH));
+        File dataDir = new File(rootDir, DEFAULT_DGNINPATH);
         if ((!dataDir.exists()) || (!dataDir.isDirectory()))
         {
             return;
@@ -172,7 +194,19 @@
             }
         }
 
-        postProcessFeatureContext();
+        if (postOutMode)
+        {
+            try
+            {
+                postProcessFeatureContext();
+            } catch (IOException e)
+            {
+                logger.warn(e.getMessage(), e);
+            } catch (IllegalAttributeException e)
+            {
+                logger.warn(e.getMessage(), e);
+            }
+        }
     }
 
     private void processFeatureElement(Element element)
@@ -184,7 +218,7 @@
         }
 
         // �P�_�O�_�ũM����
-        Feature feature = elementDispatcher.execute(element);
+        Feature feature = elementDispatcher.execute(element, false);
         if (feature == null)
         {
             FrammeAttributeData linkage =
@@ -223,12 +257,74 @@
     {
     }
 
-    private void postProcessFeatureContext()
+    private void postProcessFeatureContext() throws IOException, IllegalAttributeException
     {
         if (featuresContext.size() == 0) return;
         for (Object key : featuresContext.keySet())
         {
+            FeatureType featureType = (FeatureType) key;
             List elements = (List) featuresContext.get(key);
+
+            FeatureCollection features = FeatureCollections.newCollection();
+            features.addAll(elements);
+            createFeatureStore(featureType).addFeatures(features);
+
+            FeatureWriter writer = createFeatureWriter(featureType);
+            for (Object v : elements)
+            {
+                Feature feature1 = (Feature) v;
+                Object[] attrs = feature1.getAttributes(null);
+                if (attrs != null)
+                ((SimpleFeature) writer.next()).setAttributes(attrs);
+
+            }
+            writer.close();
         }
     }
+
+    private FeatureStore createFeatureStore(FeatureType featureType) throws IOException
+    {
+        File sfile = new File(getDataOutPath() + File.separator + featureType.getTypeName());
+
+        ShapefileDataStore shapefileDataStore = null;
+        boolean existFile = sfile.exists();
+
+        shapefileDataStore = new ShapefileDataStore(sfile.toURI().toURL(),
+                true, Charset.forName("UTF-8"));
+
+        if (!existFile)
+        {
+            shapefileDataStore.createSchema(featureType);
+        }
+        return (FeatureStore) shapefileDataStore.getFeatureSource(featureType.getTypeName());
+    }
+    private FeatureWriter createFeatureWriter(FeatureType featureType) throws IOException
+    {
+        File sfile = new File(getDataOutPath() + File.separator + featureType.getTypeName());
+
+        ShapefileDataStore shapefileDataStore = null;
+        boolean existFile = sfile.exists();
+
+        shapefileDataStore = new ShapefileDataStore(sfile.toURI().toURL(),
+                true, Charset.forName("UTF-8"));
+
+        FeatureWriter writer;
+        if (!existFile)
+        {
+            shapefileDataStore.createSchema(featureType);
+            writer = shapefileDataStore.getFeatureWriter(featureType.getTypeName(), Transaction.AUTO_COMMIT);
+        } else {
+            writer = shapefileDataStore.getFeatureWriterAppend(featureType.getTypeName(), Transaction.AUTO_COMMIT);
+        }
+
+        return writer;
+    }
+
+    public String getDataOutPath()
+    {
+        File rootDir = new File(dataConfig.get(ROOTDATAPATH));
+        File dataDir = new File(rootDir, DEFAULT_SHPOUTPATH);
+        return dataDir.toString();
+    }
+
 }

--
Gitblit v0.0.0-SNAPSHOT