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.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.om.folder.Folder;
29  import org.apache.jetspeed.om.page.Fragment;
30  import org.apache.jetspeed.page.PageManager;
31  import org.apache.jetspeed.page.document.Node;
32  import org.apache.jetspeed.request.RequestContext;
33  
34  /***
35   * Update Folder action -- updates various parts of the PSML folder
36   * 
37   * AJAX Parameters: 
38   *    action = updatefolder
39   *    General methods:
40   *    method = add | remove 
41   *    Info methods:
42   *    | info 
43   *    Meta methods:
44   *    | add-meta | update-meta | remove-meta
45   *    Security methods:
46   *    | add-secref | remove-secref
47   *    
48   * @author <a href="mailto:taylor@apache.org">David Sean Taylor </a>
49   * @version $Id: $
50   */
51  public class UpdateFolderAction 
52      extends BaseSiteUpdateAction 
53      implements AjaxAction, AjaxBuilder, Constants
54  {
55      protected Log log = LogFactory.getLog(UpdateFolderAction.class);
56  
57      public UpdateFolderAction(String template, 
58                              String errorTemplate, 
59                              PageManager pm,
60                              PortletActionSecurityBehavior securityBehavior)
61                              
62      {
63          super(template, errorTemplate, pm, securityBehavior); 
64      }
65      
66      public boolean run(RequestContext requestContext, Map resultMap)
67              throws AJAXException
68      {
69          boolean success = true;
70          String status = "success";
71          try
72          {
73              resultMap.put(ACTION, "updatefolder");
74              // Get the necessary parameters off of the request
75              String method = getActionParameter(requestContext, "method");
76              if (method == null) 
77              { 
78                  throw new RuntimeException("Method not provided"); 
79              }            
80              resultMap.put("method", method);
81              if (false == checkAccess(requestContext, JetspeedActions.EDIT))
82              {
83                  success = false;
84                  resultMap.put(REASON, "Insufficient access to administer portal permissions");                
85                  return success;
86              }           
87              int count = 0;
88              String path = getActionParameter(requestContext, "path");
89              if (path == null)
90                  throw new AJAXException("Missing 'path' parameter");             
91              Folder folder = null; 
92              if (!method.equals("add"))
93              {
94                  folder = pageManager.getFolder(path);
95              }       
96              else
97              {
98                  if (pageManager.folderExists(path))
99                  {
100                     success = false;
101                     resultMap.put(REASON, "Can't create: Folder already exists: " + path);                
102                     return success;                
103                 }
104             }            
105             if (method.equals("info"))
106             {
107                 count = updateInformation(requestContext, resultMap, folder, path);
108             }
109             else if (method.equals("add-meta"))
110             {
111                 count = insertMetadata(requestContext, resultMap, folder);
112             }
113             else if ( method.equals("update-meta"))
114             {
115                 count = updateMetadata(requestContext, resultMap, folder);
116             }
117             else if (method.equals("remove-meta"))
118             {
119                 count = removeMetadata(requestContext, resultMap, folder);
120             }
121             else if (method.equals("update-secref"))
122             {
123                 count = updateSecurityReference(requestContext, resultMap, folder);
124             }            
125             else if (method.equals("add-secref"))
126             {
127                 count = insertSecurityReference(requestContext, resultMap, folder);
128             }
129             else if (method.equals("remove-secref"))
130             {
131                 count = removeSecurityReference(requestContext, resultMap, folder);
132             }
133             else if (method.equals("remove-secdef"))
134             {
135                 count = removeSecurityDef(requestContext, resultMap, folder);
136             }                        
137             else if (method.equals("add"))
138             {
139                 folder = pageManager.newFolder(path);
140                 folder.setTitle(getActionParameter(requestContext, "title"));
141                 String s = getActionParameter(requestContext, "short-title");
142                 if (!isBlank(s))
143                     folder.setShortTitle(s);
144                 count++;                
145             }
146             else if (method.equals("copy"))
147             {            	   
148             	String destination = getActionParameter(requestContext, "destination");
149             	String name = getActionParameter(requestContext, RESOURCE_NAME);
150             	destination = destination + Folder.PATH_SEPARATOR + name;
151             	pageManager.deepCopyFolder(folder,destination,null);
152             }
153             else if (method.equals("move"))
154             {            	
155             	String destination = getActionParameter(requestContext, "destination");
156             	String name = getActionParameter(requestContext, RESOURCE_NAME);            	
157             	destination = destination + Folder.PATH_SEPARATOR + name;
158             	pageManager.deepCopyFolder(folder,destination,null);            	
159             	pageManager.removeFolder(folder);
160             }            
161             else if (method.equals("remove"))
162             {
163                 pageManager.removeFolder(folder);
164             }                        
165             else
166             {
167                 success = false;
168                 resultMap.put(REASON, "Unsupported Site Update method: " + method);                
169                 return success;                
170             }
171             if (count > 0)
172             {
173                 pageManager.updateFolder(folder);                
174             }
175             resultMap.put("count", Integer.toString(count));
176             resultMap.put(STATUS, status);
177         } 
178         catch (Exception e)
179         {
180             log.error("exception administering Site update", e);
181             resultMap.put(REASON, e.toString());
182             success = false;
183         }
184         return success;
185     }
186     
187     protected int updateInformation(RequestContext requestContext, Map resultMap, Node node, String path)
188     throws AJAXException    
189     {
190         int count = 0;
191         try
192         {
193             Folder folder = (Folder)node;            
194             String title = getActionParameter(requestContext, "title");
195             if (isFieldModified(title, folder.getTitle()))
196                 folder.setTitle(title);
197             String shortTitle = getActionParameter(requestContext, "short-title");
198             if (isFieldModified(shortTitle, folder.getShortTitle()))
199                 folder.setShortTitle(shortTitle);
200             String layoutDecorator = getActionParameter(requestContext, "layout-decorator");
201             if (isFieldModified(layoutDecorator, folder.getDefaultDecorator(Fragment.LAYOUT)))
202             {
203                 if (isBlank(layoutDecorator))
204                     layoutDecorator = null;                 
205                 folder.setDefaultDecorator(layoutDecorator, Fragment.LAYOUT);
206             }
207             String portletDecorator = getActionParameter(requestContext, "portlet-decorator");
208             if (isFieldModified(portletDecorator, folder.getDefaultDecorator(Fragment.PORTLET)))
209             {
210                 if (isBlank(portletDecorator))
211                     portletDecorator = null;                 
212                 folder.setDefaultDecorator(portletDecorator, Fragment.PORTLET);
213             }
214             String defaultPage = getActionParameter(requestContext, "default-page");
215             if (isFieldModified(defaultPage, folder.getDefaultPage()))
216                 folder.setDefaultPage(defaultPage);                        
217             String hidden = getActionParameter(requestContext, "hidden");
218             if (isBooleanModified(hidden, folder.isHidden()))
219                 folder.setHidden(!folder.isHidden());                                    
220             count++;
221         }
222         catch (Exception e)
223         {
224             throw new AJAXException(e);
225         }        
226         return count;
227     }
228     
229 }