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.Globals;
008: import org.apache.struts.action.ActionError;
009: import org.apache.struts.action.ActionErrors;
010: import org.apache.struts.action.ActionForm;
011: import org.apache.struts.action.ActionForward;
012: import org.apache.struts.action.ActionMapping;
013: import org.apache.struts.util.MessageResources;
014: import org.vfny.geoserver.action.ConfigAction;
015: import org.vfny.geoserver.action.HTMLEncoder;
016: import org.vfny.geoserver.config.CoverageConfig;
017: import org.vfny.geoserver.config.CoverageStoreConfig;
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.config.StyleConfig;
022: import org.vfny.geoserver.form.data.StylesSelectForm;
023: import org.vfny.geoserver.global.UserContainer;
024: import java.io.File;
025: import java.io.IOException;
026: import java.util.ArrayList;
027: import java.util.Iterator;
028: import java.util.List;
029: import java.util.Locale;
030: import java.util.Map;
031: import javax.servlet.ServletException;
032: import javax.servlet.http.HttpServletRequest;
033: import javax.servlet.http.HttpServletResponse;
034:
035: /**
036: * Edit selected style
037: *
038: * @author jgarnett, Refractions Research, Inc.
039: * @author $Author: dmzwiers $ (last modification)
040: * @version $Id: StylesSelectAction.java 8470 2008-02-27 10:51:54Z aaime $
041: */
042: public class StylesSelectAction extends ConfigAction {
043: public ActionForward execute(ActionMapping mapping,
044: ActionForm form, UserContainer user,
045: HttpServletRequest request, HttpServletResponse response)
046: throws IOException, ServletException {
047: final StylesSelectForm selectForm = (StylesSelectForm) form;
048: final String action = selectForm.getAction();
049: String styleId = selectForm.getSelectedStyle();
050:
051: if (styleId.endsWith("*")) {
052: styleId = styleId.substring(0, styleId.lastIndexOf("*"));
053: }
054:
055: Locale locale = (Locale) request.getLocale();
056:
057: DataConfig config = getDataConfig();
058: MessageResources messages = getResources(request);
059:
060: // Need locale wording for edit and delete
061: final String EDIT = HTMLEncoder.decode(messages.getMessage(
062: locale, "label.edit"));
063: final String DELETE = HTMLEncoder.decode(messages.getMessage(
064: locale, "label.delete"));
065: final String DEFAULT = HTMLEncoder.decode(messages.getMessage(
066: locale, "label.default"));
067:
068: StyleConfig style = config.getStyle(styleId);
069:
070: if (style == null) {
071: ActionErrors errors = new ActionErrors();
072: errors.add("selectedStyle", new ActionError(
073: "error.style.invalid", styleId));
074: request.setAttribute(Globals.ERROR_KEY, errors);
075:
076: return mapping.findForward("config.data.style");
077: }
078:
079: // Something is selected lets do the requested action
080: //
081: if (action.equals(DELETE)) {
082: // check if the style has any users
083: List userNames = getStyleUsers(config, styleId);
084:
085: if (userNames.size() > 0) {
086: doFileExistsError(styleId, userNames, request);
087:
088: return mapping.findForward("config.data.style");
089: }
090:
091: config.removeStyle(styleId);
092: getApplicationState().notifyConfigChanged();
093: selectForm.setSelectedStyle(null);
094: selectForm.reset(mapping, request);
095:
096: return mapping.findForward("config.data.style");
097: }
098:
099: if (action.equals(DEFAULT)) {
100: Map m = config.getStyles();
101: Iterator i = m.values().iterator();
102:
103: while (i.hasNext()) {
104: StyleConfig sc = (StyleConfig) i.next();
105:
106: if (sc.isDefault()) {
107: if ((sc.getId() != null)
108: && !sc.getId().equals(styleId)) {
109: sc.setDefault(false);
110: getApplicationState().notifyConfigChanged();
111: }
112: } else {
113: if ((sc.getId() != null)
114: && sc.getId().equals(styleId)) {
115: sc.setDefault(true);
116: getApplicationState().notifyConfigChanged();
117: }
118: }
119: }
120:
121: selectForm.setSelectedStyle(null);
122: selectForm.reset(mapping, request);
123:
124: return mapping.findForward("config.data.style");
125: }
126:
127: if (action.equals(EDIT)) {
128: user.setStyle(new StyleConfig(style));
129:
130: return mapping.findForward("config.data.style.editor");
131: }
132:
133: ActionErrors errors = new ActionErrors();
134: errors.add("submit", new ActionError("error.action.invalid",
135: action));
136: request.setAttribute(Globals.ERROR_KEY, errors);
137:
138: return mapping.findForward("config.data.style");
139: }
140:
141: /**
142: * Returns a list of strings, with the name of the layers using the style as the default one
143: * @param config
144: * @param styleId
145: * @return
146: */
147: private List getStyleUsers(DataConfig config, String styleId) {
148: List results = new ArrayList();
149:
150: for (Iterator it = config.getFeaturesTypes().values()
151: .iterator(); it.hasNext();) {
152: FeatureTypeConfig ft = (FeatureTypeConfig) it.next();
153:
154: if (styleId.equals(ft.getDefaultStyle())
155: || ft.getStyles().contains(styleId)) {
156: DataStoreConfig ds = config.getDataStore(ft
157: .getDataStoreId());
158:
159: // misconfigured data stores are not included int the map, ouff...
160: if (ds != null) {
161: results.add(ds.getNameSpaceId() + ":"
162: + ft.getName());
163: } else {
164: results.add(ft.getName());
165: }
166: }
167: }
168:
169: for (Iterator it = config.getCoverages().values().iterator(); it
170: .hasNext();) {
171: CoverageConfig cc = (CoverageConfig) it.next();
172:
173: if (styleId.equals(cc.getDefaultStyle())
174: || cc.getStyles().contains(styleId)) {
175: CoverageStoreConfig cs = config.getDataFormat(cc
176: .getFormatId());
177: results.add(cs.getNameSpaceId() + ":" + cc.getName());
178: }
179: }
180:
181: return results;
182: }
183:
184: /*
185: * reports an error for an attempt to upload an sld file that is already
186: * in the system.*/
187: private void doFileExistsError(String styleId, List styleUsers,
188: HttpServletRequest request) {
189: StringBuffer sb = new StringBuffer();
190:
191: for (Iterator it = styleUsers.iterator(); it.hasNext();) {
192: String user = (String) it.next();
193: sb.append(user);
194:
195: if (it.hasNext()) {
196: sb.append(", ");
197: }
198: }
199:
200: ActionErrors errors = new ActionErrors();
201: errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(
202: "error.style.sldInUse", new String[] { styleId,
203: sb.toString() }));
204: saveErrors(request, errors);
205: }
206: }
|