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.DataStoreFactorySpi;
014: import org.geotools.data.DataStoreFactorySpi.Param;
015: import org.geotools.feature.FeatureType;
016: import org.vfny.geoserver.action.ConfigAction;
017: import org.vfny.geoserver.config.DataConfig;
018: import org.vfny.geoserver.config.DataStoreConfig;
019: import org.vfny.geoserver.config.FeatureTypeConfig;
020: import org.vfny.geoserver.form.data.DataDataStoresEditorForm;
021: import org.vfny.geoserver.global.UserContainer;
022: import org.vfny.geoserver.util.DataStoreUtils;
023: import java.io.IOException;
024: import java.util.HashMap;
025: import java.util.Iterator;
026: import java.util.Map;
027: import java.util.logging.Level;
028: import javax.servlet.ServletContext;
029: import javax.servlet.ServletException;
030: import javax.servlet.http.HttpServletRequest;
031: import javax.servlet.http.HttpServletResponse;
032:
033: /**
034: * DOCUMENT ME!
035: *
036: * @author rgould To change the template for this generated type comment go to
037: * Window>Preferences>Java>Code Generation>Code and Comments
038: */
039: public class DataDataStoresEditorAction extends ConfigAction {
040: public ActionForward execute(ActionMapping mapping,
041: ActionForm form, UserContainer user,
042: HttpServletRequest request, HttpServletResponse response)
043: throws IOException, ServletException {
044: DataDataStoresEditorForm dataStoresForm = (DataDataStoresEditorForm) form;
045:
046: String dataStoreID = dataStoresForm.getDataStoreId();
047: String namespace = dataStoresForm.getNamespaceId();
048: String description = dataStoresForm.getDescription();
049:
050: DataConfig dataConfig = (DataConfig) getDataConfig();
051: DataStoreConfig config = null;
052:
053: config = (DataStoreConfig) dataConfig.getDataStore(dataStoreID);
054:
055: boolean isNewDataStore = false;
056:
057: if (config == null) {
058: // we are creating a new one.
059: dataConfig.addDataStore(getUserContainer(request)
060: .getDataStoreConfig());
061: config = (DataStoreConfig) dataConfig
062: .getDataStore(dataStoreID);
063: isNewDataStore = true;
064: }
065:
066: // After extracting params into a map
067: Map connectionParams = new HashMap(); // values used for connection
068: Map paramTexts = new HashMap(); // values as stored
069:
070: Map params = dataStoresForm.getParams();
071:
072: DataStoreFactorySpi factory = config.getFactory();
073: Param[] info = factory.getParametersInfo();
074:
075: // Convert Params into the kind of Map we actually need
076: //
077: for (Iterator i = params.keySet().iterator(); i.hasNext();) {
078: String key = (String) i.next();
079:
080: Param param = DataStoreUtils.find(info, key);
081:
082: if (param == null) {
083: ActionErrors errors = new ActionErrors();
084: errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(
085: "error.cannotProcessConnectionParams"));
086: saveErrors(request, errors);
087:
088: return mapping.findForward("config.data.store.editor");
089: }
090:
091: Object value;
092:
093: try {
094: value = param.lookUp(params);
095: } catch (IOException erp) {
096: ActionErrors errors = new ActionErrors();
097: errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(
098: "error.cannotProcessConnectionParams"));
099: saveErrors(request, errors);
100:
101: return mapping.findForward("config.data.store.editor");
102: }
103:
104: if ((value != null) && !"".equals(value)) {
105: connectionParams.put(key, value);
106:
107: String text = param.text(value);
108: paramTexts.put(key, text);
109: }
110: }
111:
112: // put magic namespace into the mix
113: // not sure if we want to do this, as we want the full namespace, not
114: //the id. But getParams in DataStore may override this - ch
115: connectionParams.put("namespace", dataStoresForm
116: .getNamespaceId());
117: paramTexts.put("namespace", dataStoresForm.getNamespaceId());
118:
119: //dump("editor", connectionParams );
120: //dump("texts ",paramTexts );
121: if (!factory.canProcess(connectionParams)) {
122: // We could not use these params!
123: //
124: ActionErrors errors = new ActionErrors();
125: errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(
126: "error.cannotProcessConnectionParams"));
127: saveErrors(request, errors);
128:
129: return mapping.findForward("config.data.store.editor");
130: }
131:
132: FeatureType singleFeatureType = null;
133:
134: DataStore victim = null;
135: try {
136: ServletContext sc = request.getSession()
137: .getServletContext();
138: victim = DataStoreUtils.acquireDataStore(paramTexts, sc);
139:
140: if (victim == null) {
141: // We *really* could not use these params!
142: //
143: ActionErrors errors = new ActionErrors();
144: errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(
145: "error.invalidConnectionParams"));
146: saveErrors(request, errors);
147:
148: return mapping.findForward("config.data.store.editor");
149: }
150:
151: String[] typeNames = victim.getTypeNames();
152:
153: //If there's only one featureType in the datastore, then we
154: //want to be nice to users and pass them directly to the editor,
155: //so we need to get the featureType here.
156: if (typeNames.length == 1) {
157: singleFeatureType = victim.getSchema(typeNames[0]);
158: }
159:
160: dump("typeNames", typeNames);
161: } catch (Throwable throwable) {
162: LOGGER
163: .log(
164: Level.WARNING,
165: "Unable to fetch a list of FeatureType names from datastore.",
166: throwable);
167:
168: ActionErrors errors = new ActionErrors();
169: errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(
170: "error.exception", throwable.getMessage()));
171:
172: saveErrors(request, errors);
173:
174: return mapping.findForward("config.data.store.editor");
175: } finally {
176: if (victim != null)
177: victim.dispose();
178: }
179:
180: boolean enabled = dataStoresForm.isEnabled();
181:
182: if (dataStoresForm.isEnabledChecked() == false) {
183: enabled = false;
184: }
185:
186: config.setEnabled(enabled);
187: config.setNameSpaceId(namespace);
188: config.setAbstract(description);
189: config.setConnectionParams(paramTexts);
190:
191: dataConfig.addDataStore(config);
192:
193: getUserContainer(request).setDataStoreConfig(null);
194: getApplicationState().notifyConfigChanged();
195:
196: if ((singleFeatureType == null) || !isNewDataStore) {
197: //If there are many featureTypes, then just forward to the normal
198: //spot.
199: return mapping.findForward("config.data.store");
200: } else {
201: //We only have one featureType, and this is the creation of a new datastore
202: //so we should forward to the editor of this featureType, since this is what
203: //users will be next in the vast majority of the cases.
204: FeatureTypeConfig ftConfig = new FeatureTypeConfig(
205: dataStoreID, singleFeatureType, false);
206:
207: request.getSession().setAttribute(
208: DataConfig.SELECTED_FEATURE_TYPE, ftConfig);
209: request.getSession().removeAttribute(
210: DataConfig.SELECTED_ATTRIBUTE_TYPE);
211:
212: user.setFeatureTypeConfig(ftConfig);
213:
214: return mapping.findForward("config.data.type.editor");
215: }
216: }
217:
218: /** Used to debug connection parameters */
219: public void dump(String msg, Map params) {
220: if (msg != null) {
221: System.out.print(msg + " ");
222: }
223:
224: System.out.print(": { ");
225:
226: for (Iterator i = params.entrySet().iterator(); i.hasNext();) {
227: Map.Entry entry = (Map.Entry) i.next();
228: System.out.print(entry.getKey());
229: System.out.print("=");
230: dump(entry.getValue());
231:
232: if (i.hasNext()) {
233: System.out.print(", ");
234: }
235: }
236:
237: System.out.println("}");
238: }
239:
240: public void dump(Object obj) {
241: if (obj == null) {
242: System.out.print("null");
243: } else if (obj instanceof String) {
244: System.out.print("\"");
245: System.out.print(obj);
246: System.out.print("\"");
247: } else {
248: System.out.print(obj);
249: }
250: }
251:
252: public void dump(String msg, Object[] array) {
253: if (msg != null) {
254: System.out.print(msg + " ");
255: }
256:
257: System.out.print(": ");
258:
259: if (array == null) {
260: System.out.print("null");
261:
262: return;
263: }
264:
265: System.out.print("(");
266:
267: for (int i = 0; i < array.length; i++) {
268: dump(array[i]);
269:
270: if (i < (array.length - 1)) {
271: System.out.print(", ");
272: }
273: }
274:
275: System.out.println(")");
276: }
277: }
|