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.HashMap;
020: import java.util.Map;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024: import org.apache.jetspeed.JetspeedActions;
025: import org.apache.jetspeed.ajax.AJAXException;
026: import org.apache.jetspeed.ajax.AjaxAction;
027: import org.apache.jetspeed.ajax.AjaxBuilder;
028: import org.apache.jetspeed.components.portletentity.PortletEntityAccessComponent;
029: import org.apache.jetspeed.components.portletentity.PortletEntityNotStoredException;
030: import org.apache.jetspeed.container.window.FailedToRetrievePortletWindow;
031: import org.apache.jetspeed.container.window.PortletWindowAccessor;
032: import org.apache.jetspeed.layout.PortletActionSecurityBehavior;
033: import org.apache.jetspeed.om.folder.Folder;
034: import org.apache.jetspeed.om.page.ContentFragment;
035: import org.apache.jetspeed.om.page.ContentFragmentImpl;
036: import org.apache.jetspeed.om.page.Fragment;
037: import org.apache.jetspeed.om.page.Page;
038: import org.apache.jetspeed.page.PageManager;
039: import org.apache.jetspeed.page.document.Node;
040: import org.apache.jetspeed.request.RequestContext;
041: import org.apache.pluto.om.window.PortletWindow;
042:
043: /**
044: * Update Page action -- updates various parts of the PSML page
045: *
046: * AJAX Parameters:
047: * action = updatepage
048: * General methods:
049: * method = add | remove
050: * Info methods:
051: * | info
052: * Meta methods:
053: * | add-meta | update-meta | remove-meta
054: * Security methods:
055: * | add-secref | remove-secref
056: * Fragment methods:
057: * | update-fragment | add-fragment | remove-fragment
058: *
059: * update-fragment params: id, layout(name), sizes, layoutid (add)
060: *
061: * @author <a href="mailto:taylor@apache.org">David Sean Taylor </a>
062: * @version $Id: $
063: */
064: public class UpdatePageAction extends BaseSiteUpdateAction implements
065: AjaxAction, AjaxBuilder, Constants {
066: protected Log log = LogFactory.getLog(UpdatePageAction.class);
067: protected PortletWindowAccessor windowAccess;
068: protected PortletEntityAccessComponent entityAccess;
069:
070: public UpdatePageAction(String template, String errorTemplate,
071: PageManager pm, PortletWindowAccessor windowAccess,
072: PortletEntityAccessComponent entityAccess,
073: PortletActionSecurityBehavior securityBehavior)
074:
075: {
076: super (template, errorTemplate, pm, securityBehavior);
077: this .windowAccess = windowAccess;
078: this .entityAccess = entityAccess;
079: }
080:
081: public boolean run(RequestContext requestContext, Map resultMap)
082: throws AJAXException {
083: boolean success = true;
084: String status = "success";
085: try {
086: resultMap.put(ACTION, "updatepage");
087: // Get the necessary parameters off of the request
088: String method = getActionParameter(requestContext, "method");
089: if (method == null) {
090: throw new RuntimeException("Method not provided");
091: }
092: resultMap.put("method", method);
093: if (false == checkAccess(requestContext,
094: JetspeedActions.EDIT)) {
095: success = false;
096: resultMap
097: .put(REASON,
098: "Insufficient access to administer portal permissions");
099: return success;
100: }
101: int count = 0;
102: Page page = null;
103: String path = getActionParameter(requestContext, "path");
104: if (path == null) {
105: page = requestContext.getPage();
106: } else {
107: if (!method.equals("add")) {
108: page = pageManager.getPage(path);
109: } else {
110: if (pageManager.pageExists(path)) {
111: success = false;
112: resultMap.put(REASON,
113: "Can't create: Page already exists: "
114: + path);
115: return success;
116: }
117: }
118: }
119: if (method.equals("info")) {
120: count = updateInformation(requestContext, resultMap,
121: page, path);
122: } else if (method.equals("add-meta")) {
123: count = insertMetadata(requestContext, resultMap, page);
124: } else if (method.equals("update-meta")) {
125: count = updateMetadata(requestContext, resultMap, page);
126: } else if (method.equals("remove-meta")) {
127: count = removeMetadata(requestContext, resultMap, page);
128: } else if (method.equals("add-secref")) {
129: count = insertSecurityReference(requestContext,
130: resultMap, page);
131: } else if (method.equals("update-secref")) {
132: count = updateSecurityReference(requestContext,
133: resultMap, page);
134: } else if (method.equals("remove-secref")) {
135: count = removeSecurityReference(requestContext,
136: resultMap, page);
137: } else if (method.equals("remove-secdef")) {
138: count = removeSecurityDef(requestContext, resultMap,
139: page);
140: } else if (method.equals("add")) {
141: page = pageManager.newPage(path);
142: page
143: .setTitle(getActionParameter(requestContext,
144: TITLE));
145: String s = getActionParameter(requestContext,
146: SHORT_TITLE);
147: if (!isBlank(s))
148: page.setShortTitle(s);
149: page.getRootFragment().setName(
150: getActionParameter(requestContext,
151: DEFAULT_LAYOUT));
152: count++;
153: } else if (method.equals("copy")) {
154: String destination = getActionParameter(requestContext,
155: "destination");
156: String name = getActionParameter(requestContext,
157: RESOURCE_NAME);
158: destination = destination + Folder.PATH_SEPARATOR
159: + name;
160: Page newPage = pageManager.copyPage(page, destination);
161: pageManager.updatePage(newPage);
162: } else if (method.equals("move")) {
163: String destination = getActionParameter(requestContext,
164: "destination");
165: String name = getActionParameter(requestContext,
166: RESOURCE_NAME);
167: destination = destination + Folder.PATH_SEPARATOR
168: + name;
169: Page newPage = pageManager.copyPage(page, destination);
170: pageManager.updatePage(newPage);
171: pageManager.removePage(page);
172: } else if (method.equals("remove")) {
173: pageManager.removePage(page);
174: } else if (method.equals("update-fragment")) {
175: String fragmentId = getActionParameter(requestContext,
176: PORTLETID);
177: String layout = getActionParameter(requestContext,
178: LAYOUT);
179: if (isBlank(fragmentId) || isBlank(layout)) {
180: resultMap.put(REASON,
181: "Missing parameter to update fragment");
182: return false;
183: }
184: count = updateFragment(requestContext, resultMap, page,
185: fragmentId, layout);
186: } else if (method.equals("add-fragment")) {
187: String parentId = getActionParameter(requestContext,
188: LAYOUTID);
189: String layout = getActionParameter(requestContext,
190: LAYOUT);
191: if (isBlank(parentId) || isBlank(layout)) {
192: resultMap.put(REASON,
193: "Missing parameter to add fragment");
194: return false;
195: }
196: count = addFragment(requestContext, resultMap, page,
197: parentId, layout);
198: } else if (method.equals("remove-fragment")) {
199: String fragmentId = getActionParameter(requestContext,
200: PORTLETID);
201: if (isBlank(fragmentId)) {
202: resultMap.put(REASON,
203: "Missing parameter to remove fragment");
204: return false;
205: }
206: count = removeFragment(requestContext, resultMap, page,
207: fragmentId);
208: } else if (method.equals("update-portlet-decorator")) {
209: String fragmentId = getActionParameter(requestContext,
210: PORTLETID);
211: String portletDecorator = getActionParameter(
212: requestContext, "portlet-decorator");
213: if (isBlank(fragmentId) || isBlank(portletDecorator)) {
214: resultMap
215: .put(REASON,
216: "Missing parameter to update portlet decorator");
217: return false;
218: }
219: count = updatePortletDecorator(requestContext,
220: resultMap, page, fragmentId, portletDecorator);
221: } else {
222: success = false;
223: resultMap.put(REASON,
224: "Unsupported Site Update method: " + method);
225: return success;
226: }
227: if (count > 0) {
228: pageManager.updatePage(page);
229: }
230: resultMap.put("count", Integer.toString(count));
231: resultMap.put(STATUS, status);
232: } catch (Exception e) {
233: log.error("exception administering Site update", e);
234: resultMap.put(REASON, e.toString());
235: success = false;
236: }
237: return success;
238: }
239:
240: protected int updatePortletDecorator(RequestContext requestContext,
241: Map resultMap, Page page, String fragmentId,
242: String portletDecorator)
243: throws PortletEntityNotStoredException,
244: FailedToRetrievePortletWindow {
245: int count = 0;
246: Fragment fragment = page.getFragmentById(fragmentId);
247: if (fragment != null) {
248: fragment.setDecorator(portletDecorator);
249: count++;
250: }
251: return count;
252: }
253:
254: protected int updateFragment(RequestContext requestContext,
255: Map resultMap, Page page, String fragmentId, String layout)
256: throws PortletEntityNotStoredException,
257: FailedToRetrievePortletWindow {
258: int count = 0;
259: String sizes = getActionParameter(requestContext, SIZES);
260: Fragment fragment = page.getFragmentById(fragmentId);
261: if (fragment != null) {
262: if (!layout.equals(fragment.getName())) {
263: fragment.setName(layout);
264: ContentFragment contentFragment = new ContentFragmentImpl(
265: fragment, new HashMap());
266: PortletWindow window = windowAccess
267: .getPortletWindow(contentFragment);
268: if (window != null) {
269: entityAccess.updatePortletEntity(window
270: .getPortletEntity(), contentFragment);
271: entityAccess.storePortletEntity(window
272: .getPortletEntity());
273: windowAccess.createPortletWindow(window
274: .getPortletEntity(), contentFragment
275: .getId());
276: count++;
277: if (isBlank(sizes)) {
278: fragment.setLayoutSizes(null);
279: } else {
280: fragment.setLayoutSizes(sizes);
281: }
282: count++;
283: }
284: } else {
285: if (!isBlank(sizes)) {
286: fragment.setLayoutSizes(sizes);
287: count++;
288: }
289: }
290: }
291: return count;
292: }
293:
294: protected int addFragment(RequestContext requestContext,
295: Map resultMap, Page page, String parentFragmentId,
296: String layout) {
297: int count = 0;
298: String sizes = getActionParameter(requestContext, SIZES);
299: Fragment fragment = page.getFragmentById(parentFragmentId);
300: if (fragment != null) {
301: Fragment newFragment = pageManager.newFragment();
302: newFragment.setType(Fragment.LAYOUT);
303: newFragment.setName(layout);
304: fragment.getFragments().add(newFragment);
305: resultMap.put(PORTLETID, newFragment.getId());
306: count++;
307: if (!isBlank(sizes)) {
308: newFragment.setLayoutSizes(sizes);
309: count++;
310: }
311: }
312: return count;
313: }
314:
315: protected int removeFragment(RequestContext requestContext,
316: Map resultMap, Page page, String fragmentId) {
317: int count = 0;
318: Fragment fragment = page.getFragmentById(fragmentId);
319: if (fragment != null) {
320: page.removeFragmentById(fragment.getId());
321: count++;
322: }
323: return count;
324: }
325:
326: protected int updateInformation(RequestContext requestContext,
327: Map resultMap, Node node, String path) throws AJAXException {
328: int count = 0;
329: try {
330: Page page = (Page) node;
331: String title = getActionParameter(requestContext, "title");
332: if (title != null
333: && isFieldModified(title, page.getTitle()))
334: page.setTitle(title);
335: String shortTitle = getActionParameter(requestContext,
336: "short-title");
337: if (shortTitle != null
338: && isFieldModified(shortTitle, page.getShortTitle()))
339: page.setShortTitle(shortTitle);
340: String layoutDecorator = getActionParameter(requestContext,
341: "layout-decorator");
342: if (layoutDecorator != null
343: && isFieldModified(layoutDecorator, page
344: .getDefaultDecorator(Fragment.LAYOUT))) {
345: if (isBlank(layoutDecorator))
346: layoutDecorator = null;
347: page.setDefaultDecorator(layoutDecorator,
348: Fragment.LAYOUT);
349: }
350: String portletDecorator = getActionParameter(
351: requestContext, "portlet-decorator");
352: if (portletDecorator != null
353: && isFieldModified(portletDecorator, page
354: .getDefaultDecorator(Fragment.PORTLET))) {
355: if (isBlank(portletDecorator))
356: portletDecorator = null;
357: page.setDefaultDecorator(portletDecorator,
358: Fragment.PORTLET);
359: }
360: String theme = getActionParameter(requestContext, "theme");
361: if (theme != null && isFieldModified(theme, page.getSkin())) {
362: if (isBlank(theme))
363: theme = null;
364: page.setSkin(theme);
365: }
366: String hidden = getActionParameter(requestContext, "hidden");
367: if (hidden != null
368: && isBooleanModified(hidden, page.isHidden()))
369: page.setHidden(!page.isHidden());
370: count++;
371: } catch (Exception e) {
372: throw new AJAXException(e);
373: }
374: return count;
375: }
376:
377: }
|