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.Link;
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 Link action -- updates various parts of the PSML link
036: *
037: * AJAX Parameters:
038: * action = updatelink
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 UpdateLinkAction extends BaseSiteUpdateAction implements
052: AjaxAction, AjaxBuilder, Constants {
053: protected Log log = LogFactory.getLog(UpdateLinkAction.class);
054:
055: public UpdateLinkAction(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, "updatelink");
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: Link link = null;
088: if (!method.equals("add")) {
089: link = pageManager.getLink(path);
090: } else {
091: if (pageManager.linkExists(path)) {
092: success = false;
093: resultMap.put(REASON,
094: "Can't create: Link already exists: "
095: + path);
096: return success;
097: }
098: }
099: if (method.equals("info")) {
100: count = updateInformation(requestContext, resultMap,
101: link, path);
102: } else if (method.equals("update-meta")) {
103: count = updateMetadata(requestContext, resultMap, link);
104: } else if (method.equals("add-meta")) {
105: count = insertMetadata(requestContext, resultMap, link);
106: } else if (method.equals("remove-meta")) {
107: count = removeMetadata(requestContext, resultMap, link);
108: } else if (method.equals("add-secref")) {
109: count = insertSecurityReference(requestContext,
110: resultMap, link);
111: } else if (method.equals("update-secref")) {
112: count = updateSecurityReference(requestContext,
113: resultMap, link);
114: } else if (method.equals("remove-secref")) {
115: count = removeSecurityReference(requestContext,
116: resultMap, link);
117: } else if (method.equals("remove-secdef")) {
118: count = removeSecurityDef(requestContext, resultMap,
119: link);
120: } else if (method.equals("add")) {
121: link = pageManager.newLink(path);
122: link.setTitle(getActionParameter(requestContext,
123: "title"));
124: String s = getActionParameter(requestContext,
125: "short-title");
126: if (!isBlank(s))
127: link.setShortTitle(s);
128: link.setUrl(getActionParameter(requestContext, "url"));
129: count++;
130: } else if (method.equals("copy")) {
131: String destination = getActionParameter(requestContext,
132: "destination");
133: String name = getActionParameter(requestContext,
134: RESOURCE_NAME);
135: destination = destination + Folder.PATH_SEPARATOR
136: + name;
137: Link newLink = pageManager.copyLink(link, destination);
138: pageManager.updateLink(newLink);
139: } else if (method.equals("move")) {
140: String destination = getActionParameter(requestContext,
141: "destination");
142: String name = getActionParameter(requestContext,
143: RESOURCE_NAME);
144: destination = destination + Folder.PATH_SEPARATOR
145: + name;
146: Link newLink = pageManager.copyLink(link, destination);
147: pageManager.updateLink(newLink);
148: pageManager.removeLink(link);
149:
150: } else if (method.equals("remove")) {
151: pageManager.removeLink(link);
152: } else {
153: success = false;
154: resultMap.put(REASON,
155: "Unsupported Site Update method: " + method);
156: return success;
157: }
158: if (count > 0) {
159: pageManager.updateLink(link);
160: }
161: resultMap.put("count", Integer.toString(count));
162: resultMap.put(STATUS, status);
163: } catch (Exception e) {
164: log.error("exception administering Site update", e);
165: resultMap.put(REASON, e.toString());
166: success = false;
167: }
168: return success;
169: }
170:
171: protected int updateInformation(RequestContext requestContext,
172: Map resultMap, Node node, String path) throws AJAXException {
173: int count = 0;
174: try {
175: Link link = (Link) node;
176: String title = getActionParameter(requestContext, "title");
177: if (isFieldModified(title, link.getTitle()))
178: link.setTitle(title);
179: String shortTitle = getActionParameter(requestContext,
180: "short-title");
181: if (isFieldModified(shortTitle, link.getShortTitle()))
182: link.setShortTitle(shortTitle);
183: String url = getActionParameter(requestContext, "url");
184: if (isFieldModified(url, link.getUrl()))
185: link.setUrl(url);
186: String target = getActionParameter(requestContext, "target");
187: if (isFieldModified(target, link.getTarget()))
188: link.setTarget(target);
189: String hidden = getActionParameter(requestContext, "hidden");
190: if (isBooleanModified(hidden, link.isHidden()))
191: link.setHidden(!link.isHidden());
192: count++;
193: } catch (Exception e) {
194: throw new AJAXException(e);
195: }
196: return count;
197: }
198:
199: }
|