From 2fae905a591201e3d4ee3d463d1d94a504171017 Mon Sep 17 00:00:00 2001
From: ?? ? <ulysseskao@ximple.com.tw>
Date: Mon, 17 Mar 2008 19:31:27 +0800
Subject: [PATCH] update for EOFM-17

---
 ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/TypeIdHandlerStrategy.java           |   11 +
 ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/ElementDispatcher.java               |    7 +
 ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/TypeCompIdHandlerStrategy.java       |   11 +
 ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/OracleConvertDgn2ShpJob.java         |    4 
 ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/OracleConvertJobContext.java         |   76 +++++++++-----
 ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/AbstractFLinkageHandlerStrategy.java |    5 +
 ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/CreateLineStringStrategy.java        |   52 ++++++++++
 ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/CreateTextStrategy.java              |   59 +++++++++++
 ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/ElementDispatchableStrategy.java     |    4 
 ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/CreateFeatureTypeStrategy.java       |    6 +
 ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/TypeCompLevelIdHandlerStrategy.java  |   11 +
 11 files changed, 207 insertions(+), 39 deletions(-)

diff --git a/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/AbstractFLinkageHandlerStrategy.java b/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/AbstractFLinkageHandlerStrategy.java
index 4fb9b53..201809f 100644
--- a/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/AbstractFLinkageHandlerStrategy.java
+++ b/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/AbstractFLinkageHandlerStrategy.java
@@ -2,12 +2,17 @@
 
 import java.util.List;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
 import com.ximple.io.dgn7.Element;
 import com.ximple.io.dgn7.FrammeAttributeData;
 import com.ximple.io.dgn7.UserAttributeData;
 
 public abstract class AbstractFLinkageHandlerStrategy implements ElementDispatchableStrategy
 {
+    protected Log logger = LogFactory.getLog(AbstractFLinkageHandlerStrategy.class);
+
     protected FrammeAttributeData getFeatureLinkage(Element element)
     {
         if (!element.hasUserAttributeData())
diff --git a/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/CreateFeatureTypeStrategy.java b/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/CreateFeatureTypeStrategy.java
index c529090..08c9ef9 100644
--- a/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/CreateFeatureTypeStrategy.java
+++ b/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/CreateFeatureTypeStrategy.java
@@ -1,9 +1,15 @@
 package com.ximple.eofms.jobs;
 
+import org.geotools.feature.Feature;
 import org.geotools.feature.FeatureType;
+import org.geotools.feature.IllegalAttributeException;
 import org.geotools.feature.SchemaException;
+
+import com.ximple.io.dgn7.Element;
 
 public interface CreateFeatureTypeStrategy
 {
     public FeatureType createFeatureElement(String featureName) throws SchemaException;
+
+    public Feature createFeature(FeatureType featureType, Element element) throws IllegalAttributeException;
 }
diff --git a/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/CreateLineStringStrategy.java b/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/CreateLineStringStrategy.java
index d573c43..5cb5372 100644
--- a/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/CreateLineStringStrategy.java
+++ b/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/CreateLineStringStrategy.java
@@ -1,14 +1,43 @@
 package com.ximple.eofms.jobs;
 
+import java.util.List;
+
 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.opengis.geometry.Geometry;
 
+import com.vividsolutions.jts.geom.GeometryFactory;
+
+import com.ximple.io.dgn7.Element;
+import com.ximple.io.dgn7.FrammeAttributeData;
+import com.ximple.io.dgn7.LineStringElement;
+import com.ximple.io.dgn7.UserAttributeData;
+
 public class CreateLineStringStrategy implements CreateFeatureTypeStrategy
 {
+    GeometryFactory geometryFactory = new GeometryFactory();
     FeatureTypeBuilder typeBuilder = null;
+
+    protected FrammeAttributeData getFeatureLinkage(Element element)
+    {
+        if (!element.hasUserAttributeData())
+            return null;
+
+        List<UserAttributeData> usrDatas = element.getUserAttributeData();
+        for (UserAttributeData anUsrData : usrDatas)
+        {
+            if (anUsrData instanceof FrammeAttributeData)
+            {
+                FrammeAttributeData featureLinkage = (FrammeAttributeData) anUsrData;
+                return featureLinkage;
+            }
+        }
+        return null;
+    }
 
     public FeatureType createFeatureElement(String featureName) throws SchemaException
     {
@@ -27,4 +56,27 @@
         }
         return typeBuilder.getFeatureType();
     }
+
+    public Feature createFeature(FeatureType featureType, Element element) throws IllegalAttributeException
+    {
+        FrammeAttributeData fLinkage = getFeatureLinkage(element);
+        if (fLinkage == null) return null;
+        if (element instanceof LineStringElement)
+        {
+            LineStringElement lineStringElement = (LineStringElement) element;
+            Feature feature = featureType.create(new Object[]{
+                    geometryFactory.createLineString(lineStringElement.getVertices()),
+                    (int) fLinkage.getFsc(),
+                    (long) fLinkage.getUfid(),
+                    (int) fLinkage.getComponentID(),
+                    0,
+                    lineStringElement.getLevelIndex(),
+                    lineStringElement.getColorIndex(),
+                    lineStringElement.getWeight(),
+                    lineStringElement.getLineStyle(),
+            });
+            return feature;
+        }
+        return null;
+    }
 }
diff --git a/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/CreateTextStrategy.java b/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/CreateTextStrategy.java
index fca43bd..413516c 100644
--- a/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/CreateTextStrategy.java
+++ b/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/CreateTextStrategy.java
@@ -1,14 +1,43 @@
 package com.ximple.eofms.jobs;
 
+import java.util.List;
+
 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.opengis.geometry.Geometry;
 
+import com.vividsolutions.jts.geom.GeometryFactory;
+
+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 CreateTextStrategy implements CreateFeatureTypeStrategy
 {
+    GeometryFactory geometryFactory = new GeometryFactory();
     FeatureTypeBuilder typeBuilder = null;
+
+    protected FrammeAttributeData getFeatureLinkage(Element element)
+    {
+        if (!element.hasUserAttributeData())
+            return null;
+
+        List<UserAttributeData> usrDatas = element.getUserAttributeData();
+        for (UserAttributeData anUsrData : usrDatas)
+        {
+            if (anUsrData instanceof FrammeAttributeData)
+            {
+                FrammeAttributeData featureLinkage = (FrammeAttributeData) anUsrData;
+                return featureLinkage;
+            }
+        }
+        return null;
+    }
 
     public FeatureType createFeatureElement(String featureName) throws SchemaException
     {
@@ -29,7 +58,37 @@
             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 Feature createFeature(FeatureType featureType, Element element) throws IllegalAttributeException
+    {
+        FrammeAttributeData fLinkage = getFeatureLinkage(element);
+        if (fLinkage == null) return null;
+        if (element instanceof TextElement)
+        {
+            TextElement txtElement = (TextElement) element;
+            Feature feature = featureType.create(new Object[]{
+                    geometryFactory.createPoint(txtElement.getUserOrigin()),
+                    (int) fLinkage.getFsc(),
+                    (long) fLinkage.getUfid(),
+                    (int) fLinkage.getComponentID(),
+                    0,
+                    txtElement.getLevelIndex(),
+                    txtElement.getColorIndex(),
+                    txtElement.getWeight(),
+                    txtElement.getLineStyle(),
+                    txtElement.getFontIndex(),
+                    txtElement.getJustification(),
+                    txtElement.getTextHeight(),
+                    txtElement.getTextWidth(),
+                    txtElement.getRotationAngle(),
+                    txtElement.getText()
+            });
+            return feature;
+        }
+        return null;
+    }
 }
diff --git a/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/ElementDispatchableStrategy.java b/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/ElementDispatchableStrategy.java
index e3c882f..2319568 100644
--- a/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/ElementDispatchableStrategy.java
+++ b/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/ElementDispatchableStrategy.java
@@ -1,6 +1,6 @@
 package com.ximple.eofms.jobs;
 
-import org.geotools.feature.SchemaException;
+import org.geotools.feature.Feature;
 
 import com.ximple.io.dgn7.Element;
 
@@ -8,5 +8,5 @@
 {
     public boolean isDispatchable(Element element);
 
-    public void execute(Element element) throws SchemaException;
+    public Feature execute(Element element);
 }
diff --git a/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/ElementDispatcher.java b/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/ElementDispatcher.java
index e19b4c7..2ce52a8 100644
--- a/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/ElementDispatcher.java
+++ b/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/ElementDispatcher.java
@@ -2,6 +2,8 @@
 
 import java.util.LinkedList;
 
+import org.geotools.feature.Feature;
+
 import com.ximple.io.dgn7.Element;
 
 public class ElementDispatcher
@@ -18,14 +20,15 @@
         rules.add(rule);
     }
 
-    public void execute(Element element)
+    public Feature execute(Element element)
     {
         for (ElementDispatchableStrategy rule : rules)
         {
             if (rule.isDispatchable(element))
             {
-                rule.execute(element);
+                return rule.execute(element);
             }
         }
+        return null;
     }
 }
