001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.layout.impl;
018:
019: import java.util.Collection;
020: import java.util.Iterator;
021: import java.util.List;
022: import java.util.Locale;
023: import java.util.Map;
024:
025: import org.apache.commons.logging.Log;
026: import org.apache.commons.logging.LogFactory;
027: import org.apache.jetspeed.ajax.AJAXException;
028: import org.apache.jetspeed.ajax.AjaxAction;
029: import org.apache.jetspeed.ajax.AjaxBuilder;
030: import org.apache.jetspeed.layout.PortletActionSecurityBehavior;
031: import org.apache.jetspeed.om.common.LocalizedField;
032: import org.apache.jetspeed.om.common.SecurityConstraints;
033: import org.apache.jetspeed.page.PageManager;
034: import org.apache.jetspeed.page.document.Node;
035: import org.apache.jetspeed.request.RequestContext;
036:
037: /**
038: * Abstract Site update action for folders, pages and links
039: *
040: * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
041: * @version $Id: $
042: */
043: public abstract class BaseSiteUpdateAction extends BasePortletAction
044: implements AjaxAction, AjaxBuilder, Constants {
045: protected static final Log log = LogFactory
046: .getLog(BaseSiteUpdateAction.class);
047:
048: public BaseSiteUpdateAction(String template, String errorTemplate,
049: PageManager pageManager) {
050: super (template, errorTemplate, pageManager);
051: }
052:
053: public BaseSiteUpdateAction(String template, String errorTemplate,
054: PortletActionSecurityBehavior securityBehavior) {
055: super (template, errorTemplate, securityBehavior);
056: }
057:
058: public BaseSiteUpdateAction(String template, String errorTemplate,
059: PageManager pageManager,
060: PortletActionSecurityBehavior securityBehavior) {
061: super (template, errorTemplate, pageManager, securityBehavior);
062: }
063:
064: protected abstract int updateInformation(
065: RequestContext requestContext, Map resultMap, Node node,
066: String path) throws AJAXException;
067:
068: protected int insertMetadata(RequestContext requestContext,
069: Map resultMap, Node node) throws AJAXException {
070: String name = getActionParameter(requestContext, "name");
071: String language = getActionParameter(requestContext, "lang");
072: String value = getActionParameter(requestContext, "value");
073: if (isBlank(name) || isBlank(language))
074: throw new AJAXException(
075: "Invalid Metadata: name, language invalid data.");
076: Locale locale = new Locale(language);
077: node.getMetadata().addField(locale, name, value);
078: return 1;
079: }
080:
081: protected int updateMetadata(RequestContext requestContext,
082: Map resultMap, Node node) throws AJAXException {
083: String name = getActionParameter(requestContext, "name");
084: String language = getActionParameter(requestContext, "lang");
085: String value = getActionParameter(requestContext, "value");
086: String oldName = getActionParameter(requestContext, "oldname");
087: String oldLanguage = getActionParameter(requestContext,
088: "oldlang");
089:
090: if (isBlank(name) || isBlank(language) || isBlank(oldName)
091: || isBlank(oldLanguage))
092: throw new AJAXException(
093: "Invalid Metadata: name, language invalid data.");
094:
095: Collection cfields = node.getMetadata().getFields(oldName);
096: if (cfields == null || cfields.size() == 0) {
097: return insertMetadata(requestContext, resultMap, node);
098: }
099: boolean found = false;
100: Iterator fields = cfields.iterator();
101: while (fields.hasNext()) {
102: LocalizedField field = (LocalizedField) fields.next();
103: if (areFieldsSame(field.getName(), oldName)
104: && areFieldsSame(field.getLocale().toString(),
105: oldLanguage)) {
106: field.setName(name);
107: field.setLocale(new Locale(language));
108: field.setValue(value);
109: found = true;
110: break;
111: }
112: }
113: if (!found)
114: return insertMetadata(requestContext, resultMap, node);
115: return 1;
116: }
117:
118: protected int removeMetadata(RequestContext requestContext,
119: Map resultMap, Node node) throws AJAXException {
120: String name = getActionParameter(requestContext, "name");
121: String language = getActionParameter(requestContext, "lang");
122: if (isBlank(name) || isBlank(language))
123: throw new AJAXException(
124: "Invalid Metadata: name, language invalid data.");
125: Collection cfields = node.getMetadata().getFields(name);
126: Collection allFields = node.getMetadata().getFields();
127: if (cfields == null || cfields.size() == 0) {
128: return 0;
129: }
130: boolean found = false;
131: Iterator fields = cfields.iterator();
132: while (fields.hasNext()) {
133: LocalizedField field = (LocalizedField) fields.next();
134: if (areFieldsSame(field.getName(), name)
135: && areFieldsSame(field.getLocale().toString(),
136: language)) {
137: cfields.remove(field);
138: if (allFields.remove(field)) {
139: node.getMetadata().setFields(allFields);
140: }
141: found = true;
142: break;
143: }
144: }
145:
146: return (found) ? 1 : 0;
147: }
148:
149: protected int insertSecurityReference(
150: RequestContext requestContext, Map resultMap, Node node)
151: throws AJAXException {
152: String name = getActionParameter(requestContext, "name");
153: String kind = getActionParameter(requestContext, "kind");
154: if (isBlank(name) || isBlank(kind))
155: throw new AJAXException(
156: "Invalid Security Ref: name invalid data.");
157: if (node.getSecurityConstraints() == null) {
158: SecurityConstraints cons = node.newSecurityConstraints();
159: node.setSecurityConstraints(cons);
160: }
161: if (kind.equals("Owner")) {
162: node.getSecurityConstraints().setOwner(name);
163: } else {
164: List refs = node.getSecurityConstraints()
165: .getSecurityConstraintsRefs();
166: if (refs.contains(name))
167: return 0; // do nothing
168: refs.add(name);
169: }
170: return 1;
171: }
172:
173: protected int updateSecurityReference(
174: RequestContext requestContext, Map resultMap, Node node)
175: throws AJAXException {
176: String name = getActionParameter(requestContext, "name");
177: String oldName = getActionParameter(requestContext, "oldname");
178: String kind = getActionParameter(requestContext, "kind");
179: if (isBlank(name) || isBlank(oldName) || isBlank(kind))
180: throw new AJAXException(
181: "Invalid Security Ref: name invalid data.");
182: if (node.getSecurityConstraints() == null) {
183: SecurityConstraints cons = node.newSecurityConstraints();
184: node.setSecurityConstraints(cons);
185: }
186: List refs = node.getSecurityConstraints()
187: .getSecurityConstraintsRefs();
188: if (refs == null || refs.size() == 0) {
189: return insertSecurityReference(requestContext, resultMap,
190: node);
191: }
192: boolean found = false;
193: if (kind.equals("Owner")) {
194: node.getSecurityConstraints().setOwner(name);
195: found = true;
196: } else {
197: for (int ix = 0; ix < refs.size(); ix++) {
198: String ref = (String) refs.get(ix);
199: if (areFieldsSame(ref, oldName)) {
200: refs.set(ix, name);
201: found = true;
202: break;
203: }
204: }
205: }
206: if (!found)
207: return insertSecurityReference(requestContext, resultMap,
208: node);
209: return 1;
210: }
211:
212: protected int removeSecurityReference(
213: RequestContext requestContext, Map resultMap, Node node)
214: throws AJAXException {
215: String name = getActionParameter(requestContext, "name");
216: String kind = getActionParameter(requestContext, "kind");
217: if (isBlank(name) || isBlank(kind))
218: throw new AJAXException(
219: "Invalid Security Ref: name invalid data.");
220: if (node.getSecurityConstraints() == null) {
221: return 0;
222: }
223: if (kind.equals("Owner")) {
224: node.getSecurityConstraints().setOwner(null);
225: } else {
226: List refs = node.getSecurityConstraints()
227: .getSecurityConstraintsRefs();
228: if (!refs.contains(name))
229: return 0; // nothing to do
230: refs.remove(name);
231: }
232: return 1;
233: }
234:
235: protected int removeSecurityDef(RequestContext requestContext,
236: Map resultMap, Node node) throws AJAXException {
237: String id = getActionParameter(requestContext, "id");
238: if (isBlank(id))
239: throw new AJAXException(
240: "Invalid Security Ref: id invalid data.");
241: if (node.getSecurityConstraints() == null) {
242: return 0;
243: }
244: List defs = node.getSecurityConstraints()
245: .getSecurityConstraints();
246: if (defs == null || defs.size() == 0) {
247: return 0;
248: }
249: if (id.length() == 1)
250: return 0;
251: id = id.substring(1);
252: int index = Integer.parseInt(id) - 1;
253: if (index < 0) {
254: return 0;
255: }
256: defs.remove(index);
257: return 1;
258: }
259:
260: protected boolean isBlank(String field) {
261: if (field == null || field.trim().length() == 0)
262: return true;
263: return false;
264: }
265:
266: protected boolean isFieldModified(String paramValue,
267: String prevValue) {
268: if (paramValue == null) {
269: if (prevValue == null)
270: return false;
271: else
272: return true;
273: } else {
274: if (prevValue == null)
275: return true;
276: if (prevValue.equals(paramValue))
277: return false;
278: else
279: return true;
280: }
281: }
282:
283: protected boolean areFieldsSame(String f1, String f2) {
284: return !isFieldModified(f1, f2);
285: }
286:
287: protected boolean isBooleanModified(String paramValue,
288: boolean prevValue) {
289: if (paramValue == null) {
290: if (prevValue == false)
291: return false;
292: else
293: return true;
294: } else {
295: if (prevValue == false)
296: return true;
297: else
298: return false;
299: }
300: }
301: }
|