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.HashMap;
20 import java.util.Map;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.apache.jetspeed.JetspeedActions;
25 import org.apache.jetspeed.ajax.AJAXException;
26 import org.apache.jetspeed.ajax.AjaxAction;
27 import org.apache.jetspeed.ajax.AjaxBuilder;
28 import org.apache.jetspeed.components.portletentity.PortletEntityAccessComponent;
29 import org.apache.jetspeed.components.portletentity.PortletEntityNotStoredException;
30 import org.apache.jetspeed.container.window.FailedToRetrievePortletWindow;
31 import org.apache.jetspeed.container.window.PortletWindowAccessor;
32 import org.apache.jetspeed.layout.PortletActionSecurityBehavior;
33 import org.apache.jetspeed.om.folder.Folder;
34 import org.apache.jetspeed.om.page.ContentFragment;
35 import org.apache.jetspeed.om.page.ContentFragmentImpl;
36 import org.apache.jetspeed.om.page.Fragment;
37 import org.apache.jetspeed.om.page.Page;
38 import org.apache.jetspeed.page.PageManager;
39 import org.apache.jetspeed.page.document.Node;
40 import org.apache.jetspeed.request.RequestContext;
41 import org.apache.pluto.om.window.PortletWindow;
42
43 /***
44 * Update Page action -- updates various parts of the PSML page
45 *
46 * AJAX Parameters:
47 * action = updatepage
48 * General methods:
49 * method = add | remove
50 * Info methods:
51 * | info
52 * Meta methods:
53 * | add-meta | update-meta | remove-meta
54 * Security methods:
55 * | add-secref | remove-secref
56 * Fragment methods:
57 * | update-fragment | add-fragment | remove-fragment
58 *
59 * update-fragment params: id, layout(name), sizes, layoutid (add)
60 *
61 * @author <a href="mailto:taylor@apache.org">David Sean Taylor </a>
62 * @version $Id: $
63 */
64 public class UpdatePageAction
65 extends BaseSiteUpdateAction
66 implements AjaxAction, AjaxBuilder, Constants
67 {
68 protected Log log = LogFactory.getLog(UpdatePageAction.class);
69 protected PortletWindowAccessor windowAccess;
70 protected PortletEntityAccessComponent entityAccess;
71
72 public UpdatePageAction(String template,
73 String errorTemplate,
74 PageManager pm,
75 PortletWindowAccessor windowAccess,
76 PortletEntityAccessComponent entityAccess,
77 PortletActionSecurityBehavior securityBehavior)
78
79 {
80 super(template, errorTemplate, pm, securityBehavior);
81 this.windowAccess = windowAccess;
82 this.entityAccess = entityAccess;
83 }
84
85 public boolean run(RequestContext requestContext, Map resultMap)
86 throws AJAXException
87 {
88 boolean success = true;
89 String status = "success";
90 try
91 {
92 resultMap.put(ACTION, "updatepage");
93
94 String method = getActionParameter(requestContext, "method");
95 if (method == null)
96 {
97 throw new RuntimeException("Method not provided");
98 }
99 resultMap.put("method", method);
100 if (false == checkAccess(requestContext, JetspeedActions.EDIT))
101 {
102 success = false;
103 resultMap.put(REASON, "Insufficient access to administer portal permissions");
104 return success;
105 }
106 int count = 0;
107 Page page = null;
108 String path = getActionParameter(requestContext, "path");
109 if (path == null)
110 {
111 page = requestContext.getPage();
112 }
113 else
114 {
115 if (!method.equals("add"))
116 {
117 page = pageManager.getPage(path);
118 }
119 else
120 {
121 if (pageManager.pageExists(path))
122 {
123 success = false;
124 resultMap.put(REASON, "Can't create: Page already exists: " + path);
125 return success;
126 }
127 }
128 }
129 if (method.equals("info"))
130 {
131 count = updateInformation(requestContext, resultMap, page, path);
132 }
133 else if (method.equals("add-meta"))
134 {
135 count = insertMetadata(requestContext, resultMap, page);
136 }
137 else if (method.equals("update-meta"))
138 {
139 count = updateMetadata(requestContext, resultMap, page);
140 }
141 else if (method.equals("remove-meta"))
142 {
143 count = removeMetadata(requestContext, resultMap, page);
144 }
145 else if (method.equals("add-secref"))
146 {
147 count = insertSecurityReference(requestContext, resultMap, page);
148 }
149 else if (method.equals("update-secref"))
150 {
151 count = updateSecurityReference(requestContext, resultMap, page);
152 }
153 else if (method.equals("remove-secref"))
154 {
155 count = removeSecurityReference(requestContext, resultMap, page);
156 }
157 else if (method.equals("remove-secdef"))
158 {
159 count = removeSecurityDef(requestContext, resultMap, page);
160 }
161 else if (method.equals("add"))
162 {
163 page = pageManager.newPage(path);
164 page.setTitle(getActionParameter(requestContext, TITLE));
165 String s = getActionParameter(requestContext, SHORT_TITLE );
166 if (!isBlank(s))
167 page.setShortTitle(s);
168 page.getRootFragment().setName(getActionParameter(requestContext, DEFAULT_LAYOUT));
169 count++;
170 }
171 else if (method.equals("copy"))
172 {
173 String destination = getActionParameter(requestContext, "destination");
174 String name = getActionParameter(requestContext, RESOURCE_NAME);
175 destination = destination + Folder.PATH_SEPARATOR + name;
176 Page newPage = pageManager.copyPage(page,destination);
177 pageManager.updatePage(newPage);
178 }
179 else if (method.equals("move"))
180 {
181 String destination = getActionParameter(requestContext, "destination");
182 String name = getActionParameter(requestContext, RESOURCE_NAME);
183 destination = destination + Folder.PATH_SEPARATOR + name;
184 Page newPage = pageManager.copyPage(page, destination);
185 pageManager.updatePage(newPage);
186 pageManager.removePage(page);
187 }
188 else if (method.equals("remove"))
189 {
190 pageManager.removePage(page);
191 }
192 else if (method.equals("update-fragment"))
193 {
194 String fragmentId = getActionParameter(requestContext, PORTLETID);
195 String layout = getActionParameter(requestContext, LAYOUT);
196 if (isBlank(fragmentId) || isBlank(layout))
197 {
198 resultMap.put(REASON, "Missing parameter to update fragment");
199 return false;
200 }
201 count = updateFragment(requestContext, resultMap, page, fragmentId, layout);
202 }
203 else if (method.equals("add-fragment"))
204 {
205 String parentId = getActionParameter(requestContext, LAYOUTID);
206 String layout = getActionParameter(requestContext, LAYOUT);
207 if (isBlank(parentId) || isBlank(layout))
208 {
209 resultMap.put(REASON, "Missing parameter to add fragment");
210 return false;
211 }
212 count = addFragment(requestContext, resultMap, page, parentId, layout);
213 }
214 else if (method.equals("remove-fragment"))
215 {
216 String fragmentId = getActionParameter(requestContext, PORTLETID);
217 if (isBlank(fragmentId))
218 {
219 resultMap.put(REASON, "Missing parameter to remove fragment");
220 return false;
221 }
222 count = removeFragment(requestContext, resultMap, page, fragmentId);
223 }
224 else if (method.equals("update-portlet-decorator"))
225 {
226 String fragmentId = getActionParameter(requestContext, PORTLETID);
227 String portletDecorator = getActionParameter(requestContext, "portlet-decorator");
228 if (isBlank(fragmentId) || isBlank(portletDecorator))
229 {
230 resultMap.put(REASON, "Missing parameter to update portlet decorator");
231 return false;
232 }
233 count = updatePortletDecorator(requestContext, resultMap, page, fragmentId, portletDecorator);
234 }
235 else
236 {
237 success = false;
238 resultMap.put(REASON, "Unsupported Site Update method: " + method);
239 return success;
240 }
241 if (count > 0)
242 {
243 pageManager.updatePage(page);
244 }
245 resultMap.put("count", Integer.toString(count));
246 resultMap.put(STATUS, status);
247 }
248 catch (Exception e)
249 {
250 log.error("exception administering Site update", e);
251 resultMap.put(REASON, e.toString());
252 success = false;
253 }
254 return success;
255 }
256
257 protected int updatePortletDecorator(RequestContext requestContext, Map resultMap, Page page, String fragmentId, String portletDecorator)
258 throws PortletEntityNotStoredException, FailedToRetrievePortletWindow
259 {
260 int count = 0;
261 Fragment fragment = page.getFragmentById(fragmentId);
262 if (fragment != null)
263 {
264 fragment.setDecorator( portletDecorator );
265 count++;
266 }
267 return count;
268 }
269
270 protected int updateFragment(RequestContext requestContext, Map resultMap, Page page, String fragmentId, String layout)
271 throws PortletEntityNotStoredException, FailedToRetrievePortletWindow
272 {
273 int count = 0;
274 String sizes = getActionParameter(requestContext, SIZES);
275 Fragment fragment = page.getFragmentById(fragmentId);
276 if (fragment != null)
277 {
278 if (!layout.equals(fragment.getName()))
279 {
280 fragment.setName(layout);
281 ContentFragment contentFragment = new ContentFragmentImpl(fragment, new HashMap());
282 PortletWindow window = windowAccess.getPortletWindow(contentFragment);
283 if (window != null)
284 {
285 entityAccess.updatePortletEntity(window.getPortletEntity(), contentFragment);
286 entityAccess.storePortletEntity(window.getPortletEntity());
287 windowAccess.createPortletWindow(window.getPortletEntity(), contentFragment.getId());
288 count++;
289 if ( isBlank(sizes) )
290 {
291 fragment.setLayoutSizes(null);
292 }
293 else
294 {
295 fragment.setLayoutSizes(sizes);
296 }
297 count++;
298 }
299 }
300 else
301 {
302 if (!isBlank(sizes))
303 {
304 fragment.setLayoutSizes(sizes);
305 count++;
306 }
307 }
308 }
309 return count;
310 }
311
312 protected int addFragment(RequestContext requestContext, Map resultMap, Page page, String parentFragmentId, String layout)
313 {
314 int count = 0;
315 String sizes = getActionParameter(requestContext, SIZES);
316 Fragment fragment = page.getFragmentById(parentFragmentId);
317 if (fragment != null)
318 {
319 Fragment newFragment = pageManager.newFragment();
320 newFragment.setType(Fragment.LAYOUT);
321 newFragment.setName(layout);
322 fragment.getFragments().add(newFragment);
323 resultMap.put(PORTLETID, newFragment.getId());
324 count++;
325 if (!isBlank(sizes))
326 {
327 newFragment.setLayoutSizes(sizes);
328 count++;
329 }
330 }
331 return count;
332 }
333
334 protected int removeFragment(RequestContext requestContext, Map resultMap, Page page, String fragmentId)
335 {
336 int count = 0;
337 Fragment fragment = page.getFragmentById(fragmentId);
338 if (fragment != null)
339 {
340 page.removeFragmentById(fragment.getId());
341 count++;
342 }
343 return count;
344 }
345
346 protected int updateInformation(RequestContext requestContext, Map resultMap, Node node, String path)
347 throws AJAXException
348 {
349 int count = 0;
350 try
351 {
352 Page page = (Page)node;
353 String title = getActionParameter(requestContext, "title");
354 if (title != null && isFieldModified(title, page.getTitle()))
355 page.setTitle(title);
356 String shortTitle = getActionParameter(requestContext, "short-title");
357 if (shortTitle != null && isFieldModified(shortTitle, page.getShortTitle()))
358 page.setShortTitle(shortTitle);
359 String layoutDecorator = getActionParameter(requestContext, "layout-decorator");
360 if (layoutDecorator != null && isFieldModified(layoutDecorator, page.getDefaultDecorator(Fragment.LAYOUT)))
361 {
362 if (isBlank(layoutDecorator))
363 layoutDecorator = null;
364 page.setDefaultDecorator(layoutDecorator, Fragment.LAYOUT);
365 }
366 String portletDecorator = getActionParameter(requestContext, "portlet-decorator");
367 if (portletDecorator != null && isFieldModified(portletDecorator, page.getDefaultDecorator(Fragment.PORTLET)))
368 {
369 if (isBlank(portletDecorator))
370 portletDecorator = null;
371 page.setDefaultDecorator(portletDecorator, Fragment.PORTLET);
372 }
373 String theme = getActionParameter(requestContext, "theme");
374 if (theme != null && isFieldModified(theme, page.getSkin()))
375 {
376 if (isBlank(theme))
377 theme = null;
378 page.setSkin(theme);
379 }
380 String hidden = getActionParameter(requestContext, "hidden");
381 if (hidden != null && isBooleanModified(hidden, page.isHidden()))
382 page.setHidden(!page.isHidden());
383 count++;
384 }
385 catch (Exception e)
386 {
387 throw new AJAXException(e);
388 }
389 return count;
390 }
391
392 }