1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.layout.impl;
18
19 import java.util.Map;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.apache.jetspeed.JetspeedActions;
24 import org.apache.jetspeed.ajax.AJAXException;
25 import org.apache.jetspeed.ajax.AjaxAction;
26 import org.apache.jetspeed.ajax.AjaxBuilder;
27 import org.apache.jetspeed.layout.PortletActionSecurityBehavior;
28 import org.apache.jetspeed.layout.PortletPlacementContext;
29 import org.apache.jetspeed.om.page.Fragment;
30 import org.apache.jetspeed.om.page.Page;
31 import org.apache.jetspeed.page.PageManager;
32 import org.apache.jetspeed.pipeline.PipelineException;
33 import org.apache.jetspeed.request.RequestContext;
34
35
36 /***
37 * Remove Portlet portlet placement action
38 *
39 * AJAX Parameters:
40 * id = the fragment id of the portlet to remove
41 * page = (implied in the URL)
42 *
43 * @author <a>David Gurney </a>
44 * @author <a href="mailto:taylor@apache.org">David Sean Taylor </a>
45 * @version $Id: $
46 */
47 public class RemovePortletAction
48 extends BasePortletAction
49 implements AjaxAction, AjaxBuilder, Constants
50 {
51 protected static final Log log = LogFactory.getLog(RemovePortletAction.class);
52
53 public RemovePortletAction(String template, String errorTemplate)
54 throws PipelineException
55 {
56 this(template, errorTemplate, null, null);
57 }
58
59 public RemovePortletAction(String template,
60 String errorTemplate,
61 PageManager pageManager,
62 PortletActionSecurityBehavior securityBehavior)
63 throws PipelineException
64 {
65 super(template, errorTemplate, pageManager, securityBehavior);
66 }
67
68 public boolean runBatch(RequestContext requestContext, Map resultMap) throws AJAXException
69 {
70 return runAction(requestContext, resultMap, true);
71 }
72
73 public boolean run(RequestContext requestContext, Map resultMap)
74 throws AJAXException
75 {
76 return runAction(requestContext, resultMap, false);
77 }
78
79 public boolean runAction(RequestContext requestContext, Map resultMap, boolean batch)
80 {
81 boolean success = true;
82 String status = "success";
83 try
84 {
85 resultMap.put(ACTION, "remove");
86
87 String portletId = getActionParameter(requestContext, PORTLETID);
88 if (portletId == null)
89 {
90 success = false;
91 resultMap.put(REASON, "Portlet ID not provided");
92 return success;
93 }
94 resultMap.put(PORTLETID, portletId);
95 if (false == checkAccess(requestContext, JetspeedActions.EDIT))
96 {
97 Page page = requestContext.getPage();
98 Fragment fragment = page.getFragmentById(portletId);
99 if (fragment == null)
100 {
101 success = false;
102 resultMap.put(REASON, "Fragment not found");
103 return success;
104 }
105 int column = fragment.getLayoutColumn();
106 int row = fragment.getLayoutRow();
107 if (!createNewPageOnEdit(requestContext))
108 {
109 success = false;
110 resultMap.put(REASON, "Insufficient access to edit page");
111 return success;
112 }
113 status = "refresh";
114
115 Fragment newFragment = getFragmentIdFromLocation(row, column, requestContext.getPage());
116 if (newFragment == null)
117 {
118 success = false;
119 resultMap.put(REASON, "Failed to find new fragment");
120 return success;
121 }
122 portletId = newFragment.getId();
123 }
124
125
126 PortletPlacementContext placement = new PortletPlacementContextImpl(requestContext);
127 Fragment fragment = placement.getFragmentById(portletId);
128 if (fragment == null)
129 {
130 success = false;
131 resultMap.put(REASON, "Fragment not found");
132 return success;
133 }
134 placement.remove(fragment);
135 Page page = placement.syncPageFragments();
136 page.removeFragmentById(fragment.getId());
137 if (!batch)
138 {
139 if (pageManager != null)
140 pageManager.updatePage(page);
141 }
142
143 resultMap.put(PORTLETID, portletId);
144 resultMap.put(STATUS, status);
145 resultMap.put(OLDCOL, String.valueOf(fragment.getLayoutColumn()));
146 resultMap.put(OLDROW, String.valueOf(fragment.getLayoutRow()));
147 }
148 catch (Exception e)
149 {
150
151 log.error("exception while adding a portlet", e);
152 resultMap.put(REASON, e.toString());
153
154 success = false;
155 }
156
157 return success;
158 }
159 }