View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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              // Get the necessary parameters off of the request
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
225             {
226                 success = false;
227                 resultMap.put(REASON, "Unsupported Site Update method: " + method);                
228                 return success;                
229             }
230             if (count > 0)
231             {
232                 pageManager.updatePage(page);                
233             }                        
234             resultMap.put("count", Integer.toString(count));
235             resultMap.put(STATUS, status);
236         } 
237         catch (Exception e)
238         {
239             log.error("exception administering Site update", e);
240             resultMap.put(REASON, e.toString());
241             success = false;
242         }
243         return success;
244     }
245     
246     protected int updateFragment(RequestContext requestContext, Map resultMap, Page page, String fragmentId, String layout)
247     throws PortletEntityNotStoredException, FailedToRetrievePortletWindow
248     {
249         int count = 0;
250         String sizes = getActionParameter(requestContext, SIZES);
251         Fragment fragment = page.getFragmentById(fragmentId);
252         if (fragment != null)
253         {                
254             if (!layout.equals(fragment.getName()))
255             {
256                 fragment.setName(layout);
257                 ContentFragment contentFragment = new ContentFragmentImpl(fragment, new HashMap());                    
258                 PortletWindow window = windowAccess.getPortletWindow(contentFragment);
259                 if (window != null)
260                 {
261                     entityAccess.updatePortletEntity(window.getPortletEntity(), contentFragment);
262                     entityAccess.storePortletEntity(window.getPortletEntity());
263                     windowAccess.createPortletWindow(window.getPortletEntity(), contentFragment.getId());
264                     count++;
265                     if ( isBlank(sizes) )
266                     {
267                         fragment.setLayoutSizes(null);
268                     }
269                     else
270                     {
271                         fragment.setLayoutSizes(sizes);
272                     }
273                     count++;
274                 }
275             }
276             else
277             {
278                 if (!isBlank(sizes))
279                 {
280                     fragment.setLayoutSizes(sizes);
281                     count++;
282                 }
283             }
284         }
285         return count;
286     }
287 
288     protected int addFragment(RequestContext requestContext, Map resultMap, Page page, String parentFragmentId, String layout)
289     {
290         int count = 0;
291         String sizes = getActionParameter(requestContext, SIZES);
292         Fragment fragment = page.getFragmentById(parentFragmentId);
293         if (fragment != null)
294         {
295             Fragment newFragment = pageManager.newFragment();
296             newFragment.setType(Fragment.LAYOUT);            
297             newFragment.setName(layout);
298             fragment.getFragments().add(newFragment);            
299             resultMap.put(PORTLETID, newFragment.getId());                        
300             count++;
301             if (!isBlank(sizes))
302             {
303                 newFragment.setLayoutSizes(sizes);
304                 count++;
305             }
306         }
307         return count;
308     }
309 
310     protected int removeFragment(RequestContext requestContext, Map resultMap, Page page, String fragmentId)
311     {
312         int count = 0;
313         Fragment fragment = page.getFragmentById(fragmentId);
314         if (fragment != null)
315         {
316             page.removeFragmentById(fragment.getId());
317             count++;
318         }
319         return count;
320     }    
321         
322     protected int updateInformation(RequestContext requestContext, Map resultMap, Node node, String path)
323     throws AJAXException    
324     {
325         int count = 0;
326         try
327         {
328             Page page = (Page)node;            
329             String title = getActionParameter(requestContext, "title");
330             if (title != null && isFieldModified(title, page.getTitle()))
331                 page.setTitle(title);
332             String shortTitle = getActionParameter(requestContext, "short-title");
333             if (shortTitle != null && isFieldModified(shortTitle, page.getShortTitle()))
334                 page.setShortTitle(shortTitle);
335             String layoutDecorator = getActionParameter(requestContext, "layout-decorator");
336             if (layoutDecorator != null && isFieldModified(layoutDecorator, page.getDefaultDecorator(Fragment.LAYOUT)))
337             {
338                 if (isBlank(layoutDecorator))
339                     layoutDecorator = null; 
340                 page.setDefaultDecorator(layoutDecorator, Fragment.LAYOUT);
341             }
342             String portletDecorator = getActionParameter(requestContext, "portlet-decorator");
343             if (portletDecorator != null && isFieldModified(portletDecorator, page.getDefaultDecorator(Fragment.PORTLET)))
344             {
345                 if (isBlank(portletDecorator))
346                     portletDecorator = null;                 
347                 page.setDefaultDecorator(portletDecorator, Fragment.PORTLET);
348             }
349             String theme = getActionParameter(requestContext, "theme");
350             if (theme != null && isFieldModified(theme, page.getSkin()))
351             {
352                 if (isBlank(theme))
353                     theme = null;                 
354                 page.setSkin(theme);
355             }
356             String hidden = getActionParameter(requestContext, "hidden");
357             if (hidden != null && isBooleanModified(hidden, page.isHidden()))
358                 page.setHidden(!page.isHidden());                                    
359             count++;
360         }
361         catch (Exception e)
362         {
363             throw new AJAXException(e);
364         }        
365         return count;
366     }
367     
368 }