forked from geodmms/xdgnjobs

?? ?
2008-05-06 0ffea4eece9090db61ff0042ac26a6cb2a356cab
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package com.ximple.eofms.jobs;
 
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;
 
import org.apache.commons.digester.Digester;
import org.apache.commons.digester.xmlrules.DigesterLoader;
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.Feature;
import org.geotools.feature.FeatureType;
import org.geotools.feature.IllegalAttributeException;
import org.geotools.feature.SimpleFeature;
import org.quartz.JobExecutionContext;
import org.xml.sax.SAXException;
 
import com.vividsolutions.jts.util.Assert;
 
import com.ximple.eofms.filter.AbstractFLinkageDispatchableFilter;
import com.ximple.eofms.filter.ElementDispatcher;
import com.ximple.io.dgn7.Element;
import com.ximple.io.dgn7.FrammeAttributeData;
 
 
public class OracleConvertJobContext extends AbstractOracleJobContext
{
    static Log logger = LogFactory.getLog(OracleConvertJobContext.class);
    static final LoggerFacade sLogger = new CommonsLoggingLogger(logger);
 
    private static final String SHPOUTPATH = "shpout";
 
    static
    {
        try
        {
            DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
        } catch (SQLException e)
        {
            Assert.shouldNeverReachHere(e.getMessage());
        }
    }
 
    private String _filterConfig;
 
    private ElementDispatcher elementDispatcher;
 
    private HashMap featuresContext = new HashMap();
    private HashMap featuresWriterContext = new HashMap();
    private PessimisticMapWrapper txFeaturesContext;
 
    private JobExecutionContext executionContext;
 
    private String dataOut = null;
 
    public OracleConvertJobContext(String filterConfig)
    {
        properties = new Properties();
        _filterConfig = filterConfig;
        elementDispatcher = createElementDispatcher();
        txFeaturesContext = new PessimisticMapWrapper(featuresContext, sLogger);
    }
 
    private ElementDispatcher createElementDispatcher()
    {
        try
        {
            URL rulesURL = ElementDispatcher.class.getResource("ElementDispatcherRules.xml");
            assert rulesURL != null;
            Digester digester = DigesterLoader.createDigester(rulesURL);
            URL filterURL = null;
            if (_filterConfig != null)
            {
                File config = new File(_filterConfig);
                if (config.exists())
                {
                    filterURL = config.toURI().toURL();
                }
            }
            if (filterURL == null)
            {
                // config = new File("conf/DefaultConvertShpFilter.xml");
                filterURL = this.getClass().getResource("/conf/DefaultConvertShpFilter.xml");
               // filterURL = this.getClass().getResource("/conf/ConvertShpFilterForLevel.xml");
            }
            assert filterURL != null;
            return (ElementDispatcher) digester.parse(filterURL);
        } catch (UnsupportedEncodingException e)
        {
            logger.info(e.getMessage(), e);
            throw new RuntimeException(e.getMessage(), e);
        } catch (MalformedURLException e)
        {
            logger.info(e.getMessage(), e);
            throw new RuntimeException(e.getMessage(), e);
        } catch (IOException e)
        {
            logger.info(e.getMessage(), e);
            throw new RuntimeException(e.getMessage(), e);
        } catch (SAXException e)
        {
            logger.info(e.getMessage(), e);
            throw new RuntimeException(e.getMessage(), e);
        }
    }
 
    public void putFeatureCollection(Element element)
    {
        // 判斷是否符和條件
        Feature feature = elementDispatcher.execute(element);
        if (feature == null)
        {
            FrammeAttributeData linkage =
                    AbstractFLinkageDispatchableFilter.getFeatureLinkage(element);
            logger.warn("Unknown Element:" + element.getElementType().toString() +
                    ":type=" + element.getType() + ":lv=" + element.getLevelIndex() + ":id=" +
                    (linkage == null ? "NULL" : (linkage.getFsc() + "|" + linkage.getComponentID())));
 
            return;
        }
 
        if (!txFeaturesContext.containsKey(feature.getFeatureType()))
        {
            txFeaturesContext.put(feature.getFeatureType(), new ArrayList());
        }
        ArrayList arrayList = (ArrayList) txFeaturesContext.get(feature.getFeatureType());
        arrayList.add(feature);
    }
 
    public void startTransaction()
    {
       //txFeaturesContext.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()
    {
        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 = null;
                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 JobExecutionContext getExecutionContext()
    {
        return executionContext;
    }
 
    public void setExecutionContext(JobExecutionContext context)
    {
        executionContext = context;
    }
 
    /**
     * 關閉設備寫入器
     * @throws IOException IO發生錯誤
     */
    public void closeFeatureWrite() throws IOException {
       Iterator iter =   this.featuresWriterContext.values().iterator();
 
        while(iter.hasNext())
        {
           FeatureWriter writer = (FeatureWriter) iter.next();
           writer.close();
        }
 
        this.featuresWriterContext = null;
    }
 
    /**
     * 取得資料輸出路徑
     * @return 路徑的字串
     */
    public String getDataOutPath()
    {
        if (dataOut == null)
        {
            File outPath = new File(getDataPath(), SHPOUTPATH);
            if (!outPath.exists())
            {
                outPath.mkdir();
            } else if (!outPath.isDirectory())
            {
                outPath.mkdir();
            }
            dataOut = getDataPath() + SHPOUTPATH;
        }
        return dataOut;
    }
}