diff --git a/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/OracleConvertDgn2ShpJob.java b/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/OracleConvertDgn2ShpJob.java
index 4e410fa..1f0c9b2 100644
--- a/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/OracleConvertDgn2ShpJob.java
+++ b/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/OracleConvertDgn2ShpJob.java
@@ -117,8 +117,8 @@
             if ((order % COMMITSIZE) == 0)
             {
                 // OracleConnection connection = jobContext.getOracleConnection();
-                // connection.commit();
-                jobContext.commit();
+                // connection.commitTransaction();
+                jobContext.commitTransaction();
                 System.gc();
             }
         }
diff --git a/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/OracleConvertJobContext.java b/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/OracleConvertJobContext.java
index 1b0a17a..fe44141 100644
--- a/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/OracleConvertJobContext.java
+++ b/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/OracleConvertJobContext.java
@@ -2,14 +2,16 @@
 
 import java.sql.DriverManager;
 import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.Properties;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import org.geotools.feature.AttributeTypeFactory;
-import org.geotools.feature.FeatureCollection;
-import org.geotools.feature.FeatureTypeBuilder;
-import org.opengis.geometry.Geometry;
+import org.apache.commons.transaction.memory.PessimisticMapWrapper;
+import org.apache.commons.transaction.util.CommonsLoggingLogger;
+import org.apache.commons.transaction.util.LoggerFacade;
+import org.geotools.feature.Feature;
 
 import com.vividsolutions.jts.util.Assert;
 
