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.components.portletregistry.PortletRegistry;
28 import org.apache.jetspeed.layout.PortletActionSecurityBehavior;
29 import org.apache.jetspeed.layout.PortletPlacementContext;
30 import org.apache.jetspeed.om.page.Fragment;
31 import org.apache.jetspeed.om.page.Page;
32 import org.apache.jetspeed.page.PageManager;
33 import org.apache.jetspeed.pipeline.PipelineException;
34 import org.apache.jetspeed.request.RequestContext;
35
36
37 /***
38 * Remove Portlet portlet placement action
39 *
40 * AJAX Parameters:
41 * id = the fragment id of the portlet to remove
42 * page = (implied in the URL)
43 *
44 * @author <a>David Gurney </a>
45 * @author <a href="mailto:taylor@apache.org">David Sean Taylor </a>
46 * @version $Id: $
47 */
48 public class RemovePortletAction
49 extends BasePortletAction
50 implements AjaxAction, AjaxBuilder, Constants
51 {
52 protected static final Log log = LogFactory.getLog(RemovePortletAction.class);
53
54 private PortletRegistry registry;
55
56 public RemovePortletAction( String template, String errorTemplate, PortletRegistry registry )
57 throws PipelineException
58 {
59 this( template, errorTemplate, registry, null, null );
60 }
61
62 public RemovePortletAction( String template,
63 String errorTemplate,
64 PortletRegistry registry,
65 PageManager pageManager,
66 PortletActionSecurityBehavior securityBehavior )
67 throws PipelineException
68 {
69 super( template, errorTemplate, pageManager, securityBehavior );
70 this.registry = registry;
71 }
72
73 public boolean runBatch(RequestContext requestContext, Map resultMap) throws AJAXException
74 {
75 return runAction(requestContext, resultMap, true);
76 }
77
78 public boolean run(RequestContext requestContext, Map resultMap)
79 throws AJAXException
80 {
81 return runAction(requestContext, resultMap, false);
82 }
83
84 public boolean runAction(RequestContext requestContext, Map resultMap, boolean batch)
85 {
86 boolean success = true;
87 String status = "success";
88 try
89 {
90 resultMap.put( ACTION, "remove" );
91
92 String portletId = getActionParameter( requestContext, PORTLETID );
93 if (portletId == null)
94 {
95 success = false;
96 resultMap.put( REASON, "Portlet ID not provided" );
97 return success;
98 }
99 resultMap.put( PORTLETID, portletId );
100 if ( false == checkAccess( requestContext, JetspeedActions.EDIT ) )
101 {
102 Page page = requestContext.getPage();
103 Fragment fragment = page.getFragmentById( portletId );
104 if ( fragment == null )
105 {
106 success = false;
107 resultMap.put( REASON, "Fragment not found" );
108 return success;
109 }
110
111 NestedFragmentContext removeFragmentContext = null;
112 try
113 {
114 removeFragmentContext = new NestedFragmentContext( fragment, page, registry );
115 }
116 catch ( Exception ex )
117 {
118 log.error( "Failure to construct nested context for fragment " + portletId, ex );
119 success = false;
120 resultMap.put( REASON, "Cannot construct nested context for fragment" );
121 return success;
122 }
123
124 if ( ! createNewPageOnEdit( requestContext ) )
125 {
126 success = false;
127 resultMap.put( REASON, "Insufficient access to edit page" );
128 return success;
129 }
130 status = "refresh";
131
132 Page newPage = requestContext.getPage();
133
134
135 Fragment newFragment = null;
136 try
137 {
138 newFragment = removeFragmentContext.getFragmentOnNewPage( newPage, registry );
139 }
140 catch ( Exception ex )
141 {
142 log.error( "Failure to locate copy of fragment " + portletId, ex );
143 success = false;
144 resultMap.put( REASON, "Failed to find new fragment for portlet id: " + portletId );
145 return success;
146 }
147 portletId = newFragment.getId();
148 }
149
150
151 Page page = requestContext.getPage();
152 Fragment root = page.getRootFragment();
153 Fragment layoutContainerFragment = getParentFragmentById( portletId, root );
154 PortletPlacementContext placement = null;
155 Fragment fragment = null;
156 if ( layoutContainerFragment != null )
157 {
158 placement = new PortletPlacementContextImpl( page, registry, layoutContainerFragment );
159 fragment = placement.getFragmentById( portletId );
160 }
161 if ( fragment == null )
162 {
163 success = false;
164 resultMap.put( REASON, "Fragment not found" );
165 return success;
166 }
167 placement.remove(fragment);
168 page = placement.syncPageFragments();
169 page.removeFragmentById( fragment.getId() );
170 if (!batch)
171 {
172 if (pageManager != null)
173 pageManager.updatePage( page );
174 }
175
176 resultMap.put( PORTLETID, portletId );
177 resultMap.put( STATUS, status );
178 resultMap.put( OLDCOL, String.valueOf( fragment.getLayoutColumn() ) );
179 resultMap.put( OLDROW, String.valueOf( fragment.getLayoutRow() ) );
180 }
181 catch ( Exception e )
182 {
183
184 log.error("exception while adding a portlet", e);
185 resultMap.put(REASON, e.toString());
186
187 success = false;
188 }
189
190 return success;
191 }
192
193 protected PortletRegistry getPortletRegistry()
194 {
195 return this.registry;
196 }
197 }