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.Iterator;
20  import java.util.List;
21  import java.util.Map;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
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.layout.PortletActionSecurityBehavior;
29  import org.apache.jetspeed.om.page.Fragment;
30  import org.apache.jetspeed.om.page.Page;
31  import org.apache.jetspeed.page.PageManager;
32  import org.apache.jetspeed.request.RequestContext;
33  import org.apache.jetspeed.security.UserManager;
34  
35  /***
36   * Abstract portlet placement action
37   *
38   * @author <a>David Gurney</a>
39   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
40   * @author <a href="mailto:mikko.wuokko@evtek.fi">Mikko Wuokko</a>
41   * @version $Id: $
42   */
43  public abstract class BaseUserAction 
44      implements AjaxAction, AjaxBuilder, Constants 
45  {
46      protected Log log = LogFactory.getLog(BaseUserAction.class);    
47  	protected String template = null;
48      protected UserManager userManager = null;
49      protected String errorTemplate = null;
50      protected RolesSecurityBehavior securityBehavior;
51      
52      public BaseUserAction(String template, 
53                               String errorTemplate, 
54                               RolesSecurityBehavior securityBehavior)
55      {
56          this.template = template;
57          this.errorTemplate = errorTemplate;
58          this.securityBehavior = securityBehavior;
59      }
60  
61      public BaseUserAction(String template, 
62              String errorTemplate, 
63              UserManager userManager)
64      {
65          this.template = template;
66          this.errorTemplate = errorTemplate;
67          this.userManager = userManager;
68          this.securityBehavior = null;
69      }
70      
71      public BaseUserAction(String template, 
72                               String errorTemplate, 
73                               UserManager userManager,
74                               RolesSecurityBehavior securityBehavior)
75      {
76          this(template, errorTemplate, securityBehavior);
77          this.userManager = userManager;
78      }
79  
80      public boolean buildContext(RequestContext requestContext, Map responseContext)
81      {
82          return true;
83      }
84  
85      public boolean buildErrorContext(RequestContext requestContext,
86              Map responseContext) 
87      {
88          responseContext.put(STATUS, "failure");
89  
90          // Check for the case where we don't know basic information
91          if (responseContext.get(ACTION) == null)
92          {
93              responseContext.put(ACTION, "unknown");
94          }
95  
96          if (responseContext.get(PORTLETID) == null)
97          {
98              responseContext.put(PORTLETID, "unknown");
99          }
100 
101         return true;
102     }
103 
104     public String getErrorTemplate()
105     {
106         return errorTemplate;
107     }
108 
109     public String getTemplate()
110     {
111         return template;
112     }
113 
114     public boolean checkAccess(RequestContext context, String action)
115     {
116         boolean access = true;
117         if (null != securityBehavior)
118         {
119             access = securityBehavior.checkAccess(context, action);
120         }
121         return access;
122     }
123 
124     public boolean createNewPageOnEdit(RequestContext context)
125     {
126         return securityBehavior.createNewPageOnEdit(context);        
127     }
128         
129     // TODO: support nested fragments
130     public Fragment getFragmentIdFromLocation(int row, int column, Page page)
131     {
132         Fragment root = page.getRootFragment();
133         Iterator fragments = root.getFragments().iterator();
134         while (fragments.hasNext())
135         {
136             Fragment fragment = (Fragment)fragments.next();
137             if (fragment.getLayoutColumn() == column &&
138                 fragment.getLayoutRow() == row)
139             {
140                 return fragment;
141             }
142         }
143         return null;
144     }
145     
146     public boolean runBatch(RequestContext requestContext, Map resultMap) throws AJAXException
147     {
148         return run(requestContext, resultMap);
149     }
150     
151     public String getActionParameter(RequestContext requestContext, String name)
152     {
153         String parameter = requestContext.getRequestParameter(name);
154         if (parameter == null)
155         {
156             Object o = requestContext.getAttribute(name);
157             if (o != null)
158             {
159                 if (o instanceof String)
160                     return (String)o;
161             }
162         }
163         return parameter;
164     }
165     
166     public Fragment getParentFragmentById(String id, Fragment root)
167     {
168         if ( id == null )
169         {
170             return null;
171         }
172         return searchForParentFragmentById( id, root );
173     }
174     
175     protected Fragment searchForParentFragmentById( String id, Fragment parent )
176     {   
177         // find fragment by id, tracking fragment parent
178         Fragment matchedParent = null;
179         if( parent != null ) 
180         {
181             // process the children
182             List children = parent.getFragments();
183             for( int i = 0, cSize = children.size() ; i < cSize ; i++) 
184             {
185                 Fragment childFrag = (Fragment)children.get( i );
186                 if ( childFrag != null ) 
187                 {
188                     if ( id.equals( childFrag.getId() ) )
189                     {
190                         matchedParent = parent;
191                         break;
192                     }
193                     else
194                     {
195                         matchedParent = searchForParentFragmentById( id, childFrag );
196                         if ( matchedParent != null )
197                         {
198                             break;
199                         }
200                     }
201                 }
202             }
203         }
204         return matchedParent;
205     }
206     
207        
208     /***
209      * Helper method to determine if a parameter is true. Prevents
210      * accidental NullPointerExceptions when comparing or or using
211      * the parameter value.
212      * @param parameter The value to be determined as boolean true or false.
213      * @return boolean true or false according to the @param value.
214      */
215     public boolean isTrue(String parameter)
216     {
217     	boolean isTrue = false;
218     	if(parameter != null)
219     	{
220     		if(parameter.equalsIgnoreCase("true"))
221     		{
222     			isTrue = true;
223     		}   			
224     	}
225     	return isTrue;
226     }
227     
228 }