package com.ximple.eofms.jobs.context.shapefile; import java.io.File; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URL; import java.nio.charset.Charset; 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 java.util.TimeZone; import com.vividsolutions.jts.util.Assert; import com.ximple.eofms.filter.AbstractFLinkageDispatchableFilter; import com.ximple.eofms.filter.ElementDispatcher; import com.ximple.eofms.jobs.OracleElementLogger; import com.ximple.eofms.jobs.context.AbstractOracleJobContext; import com.ximple.eofms.util.ElementDigesterUtils; import com.ximple.io.dgn7.ComplexElement; import com.ximple.io.dgn7.Element; import com.ximple.io.dgn7.FrammeAttributeData; import org.apache.commons.digester3.Digester; 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.opengis.feature.IllegalAttributeException; import org.opengis.feature.simple.SimpleFeature; import org.opengis.feature.simple.SimpleFeatureType; import org.quartz.JobExecutionContext; import org.xml.sax.SAXException; public class OracleConvertShapefilesJobContext extends AbstractOracleJobContext { static Log logger = LogFactory.getLog(OracleConvertShapefilesJobContext.class); static final LoggerFacade sLogger = new CommonsLoggingLogger(logger); public static final String SHPOUTPATH = "shpout"; protected boolean profileMode = false; private OracleElementLogger elmLogger = null; 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; private String _convertDB = null; private String _convertFile = null; private String _convertElementIn = null; private String currentSchema = null; private boolean schemaChanged = false; private boolean withIndex = false; public OracleConvertShapefilesJobContext(String filterConfig, boolean profileMode, boolean useTransform) { super(profileMode, useTransform); properties = new Properties(); _filterConfig = filterConfig; elementDispatcher = createElementDispatcher(); txFeaturesContext = new PessimisticMapWrapper(featuresContext, sLogger); } private ElementDispatcher createElementDispatcher() { try { 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; Digester digester = ElementDigesterUtils.getElementDigester(); 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) { assert elementDispatcher != null; // �P�_�O�_�ũM��� SimpleFeature feature = elementDispatcher.execute(element, getDistId(), isTransformed()); if (feature == null) { boolean isEmptySize = false; FrammeAttributeData linkage = AbstractFLinkageDispatchableFilter.getFeatureLinkage(element); logger.warn("Unknown Element:" + element.getElementType().toString() + ":type=" + element.getType() + ":lv=" + element.getLevelIndex() + ":id=" + (linkage == null ? "NULL" : "FSC=" + (linkage.getFsc() + "|COMPID=" + linkage.getComponentID()))); if (element instanceof ComplexElement) { ComplexElement complex = (ComplexElement) element; logger.warn("----Complex Element size=" + complex.size() + ":" + (linkage == null ? "NULL" : (linkage.getUfid()))); isEmptySize = true; } if (getElementLogging() && (!isEmptySize)) { getElementLogger().logElement(element, getCurrentSchema()); } 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(); } if (this.getElementLogger() != null) this.getElementLogger().flashLogging(); } public void rollbackTransaction() { //txFeaturesContext.rollbackTransaction(); if (!featuresContext.isEmpty()) { updateDataStore(); } } private void updateDataStore() { if (isProfileMode()) markUpdateTime(); Iterator it = featuresContext.keySet().iterator(); try { while (it.hasNext()) { SimpleFeatureType featureType = (SimpleFeatureType) 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 { boolean existFile = sfile.exists(); ShapefileDataStore shapefileDataStore = new ShapefileDataStore(sfile.toURI().toURL()); /* if(namespace != null) { shapefileDataStore.setNamespaceURI(namespace.toString()); } */ shapefileDataStore.setMemoryMapped(true); // shapefileDataStore.setBufferCachingEnabled(cacheMemoryMaps); shapefileDataStore.setCharset(Charset.forName("UTF-8")); shapefileDataStore.setTimeZone(TimeZone.getDefault()); shapefileDataStore.setIndexed(withIndex); shapefileDataStore.setIndexCreationEnabled(withIndex); if (!existFile) { shapefileDataStore.createSchema(featureType); writer = shapefileDataStore.getFeatureWriter(featureType.getTypeName(), Transaction.AUTO_COMMIT); } else { writer = shapefileDataStore.getFeatureWriterAppend(featureType.getTypeName(), Transaction.AUTO_COMMIT); } featuresWriterContext.put(featureType.getTypeName(), writer); } ArrayList features = (ArrayList) featuresContext.get(featureType); Iterator itFeature = features.iterator(); while (itFeature.hasNext()) { SimpleFeature feature = (SimpleFeature) itFeature.next(); ((SimpleFeature) writer.next()).setAttributes(feature.getAttributes()); } //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); } finally { if (isProfileMode()) accumulateUpdateTime(); } } public JobExecutionContext getExecutionContext() { return executionContext; } public void setExecutionContext(JobExecutionContext context) { executionContext = context; } /** * �����]�Ƽg�J�� * * @throws IOException IO�o�Ϳ�~ */ public void closeFeatureWriter() throws IOException { for (FeatureWriter featureWriter : this.featuresWriterContext.values()) { featureWriter.close(); } this.featuresWriterContext.clear(); } /** * ��o��ƿ�X���| * * @return ���|���r�� */ 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 setConvertDB(String convertDB) { _convertDB = convertDB; } public void setConvertFile(String convertFile) { _convertFile = convertFile; } protected OracleElementLogger getElementLogger() { if (elmLogger == null) { elmLogger = new OracleElementLogger(getOracleConnection()); elmLogger.setDataPath(this.getDataPath()); } return elmLogger; } public String getCurrentSchema() { return currentSchema; } public void setCurrentSchema(String querySchema) { this.currentSchema = querySchema; this.schemaChanged = true; } public void setConvertElementIn(String convertElementIn) { _convertElementIn = convertElementIn; } public boolean isWithIndex() { return withIndex; } public void setWithIndex(boolean withIndex) { this.withIndex = withIndex; } protected Log getLogger() { return logger; } }