001: /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
002: * This code is licensed under the GPL 2.0 license, availible at the root
003: * application directory.
004: */
005: package org.vfny.geoserver.action.data;
006:
007: import org.apache.struts.action.ActionError;
008: import org.apache.struts.action.ActionErrors;
009: import org.apache.struts.action.ActionForm;
010: import org.apache.struts.action.ActionForward;
011: import org.apache.struts.action.ActionMapping;
012: import org.geotools.data.DataStore;
013: import org.geotools.data.FeatureSource;
014: import org.geotools.feature.FeatureType;
015: import org.opengis.metadata.Identifier;
016: import org.opengis.referencing.crs.CoordinateReferenceSystem;
017: import org.vfny.geoserver.action.ConfigAction;
018: import org.vfny.geoserver.config.DataConfig;
019: import org.vfny.geoserver.config.DataStoreConfig;
020: import org.vfny.geoserver.config.FeatureTypeConfig;
021: import org.vfny.geoserver.form.data.DataFeatureTypesNewForm;
022: import org.vfny.geoserver.global.UserContainer;
023: import java.io.FileNotFoundException;
024: import java.io.IOException;
025: import java.util.Iterator;
026: import java.util.Set;
027: import javax.servlet.http.HttpServletRequest;
028: import javax.servlet.http.HttpServletResponse;
029:
030: /**
031: * DataFeatureTypesNewAction purpose.
032: *
033: * <p>
034: * Description of DataFeatureTypesNewAction ...
035: * </p>
036: *
037: * <p>
038: * Capabilities:
039: * </p>
040: *
041: * <ul>
042: * <li>
043: * Feature: description
044: * </li>
045: * </ul>
046: *
047: * <p>
048: * Example Use:
049: * </p>
050: * <pre><code>
051: * DataFeatureTypesNewAction x = new DataFeatureTypesNewAction(...);
052: * </code></pre>
053: *
054: * @author rgould, Refractions Research, Inc.
055: * @author $Author: cholmesny $ (last modification)
056: * @version $Id: DataFeatureTypesNewAction.java 7693 2007-11-01 09:31:11Z aaime $
057: */
058: public class DataFeatureTypesNewAction extends ConfigAction {
059: public final static String NEW_FEATURE_TYPE_KEY = "newFeatureType";
060:
061: public ActionForward execute(ActionMapping mapping,
062: ActionForm incomingForm, UserContainer user,
063: HttpServletRequest request, HttpServletResponse response)
064: throws IOException {
065: DataFeatureTypesNewForm form = (DataFeatureTypesNewForm) incomingForm;
066: String selectedNewFeatureType = form
067: .getSelectedNewFeatureType();
068:
069: DataConfig dataConfig = (DataConfig) request.getSession()
070: .getServletContext()
071: .getAttribute(DataConfig.CONFIG_KEY);
072: int index = selectedNewFeatureType
073: .indexOf(DataConfig.SEPARATOR);
074: String dataStoreID = selectedNewFeatureType.substring(0, index);
075: String featureTypeName = selectedNewFeatureType.substring(index
076: + DataConfig.SEPARATOR.length());
077:
078: DataStoreConfig dsConfig = dataConfig.getDataStore(dataStoreID);
079: DataStore dataStore = null;
080:
081: FeatureTypeConfig ftConfig;
082:
083: //JD: GEOS-399, wrap rest of method in try catch block in order to
084: // report back nicely to app
085: try {
086: dataStore = dsConfig.findDataStore(request.getSession()
087: .getServletContext());
088: FeatureType featureType = dataStore
089: .getSchema(featureTypeName);
090:
091: ftConfig = new FeatureTypeConfig(dataStoreID, featureType,
092: false);
093:
094: // DJB: this comment looks old - SRS support is much better now.
095: // TODO: delete this comment (but wait a bit)
096: // What is the Spatial Reference System for this FeatureType?
097: //
098: // getDefaultGeometry().getCoordinateSystem() should help but is null
099: // getDefaultGeometry().getGeometryFactory() could help, with getSRID(), but it is null
100: //
101: // So we will use 0 which means Cartisian Coordinates aka don't know
102: //
103: // Only other thing we could do is ask for a geometry and see what it's
104: // SRID number is?
105: //
106: // ftConfig.setSRS(0);
107:
108: // attempt to get a better SRS
109: try {
110: CoordinateReferenceSystem crs = featureType
111: .getDefaultGeometry().getCoordinateSystem();
112:
113: if (crs != null) {
114: Set idents = crs.getIdentifiers();
115: Iterator it = idents.iterator();
116:
117: while (it.hasNext()) {
118: Identifier id = (Identifier) it.next();
119:
120: if (id.toString().indexOf("EPSG:") != -1) // this should probably use the Citation, but this is easier!
121: {
122: //we have an EPSG #, so lets use it!
123: String str_num = id.toString().substring(
124: id.toString().indexOf(':') + 1);
125: int num = Integer.parseInt(str_num);
126: ftConfig.setSRS(num);
127:
128: break; // take the first EPSG
129: }
130: }
131: }
132: } catch (Exception e) {
133: e.printStackTrace(); // not a big deal - we'll default to 0.
134: }
135:
136: FeatureSource fs = dataStore.getFeatureSource(featureType
137: .getTypeName());
138:
139: // TODO translate to lat long, pending
140: //This should not be done by default, as it is an expensive operation.
141: //especially for very large tables. User may know it, if not he
142: //can hit the generate button (which is why it's there).
143: //ftConfig.setLatLongBBox(DataStoreUtils.getBoundingBoxEnvelope(fs));
144:
145: //Extent ex = featureType.getDefaultGeometry().getCoordinateSystem().getValidArea();
146: //ftConfig.setLatLongBBox(ex);
147: } catch (IOException e) {
148: e.printStackTrace();
149:
150: if (e instanceof FileNotFoundException) {
151: ActionErrors errors = new ActionErrors();
152: errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(
153: "error.exception", "File not found: "
154: + e.getMessage()));
155: saveErrors(request, errors);
156:
157: return mapping.findForward("config.data.type.new");
158: }
159:
160: ActionErrors errors = new ActionErrors();
161: errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(
162: "error.exception", e.getMessage()));
163:
164: saveErrors(request, errors);
165:
166: return mapping.findForward("config.data.type.new");
167: } finally {
168: if (dataStore != null)
169: dataStore.dispose();
170: }
171:
172: request.getSession().setAttribute(
173: DataConfig.SELECTED_FEATURE_TYPE, ftConfig);
174: request.getSession().removeAttribute(
175: DataConfig.SELECTED_ATTRIBUTE_TYPE);
176:
177: user.setFeatureTypeConfig(ftConfig);
178:
179: return mapping.findForward("config.data.type.editor");
180: }
181: }
|