@@ -20,6 +22,7 @@
 public class OracleConvertJobContext
 {
     static Log logger = LogFactory.getLog(OracleConvertJobContext.class);
+    static final LoggerFacade sLogger = new CommonsLoggingLogger(logger);
 
     private static final String ORACLE_URL = "jdbc:oracle:thin:@";
     private static final String PROPUsrKey = "user";
@@ -208,6 +211,8 @@
 
     protected static final String SMTM_GRANTOBJECTTYPE = "GRANT EXECUTE ANY TYPE TO \"" + UDT_SCHEMA + "\"";
 
+    protected static final long TIMEOUT = Long.MAX_VALUE;
+
     static
     {
         try
@@ -242,16 +247,21 @@
 
     private ElementDispatcher elementDispatcher;
 
+    private HashMap featuresContext = new HashMap();
+    private PessimisticMapWrapper txFeaturesContext;
+
     public OracleConvertJobContext()
     {
         properties = new Properties();
         elementDispatcher = createElementDispatcher();
+        txFeaturesContext = new PessimisticMapWrapper(featuresContext, sLogger);
+
     }
 
     private ElementDispatcher createElementDispatcher()
     {
         ElementDispatcher dispatcher = new ElementDispatcher();
-        dispatcher.addRule(new TypeCompIdHandlerStrategy(106, 0));
+        dispatcher.addRule(new TypeCompIdHandlerStrategy("Conductor", new CreateLineStringStrategy(), 106, 0));
         return dispatcher;
     }
 
@@ -305,34 +315,46 @@
         _oraclePort = oraclePort;
     }
 
-    public FeatureCollection putFeatureCollection(Element element)
+    public void putFeatureCollection(Element element)
     {
-        return null;
+        Feature feature = elementDispatcher.execute(element);
+        if (feature == null)
+            return;
+
+        if (!txFeaturesContext.containsKey(feature.getFeatureType()))
+        {
+            txFeaturesContext.put(feature.getFeatureType(), new ArrayList());
+        }
+        ArrayList arrayList = (ArrayList) txFeaturesContext.get(feature.getFeatureType());
+        arrayList.add(feature);
     }
 
-    public FeatureCollection createFeatureCollection(Element element)
+    public void startTransaction()
     {
-        FeatureTypeBuilder typeBuilder = FeatureTypeBuilder.newInstance("Line");
-
-        typeBuilder.addType(AttributeTypeFactory.newAttributeType("GEOM", Geometry.class));
-        typeBuilder.addType(AttributeTypeFactory.newAttributeType("TID", Integer.class));
-        typeBuilder.addType(AttributeTypeFactory.newAttributeType("OID", Long.class));
-        typeBuilder.addType(AttributeTypeFactory.newAttributeType("CID", Integer.class));
-        typeBuilder.addType(AttributeTypeFactory.newAttributeType("LID", Integer.class));
-        typeBuilder.addType(AttributeTypeFactory.newAttributeType("LEVEL", Integer.class));
-        typeBuilder.addType(AttributeTypeFactory.newAttributeType("SYMCOLOR", Integer.class));
-        typeBuilder.addType(AttributeTypeFactory.newAttributeType("SYMWEIGHT", Integer.class));
-        typeBuilder.addType(AttributeTypeFactory.newAttributeType("SYMSTYLE", Integer.class));
-        typeBuilder.addType(AttributeTypeFactory.newAttributeType("FONT", String.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));
-        return null;
+        txFeaturesContext.startTransaction();
     }
 
-    public void commit()
+    public void commitTransaction()
     {
-
+        txFeaturesContext.commitTransaction();
+        if (!featuresContext.isEmpty())
+        {
+            updateDataStore();
+        }
     }
+
+    public void rollbackTransaction()
+    {
+        txFeaturesContext.rollbackTransaction();
+        if (!featuresContext.isEmpty())
+        {
+            updateDataStore();
+        }
+    }
+
+    private void updateDataStore()
+    {
+        // todo:
+    }
+
 }
