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.decoration;
18  
19  import java.util.ArrayList;
20  import java.util.HashMap;
21  import java.util.Iterator;
22  import java.util.List;
23  import java.util.Locale;
24  import java.util.MissingResourceException;
25  import java.util.ResourceBundle;
26  
27  import javax.portlet.PortletMode;
28  import javax.portlet.WindowState;
29  
30  import org.apache.jetspeed.JetspeedActions;
31  import org.apache.jetspeed.container.url.PortalURL;
32  import org.apache.jetspeed.om.common.portlet.PortletApplication;
33  import org.apache.jetspeed.om.common.portlet.PortletDefinitionComposite;
34  import org.apache.jetspeed.om.page.ContentFragment;
35  import org.apache.jetspeed.request.RequestContext;
36  import org.apache.jetspeed.security.SecurityAccessController;
37  import org.apache.pluto.om.window.PortletWindow;
38  
39  public abstract class AbstractDecoratorActionsFactory implements DecoratorActionsFactory
40  {
41      private static ThreadLocal actionResourcesMap = new ThreadLocal();
42      private boolean editMaximizesOption = false;
43      private boolean configMaximizesOption = false;
44      private boolean editDefaultsMaximizesOption = false;
45      
46      /***
47       * When Edit is clicked, also maximize the window state
48       * 
49       * @param editMaxOption
50       */
51      public AbstractDecoratorActionsFactory()
52      {
53      }
54      
55      public List getDecoratorActions(RequestContext rc, PortletApplication pa, PortletWindow pw, PortletMode pm,
56                      WindowState ws, Decoration decoration, List actionTemplates,PortletDefinitionComposite portlet, 
57                      ContentFragment fragment,SecurityAccessController accessController)
58      {
59          DecoratorAction action;
60          boolean checkConstraints=false;      
61          ArrayList actions = new ArrayList();
62          
63          Iterator iter = actionTemplates.iterator();
64          while (iter.hasNext())
65          {
66              checkConstraints = false;
67              DecoratorActionTemplate template = (DecoratorActionTemplate)iter.next();
68              //checking the constraints only on EDIT and HELP Action, as VIEW will taken care with portlet view.
69              if (template.getAction().equals(JetspeedActions.EDIT) || template.getAction().equals(JetspeedActions.HELP)) 
70                  checkConstraints = true; 
71              if (checkConstraints && checkSecurityConstraint(portlet,fragment,accessController,template.getAction()))
72              {
73                  action = createAction(rc, pw, decoration,template );
74                  if ( action != null)
75                  {
76                      actions.add(action);
77                  }
78              }
79              else if (!checkConstraints)
80              {
81                  action = createAction(rc, pw, decoration,template );
82                  if ( action != null)
83                  {
84                      actions.add(action);
85                  }
86              }            
87          }
88          return actions;
89      }
90      
91      public List getDecoratorActions(RequestContext rc, PortletApplication pa, PortletWindow pw, PortletMode pm,
92               WindowState ws, Decoration decoration, List actionTemplates)
93      {
94          DecoratorAction action;
95          ArrayList actions = new ArrayList();
96          Iterator iter = actionTemplates.iterator();
97          while (iter.hasNext())
98          {
99              action = createAction(rc, pw, decoration,(DecoratorActionTemplate)iter.next() );
100             if ( action != null)
101             {
102                 actions.add(action);
103             }
104         }
105         return actions;
106     }        
107 
108     protected DecoratorAction createAction(RequestContext rc, PortletWindow pw, Decoration decoration,
109             DecoratorActionTemplate template)
110     {
111         String actionName = template.getAction();
112 
113         PortalURL portalURL = rc.getPortalURL();
114         Boolean isAjaxRequest = (Boolean) rc
115                 .getAttribute(DecorationValve.IS_AJAX_DECORATION_REQUEST);
116 
117         WindowState ws;
118         PortletMode pm;
119         if (editMaximizesOption || configMaximizesOption || editDefaultsMaximizesOption)
120         {
121             if (editMaximizesOption && template.getAction().equals(JetspeedActions.EDIT))
122             {
123                 ws = WindowState.MAXIMIZED;
124                 pm = template.getCustomMode();
125             }
126             else if (configMaximizesOption && template.getAction().equals(JetspeedActions.CONFIG))
127             {
128                 ws = WindowState.MAXIMIZED;
129                 pm = template.getCustomMode();
130             }
131             else if (editDefaultsMaximizesOption && template.getAction().equals(JetspeedActions.EDIT_DEFAULTS))
132             {
133                 ws = WindowState.MAXIMIZED;
134                 pm = template.getCustomMode();
135             }
136             else if (template.getAction().equals(JetspeedActions.VIEW))
137             {
138                 ws = WindowState.NORMAL;
139                 pm = template.getCustomMode();                
140             }
141             else if (template.getAction().equals(JetspeedActions.NORMAL))
142             {
143                 pm = PortletMode.VIEW;   
144                 ws = template.getCustomState();                
145             }
146             else
147             {
148                 ws = template.getCustomState();
149                 pm = template.getCustomMode();
150             }
151         }
152         else
153         {
154             ws = template.getCustomState();
155             pm = template.getCustomMode();            
156         }
157         /////////////////////////////////////
158         
159         String actionURL = rc.getResponse().encodeURL(
160                 (isAjaxRequest == null) ? portalURL.createPortletURL(pw, pm, ws, portalURL.isSecure()).toString() 
161                         : portalURL.createNavigationalEncoding(pw, pm, ws));
162 
163         String linkURL = decoration
164                 .getResource("images/" + actionName + ".gif");
165 
166         boolean customAction = (template.getMode() != null && !template
167                 .getMode().equals(template.getCustomMode()))
168                 || (template.getState() != null && !template.getState().equals(
169                         template.getCustomState()));
170 
171         HashMap resourcesMap = (HashMap)actionResourcesMap.get();
172         ResourceBundle bundle = DecoratorAction.getResourceBundle(rc.getLocale());
173         String localizedName = null;
174         
175         if (resourcesMap == null)
176         {
177             resourcesMap = new HashMap();
178             actionResourcesMap.set(resourcesMap);
179             resourcesMap.put(DecoratorAction.RESOURCE_BUNDLE, bundle);
180             localizedName = DecoratorAction.getResourceString(bundle, actionName, actionName);
181             resourcesMap.put(actionName,localizedName);
182         }
183         else
184         {
185             localizedName = (String)resourcesMap.get(actionName);
186             if (localizedName == null)
187             {
188                 localizedName = DecoratorAction.getResourceString(bundle, actionName, actionName);
189                 resourcesMap.put(actionName,localizedName);
190             }
191         }
192         return new DecoratorAction(actionName, localizedName, localizedName, linkURL, actionURL, customAction, template.getActionType());
193     }
194     
195     //added for checkin the constraints on actions
196     protected boolean checkSecurityConstraint(
197             PortletDefinitionComposite portlet, ContentFragment fragment,
198             SecurityAccessController accessController, String action)
199     {
200         if (fragment.getType().equals(ContentFragment.PORTLET))
201         {
202             if (accessController != null) 
203             { 
204                 return accessController
205                     .checkPortletAccess(portlet, JetspeedActions
206                             .getContainerActionMask(action)); 
207             }
208         }
209         return true;
210     }    
211     
212     public void setMaximizeOnEdit(boolean maxOnEdit)
213     {
214         this.editMaximizesOption = maxOnEdit;
215     }
216     
217     public boolean getMaximizeOnEdit()
218     {
219         return this.editMaximizesOption;
220     }
221     
222     public void setMaximizeOnConfig(boolean maxOnConfig)
223     {
224         this.configMaximizesOption = maxOnConfig;
225     }
226     
227     public boolean getMaximizeOnConfig()
228     {
229         return this.configMaximizesOption;
230     }
231     
232     public void setMaximizeOnEditDefaults(boolean maxOnEditDefaults)
233     {
234         this.editDefaultsMaximizesOption = maxOnEditDefaults;
235     }
236     
237     public boolean getMaximizeOnEditDefaults()
238     {
239         return this.editDefaultsMaximizesOption;
240     }
241 }