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.Collection;
20  import java.util.Iterator;
21  import java.util.List;
22  import java.util.Locale;
23  import java.util.Map;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.apache.jetspeed.ajax.AJAXException;
28  import org.apache.jetspeed.ajax.AjaxAction;
29  import org.apache.jetspeed.ajax.AjaxBuilder;
30  import org.apache.jetspeed.layout.PortletActionSecurityBehavior;
31  import org.apache.jetspeed.om.common.LocalizedField;
32  import org.apache.jetspeed.om.common.SecurityConstraints;
33  import org.apache.jetspeed.page.PageManager;
34  import org.apache.jetspeed.page.document.Node;
35  import org.apache.jetspeed.request.RequestContext;
36  
37  /***
38   * Abstract Site update action for folders, pages and links
39   *
40   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
41   * @version $Id: $
42   */
43  public abstract class BaseSiteUpdateAction
44      extends BasePortletAction
45      implements AjaxAction, AjaxBuilder, Constants 
46  {
47      protected static final Log log = LogFactory.getLog(BaseSiteUpdateAction.class);    
48      
49      public BaseSiteUpdateAction(String template, 
50              String errorTemplate, 
51              PageManager pageManager)
52      {
53          super(template, errorTemplate, pageManager);
54      }
55      
56      public BaseSiteUpdateAction(String template, 
57                               String errorTemplate, 
58                               PortletActionSecurityBehavior securityBehavior)
59      {
60          super(template, errorTemplate, securityBehavior);
61      }
62  
63      public BaseSiteUpdateAction(String template, 
64                               String errorTemplate, 
65                               PageManager pageManager,
66                               PortletActionSecurityBehavior securityBehavior)
67      {
68          super(template, errorTemplate, pageManager, securityBehavior);        
69      }
70  
71      protected abstract int updateInformation(RequestContext requestContext, Map resultMap, Node node, String path)
72      throws AJAXException;
73      
74      protected int insertMetadata(RequestContext requestContext, Map resultMap, Node node)
75      throws AJAXException
76      {
77          String name = getActionParameter(requestContext, "name");
78          String language = getActionParameter(requestContext, "lang");
79          String value = getActionParameter(requestContext, "value");
80          if (isBlank(name) || isBlank(language))
81              throw new AJAXException("Invalid Metadata: name, language invalid data.");
82          Locale locale = new Locale(language);
83          node.getMetadata().addField(locale, name, value);        
84          return 1;
85      }
86  
87      protected int updateMetadata(RequestContext requestContext, Map resultMap, Node node)
88      throws AJAXException
89      {
90          String name = getActionParameter(requestContext, "name");
91          String language = getActionParameter(requestContext, "lang");
92          String value = getActionParameter(requestContext, "value");
93          String oldName = getActionParameter(requestContext, "oldname");
94          String oldLanguage = getActionParameter(requestContext, "oldlang");
95  
96          if (isBlank(name) || isBlank(language) || isBlank(oldName) || isBlank(oldLanguage))
97              throw new AJAXException("Invalid Metadata: name, language invalid data.");
98                  
99          Collection cfields = node.getMetadata().getFields(oldName);
100         if (cfields == null || cfields.size() == 0)
101         {
102             return insertMetadata(requestContext, resultMap, node);            
103         }
104         boolean found = false;
105         Iterator fields = cfields.iterator();
106         while (fields.hasNext())
107         {
108             LocalizedField field  = (LocalizedField)fields.next();
109             if (areFieldsSame(field.getName(), oldName) &&
110                 areFieldsSame(field.getLocale().toString(), oldLanguage))
111             {
112                 field.setName(name);
113                 field.setLocale(new Locale(language));
114                 field.setValue(value);
115                 found = true;
116                 break;
117             }
118         }
119         if (!found)
120             return insertMetadata(requestContext, resultMap, node);
121         return 1;
122     }
123     
124     protected int removeMetadata(RequestContext requestContext, Map resultMap, Node node)
125     throws AJAXException
126     {
127         String name = getActionParameter(requestContext, "name");
128         String language = getActionParameter(requestContext, "lang");
129         if (isBlank(name) || isBlank(language))
130             throw new AJAXException("Invalid Metadata: name, language invalid data.");
131         Collection cfields = node.getMetadata().getFields(name);
132         Collection allFields = node.getMetadata().getFields();
133         if (cfields == null || cfields.size() == 0)
134         {
135             return 0;            
136         }
137         boolean found = false;        
138         Iterator fields = cfields.iterator();
139         while (fields.hasNext())
140         {
141             LocalizedField field  = (LocalizedField)fields.next();
142             if (areFieldsSame(field.getName(), name) &&
143                 areFieldsSame(field.getLocale().toString(), language))
144             {
145                 cfields.remove(field);
146                 if (allFields.remove(field))
147                 {
148                     node.getMetadata().setFields(allFields);
149                 }
150                 found = true;
151                 break;
152             }
153         }    
154         
155         return (found) ? 1 : 0;
156     }
157 
158     protected int insertSecurityReference(RequestContext requestContext, Map resultMap, Node node)
159     throws AJAXException
160     {
161         String name = getActionParameter(requestContext, "name");
162         String kind = getActionParameter(requestContext, "kind");
163         if (isBlank(name) || isBlank(kind))
164             throw new AJAXException("Invalid Security Ref: name invalid data.");
165         if (node.getSecurityConstraints() == null)
166         {
167             SecurityConstraints cons = node.newSecurityConstraints();
168             node.setSecurityConstraints(cons);             
169         }
170         if (kind.equals("Owner"))
171         {
172             node.getSecurityConstraints().setOwner(name);
173         }
174         else
175         {
176             List refs = node.getSecurityConstraints().getSecurityConstraintsRefs();
177             if (refs.contains(name))
178                 return 0; // do nothing
179             refs.add(name);
180         }
181         return 1;        
182     }
183 
184     protected int updateSecurityReference(RequestContext requestContext, Map resultMap, Node node)
185     throws AJAXException
186     {
187         String name = getActionParameter(requestContext, "name");
188         String oldName = getActionParameter(requestContext, "oldname");
189         String kind = getActionParameter(requestContext, "kind");
190         if (isBlank(name) || isBlank(oldName) || isBlank(kind))
191             throw new AJAXException("Invalid Security Ref: name invalid data.");
192         if (node.getSecurityConstraints() == null)
193         {
194             SecurityConstraints cons = node.newSecurityConstraints();
195             node.setSecurityConstraints(cons);             
196         }                
197         List refs = node.getSecurityConstraints().getSecurityConstraintsRefs();        
198         if (refs == null || refs.size() == 0)
199         {
200             return insertSecurityReference(requestContext, resultMap, node);            
201         }
202         boolean found = false;
203         if (kind.equals("Owner"))
204         {
205             node.getSecurityConstraints().setOwner(name);
206             found = true;
207         }
208         else
209         {            
210             for (int ix = 0; ix < refs.size(); ix++)
211             {
212                 String ref = (String)refs.get(ix);  
213                 if (areFieldsSame(ref, oldName))
214                 {
215                     refs.set(ix, name);
216                     found = true;
217                     break;
218                 }
219             }
220         }
221         if (!found)
222             return insertSecurityReference(requestContext, resultMap, node);
223         return 1;
224     }
225     
226     protected int removeSecurityReference(RequestContext requestContext, Map resultMap, Node node)
227     throws AJAXException
228     {
229         String name = getActionParameter(requestContext, "name");
230         String kind = getActionParameter(requestContext, "kind");
231         if (isBlank(name) || isBlank(kind))
232             throw new AJAXException("Invalid Security Ref: name invalid data.");
233         if (node.getSecurityConstraints() == null)
234         {
235             return 0;
236         }
237         if (kind.equals("Owner"))
238         {
239             node.getSecurityConstraints().setOwner(null);
240         }
241         else
242         {
243             List refs = node.getSecurityConstraints().getSecurityConstraintsRefs();
244             if (!refs.contains(name))
245                 return 0; // nothing to do
246             refs.remove(name);
247         }
248         return 1;
249     }
250 
251     protected int removeSecurityDef(RequestContext requestContext, Map resultMap, Node node)
252     throws AJAXException
253     {
254         String id = getActionParameter(requestContext, "id");
255         if (isBlank(id))
256             throw new AJAXException("Invalid Security Ref: id invalid data.");
257         if (node.getSecurityConstraints() == null)
258         {
259             return 0;
260         }
261         List defs = node.getSecurityConstraints().getSecurityConstraints();
262         if (defs == null || defs.size() == 0)
263         {
264             return 0;
265         }
266         if (id.length() == 1)
267             return 0;
268         id = id.substring(1);
269         int index = Integer.parseInt(id) - 1;
270         if (index < 0)
271         {
272             return 0;
273         }
274         defs.remove(index);
275         return 1;
276     }
277     
278     protected boolean isBlank(String field)
279     {
280         if (field == null || field.trim().length() == 0)
281             return true;
282         return false;
283     }
284     protected boolean isFieldModified(String paramValue, String prevValue)
285     {
286         if (paramValue == null)
287         {
288             if (prevValue == null)
289                 return false;
290             else
291                 return true;
292         }
293         else
294         {
295             if (prevValue == null)
296                 return true;
297             if (prevValue.equals(paramValue))
298                 return false;
299             else
300                 return true;
301         }
302     }
303     protected boolean areFieldsSame(String f1, String f2)
304     {
305         return !isFieldModified(f1, f2);
306     }
307     protected boolean isBooleanModified(String paramValue, boolean prevValue)
308     {
309         if (paramValue == null)
310         {
311             if (prevValue == false)
312                 return false;
313             else
314                 return true;
315         }
316         else
317         {
318             if (prevValue == false)
319                 return true;
320             else
321                 return false;
322         }
323     }        
324 }