New file |
| | |
| | | package com.ximple.eofms.jobs; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.HashMap; |
| | | import java.util.Iterator; |
| | | import java.util.List; |
| | | import java.net.URL; |
| | | import java.net.MalformedURLException; |
| | | import java.io.File; |
| | | import java.io.UnsupportedEncodingException; |
| | | import java.io.IOException; |
| | | import java.nio.charset.Charset; |
| | | |
| | | import org.apache.commons.logging.Log; |
| | | import org.apache.commons.logging.LogFactory; |
| | | import org.apache.commons.transaction.util.LoggerFacade; |
| | | import org.apache.commons.transaction.util.CommonsLoggingLogger; |
| | | import org.apache.commons.transaction.memory.PessimisticMapWrapper; |
| | | import org.apache.commons.digester.Digester; |
| | | import org.apache.commons.digester.xmlrules.DigesterLoader; |
| | | import org.geotools.feature.Feature; |
| | | import org.geotools.feature.IllegalAttributeException; |
| | | import org.geotools.feature.SchemaException; |
| | | import org.geotools.feature.FeatureType; |
| | | import org.geotools.feature.SimpleFeature; |
| | | import org.geotools.data.FeatureWriter; |
| | | import org.geotools.data.Transaction; |
| | | import org.geotools.data.shapefile.ShapefileDataStore; |
| | | import org.geotools.data.shapefile.indexed.IndexedShapefileDataStore; |
| | | import org.xml.sax.SAXException; |
| | | |
| | | import com.vividsolutions.jts.geom.GeometryFactory; |
| | | |
| | | import com.ximple.eofms.filter.ElementDispatcher; |
| | | import com.ximple.eofms.filter.AbstractFLinkageDispatchableFilter; |
| | | import com.ximple.eofms.filter.ElementDispatchableFilter; |
| | | import com.ximple.eofms.filter.TypeCompIdDispatchableFilter; |
| | | import com.ximple.eofms.filter.TypeCompLevelIdDispatchableFilter; |
| | | import com.ximple.eofms.filter.TypeIdDispatchableFilter; |
| | | import com.ximple.io.dgn7.Element; |
| | | import com.ximple.io.dgn7.FrammeAttributeData; |
| | | import com.ximple.io.dgn7.ComplexElement; |
| | | import com.ximple.io.dgn7.UserAttributeData; |
| | | |
| | | public class DummyFeatureConvertJobContext extends AbstractDgnFileJobContext |
| | | { |
| | | static final Log logger = LogFactory.getLog(DummyFeatureConvertJobContext.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<String, ArrayList<Feature>> featuresContext = new HashMap<String, ArrayList<Feature>>(); |
| | | private HashMap<String, FeatureWriter> featuresWriterContext = new HashMap<String, FeatureWriter>(); |
| | | |
| | | private PessimisticMapWrapper txFeaturesContext; |
| | | |
| | | private ElementDispatcher elementDispatcher; |
| | | private String _filterConfig; |
| | | |
| | | public DummyFeatureConvertJobContext(String dataPath, String filterConfig) |
| | | { |
| | | super(dataPath); |
| | | txFeaturesContext = new PessimisticMapWrapper(featuresContext, sLogger); |
| | | _filterConfig = filterConfig; |
| | | elementDispatcher = createElementDispatcher(); |
| | | } |
| | | |
| | | 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) throws IllegalAttributeException, SchemaException |
| | | { |
| | | assert elementDispatcher != null; |
| | | |
| | | if (element == null) |
| | | { |
| | | logger.warn("Unknown Element:" + null); |
| | | return; |
| | | } |
| | | |
| | | // §PÂ_¬O§_²Å©M±ø¥ó |
| | | 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()))); |
| | | if (element instanceof ComplexElement) |
| | | { |
| | | ComplexElement complex = (ComplexElement) element; |
| | | logger.warn("----Complex Element size=" + complex.size()); |
| | | } |
| | | |
| | | 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() |
| | | { |
| | | assert elementDispatcher != null; |
| | | for (ElementDispatchableFilter filter : elementDispatcher.getRules()) |
| | | { |
| | | if (filter instanceof TypeCompIdDispatchableFilter) |
| | | { |
| | | ((TypeCompIdDispatchableFilter) filter).getCreateStrategy(); |
| | | } else if (filter instanceof TypeCompLevelIdDispatchableFilter) |
| | | { |
| | | ((TypeCompIdDispatchableFilter) filter).getCreateStrategy(); |
| | | } else if(filter instanceof TypeIdDispatchableFilter) |
| | | { |
| | | ((TypeCompIdDispatchableFilter) filter).getCreateStrategy(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | 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() + File.separator + featureType.getTypeName()); |
| | | logger.debug("Begin Save shapefile:" + sfile.toURI()); |
| | | |
| | | FeatureWriter writer; |
| | | if (featuresWriterContext.containsKey(featureType.getTypeName())) |
| | | { |
| | | writer = featuresWriterContext.get(featureType.getTypeName()); |
| | | } else |
| | | { |
| | | // ShapefileDataStore shapefileDataStore = new ShapefileDataStore(sfile.toURI().toURL()); |
| | | /* |
| | | ShapefileDataStore shapefileDataStore = new ShapefileDataStore(sfile.toURI().toURL(), |
| | | true, Charset.forName("UTF-8")); |
| | | */ |
| | | if (!sfile.exists()) |
| | | { |
| | | ShapefileDataStore shapefileDataStore = new IndexedShapefileDataStore(sfile.toURI().toURL(), |
| | | null, true, true, IndexedShapefileDataStore.TREE_QIX, Charset.forName("UTF-8")); |
| | | shapefileDataStore.createSchema(featureType); |
| | | writer = shapefileDataStore.getFeatureWriter(featureType.getTypeName(), |
| | | Transaction.AUTO_COMMIT); |
| | | } else { |
| | | ShapefileDataStore shapefileDataStore = new IndexedShapefileDataStore(sfile.toURI().toURL(), |
| | | null, true, true, IndexedShapefileDataStore.TREE_QIX, Charset.forName("UTF-8")); |
| | | writer = shapefileDataStore.getFeatureWriterAppend(featureType.getTypeName(), |
| | | Transaction.AUTO_COMMIT); |
| | | } |
| | | featuresWriterContext.put(featureType.getTypeName(), writer); |
| | | } |
| | | |
| | | ArrayList<Feature> features = 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() |
| | | { |
| | | if (dataOut == null) |
| | | { |
| | | File outPath = new File(getDataPath(), SHPOUTPATH); |
| | | if (!outPath.exists()) |
| | | { |
| | | outPath.mkdir(); |
| | | } else if (!outPath.isDirectory()) |
| | | { |
| | | outPath.mkdir(); |
| | | } |
| | | dataOut = outPath.toString(); |
| | | } |
| | | return dataOut; |
| | | } |
| | | |
| | | public void closeFeatureWriter() throws IOException { |
| | | |
| | | for (FeatureWriter featureWriter : this.featuresWriterContext.values()) |
| | | { |
| | | featureWriter.close(); |
| | | } |
| | | |
| | | this.featuresWriterContext.clear(); |
| | | } |
| | | |
| | | 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; |
| | | } |
| | | } |