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 createNewPageOnEdit(RequestContext context)
123     {
124         if (securityBehavior == null)
125             return false;
126         
127         return securityBehavior.createNewPageOnEdit(context);        
128     }
129         
130     // TODO: support nested fragments
131     public Fragment getFragmentIdFromLocation(int row, int column, Page page)
132     {
133         Fragment root = page.getRootFragment();
134         Iterator fragments = root.getFragments().iterator();
135         while (fragments.hasNext())
136         {
137             Fragment fragment = (Fragment)fragments.next();
138             if (fragment.getLayoutColumn() == column &&
139                 fragment.getLayoutRow() == row)
140             {
141                 return fragment;
142             }
143         }
144         return null;
145     }
146     
147     public boolean runBatch(RequestContext requestContext, Map resultMap) throws AJAXException
148     {
149         return run(requestContext, resultMap);
150     }
151     
152     public String getActionParameter(RequestContext requestContext, String name)
153     {
154         String parameter = requestContext.getRequestParameter(name);
155         if (parameter == null)
156         {
157             Object o = requestContext.getAttribute(name);
158             if (o != null)
159             {
160                 if (o instanceof String)
161                     return (String)o;
162             }
163         }
164         return parameter;
165     }
166     
167     public Fragment getParentFragmentById(String id, Fragment root)
168     {
169         if ( id == null )
170         {
171             return null;
172         }
173         return searchForParentFragmentById( id, root );
174     }
175     
176     protected Fragment searchForParentFragmentById( String id, Fragment parent )
177     {   
178         // find fragment by id, tracking fragment parent
179         Fragment matchedParent = null;
180         if( parent != null ) 
181         {
182             // process the children
183             List children = parent.getFragments();
184             for( int i = 0, cSize = children.size() ; i < cSize ; i++) 
185             {
186                 Fragment childFrag = (Fragment)children.get( i );
187                 if ( childFrag != null ) 
188                 {
189                     if ( id.equals( childFrag.getId() ) )
190                     {
191                         matchedParent = parent;
192                         break;
193                     }
194                     else
195                     {
196                         matchedParent = searchForParentFragmentById( id, childFrag );
197                         if ( matchedParent != null )
198                         {
199                             break;
200                         }
201                     }
202                 }
203             }
204         }
205         return matchedParent;
206     }
207     
208 }