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.Map;
020:
021: import org.apache.commons.logging.Log;
022: import org.apache.commons.logging.LogFactory;
023: import org.apache.jetspeed.JetspeedActions;
024: import org.apache.jetspeed.ajax.AJAXException;
025: import org.apache.jetspeed.ajax.AjaxAction;
026: import org.apache.jetspeed.ajax.AjaxBuilder;
027: import org.apache.jetspeed.layout.PortletActionSecurityBehavior;
028: import org.apache.jetspeed.om.folder.Folder;
029: import org.apache.jetspeed.om.page.Fragment;
030: import org.apache.jetspeed.page.PageManager;
031: import org.apache.jetspeed.page.document.Node;
032: import org.apache.jetspeed.request.RequestContext;
033:
034: /**
035: * Update Folder action -- updates various parts of the PSML folder
036: *
037: * AJAX Parameters:
038: * action = updatefolder
039: * General methods:
040: * method = add | remove
041: * Info methods:
042: * | info
043: * Meta methods:
044: * | add-meta | update-meta | remove-meta
045: * Security methods:
046: * | add-secref | remove-secref
047: *
048: * @author <a href="mailto:taylor@apache.org">David Sean Taylor </a>
049: * @version $Id: $
050: */
051: public class UpdateFolderAction extends BaseSiteUpdateAction implements
052: AjaxAction, AjaxBuilder, Constants {
053: protected Log log = LogFactory.getLog(UpdateFolderAction.class);
054:
055: public UpdateFolderAction(String template, String errorTemplate,
056: PageManager pm,
057: PortletActionSecurityBehavior securityBehavior)
058:
059: {
060: super (template, errorTemplate, pm, securityBehavior);
061: }
062:
063: public boolean run(RequestContext requestContext, Map resultMap)
064: throws AJAXException {
065: boolean success = true;
066: String status = "success";
067: try {
068: resultMap.put(ACTION, "updatefolder");
069: // Get the necessary parameters off of the request
070: String method = getActionParameter(requestContext, "method");
071: if (method == null) {
072: throw new RuntimeException("Method not provided");
073: }
074: resultMap.put("method", method);
075: if (false == checkAccess(requestContext,
076: JetspeedActions.EDIT)) {
077: success = false;
078: resultMap
079: .put(REASON,
080: "Insufficient access to administer portal permissions");
081: return success;
082: }
083: int count = 0;
084: String path = getActionParameter(requestContext, "path");
085: if (path == null)
086: throw new AJAXException("Missing 'path' parameter");
087: Folder folder = null;
088: if (!method.equals("add")) {
089: folder = pageManager.getFolder(path);
090: } else {
091: if (pageManager.folderExists(path)) {
092: success = false;
093: resultMap.put(REASON,
094: "Can't create: Folder already exists: "
095: + path);
096: return success;
097: }
098: }
099: if (method.equals("info")) {
100: count = updateInformation(requestContext, resultMap,
101: folder, path);
102: } else if (method.equals("add-meta")) {
103: count = insertMetadata(requestContext, resultMap,
104: folder);
105: } else if (method.equals("update-meta")) {
106: count = updateMetadata(requestContext, resultMap,
107: folder);
108: } else if (method.equals("remove-meta")) {
109: count = removeMetadata(requestContext, resultMap,
110: folder);
111: } else if (method.equals("update-secref")) {
112: count = updateSecurityReference(requestContext,
113: resultMap, folder);
114: } else if (method.equals("add-secref")) {
115: count = insertSecurityReference(requestContext,
116: resultMap, folder);
117: } else if (method.equals("remove-secref")) {
118: count = removeSecurityReference(requestContext,
119: resultMap, folder);
120: } else if (method.equals("remove-secdef")) {
121: count = removeSecurityDef(requestContext, resultMap,
122: folder);
123: } else if (method.equals("add")) {
124: folder = pageManager.newFolder(path);
125: folder.setTitle(getActionParameter(requestContext,
126: "title"));
127: String s = getActionParameter(requestContext,
128: "short-title");
129: if (!isBlank(s))
130: folder.setShortTitle(s);
131: count++;
132: } else if (method.equals("copy")) {
133: String destination = getActionParameter(requestContext,
134: "destination");
135: String name = getActionParameter(requestContext,
136: RESOURCE_NAME);
137: destination = destination + Folder.PATH_SEPARATOR
138: + name;
139: pageManager.deepCopyFolder(folder, destination, null);
140: } else if (method.equals("move")) {
141: String destination = getActionParameter(requestContext,
142: "destination");
143: String name = getActionParameter(requestContext,
144: RESOURCE_NAME);
145: destination = destination + Folder.PATH_SEPARATOR
146: + name;
147: pageManager.deepCopyFolder(folder, destination, null);
148: pageManager.removeFolder(folder);
149: } else if (method.equals("remove")) {
150: pageManager.removeFolder(folder);
151: } else {
152: success = false;
153: resultMap.put(REASON,
154: "Unsupported Site Update method: " + method);
155: return success;
156: }
157: if (count > 0) {
158: pageManager.updateFolder(folder);
159: }
160: resultMap.put("count", Integer.toString(count));
161: resultMap.put(STATUS, status);
162: } catch (Exception e) {
163: log.error("exception administering Site update", e);
164: resultMap.put(REASON, e.toString());
165: success = false;
166: }
167: return success;
168: }
169:
170: protected int updateInformation(RequestContext requestContext,
171: Map resultMap, Node node, String path) throws AJAXException {
172: int count = 0;
173: try {
174: Folder folder = (Folder) node;
175: String title = getActionParameter(requestContext, "title");
176: if (isFieldModified(title, folder.getTitle()))
177: folder.setTitle(title);
178: String shortTitle = getActionParameter(requestContext,
179: "short-title");
180: if (isFieldModified(shortTitle, folder.getShortTitle()))
181: folder.setShortTitle(shortTitle);
182: String layoutDecorator = getActionParameter(requestContext,
183: "layout-decorator");
184: if (isFieldModified(layoutDecorator, folder
185: .getDefaultDecorator(Fragment.LAYOUT))) {
186: if (isBlank(layoutDecorator))
187: layoutDecorator = null;
188: folder.setDefaultDecorator(layoutDecorator,
189: Fragment.LAYOUT);
190: }
191: String portletDecorator = getActionParameter(
192: requestContext, "portlet-decorator");
193: if (isFieldModified(portletDecorator, folder
194: .getDefaultDecorator(Fragment.PORTLET))) {
195: if (isBlank(portletDecorator))
196: portletDecorator = null;
197: folder.setDefaultDecorator(portletDecorator,
198: Fragment.PORTLET);
199: }
200: String defaultPage = getActionParameter(requestContext,
201: "default-page");
202: if (isFieldModified(defaultPage, folder.getDefaultPage()))
203: folder.setDefaultPage(defaultPage);
204: String hidden = getActionParameter(requestContext, "hidden");
205: if (isBooleanModified(hidden, folder.isHidden()))
206: folder.setHidden(!folder.isHidden());
207: count++;
208: } catch (Exception e) {
209: throw new AJAXException(e);
210: }
211: return count;
212: }
213:
214: }
|