001: /*
002: * Geotools2 - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-2006, Geotools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: */
017: package org.geotools.arcsde.data;
018:
019: import java.util.HashMap;
020: import java.util.Map;
021: import java.util.logging.Logger;
022:
023: import org.geotools.arcsde.pool.ArcSDEPooledConnection;
024: import org.geotools.arcsde.pool.UnavailableArcSDEConnectionException;
025: import org.geotools.data.DataSourceException;
026: import org.geotools.referencing.crs.DefaultGeographicCRS;
027: import org.opengis.referencing.crs.CoordinateReferenceSystem;
028:
029: import com.esri.sde.sdk.client.SDEPoint;
030: import com.esri.sde.sdk.client.SeColumnDefinition;
031: import com.esri.sde.sdk.client.SeCoordinateReference;
032: import com.esri.sde.sdk.client.SeException;
033: import com.esri.sde.sdk.client.SeInsert;
034: import com.esri.sde.sdk.client.SeLayer;
035: import com.esri.sde.sdk.client.SeRow;
036: import com.esri.sde.sdk.client.SeShape;
037: import com.esri.sde.sdk.client.SeTable;
038:
039: /**
040: * Data setup and utilities for testing the support of in-process views
041: *
042: * @author Gabriel Roldan, Axios Engineering
043: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/unsupported/arcsde/datastore/src/test/java/org/geotools/arcsde/data/InProcessViewSupportTestData.java $
044: * @version $Id: InProcessViewSupportTestData.java 29135 2008-02-07 19:49:09Z desruisseaux $
045: * @since 2.4.x
046: */
047: public class InProcessViewSupportTestData {
048:
049: private static final Logger LOGGER = org.geotools.util.logging.Logging
050: .getLogger(InProcessViewSupportTestData.class.getPackage()
051: .getName());
052:
053: public static final String MASTER_UNQUALIFIED = "GT_SDE_TEST_MASTER";
054:
055: public static final String CHILD_UNQUALIFIED = "GT_SDE_TEST_CHILD";
056:
057: public static String MASTER;
058:
059: public static String CHILD;
060:
061: public static String masterChildSql;
062:
063: /**
064: * Extra datastore creation parameters to set up {@link #typeName} as a
065: * FeatureType defined by {@link #masterChildSql}
066: */
067: public static Map registerViewParams;
068:
069: public static final String typeName = "MasterChildTest";
070:
071: public static void setUp(ArcSDEPooledConnection conn)
072: throws SeException, DataSourceException,
073: UnavailableArcSDEConnectionException {
074:
075: testCrs = DefaultGeographicCRS.WGS84;
076:
077: /**
078: * Remember, shape field has to be the last one
079: */
080: masterChildSql = "SELECT " + MASTER_UNQUALIFIED + ".ID, "
081: + MASTER_UNQUALIFIED + ".NAME, " + CHILD_UNQUALIFIED
082: + ".DESCRIPTION, " + MASTER_UNQUALIFIED + ".SHAPE "
083: + "FROM " + MASTER_UNQUALIFIED + ", "
084: + CHILD_UNQUALIFIED + " WHERE " + CHILD_UNQUALIFIED
085: + ".MASTER_ID = " + MASTER_UNQUALIFIED
086: + ".ID ORDER BY " + MASTER_UNQUALIFIED + ".ID";
087:
088: MASTER = conn.getDatabaseName() + "." + conn.getUser() + "."
089: + MASTER_UNQUALIFIED;
090: CHILD = conn.getDatabaseName() + "." + conn.getUser() + "."
091: + CHILD_UNQUALIFIED;
092: createMasterTable(conn);
093: createChildTable(conn);
094:
095: registerViewParams = new HashMap();
096: registerViewParams.put("sqlView.1.typeName", typeName);
097: registerViewParams.put("sqlView.1.sqlQuery", masterChildSql);
098: }
099:
100: private static void createMasterTable(ArcSDEPooledConnection conn)
101: throws SeException, DataSourceException,
102: UnavailableArcSDEConnectionException {
103: SeTable table = new SeTable(conn, MASTER);
104: SeLayer layer = null;
105: try {
106:
107: try {
108: table.delete();
109: } catch (SeException e) {
110: // no-op, table didn't existed
111: }
112:
113: SeColumnDefinition[] colDefs = new SeColumnDefinition[2];
114:
115: layer = new SeLayer(conn);
116: layer.setTableName(MASTER);
117:
118: colDefs[0] = new SeColumnDefinition("ID",
119: SeColumnDefinition.TYPE_INT32, 10, 0, false);
120: colDefs[1] = new SeColumnDefinition("NAME",
121: SeColumnDefinition.TYPE_STRING, 255, 0, false);
122:
123: table.create(colDefs, "DEFAULTS");
124:
125: layer.setSpatialColumnName("SHAPE");
126: layer.setShapeTypes(SeLayer.SE_POINT_TYPE_MASK);
127: layer.setGridSizes(1100.0, 0.0, 0.0);
128: layer
129: .setDescription("Geotools sde pluing join support testing master table");
130: SeCoordinateReference coordref = new SeCoordinateReference();
131: coordref.setCoordSysByDescription(testCrs.toWKT());
132: layer.create(3, 4);
133:
134: insertMasterData(conn, layer);
135: } finally {
136: conn.close();
137: }
138: LOGGER.info("successfully created master table "
139: + layer.getQualifiedName());
140: }
141:
142: private static void createChildTable(ArcSDEPooledConnection conn)
143: throws DataSourceException,
144: UnavailableArcSDEConnectionException, SeException {
145: SeTable table = new SeTable(conn, CHILD);
146: try {
147: try {
148: table.delete();
149: } catch (SeException e) {
150: // no-op, table didn't existed
151: }
152:
153: SeColumnDefinition[] colDefs = new SeColumnDefinition[4];
154:
155: colDefs[0] = new SeColumnDefinition("ID",
156: SeColumnDefinition.TYPE_INTEGER, 10, 0, false);
157: colDefs[1] = new SeColumnDefinition("MASTER_ID",
158: SeColumnDefinition.TYPE_INTEGER, 10, 0, false);
159: colDefs[2] = new SeColumnDefinition("NAME",
160: SeColumnDefinition.TYPE_STRING, 255, 0, false);
161: colDefs[3] = new SeColumnDefinition("DESCRIPTION",
162: SeColumnDefinition.TYPE_STRING, 255, 0, false);
163:
164: table.create(colDefs, "DEFAULTS");
165:
166: /*
167: * SeRegistration tableRegistration = new SeRegistration(conn,
168: * CHILD);
169: * tableRegistration.setRowIdColumnType(SeRegistration.SE_REGISTRATION_ROW_ID_COLUMN_TYPE_USER);
170: * tableRegistration.setRowIdColumnName("ID");
171: * tableRegistration.alter();
172: */
173: insertChildData(conn, table);
174: } finally {
175: conn.close();
176: }
177:
178: LOGGER.info("successfully created child table " + CHILD);
179: }
180:
181: /**
182: * <pre>
183: * <code>
184: * -----------------------------------------------
185: * | GT_SDE_TEST_MASTER |
186: * -----------------------------------------------
187: * | ID(int) | NAME (string) | SHAPE (Point) |
188: * -----------------------------------------------
189: * | 1 | name1 | POINT(1, 1) |
190: * -----------------------------------------------
191: * | 2 | name2 | POINT(2, 2) |
192: * -----------------------------------------------
193: * | 3 | name3 | POINT(3, 3) |
194: * -----------------------------------------------
195: * </code>
196: * </pre>
197: *
198: * @param conn
199: * @throws SeException
200: * @throws Exception
201: */
202: private static void insertMasterData(ArcSDEPooledConnection conn,
203: SeLayer layer) throws SeException {
204: SeInsert insert = null;
205:
206: SeCoordinateReference coordref = layer.getCoordRef();
207: final String[] columns = { "ID", "NAME", "SHAPE" };
208:
209: for (int i = 1; i < 4; i++) {
210: insert = new SeInsert(conn);
211: insert.intoTable(layer.getName(), columns);
212: insert.setWriteMode(true);
213:
214: SeRow row = insert.getRowToSet();
215: SeShape shape = new SeShape(coordref);
216: SDEPoint[] points = { new SDEPoint(i, i) };
217: shape.generatePoint(1, points);
218:
219: row.setInteger(0, new Integer(i));
220: row.setString(1, "name" + i);
221: row.setShape(2, shape);
222: insert.execute();
223: }
224: conn.commitTransaction();
225: }
226:
227: /**
228: * <pre>
229: * <code>
230: * ---------------------------------------------------------------------
231: * | GT_SDE_TEST_CHILD |
232: * ---------------------------------------------------------------------
233: * | ID(int) | MASTER_ID | NAME (string) | DESCRIPTION(string |
234: * ---------------------------------------------------------------------
235: * | 1 | 1 | child1 | description1 |
236: * ---------------------------------------------------------------------
237: * | 2 | 2 | child2 | description2 |
238: * ---------------------------------------------------------------------
239: * | 3 | 2 | child3 | description3 |
240: * ---------------------------------------------------------------------
241: * | 4 | 3 | child4 | description4 |
242: * ---------------------------------------------------------------------
243: * | 5 | 3 | child5 | description5 |
244: * ---------------------------------------------------------------------
245: * | 6 | 3 | child6 | description6 |
246: * ---------------------------------------------------------------------
247: * | 7 | 3 | child6 | description7 |
248: * ---------------------------------------------------------------------
249: * </code>
250: * </pre>
251: *
252: * Note last row has the same name than child6, for testing group by.
253: *
254: * @param conn
255: * @param table
256: * @throws SeException
257: * @throws Exception
258: */
259: private static void insertChildData(ArcSDEPooledConnection conn,
260: SeTable table) throws SeException {
261: SeInsert insert = null;
262:
263: final String[] columns = { "ID", "MASTER_ID", "NAME",
264: "DESCRIPTION" };
265:
266: int childId = 0;
267:
268: for (int master = 1; master < 4; master++) {
269: for (int child = 0; child < master; child++) {
270: childId++;
271:
272: insert = new SeInsert(conn);
273: insert.intoTable(table.getName(), columns);
274: insert.setWriteMode(true);
275:
276: SeRow row = insert.getRowToSet();
277:
278: row.setInteger(0, new Integer(childId));
279: row.setInteger(1, new Integer(master));
280: row.setString(2, "child" + (childId));
281: row.setString(3, "description" + (childId));
282: insert.execute();
283: }
284: }
285: // add the 7th row to test group by
286: SeRow row = insert.getRowToSet();
287:
288: row.setInteger(0, new Integer(7));
289: row.setInteger(1, new Integer(3));
290: row.setString(2, "child6");
291: row.setString(3, "description7");
292: insert.execute();
293:
294: conn.commitTransaction();
295: }
296:
297: public static CoordinateReferenceSystem testCrs;
298:
299: }
|