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.Link;
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 Link action -- updates various parts of the PSML link
36   * 
37   * AJAX Parameters: 
38   *    action = updatelink
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 UpdateLinkAction 
52      extends BaseSiteUpdateAction 
53      implements AjaxAction, AjaxBuilder, Constants
54  {
55      protected Log log = LogFactory.getLog(UpdateLinkAction.class);
56  
57      public UpdateLinkAction(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, "updatelink");
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              Link link = null; 
92              if (!method.equals("add"))
93              {
94                  link = pageManager.getLink(path);                
95              }                        
96              else
97              {
98                  if (pageManager.linkExists(path))
99                  {
100                     success = false;
101                     resultMap.put(REASON, "Can't create: Link already exists: " + path);                
102                     return success;                
103                 }
104             }
105             if (method.equals("info"))
106             {
107                 count = updateInformation(requestContext, resultMap, link, path);
108             }
109             else if (method.equals("update-meta"))
110             {
111                 count = updateMetadata(requestContext, resultMap, link);
112             }
113             else if (method.equals("add-meta"))
114             {
115                 count = insertMetadata(requestContext, resultMap, link);
116             }
117             else if (method.equals("remove-meta"))
118             {
119                 count = removeMetadata(requestContext, resultMap, link);
120             }
121             else if (method.equals("add-secref"))
122             {
123                 count = insertSecurityReference(requestContext, resultMap, link);
124             }
125             else if (method.equals("update-secref"))
126             {
127                 count = updateSecurityReference(requestContext, resultMap, link);
128             }                        
129             else if (method.equals("remove-secref"))
130             {
131                 count = removeSecurityReference(requestContext, resultMap, link);
132             }
133             else if (method.equals("remove-secdef"))
134             {
135                 count = removeSecurityDef(requestContext, resultMap, link);
136             }                        
137             else if (method.equals("add"))
138             {
139                 link = pageManager.newLink(path);
140                 link.setTitle(getActionParameter(requestContext, "title"));
141                 String s = getActionParameter(requestContext, "short-title");
142                 if (!isBlank(s))
143                     link.setShortTitle(s);
144                 link.setUrl(getActionParameter(requestContext, "url"));
145                 count++;
146             }
147             else if (method.equals("copy"))
148             {            	
149             	String destination = getActionParameter(requestContext, "destination");
150             	String name = getActionParameter(requestContext, RESOURCE_NAME);
151             	destination = destination + Folder.PATH_SEPARATOR + name;
152             	Link newLink = pageManager.copyLink(link, destination);
153             	pageManager.updateLink(newLink);
154             }
155             else if (method.equals("move"))
156             {            	
157             	String destination = getActionParameter(requestContext, "destination");
158             	String name = getActionParameter(requestContext, RESOURCE_NAME);            	
159             	destination = destination + Folder.PATH_SEPARATOR + name;
160             	Link newLink = pageManager.copyLink(link, destination);            	
161             	pageManager.updateLink(newLink);
162             	pageManager.removeLink(link);
163             	
164             } 
165             else if (method.equals("remove"))
166             {
167                 pageManager.removeLink(link);
168             }                        
169             else
170             {
171                 success = false;
172                 resultMap.put(REASON, "Unsupported Site Update method: " + method);                
173                 return success;                
174             }
175             if (count > 0)
176             {
177                 pageManager.updateLink(link);                
178             }            
179             resultMap.put("count", Integer.toString(count));
180             resultMap.put(STATUS, status);
181         } 
182         catch (Exception e)
183         {
184             log.error("exception administering Site update", e);
185             resultMap.put(REASON, e.toString());
186             success = false;
187         }
188         return success;
189     }
190     
191     protected int updateInformation(RequestContext requestContext, Map resultMap, Node node, String path)
192     throws AJAXException    
193     {
194         int count = 0;
195         try
196         {
197             Link link = (Link)node;            
198             String title = getActionParameter(requestContext, "title");
199             if (isFieldModified(title, link.getTitle()))
200                 link.setTitle(title);
201             String shortTitle = getActionParameter(requestContext, "short-title");
202             if (isFieldModified(shortTitle, link.getShortTitle()))
203                 link.setShortTitle(shortTitle);
204             String url = getActionParameter(requestContext, "url");
205             if (isFieldModified(url, link.getUrl()))
206                 link.setUrl(url);
207             String target = getActionParameter(requestContext, "target");
208             if (isFieldModified(target, link.getTarget()))
209                 link.setTarget(target);                        
210             String hidden = getActionParameter(requestContext, "hidden");
211             if (isBooleanModified(hidden, link.isHidden()))
212                 link.setHidden(!link.isHidden());                                    
213             count++;
214         }
215         catch (Exception e)
216         {
217             throw new AJAXException(e);
218         }        
219         return count;
220     }
221     
222 }