From a4b6a7552fa976574b985fb7bac21cf2fe486d5c Mon Sep 17 00:00:00 2001 From: ?? ? <ulysseskao@ximple.com.tw> Date: Fri, 13 Jun 2008 16:49:01 +0800 Subject: [PATCH] update for EOFM-117 --- xdgnjobs/ximple-jobcarrier/src/main/resources/quartz_jobs.xml | 4 xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/context/postgis/AbstractOracleToPostGISJobContext.java | 442 +++++++++++------------- xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/filter/CreateComplexChainStrategy.java | 186 ++++++++++ .gitattributes | 1 xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/context/postgis/GeneralDgnConvertPostGISJobContext.java | 11 xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/context/postgis/OracleConvertPostGISJobContext.java | 50 +- xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/filter/CreateLineStringStrategy.java | 21 + xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/context/postgis/IndexDgnConvertPostGISJobContext.java | 2 xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/context/postgis/AbstractDgnToPostGISJobContext.java | 344 ------------------- 9 files changed, 446 insertions(+), 615 deletions(-) diff --git a/.gitattributes b/.gitattributes index b80d943..5d98d7a 100644 --- a/.gitattributes +++ b/.gitattributes @@ -54,6 +54,7 @@ xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/filter/AbstractDispatchableFilter.java svneol=native#text/plain xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/filter/AbstractFLinkageDispatchableFilter.java svneol=native#text/plain xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/filter/CreateArcLineStringStrategy.java svneol=native#text/plain +xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/filter/CreateComplexChainStrategy.java svneol=native#text/plain xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/filter/CreateEllipseShapeStrategy.java svneol=native#text/plain xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/filter/CreateFeatureTypeEventListener.java svneol=native#text/plain xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/filter/CreateFeatureTypeStrategy.java svneol=native#text/plain diff --git a/xdgnjobs/ximple-jobcarrier/src/main/resources/quartz_jobs.xml b/xdgnjobs/ximple-jobcarrier/src/main/resources/quartz_jobs.xml index 76ea5f6..1bd55fc 100644 --- a/xdgnjobs/ximple-jobcarrier/src/main/resources/quartz_jobs.xml +++ b/xdgnjobs/ximple-jobcarrier/src/main/resources/quartz_jobs.xml @@ -76,7 +76,7 @@ </entry> <entry> <key>CONVERTDB</key> - <value>false</value> + <value>true</value> </entry> <entry> <key>CONVERTFILE</key> @@ -108,7 +108,7 @@ </entry> <entry> <key>COPYCONNECTIVITYMODE</key> - <value>false</value> + <value>true</value> </entry> </job-data-map> </job-detail> diff --git a/xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/filter/CreateComplexChainStrategy.java b/xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/filter/CreateComplexChainStrategy.java new file mode 100644 index 0000000..87ecfec --- /dev/null +++ b/xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/filter/CreateComplexChainStrategy.java @@ -0,0 +1,186 @@ +package com.ximple.eofms.filter; + +import java.util.TreeMap; +import java.util.List; +import javax.swing.event.EventListenerList; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.geotools.feature.FeatureTypeBuilder; +import org.geotools.feature.FeatureType; +import org.geotools.feature.SchemaException; +import org.geotools.feature.Feature; +import org.geotools.feature.IllegalAttributeException; + +import com.vividsolutions.jts.geom.GeometryFactory; +import com.vividsolutions.jts.geom.Geometry; +import com.vividsolutions.jts.geom.MultiLineString; +import com.vividsolutions.jts.geom.CoordinateList; +import com.vividsolutions.jts.geom.LineString; + +import com.ximple.eofms.util.TWD97GeometryConverterDecorator; +import com.ximple.eofms.util.FeatureTypeBuilderUtil; +import com.ximple.eofms.util.DefaultColorTable; +import com.ximple.io.dgn7.FrammeAttributeData; +import com.ximple.io.dgn7.Element; +import com.ximple.io.dgn7.UserAttributeData; +import com.ximple.io.dgn7.LineStringElement; +import com.ximple.io.dgn7.ComplexChainElement; +import com.ximple.io.dgn7.LineElement; +import com.ximple.io.dgn7.ArcElement; + +public class CreateComplexChainStrategy implements CreateFeatureTypeStrategy +{ + static final Log logger = LogFactory.getLog(CreateLineStringStrategy.class); + GeometryFactory geometryFactory = new GeometryFactory(); + TreeMap<String, FeatureTypeBuilder> typeBuilders = new TreeMap<String, FeatureTypeBuilder>(); + TWD97GeometryConverterDecorator convertDecorator = new TWD97GeometryConverterDecorator(); + + // Create the listener list + protected EventListenerList listenerList = new EventListenerList(); + + public CreateComplexChainStrategy() + { + } + + 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; + } + + public FeatureType createFeatureElement(String featureName) throws SchemaException + { + if (!typeBuilders.containsKey(featureName)) + { + FeatureTypeBuilder typeBuilder = FeatureTypeBuilderUtil.createMultiLineFeatureTypeBuilder(featureName); + typeBuilders.put(featureName, typeBuilder); + fireFeatureTypeEvent(new FeatureTypeEvent(this, typeBuilder.getFeatureType())); + } + return typeBuilders.get(featureName).getFeatureType(); + } + + public Feature createFeature(FeatureType featureType, Element element) throws IllegalAttributeException + { + DefaultColorTable colorTable = (DefaultColorTable) DefaultColorTable.getInstance(); + FrammeAttributeData fLinkage = getFeatureLinkage(element); + Feature feature = null; + if (fLinkage == null) return null; + if (element instanceof LineStringElement) + { + LineStringElement lineStringElement = (LineStringElement) element; + convertDecorator.setConverter(lineStringElement); + Geometry gobj = convertDecorator.toGeometry(geometryFactory); + if (gobj instanceof LineString) + { + gobj = geometryFactory.createMultiLineString(new LineString[] {(LineString) gobj}); + } + feature = featureType.create(new Object[]{ + gobj, + fLinkage.getFsc(), + (long) fLinkage.getUfid(), + (short) fLinkage.getComponentID(), + (short) 0, + (short) lineStringElement.getLevelIndex(), + colorTable.getColorCode(lineStringElement.getColorIndex()), + (short) lineStringElement.getWeight(), + (short) lineStringElement.getLineStyle() + }); + } else if (element instanceof ComplexChainElement) + { + ComplexChainElement complexChain = (ComplexChainElement) element; + convertDecorator.setConverter(complexChain); + Geometry gobj = convertDecorator.toGeometry(geometryFactory); + if (gobj instanceof LineString) + { + gobj = geometryFactory.createMultiLineString(new LineString[] {(LineString) gobj}); + } + feature = featureType.create(new Object[]{ + gobj, + fLinkage.getFsc(), + (long) fLinkage.getUfid(), + (short) fLinkage.getComponentID(), + (short) 0, + (short) complexChain.getLevelIndex(), + colorTable.getColorCode(complexChain.getColorIndex()), + (short) complexChain.getWeight(), + (short) complexChain.getLineStyle() + }); + } else if (element instanceof LineElement) + { + LineElement lineElement = (LineElement) element; + convertDecorator.setConverter(lineElement); + Geometry gobj = convertDecorator.toGeometry(geometryFactory); + if (gobj instanceof LineString) + { + gobj = geometryFactory.createMultiLineString(new LineString[] {(LineString) gobj}); + } + feature = featureType.create(new Object[]{ + gobj, + fLinkage.getFsc(), + (long) fLinkage.getUfid(), + (short) fLinkage.getComponentID(), + (short) 0, + (short) lineElement.getLevelIndex(), + colorTable.getColorCode(lineElement.getColorIndex()), + (short) lineElement.getWeight(), + (short) lineElement.getLineStyle() + }); + return feature; + } else if (element instanceof ArcElement) + { + ArcElement lineStringElement = (ArcElement) element; + convertDecorator.setConverter(lineStringElement); + Geometry gobj = convertDecorator.toGeometry(geometryFactory); + if (gobj instanceof LineString) + { + gobj = geometryFactory.createMultiLineString(new LineString[] {(LineString) gobj}); + } + feature = featureType.create(new Object[]{ + gobj, + fLinkage.getFsc(), + (long) fLinkage.getUfid(), + (short) fLinkage.getComponentID(), + (short) 0, + (short) lineStringElement.getLevelIndex(), + colorTable.getColorCode(lineStringElement.getColorIndex()), + (short) lineStringElement.getWeight(), + (short) lineStringElement.getLineStyle() + }); + } + + return feature; + } + + public void addCreateFeatureTypeEventListener(CreateFeatureTypeEventListener listener) + { + listenerList.add(CreateFeatureTypeEventListener.class, listener); + } + + public void removeCreateFeatureTypeEventListener(CreateFeatureTypeEventListener listener) + { + listenerList.remove(CreateFeatureTypeEventListener.class, listener); + } + + protected void fireFeatureTypeEvent(FeatureTypeEvent evt) + { + Object[] listeners = listenerList.getListenerList(); + for (int i = 0; i < listeners.length; i += 2) + { + if (listeners[i] == CreateFeatureTypeEventListener.class) + { + ((CreateFeatureTypeEventListener) listeners[i + 1]).createFeatureTypeOccurred(evt); + } + } + } +} diff --git a/xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/filter/CreateLineStringStrategy.java b/xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/filter/CreateLineStringStrategy.java index 881cfc0..de94757 100644 --- a/xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/filter/CreateLineStringStrategy.java +++ b/xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/filter/CreateLineStringStrategy.java @@ -14,6 +14,10 @@ import org.geotools.feature.SchemaException; import com.vividsolutions.jts.geom.GeometryFactory; +import com.vividsolutions.jts.geom.Geometry; +import com.vividsolutions.jts.geom.MultiLineString; +import com.vividsolutions.jts.geom.CoordinateArrays; +import com.vividsolutions.jts.geom.CoordinateList; import com.ximple.eofms.util.DefaultColorTable; import com.ximple.eofms.util.FeatureTypeBuilderUtil; @@ -92,8 +96,23 @@ { ComplexChainElement complexChain = (ComplexChainElement) element; convertDecorator.setConverter(complexChain); + Geometry gobj = convertDecorator.toGeometry(geometryFactory); + if (gobj instanceof MultiLineString) + { + MultiLineString mline = (MultiLineString) gobj; + CoordinateList coordinateList = new CoordinateList(); + if (mline.getNumGeometries() == 1) + { + for (int i = 0; i < mline.getNumGeometries(); i++) + { + coordinateList.add(mline.getGeometryN(i).getCoordinates(), true); + } + } + + gobj = geometryFactory.createLineString(coordinateList.toCoordinateArray()); + } feature = featureType.create(new Object[]{ - convertDecorator.toGeometry(geometryFactory), + gobj, fLinkage.getFsc(), (long) fLinkage.getUfid(), (short) fLinkage.getComponentID(), diff --git a/xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/context/postgis/AbstractDgnToPostGISJobContext.java b/xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/context/postgis/AbstractDgnToPostGISJobContext.java index 93a0b8b..fff840d 100644 --- a/xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/context/postgis/AbstractDgnToPostGISJobContext.java +++ b/xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/context/postgis/AbstractDgnToPostGISJobContext.java @@ -274,260 +274,6 @@ return sql.toString(); } - public ArrayList<String> createSchemaTexts(FeatureType featureType) throws IOException - { - String tableName = featureType.getTypeName(); - - // String lcTableName = tableName.toLowerCase(); - ArrayList<String> result = new ArrayList<String>(); - - AttributeType[] attributeType = featureType.getAttributeTypes(); - String dbSchema = targetDataStore.getDatabaseSchemaName(); - - Connection con = getConnection(); - - boolean shouldExecute = !tablePresent(tableName, con); - - try - { - StringBuffer sql = new StringBuffer("CREATE TABLE "); - sql.append(encodeSchemaTableName(tableName)); - sql.append(" ( gid serial PRIMARY KEY, "); - sql.append(makeSqlCreate(attributeType)); - sql.append(");"); - - String sqlStr = sql.toString(); - getLogger().info(sqlStr); - - if (shouldExecute) - { - result.add(sqlStr); - } - - //fix from pr: it may be that table existed and then was dropped - //without removing its geometry info from GEOMETRY_COLUMNS. - //To support this, try to delete before inserting. - //Preserving case for table names gives problems, - //so convert to lower case - - sql = new StringBuffer("DELETE FROM GEOMETRY_COLUMNS WHERE f_table_catalog=''"); - sql.append(" AND f_table_schema = '"); - sql.append(dbSchema); - sql.append("'"); - sql.append("AND f_table_name = '"); - sql.append(tableName); - sql.append("';"); - - //prints statement for later reuse - sqlStr = sql.toString(); - getLogger().info(sqlStr); - - if (shouldExecute) - { - result.add(sqlStr); - } - - //Ok, so Paolo Rizzi suggested that we get rid of our hand-adding - //of geometry column information and use AddGeometryColumn instead - //as it is better (this is in GEOT-379, he attached an extended - //datastore that does postgis fixes). But I am pretty positive - //the reason we are doing things this way is to preserve the order - //of FeatureTypes. I know this is fairly silly, from most - //information perspectives, but from another perspective it seems - //to make sense - if you were transfering a featureType from one - //data store to another then it should have the same order, right? - //And order is important in WFS. There are a few caveats though - //for one I don't even know if things work right. I imagine the - //proper constraints that a AddGeometryColumn operation does are - //not set in our hand version, for one. I would feel better about - //ignoring the order and just doing things as we like if we had - //views in place, if users could add the schema, and then be able - //to get it back in exactly the order they wanted. So for now - //let's leave things as is, and maybe talk about it in an irc. -ch - for (AttributeType anAttributeType : attributeType) - { - if (!(anAttributeType instanceof GeometryAttributeType)) - { - continue; - } - GeometryAttributeType geomAttribute = (GeometryAttributeType) anAttributeType; - String columnName = anAttributeType.getLocalName(); - - CoordinateReferenceSystem refSys = geomAttribute - .getCoordinateSystem(); - int SRID; - - if (refSys != null) - { - try - { - Set ident = refSys.getIdentifiers(); - if ((ident == null || ident.isEmpty()) && refSys == DefaultGeographicCRS.WGS84) - { - SRID = 4326; - } else - { - String code = ((NamedIdentifier) ident.toArray()[0]).getCode(); - SRID = Integer.parseInt(code); - } - } catch (Exception e) - { - getLogger().warn("SRID could not be determined"); - SRID = -1; - } - } else - { - SRID = -1; - } - - String typeName; - - //this construct seems unnecessary, since we already would - //pass over if this wasn't a geometry... - Class type = geomAttribute.getType(); - - if (geomAttribute instanceof GeometryAttributeType) - { - typeName = getGeometrySQLTypeName(type); - } else - { - typeName = CLASS_MAPPINGS.get(type); - } - - if (typeName != null) - { - //add a row to the geometry_columns table - sql = new StringBuffer("INSERT INTO GEOMETRY_COLUMNS VALUES ("); - sql.append("'','"); - sql.append(dbSchema); - sql.append("','"); - sql.append(tableName); - sql.append("','"); - sql.append(columnName); - sql.append("',2,"); - sql.append(SRID); - sql.append(",'"); - sql.append(typeName); - sql.append("');"); - - sqlStr = sql.toString(); - getLogger().info(sqlStr); - - if (shouldExecute) - { - result.add(sqlStr); - } - - //add geometry constaints to the table - if (SRID > -1) - { - sql = new StringBuffer("ALTER TABLE "); - sql.append(encodeSchemaTableName(tableName)); - sql.append(" ADD CONSTRAINT enforce_srid_"); - sql.append(columnName); - sql.append(" CHECK (SRID("); - sql.append(encodeSchemaColumnName(columnName)); - sql.append(") = "); - sql.append(SRID); - sql.append(");"); - sqlStr = sql.toString(); - getLogger().info(sqlStr); - if (shouldExecute) - { - result.add(sqlStr); - } - } - - sql = new StringBuffer("ALTER TABLE "); - sql.append(encodeSchemaTableName(tableName)); - sql.append(" ADD CONSTRAINT enforce_dims_"); - sql.append(columnName); - sql.append(" CHECK (ndims("); - sql.append(encodeSchemaColumnName(columnName)); - sql.append(") = 2);"); - sqlStr = sql.toString(); - getLogger().info(sqlStr); - if (shouldExecute) - { - result.add(sqlStr); - } - - /* - if (!typeName.equals("GEOMETRY")) - { - sql = new StringBuffer("ALTER TABLE "); - sql.append(encodeSchemaTableName(tableName)); - sql.append(" ADD CONSTRAINT enforce_geotype_"); - sql.append(columnName); - sql.append(" CHECK (geometrytype("); - sql.append(encodeSchemaColumnName(columnName)); - sql.append(") = '"); - sql.append(typeName); - sql.append("'::text OR "); - sql.append(encodeSchemaColumnName(columnName)); - sql.append(" IS NULL);"); - sqlStr = sql.toString(); - getLogger().info(sqlStr); - if (shouldExecute) - { - result.add(sqlStr); - } - } - */ - } else - { - getLogger().warn("Error: " + geomAttribute.getLocalName() + " unknown type!!!"); - } - - String indexName = tableName.replace('-', '_'); - //also build a spatial index on each geometry column. - sql = new StringBuffer("CREATE INDEX spatial_"); - sql.append(indexName); - sql.append("_"); - sql.append(anAttributeType.getLocalName().toLowerCase()); - sql.append(" ON "); - sql.append(encodeSchemaTableName(tableName)); - sql.append(" USING GIST ("); - sql.append(encodeSchemaColumnName(anAttributeType.getLocalName())); - sql.append(");"); - - sqlStr = sql.toString(); - getLogger().info(sqlStr); - - if (shouldExecute) - { - result.add(sqlStr); - } - } - - con.commit(); - - } catch (SQLException e) - { - try - { - if (con != null) - { - con.rollback(); - } - } catch (SQLException sqle) - { - throw new IOException(sqle.getMessage()); - } - - throw (IOException) new IOException(e.getMessage()).initCause(e); - } finally - { - } - - if (!shouldExecute) - { - throw new IOException("The table " + tableName + " already exists."); - } - - return result; - } - public ArrayList<String> createNewSchemaTexts(FeatureType featureType) throws IOException { String origintableName = featureType.getTypeName(); @@ -678,96 +424,6 @@ return true; } - - private StringBuffer makeSqlCreate(AttributeType[] attributeType) - throws IOException - { - StringBuffer buf = new StringBuffer(""); - - for (AttributeType anAttributeType : attributeType) - { - String typeName; - typeName = CLASS_MAPPINGS.get(anAttributeType.getBinding()); - if (typeName == null) - typeName = GEOM_CLASS_MAPPINGS.get(anAttributeType.getBinding()); - - if (typeName != null) - { - if (anAttributeType instanceof GeometryAttributeType) - { - typeName = "GEOMETRY"; - } else if (typeName.equals("VARCHAR")) - { - int length = -1; - Filter f = anAttributeType.getRestriction(); - if (f != null && f != Filter.EXCLUDE && f != Filter.INCLUDE && - (f instanceof PropertyIsLessThan || f instanceof PropertyIsLessThanOrEqualTo)) - { - try - { - BinaryComparisonOperator cf = (BinaryComparisonOperator) f; - if (cf.getExpression1() instanceof LengthFunction) - { - length = Integer.parseInt(((Literal) cf.getExpression2()).getValue().toString()); - } else - { - if (cf.getExpression2() instanceof LengthFunction) - { - length = Integer.parseInt(((Literal) cf.getExpression1()).getValue().toString()); - } - } - } catch (NumberFormatException e) - { - length = 256; - } - } else - { - length = 256; - } - - if (length < 1) - { - getLogger().warn("FeatureType did not specify string length; defaulted to 256"); - length = 256; - } else if (length > MAX_ALLOWED_VALUE) - { - length = MAX_ALLOWED_VALUE; - } - typeName = typeName + "(" + length + ")"; - } - - if (!anAttributeType.isNillable()) - { - typeName = typeName + " NOT NULL"; - } - - //TODO review!!! Is toString() always OK??? - Object defaultValue = anAttributeType.createDefaultValue(); - - if (defaultValue != null) - { - typeName = typeName + " DEFAULT '" - + defaultValue.toString() + "'"; - } - - buf.append(" \"").append(anAttributeType.getLocalName()).append("\" ").append(typeName).append(","); - - } else - { - String msg; - if (anAttributeType == null) - { - msg = "AttributeType was null!"; - } else - { - msg = "Type '" + anAttributeType.getBinding() + "' not supported!"; - } - throw (new IOException(msg)); - } - } - - return buf.deleteCharAt(buf.length() - 1); - } private StringBuffer makeNonGeomSqlCreate(AttributeType[] attributeType) throws IOException diff --git a/xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/context/postgis/AbstractOracleToPostGISJobContext.java b/xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/context/postgis/AbstractOracleToPostGISJobContext.java index 9f8d017..4cddb96 100644 --- a/xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/context/postgis/AbstractOracleToPostGISJobContext.java +++ b/xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/context/postgis/AbstractOracleToPostGISJobContext.java @@ -6,6 +6,7 @@ import java.sql.Statement; import java.sql.DatabaseMetaData; import java.sql.ResultSet; +import java.sql.PreparedStatement; import java.util.Map; import java.util.HashMap; import java.util.Set; @@ -237,254 +238,138 @@ conn.commit(); } - public ArrayList<String> createSchemaTexts(FeatureType featureType) throws IOException + protected String dropGeometryColumn(String dbSchema, String tableName, String geomField) { - String tableName = featureType.getTypeName(); + StringBuilder sb = new StringBuilder(); + sb.append("SELECT DropGeometryColumn('"); + sb.append(dbSchema); + sb.append("','"); + sb.append(tableName); + sb.append("','"); + sb.append(geomField); + sb.append("')"); + getLogger().info("Execute-" + sb.toString()); + return sb.toString(); + } - // String lcTableName = tableName.toLowerCase(); + private String addGeometryColumn(String dbSchema, String tableName, GeometryAttributeType geometryAttribute, int srid) + { + StringBuilder sql; + String typeName = getGeometrySQLTypeName(geometryAttribute.getBinding()); + if (typeName == null) + { + getLogger().warn("Error: " + geometryAttribute.getLocalName() + " unknown type!!!"); + throw new RuntimeException("Error: " + geometryAttribute.getLocalName() + " unknown type!!!"); + } + + sql = new StringBuilder("SELECT AddGeometryColumn('"); + sql.append(dbSchema); + sql.append("','"); + sql.append(tableName); + sql.append("','"); + sql.append(geometryAttribute.getLocalName()); + sql.append("','"); + sql.append(srid); + sql.append("','"); + sql.append(typeName); + sql.append("', 2);"); + + //prints statement for later reuse + return sql.toString(); + } + + public ArrayList<String> createNewSchemaTexts(FeatureType featureType) throws IOException + { + String origintableName = featureType.getTypeName(); + String tableName = origintableName.toLowerCase(); + ArrayList<String> result = new ArrayList<String>(); AttributeType[] attributeType = featureType.getAttributeTypes(); - String dbSchema = targetDataStore.getDatabaseSchemaName(); + // String dbSchema = targetDataStore.getDatabaseSchemaName(); Connection con = getConnection(); - boolean shouldExecute = !tablePresent(tableName, con); - - try + boolean shouldDrop = tablePresent(tableName, con); + if (shouldDrop) { - StringBuffer sql = new StringBuffer("CREATE TABLE "); - sql.append(encodeSchemaTableName(tableName)); - sql.append(" ( gid serial PRIMARY KEY, "); - sql.append(makeSqlCreate(attributeType)); - sql.append(");"); - - String sqlStr = sql.toString(); + String sqlStr = "DROP TABLE " + encodeSchemaTableName(tableName) + ";"; getLogger().info(sqlStr); + result.add(sqlStr); + } - if (shouldExecute) + StringBuffer sql = new StringBuffer("CREATE TABLE "); + sql.append(encodeSchemaTableName(tableName)); + sql.append(" ( gid serial PRIMARY KEY, "); + sql.append(makeNonGeomSqlCreate(attributeType)); + sql.append(");"); + + String sqlStr = sql.toString(); + getLogger().info(sqlStr); + result.add(sqlStr); + + for (AttributeType anAttributeType : attributeType) + { + if (!(anAttributeType instanceof GeometryAttributeType)) { - result.add(sqlStr); + continue; + } + GeometryAttributeType geomAttribute = (GeometryAttributeType) anAttributeType; + + if (shouldDrop) + { + sqlStr = dropGeometryColumn("", tableName, geomAttribute.getLocalName()); + getLogger().info(sqlStr); + result.add(sqlStr); } - //fix from pr: it may be that table existed and then was dropped - //without removing its geometry info from GEOMETRY_COLUMNS. - //To support this, try to delete before inserting. - //Preserving case for table names gives problems, - //so convert to lower case + CoordinateReferenceSystem refSys = geomAttribute.getCoordinateSystem(); + int SRID; - sql = new StringBuffer("DELETE FROM GEOMETRY_COLUMNS WHERE f_table_catalog=''"); - sql.append(" AND f_table_schema = '"); - sql.append(dbSchema); - sql.append("'"); - sql.append("AND f_table_name = '"); - sql.append(tableName); - sql.append("';"); + if (refSys != null) + { + try + { + Set ident = refSys.getIdentifiers(); + if ((ident == null || ident.isEmpty()) && refSys == DefaultGeographicCRS.WGS84) + { + SRID = 4326; + } else + { + String code = ((NamedIdentifier) ident.toArray()[0]).getCode(); + SRID = Integer.parseInt(code); + } + } catch (Exception e) + { + getLogger().warn("SRID could not be determined"); + SRID = -1; + } + } else + { + SRID = -1; + } - //prints statement for later reuse + sqlStr = addGeometryColumn("", tableName, geomAttribute, SRID); + getLogger().info(sqlStr); + result.add(sqlStr); + + + String indexName = tableName.replace('-', '_'); + //also build a spatial index on each geometry column. + sql = new StringBuffer("CREATE INDEX spatial_"); + sql.append(indexName); + sql.append("_"); + sql.append(anAttributeType.getLocalName().toLowerCase()); + sql.append(" ON "); + sql.append(encodeSchemaTableName(tableName)); + sql.append(" USING GIST ("); + sql.append(encodeSchemaColumnName(anAttributeType.getLocalName())); + sql.append(" gist_geometry_ops);"); + sqlStr = sql.toString(); getLogger().info(sqlStr); - if (shouldExecute) - { - result.add(sqlStr); - } - - //Ok, so Paolo Rizzi suggested that we get rid of our hand-adding - //of geometry column information and use AddGeometryColumn instead - //as it is better (this is in GEOT-379, he attached an extended - //datastore that does postgis fixes). But I am pretty positive - //the reason we are doing things this way is to preserve the order - //of FeatureTypes. I know this is fairly silly, from most - //information perspectives, but from another perspective it seems - //to make sense - if you were transfering a featureType from one - //data store to another then it should have the same order, right? - //And order is important in WFS. There are a few caveats though - //for one I don't even know if things work right. I imagine the - //proper constraints that a AddGeometryColumn operation does are - //not set in our hand version, for one. I would feel better about - //ignoring the order and just doing things as we like if we had - //views in place, if users could add the schema, and then be able - //to get it back in exactly the order they wanted. So for now - //let's leave things as is, and maybe talk about it in an irc. -ch - for (AttributeType anAttributeType : attributeType) - { - if (!(anAttributeType instanceof GeometryAttributeType)) - { - continue; - } - GeometryAttributeType geomAttribute = (GeometryAttributeType) anAttributeType; - String columnName = anAttributeType.getLocalName(); - - CoordinateReferenceSystem refSys = geomAttribute - .getCoordinateSystem(); - int SRID; - - if (refSys != null) - { - try - { - Set ident = refSys.getIdentifiers(); - if ((ident == null || ident.isEmpty()) && refSys == DefaultGeographicCRS.WGS84) - { - SRID = 4326; - } else - { - String code = ((NamedIdentifier) ident.toArray()[0]).getCode(); - SRID = Integer.parseInt(code); - } - } catch (Exception e) - { - getLogger().warn("SRID could not be determined"); - SRID = -1; - } - } else - { - SRID = -1; - } - - String typeName; - - //this construct seems unnecessary, since we already would - //pass over if this wasn't a geometry... - Class type = geomAttribute.getType(); - - if (geomAttribute instanceof GeometryAttributeType) - { - typeName = getGeometrySQLTypeName(type); - } else - { - typeName = CLASS_MAPPINGS.get(type); - } - - if (typeName != null) - { - //add a row to the geometry_columns table - sql = new StringBuffer("INSERT INTO GEOMETRY_COLUMNS VALUES ("); - sql.append("'','"); - sql.append(dbSchema); - sql.append("','"); - sql.append(tableName); - sql.append("','"); - sql.append(columnName); - sql.append("',2,"); - sql.append(SRID); - sql.append(",'"); - sql.append(typeName); - sql.append("');"); - - sqlStr = sql.toString(); - getLogger().info(sqlStr); - - if (shouldExecute) - { - result.add(sqlStr); - } - - //add geometry constaints to the table - if (SRID > -1) - { - sql = new StringBuffer("ALTER TABLE "); - sql.append(encodeSchemaTableName(tableName)); - sql.append(" ADD CONSTRAINT enforce_srid_"); - sql.append(columnName); - sql.append(" CHECK (SRID("); - sql.append(encodeSchemaColumnName(columnName)); - sql.append(") = "); - sql.append(SRID); - sql.append(");"); - sqlStr = sql.toString(); - getLogger().info(sqlStr); - if (shouldExecute) - { - result.add(sqlStr); - } - } - - sql = new StringBuffer("ALTER TABLE "); - sql.append(encodeSchemaTableName(tableName)); - sql.append(" ADD CONSTRAINT enforce_dims_"); - sql.append(columnName); - sql.append(" CHECK (ndims("); - sql.append(encodeSchemaColumnName(columnName)); - sql.append(") = 2);"); - sqlStr = sql.toString(); - getLogger().info(sqlStr); - if (shouldExecute) - { - result.add(sqlStr); - } - - if (!typeName.equals("GEOMETRY")) - { - sql = new StringBuffer("ALTER TABLE "); - sql.append(encodeSchemaTableName(tableName)); - sql.append(" ADD CONSTRAINT enforce_geotype_"); - sql.append(columnName); - sql.append(" CHECK (geometrytype("); - sql.append(encodeSchemaColumnName(columnName)); - sql.append(") = '"); - sql.append(typeName); - sql.append("'::text OR "); - sql.append(encodeSchemaColumnName(columnName)); - sql.append(" IS NULL);"); - sqlStr = sql.toString(); - getLogger().info(sqlStr); - if (shouldExecute) - { - result.add(sqlStr); - } - } - - } else - { - getLogger().warn("Error: " + geomAttribute.getLocalName() + " unknown type!!!"); - } - - String indexName = tableName.replace('-', '_'); - //also build a spatial index on each geometry column. - sql = new StringBuffer("CREATE INDEX spatial_"); - sql.append(indexName); - sql.append("_"); - sql.append(anAttributeType.getLocalName().toLowerCase()); - sql.append(" ON "); - sql.append(encodeSchemaTableName(tableName)); - sql.append(" USING GIST ("); - sql.append(encodeSchemaColumnName(anAttributeType.getLocalName())); - sql.append(");"); - - sqlStr = sql.toString(); - getLogger().info(sqlStr); - - if (shouldExecute) - { - result.add(sqlStr); - } - } - - con.commit(); - - } catch (SQLException e) - { - try - { - if (con != null) - { - con.rollback(); - } - } catch (SQLException sqle) - { - throw new IOException(sqle.getMessage()); - } - - throw (IOException) new IOException(e.getMessage()).initCause(e); - } finally - { - } - - if (!shouldExecute) - { - throw new IOException("The table " + tableName + " already exists."); + result.add(sqlStr); } return result; @@ -544,7 +429,7 @@ } - private StringBuffer makeSqlCreate(AttributeType[] attributeType) + private StringBuffer makeNonGeomSqlCreate(AttributeType[] attributeType) throws IOException { StringBuffer buf = new StringBuffer(""); @@ -554,14 +439,14 @@ String typeName; typeName = CLASS_MAPPINGS.get(anAttributeType.getBinding()); if (typeName == null) + { typeName = GEOM_CLASS_MAPPINGS.get(anAttributeType.getBinding()); + if (typeName != null) continue; + } if (typeName != null) { - if (anAttributeType instanceof GeometryAttributeType) - { - typeName = "GEOMETRY"; - } else if (typeName.equals("VARCHAR")) + if (typeName.equals("VARCHAR")) { int length = -1; Filter f = anAttributeType.getRestriction(); @@ -739,6 +624,38 @@ return (statementSQL.toString()); } + protected String makePrepareInsertSql(FeatureType featureType) + { + String tableName = encodeSchemaTableName(featureType.getTypeName()); + AttributeType[] attributeTypes = featureType.getAttributeTypes(); + + String attrValue; + + StringBuffer statementSQL = new StringBuffer("INSERT INTO " + tableName + " ("); + + // encode insertion for attributes, but remember to avoid auto-increment ones, + // they may be included in the feature type as well + for (AttributeType attributeType : attributeTypes) + { + String attName = attributeType.getLocalName(); + + String colName = encodeSchemaColumnName(attName); + statementSQL.append(colName).append(","); + } + + statementSQL.setCharAt(statementSQL.length() - 1, ')'); + statementSQL.append(" VALUES ("); + + for (AttributeType attributeType : attributeTypes) + { + statementSQL.append(" ? ,"); + } + + statementSQL.setCharAt(statementSQL.length() - 1, ')'); + + return (statementSQL.toString()); + } + protected String addQuotes(Object value) { String retString; @@ -783,4 +700,61 @@ public String encodeSchemaColumnName(String columnName) { return "\"" + columnName + "\""; } + + protected void bindFeatureParameters(PreparedStatement pstmt, Feature feature) throws SQLException + { + FeatureType featureType = feature.getFeatureType(); + + AttributeType[] attributeTypes = featureType.getAttributeTypes(); + Object[] attributes = feature.getAttributes(null); + + for (int i = 0; i < attributeTypes.length; i++) + { + if (attributeTypes[i] instanceof GeometryAttributeType) + { + pstmt.setBytes(i + 1, binaryWriter.writeBinary((Geometry) attributes[i])); + } else + { + if (attributeTypes[i].getBinding().equals(Short.class)) + { + pstmt.setShort(i + 1, (Short) attributes[i]); + } else if (attributeTypes[i].getBinding().equals(Integer.class)) + { + pstmt.setInt(i + 1, (Short) attributes[i]); + } else if (attributeTypes[i].getBinding().equals(Long.class)) + { + pstmt.setLong(i + 1, (Long) attributes[i]); + } else if (attributeTypes[i].getBinding().equals(String.class)) + { + pstmt.setString(i + 1, (String) attributes[i]); + } else if (attributeTypes[i].getBinding().equals(Float.class)) + { + pstmt.setFloat(i + 1, (Float) attributes[i]); + } else if (attributeTypes[i].getBinding().equals(Double.class)) + { + pstmt.setDouble(i + 1, (Double) attributes[i]); + } else if (attributeTypes[i].getBinding().equals(Boolean.class)) + { + pstmt.setBoolean(i + 1, (Boolean) attributes[i]); + } else if (attributeTypes[i].getBinding().equals(BigDecimal.class)) + { + pstmt.setBigDecimal(i + 1, (BigDecimal) attributes[i]); + } else if (attributeTypes[i].getBinding().equals(java.sql.Date.class)) + { + pstmt.setDate(i + 1, (java.sql.Date) attributes[i]); + } else if (attributeTypes[i].getBinding().equals(java.sql.Time.class)) + { + pstmt.setTime(i + 1, (java.sql.Time) attributes[i]); + } else if (attributeTypes[i].getBinding().equals(java.sql.Timestamp.class)) + { + pstmt.setTimestamp(i + 1, (java.sql.Timestamp) attributes[i]); + } else if (attributeTypes[i].getBinding().equals(java.util.Date.class)) + { + java.sql.Date sDate = new java.sql.Date(((java.util.Date) attributes[i]).getTime()); + pstmt.setDate(i + 1, sDate); + } + } + + } + } } diff --git a/xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/context/postgis/GeneralDgnConvertPostGISJobContext.java b/xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/context/postgis/GeneralDgnConvertPostGISJobContext.java index da212f0..9ae4316 100644 --- a/xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/context/postgis/GeneralDgnConvertPostGISJobContext.java +++ b/xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/context/postgis/GeneralDgnConvertPostGISJobContext.java @@ -136,13 +136,10 @@ { updateDataStore(); } - - txFeaturesContext.clear(); } public void rollbackTransaction() { - txFeaturesContext.clear(); } private void updateDataStore() @@ -156,7 +153,7 @@ FeatureType featureType = it.next(); logger.debug("Begin Save into PostGIS:" + featureType.getTypeName()); - String bindingStmt = makePrepareInsertSql(featureType, -1); + String bindingStmt = makePrepareInsertSql(featureType); ArrayList<Feature> features = txFeaturesContext.get(featureType); Connection conn = getConnection(); boolean autoCommit = conn.getAutoCommit(); @@ -180,10 +177,6 @@ } logger.error(e.getServerErrorMessage()); logger.error(e.getMessage(), e); - /* - } finally { - stmt.close(); - */ } /* @@ -197,6 +190,7 @@ i++; */ } + pstmt.close(); features.clear(); conn.setAutoCommit(autoCommit); @@ -211,6 +205,7 @@ public void closeFeatureWriter() throws IOException { + txFeaturesContext.clear(); /* for (FeatureWriter featureWriter : this.featuresWriterContext.values()) { diff --git a/xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/context/postgis/IndexDgnConvertPostGISJobContext.java b/xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/context/postgis/IndexDgnConvertPostGISJobContext.java index 123221a..8ac2226 100644 --- a/xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/context/postgis/IndexDgnConvertPostGISJobContext.java +++ b/xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/context/postgis/IndexDgnConvertPostGISJobContext.java @@ -144,7 +144,7 @@ FeatureType featureType = it.next(); logger.debug("Begin Save PostGIS:" + featureType.getTypeName()); - String bindingStmt = makePrepareInsertSql(featureType, -1); + String bindingStmt = makePrepareInsertSql(featureType); ArrayList<Feature> features = txFeaturesContext.get(featureType); Connection conn = getConnection(); boolean autoCommit = conn.getAutoCommit(); diff --git a/xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/context/postgis/OracleConvertPostGISJobContext.java b/xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/context/postgis/OracleConvertPostGISJobContext.java index 8240963..c9a676e 100644 --- a/xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/context/postgis/OracleConvertPostGISJobContext.java +++ b/xdgnjobs/ximple-spatialjob/src/main/java/com/ximple/eofms/jobs/context/postgis/OracleConvertPostGISJobContext.java @@ -9,6 +9,7 @@ import java.sql.SQLException; import java.sql.Connection; import java.sql.Statement; +import java.sql.PreparedStatement; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; @@ -70,17 +71,14 @@ private ElementDispatcher elementDispatcher; - // private HashMap featuresContext = new HashMap(); - // private HashMap<String, FeatureWriter> featuresWriterContext = new HashMap<String, FeatureWriter>(); - // private PessimisticMapWrapper txFeaturesContext; - private HashMap<FeatureType, ArrayList<String>> txFeaturesContext = new HashMap<FeatureType, ArrayList<String>>(); + private HashMap<FeatureType, ArrayList<Feature>> txFeaturesContext = new HashMap<FeatureType, ArrayList<Feature>>(); private JobExecutionContext executionContext; private String currentSchema = null; - private String pgCurrentSchema = null; private boolean schemaChanged = false; private boolean dropTableMode = true; + private int accumulate = 0; public OracleConvertPostGISJobContext(String dataPath, DataStore pgDS, String filterConfig) { @@ -165,10 +163,15 @@ if (!txFeaturesContext.containsKey(feature.getFeatureType())) { - txFeaturesContext.put(feature.getFeatureType(), new ArrayList<String>()); + txFeaturesContext.put(feature.getFeatureType(), new ArrayList<Feature>()); } - ArrayList<String> arrayList = txFeaturesContext.get(feature.getFeatureType()); - arrayList.add(makeInsertSql(feature, -1)); + ArrayList<Feature> arrayList = txFeaturesContext.get(feature.getFeatureType()); + arrayList.add(feature); + accumulate++; + if (accumulate > BATCHSIZE) + { + commitTransaction(); + } } public void startTransaction() @@ -197,10 +200,6 @@ public void rollbackTransaction() { - if (!txFeaturesContext.isEmpty()) - { - updateDataStore(); - } } public void resetFeatureContext() @@ -219,40 +218,41 @@ FeatureType featureType = it.next(); logger.debug("Begin Save into PostGIS:" + featureType.getTypeName()); - ArrayList<String> stmtTexts = txFeaturesContext.get(featureType); + String bindingStmt = makePrepareInsertSql(featureType); + ArrayList<Feature> features = txFeaturesContext.get(featureType); Connection conn = getConnection(); boolean autoCommit = conn.getAutoCommit(); conn.setAutoCommit(true); + PreparedStatement pstmt = conn.prepareStatement(bindingStmt); - for (String stmtText : stmtTexts) + for (Feature feature : features) { - currentStmt = stmtText; - Statement stmt = conn.createStatement(); try { - stmt.execute(stmtText); + // stmt.execute(feature); + bindFeatureParameters(pstmt, feature); + pstmt.execute(); } catch (PSQLException e) { - if (currentStmt != null) + if (bindingStmt != null) { - logger.error("Execute:" + currentStmt); + logger.error("Execute:" + bindingStmt); } logger.error(e.getServerErrorMessage()); logger.error(e.getMessage(), e); - } finally { - stmt.close(); } } - stmtTexts.clear(); + pstmt.close(); + features.clear(); conn.setAutoCommit(autoCommit); logger.debug("End Save into PostGIS:" + featureType.getTypeName()); } + accumulate = 0; } catch (SQLException e) { logger.error(e.getMessage(), e); } - } public JobExecutionContext getExecutionContext() @@ -335,7 +335,7 @@ featureType.getDefaultGeometry().getLocalName()); dropTable(conn, featureName); - ArrayList<String> schemaTexts = createSchemaTexts(featureType); + ArrayList<String> schemaTexts = createNewSchemaTexts(featureType); for (String stmtText : schemaTexts) { Statement stmt = conn.createStatement(); @@ -358,7 +358,7 @@ try { Connection conn = targetDataStore.getConnection(Transaction.AUTO_COMMIT); - ArrayList<String> schemaTexts = createSchemaTexts(featureType); + ArrayList<String> schemaTexts = createNewSchemaTexts(featureType); for (String stmtText : schemaTexts) { Statement stmt = conn.createStatement(); -- Gitblit v0.0.0-SNAPSHOT