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.components.portletregistry.PortletRegistry;
028: import org.apache.jetspeed.layout.PortletActionSecurityBehavior;
029: import org.apache.jetspeed.layout.PortletPlacementContext;
030: import org.apache.jetspeed.om.page.Fragment;
031: import org.apache.jetspeed.om.page.Page;
032: import org.apache.jetspeed.page.PageManager;
033: import org.apache.jetspeed.pipeline.PipelineException;
034: import org.apache.jetspeed.request.RequestContext;
035:
036: /**
037: * Remove Portlet portlet placement action
038: *
039: * AJAX Parameters:
040: * id = the fragment id of the portlet to remove
041: * page = (implied in the URL)
042: *
043: * @author <a>David Gurney </a>
044: * @author <a href="mailto:taylor@apache.org">David Sean Taylor </a>
045: * @version $Id: $
046: */
047: public class RemovePortletAction extends BasePortletAction implements
048: AjaxAction, AjaxBuilder, Constants {
049: protected static final Log log = LogFactory
050: .getLog(RemovePortletAction.class);
051:
052: private PortletRegistry registry;
053:
054: public RemovePortletAction(String template, String errorTemplate,
055: PortletRegistry registry) throws PipelineException {
056: this (template, errorTemplate, registry, null, null);
057: }
058:
059: public RemovePortletAction(String template, String errorTemplate,
060: PortletRegistry registry, PageManager pageManager,
061: PortletActionSecurityBehavior securityBehavior)
062: throws PipelineException {
063: super (template, errorTemplate, pageManager, securityBehavior);
064: this .registry = registry;
065: }
066:
067: public boolean runBatch(RequestContext requestContext, Map resultMap)
068: throws AJAXException {
069: return runAction(requestContext, resultMap, true);
070: }
071:
072: public boolean run(RequestContext requestContext, Map resultMap)
073: throws AJAXException {
074: return runAction(requestContext, resultMap, false);
075: }
076:
077: public boolean runAction(RequestContext requestContext,
078: Map resultMap, boolean batch) {
079: boolean success = true;
080: String status = "success";
081: try {
082: resultMap.put(ACTION, "remove");
083: // Get the necessary parameters off of the request
084: String portletId = getActionParameter(requestContext,
085: PORTLETID);
086: if (portletId == null) {
087: success = false;
088: resultMap.put(REASON, "Portlet ID not provided");
089: return success;
090: }
091: resultMap.put(PORTLETID, portletId);
092: if (false == checkAccess(requestContext,
093: JetspeedActions.EDIT)) {
094: Page page = requestContext.getPage();
095: Fragment fragment = page.getFragmentById(portletId);
096: if (fragment == null) {
097: success = false;
098: resultMap.put(REASON, "Fragment not found");
099: return success;
100: }
101:
102: NestedFragmentContext removeFragmentContext = null;
103: try {
104: removeFragmentContext = new NestedFragmentContext(
105: fragment, page, registry);
106: } catch (Exception ex) {
107: log.error(
108: "Failure to construct nested context for fragment "
109: + portletId, ex);
110: success = false;
111: resultMap
112: .put(REASON,
113: "Cannot construct nested context for fragment");
114: return success;
115: }
116:
117: if (!createNewPageOnEdit(requestContext)) {
118: success = false;
119: resultMap.put(REASON,
120: "Insufficient access to edit page");
121: return success;
122: }
123: status = "refresh";
124:
125: Page newPage = requestContext.getPage();
126:
127: // using NestedFragmentContext, find portlet id for copy of target portlet in the new page
128: Fragment newFragment = null;
129: try {
130: newFragment = removeFragmentContext
131: .getFragmentOnNewPage(newPage, registry);
132: } catch (Exception ex) {
133: log.error("Failure to locate copy of fragment "
134: + portletId, ex);
135: success = false;
136: resultMap.put(REASON,
137: "Failed to find new fragment for portlet id: "
138: + portletId);
139: return success;
140: }
141: portletId = newFragment.getId();
142: }
143:
144: // Use the Portlet Placement Manager to accomplish the removal
145: Page page = requestContext.getPage();
146: Fragment root = page.getRootFragment();
147: Fragment layoutContainerFragment = getParentFragmentById(
148: portletId, root);
149: PortletPlacementContext placement = null;
150: Fragment fragment = null;
151: if (layoutContainerFragment != null) {
152: placement = new PortletPlacementContextImpl(page,
153: registry, layoutContainerFragment);
154: fragment = placement.getFragmentById(portletId);
155: }
156: if (fragment == null) {
157: success = false;
158: resultMap.put(REASON, "Fragment not found");
159: return success;
160: }
161: placement.remove(fragment);
162: page = placement.syncPageFragments();
163: page.removeFragmentById(fragment.getId());
164: if (!batch) {
165: if (pageManager != null)
166: pageManager.updatePage(page);
167: }
168: // Build the results for the response
169: resultMap.put(PORTLETID, portletId);
170: resultMap.put(STATUS, status);
171: resultMap.put(OLDCOL, String.valueOf(fragment
172: .getLayoutColumn()));
173: resultMap.put(OLDROW, String.valueOf(fragment
174: .getLayoutRow()));
175: } catch (Exception e) {
176: // Log the exception
177: log.error("exception while adding a portlet", e);
178: resultMap.put(REASON, e.toString());
179: // Return a failure indicator
180: success = false;
181: }
182:
183: return success;
184: }
185:
186: protected PortletRegistry getPortletRegistry() {
187: return this.registry;
188: }
189: }
|