001: /*
002: * Created on Feb 16, 2004
003: *
004: * To change the template for this generated file go to
005: * Window - Preferences - Java - Code Generation - Code and Comments
006: */
007: package org.vfny.geoserver.action.data;
008:
009: import org.apache.struts.Globals;
010: import org.apache.struts.action.ActionError;
011: import org.apache.struts.action.ActionErrors;
012: import org.apache.struts.action.ActionForm;
013: import org.apache.struts.action.ActionForward;
014: import org.apache.struts.action.ActionMapping;
015: import org.apache.struts.util.MessageResources;
016: import org.vfny.geoserver.action.ConfigAction;
017: import org.vfny.geoserver.action.HTMLEncoder;
018: import org.vfny.geoserver.config.DataConfig;
019: import org.vfny.geoserver.config.DataStoreConfig;
020: import org.vfny.geoserver.config.NameSpaceConfig;
021: import org.vfny.geoserver.form.data.DataNamespacesSelectForm;
022: import org.vfny.geoserver.global.UserContainer;
023: import java.io.IOException;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.Locale;
027: import javax.servlet.ServletException;
028: import javax.servlet.http.HttpServletRequest;
029: import javax.servlet.http.HttpServletResponse;
030:
031: /**
032: * Select Namespaces for editing.
033: *
034: * @author rgould, Refractions Research, Inc.
035: * @author $Author: dmzwiers $ (last modification)
036: * @version $Id: DataNamespacesSelectAction.java 6177 2007-02-19 10:11:27Z aaime $
037: */
038: public class DataNamespacesSelectAction extends ConfigAction {
039: public ActionForward execute(ActionMapping mapping,
040: ActionForm form, UserContainer user,
041: HttpServletRequest request, HttpServletResponse response)
042: throws IOException, ServletException {
043: DataNamespacesSelectForm namespacesForm = (DataNamespacesSelectForm) form;
044:
045: String action = namespacesForm.getAction();
046:
047: DataConfig dataConfig = (DataConfig) getDataConfig();
048: NameSpaceConfig config = null;
049:
050: Locale locale = (Locale) request.getLocale();
051: MessageResources messages = getResources(request);
052:
053: String edit = HTMLEncoder.decode(messages.getMessage(locale,
054: "label.edit"));
055: String delete = HTMLEncoder.decode(messages.getMessage(locale,
056: "label.delete"));
057: String _default = HTMLEncoder.decode(messages.getMessage(
058: locale, "label.default"));
059:
060: String nsSelected = namespacesForm.getSelectedNamespace();
061:
062: if ((nsSelected == null) || nsSelected.equalsIgnoreCase("")) { // if it is empty (nothing selected)
063:
064: return mapping.findForward("config.data.namespace"); // return to same page and do nothing
065: }
066:
067: if (nsSelected.endsWith("*")) {
068: nsSelected = nsSelected.substring(0, nsSelected
069: .lastIndexOf("*"));
070: }
071:
072: config = (NameSpaceConfig) dataConfig.getNameSpace(nsSelected);
073:
074: if (config == null) {
075: throw new NullPointerException();
076: }
077:
078: getUserContainer(request).setNamespaceConfig(config);
079:
080: if (action.equals(delete)) {
081: if (dataStoresUseNamespace(dataConfig, nsSelected)) {
082: //dont delete a namespace thats in use!
083: ActionErrors errors = new ActionErrors();
084: errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(
085: "error.namespace.namespaceInUse"));
086: request.setAttribute(Globals.ERROR_KEY, errors);
087:
088: return mapping.findForward("config.data.namespace");
089: }
090:
091: dataConfig.removeNameSpace(nsSelected);
092: getApplicationState().notifyConfigChanged();
093:
094: getUserContainer(request).setNamespaceConfig(null);
095: namespacesForm.reset(mapping, request);
096:
097: return mapping.findForward("config.data.namespace");
098: }
099:
100: if (action.equals(_default)) {
101: if (!nsSelected.equals(dataConfig.getDefaultNameSpace()
102: .getPrefix())) {
103: dataConfig.setDefaultNameSpace(dataConfig
104: .getNameSpace(nsSelected));
105: getApplicationState().notifyConfigChanged();
106: }
107:
108: getUserContainer(request).setNamespaceConfig(null);
109: namespacesForm.reset(mapping, request);
110:
111: return mapping.findForward("config.data.namespace");
112: }
113:
114: if (action.equals(edit)) {
115: getUserContainer(request).setNamespaceConfig(config);
116:
117: return mapping.findForward("config.data.namespace.editor");
118: }
119:
120: ActionErrors errors = new ActionErrors();
121: errors.add("submit", new ActionError("error.action.invalid",
122: action));
123: request.setAttribute(Globals.ERROR_KEY, errors);
124:
125: return mapping.findForward("config.data.style");
126: }
127:
128: /**
129: * return true if the namespace is being used by a datastore.
130: * You dont want to delete a namespace thats actually being used.
131: *
132: * @param dataConfig
133: * @param nsSelected
134: * @return
135: */
136: private boolean dataStoresUseNamespace(DataConfig dataConfig,
137: String nsSelected) {
138: List stores = dataConfig.getDataStoreIds();
139:
140: Iterator it = stores.iterator();
141:
142: while (it.hasNext()) {
143: DataStoreConfig dsc = dataConfig.getDataStore((String) it
144: .next());
145:
146: if (dsc.getNameSpaceId().equals(nsSelected)) {
147: return true;
148: }
149: }
150:
151: return false;
152: }
153: }
|