diff --git a/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/TypeCompIdHandlerStrategy.java b/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/TypeCompIdHandlerStrategy.java
index cf77a3f..659157d 100644
--- a/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/TypeCompIdHandlerStrategy.java
+++ b/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/TypeCompIdHandlerStrategy.java
@@ -1,6 +1,8 @@
 package com.ximple.eofms.jobs;
 
+import org.geotools.feature.Feature;
 import org.geotools.feature.FeatureType;
+import org.geotools.feature.IllegalAttributeException;
 import org.geotools.feature.SchemaException;
 
 import com.ximple.io.dgn7.Element;
@@ -30,14 +32,19 @@
                 (cid == featureLinkage.getComponentID());
     }
 
-    public void execute(Element element)
+    public Feature execute(Element element)
     {
         try
         {
             FeatureType ftype = createStrategy.createFeatureElement(name);
+            return createStrategy.createFeature(ftype, element);
         } catch (SchemaException e)
         {
-            e.printStackTrace();
+            logger.error(e.getMessage(), e);
+        } catch (IllegalAttributeException e)
+        {
+            logger.error(e.getMessage(), e);
         }
+        return null;
     }
 }
diff --git a/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/TypeCompLevelIdHandlerStrategy.java b/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/TypeCompLevelIdHandlerStrategy.java
index c937241..9021b14 100644
--- a/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/TypeCompLevelIdHandlerStrategy.java
+++ b/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/TypeCompLevelIdHandlerStrategy.java
@@ -1,6 +1,8 @@
 package com.ximple.eofms.jobs;
 
+import org.geotools.feature.Feature;
 import org.geotools.feature.FeatureType;
+import org.geotools.feature.IllegalAttributeException;
 import org.geotools.feature.SchemaException;
 
 import com.ximple.io.dgn7.Element;
@@ -33,14 +35,19 @@
                 (lid == element.getLevelIndex());
     }
 
-    public void execute(Element element)
+    public Feature execute(Element element)
     {
         try
         {
             FeatureType ftype = createStrategy.createFeatureElement(name);
+            return createStrategy.createFeature(ftype, element);
         } catch (SchemaException e)
         {
-            e.printStackTrace();
+            logger.error(e.getMessage(), e);
+        } catch (IllegalAttributeException e)
+        {
+            logger.error(e.getMessage(), e);
         }
+        return null;
     }
 }
diff --git a/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/TypeIdHandlerStrategy.java b/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/TypeIdHandlerStrategy.java
index 86b36d8..1a48dcb 100644
--- a/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/TypeIdHandlerStrategy.java
+++ b/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/TypeIdHandlerStrategy.java
@@ -1,6 +1,8 @@
 package com.ximple.eofms.jobs;
 
+import org.geotools.feature.Feature;
 import org.geotools.feature.FeatureType;
+import org.geotools.feature.IllegalAttributeException;
 import org.geotools.feature.SchemaException;
 
 import com.ximple.io.dgn7.Element;
@@ -26,14 +28,19 @@
         return featureLinkage != null && tid == featureLinkage.getFsc();
     }
 
-    public void execute(Element element)
+    public Feature execute(Element element)
     {
         try
         {
             FeatureType ftype = createStrategy.createFeatureElement(name);
+            return createStrategy.createFeature(ftype, element);
         } catch (SchemaException e)
         {
-            e.printStackTrace();
+            logger.error(e.getMessage(), e);
+        } catch (IllegalAttributeException e)
+        {
+            logger.error(e.getMessage(), e);
         }
+        return null;
     }
 }

--
Gitblit v0.0.0-SNAPSHOT