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  
34  /***
35   * Abstract portlet placement action
36   *
37   * @author <a>David Gurney</a>
38   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
39   * @version $Id: $
40   */
41  public abstract class BasePortletAction 
42      implements AjaxAction, AjaxBuilder, Constants 
43  {
44      protected static final Log log = LogFactory.getLog(BasePortletAction.class);    
45  	protected String template = null;
46      protected PageManager pageManager = null;
47      protected String errorTemplate = null;
48      protected PortletActionSecurityBehavior securityBehavior;
49      
50      public BasePortletAction(String template, 
51                               String errorTemplate, 
52                               PortletActionSecurityBehavior securityBehavior)
53      {
54          this.template = template;
55          this.errorTemplate = errorTemplate;
56          this.securityBehavior = securityBehavior;
57      }
58  
59      public BasePortletAction(String template, 
60              String errorTemplate, 
61              PageManager pageManager)
62      {
63          this.template = template;
64          this.errorTemplate = errorTemplate;
65          this.pageManager = pageManager;
66          this.securityBehavior = null;
67      }
68      
69      public BasePortletAction(String template, 
70                               String errorTemplate, 
71                               PageManager pageManager,
72                               PortletActionSecurityBehavior securityBehavior)
73      {
74          this(template, errorTemplate, securityBehavior);
75          this.pageManager = pageManager;
76      }
77  
78      public boolean buildContext(RequestContext requestContext, Map responseContext)
79      {
80          return true;
81      }
82  
83      public boolean buildErrorContext(RequestContext requestContext,
84              Map responseContext) 
85      {
86          responseContext.put(STATUS, "failure");
87  
88          // Check for the case where we don't know basic information
89          if (responseContext.get(ACTION) == null)
90          {
91              responseContext.put(ACTION, "unknown");
92          }
93  
94          if (responseContext.get(PORTLETID) == null)
95          {
96              responseContext.put(PORTLETID, "unknown");
97          }
98  
99          return true;
100     }
101 
102     public String getErrorTemplate()
103     {
104         return errorTemplate;
105     }
106 
107     public String getTemplate()
108     {
109         return template;
110     }
111 
112     public boolean checkAccess(RequestContext context, String action)
113     {
114         boolean access = true;
115         if (null != securityBehavior)
116         {
117             access = securityBehavior.checkAccess(context, action);
118         }
119         return access;
120     }
121     
122     public boolean isCreateNewPageOnEditEnabled()
123     {
124     	if (securityBehavior == null)
125             return false;
126     	return securityBehavior.isCreateNewPageOnEditEnabled();
127     }
128     public boolean isPageQualifiedForCreateNewPageOnEdit(RequestContext context)
129     {
130     	if (securityBehavior == null)
131             return false;
132     	return securityBehavior.isPageQualifiedForCreateNewPageOnEdit(context);
133     }
134     public boolean createNewPageOnEdit(RequestContext context)
135     {
136         if (securityBehavior == null)
137             return false;
138         
139         return securityBehavior.createNewPageOnEdit(context);        
140     }
141     
142     public Fragment getFragmentIdFromLocation( int row, int column, Page page )
143     {
144     	return getFragmentIdFromLocation( row, column, page.getRootFragment() );
145     }
146     public Fragment getFragmentIdFromLocation( int row, int column, Fragment parentFragment )
147     {
148         Iterator fragments = parentFragment.getFragments().iterator();
149         while ( fragments.hasNext() )
150         {
151             Fragment fragment = (Fragment)fragments.next();
152             if ( fragment.getLayoutColumn() == column && fragment.getLayoutRow() == row )
153             {
154                 return fragment;
155             }
156         }
157         return null;
158     }
159     
160     public boolean runBatch(RequestContext requestContext, Map resultMap) throws AJAXException
161     {
162         return run(requestContext, resultMap);
163     }
164     
165     public String getActionParameter(RequestContext requestContext, String name)
166     {
167         String parameter = requestContext.getRequestParameter(name);
168         if (parameter == null)
169         {
170             Object o = requestContext.getAttribute(name);
171             if (o != null)
172             {
173                 if (o instanceof String)
174                     return (String)o;
175             }
176         }
177         return parameter;
178     }
179     
180     public String getNonNullActionParameter(RequestContext requestContext, String name)
181     {
182         String result = getActionParameter(requestContext, name);
183         if (result == null)
184             return "";
185         return result;
186     }
187     
188     public Fragment getParentFragmentById(String id, Fragment root)
189     {
190     	return NestedFragmentContext.getParentFragmentById( id, root );
191     }    
192 }