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     	if (securityBehavior == null)
127             return false;
128     	
129         return securityBehavior.createNewPageOnEdit(context);        
130     }
131         
132     // TODO: support nested fragments
133     public Fragment getFragmentIdFromLocation(int row, int column, Page page)
134     {
135         Fragment root = page.getRootFragment();
136         Iterator fragments = root.getFragments().iterator();
137         while (fragments.hasNext())
138         {
139             Fragment fragment = (Fragment)fragments.next();
140             if (fragment.getLayoutColumn() == column &&
141                 fragment.getLayoutRow() == row)
142             {
143                 return fragment;
144             }
145         }
146         return null;
147     }
148     
149     public boolean runBatch(RequestContext requestContext, Map resultMap) throws AJAXException
150     {
151         return run(requestContext, resultMap);
152     }
153     
154     public String getActionParameter(RequestContext requestContext, String name)
155     {
156         String parameter = requestContext.getRequestParameter(name);
157         if (parameter == null)
158         {
159             Object o = requestContext.getAttribute(name);
160             if (o != null)
161             {
162                 if (o instanceof String)
163                     return (String)o;
164             }
165         }
166         return parameter;
167     }
168     
169     public Fragment getParentFragmentById(String id, Fragment root)
170     {
171         if ( id == null )
172         {
173             return null;
174         }
175         return searchForParentFragmentById( id, root );
176     }
177     
178     protected Fragment searchForParentFragmentById( String id, Fragment parent )
179     {   
180         // find fragment by id, tracking fragment parent
181         Fragment matchedParent = null;
182         if( parent != null ) 
183         {
184             // process the children
185             List children = parent.getFragments();
186             for( int i = 0, cSize = children.size() ; i < cSize ; i++) 
187             {
188                 Fragment childFrag = (Fragment)children.get( i );
189                 if ( childFrag != null ) 
190                 {
191                     if ( id.equals( childFrag.getId() ) )
192                     {
193                         matchedParent = parent;
194                         break;
195                     }
196                     else
197                     {
198                         matchedParent = searchForParentFragmentById( id, childFrag );
199                         if ( matchedParent != null )
200                         {
201                             break;
202                         }
203                     }
204                 }
205             }
206         }
207         return matchedParent;
208     }
209     
210        
211     /***
212      * Helper method to determine if a parameter is true. Prevents
213      * accidental NullPointerExceptions when comparing or or using
214      * the parameter value.
215      * @param parameter The value to be determined as boolean true or false.
216      * @return boolean true or false according to the @param value.
217      */
218     public boolean isTrue(String parameter)
219     {
220     	boolean isTrue = false;
221     	if(parameter != null)
222     	{
223     		if(parameter.equalsIgnoreCase("true"))
224     		{
225     			isTrue = true;
226     		}   			
227     	}
228     	return isTrue;
229     }
230     
231 }