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      
44      /***
45       * When Edit is clicked, also maximize the window state
46       * 
47       * @param editMaxOption
48       */
49      public AbstractDecoratorActionsFactory()
50      {
51      }
52      
53      public List getDecoratorActions(RequestContext rc, PortletApplication pa, PortletWindow pw, PortletMode pm,
54                      WindowState ws, Decoration decoration, List actionTemplates,PortletDefinitionComposite portlet, 
55                      ContentFragment fragment,SecurityAccessController accessController)
56      {
57          DecoratorAction action;
58          boolean checkConstraints=false;      
59          ArrayList actions = new ArrayList();
60          
61          Iterator iter = actionTemplates.iterator();
62          while (iter.hasNext())
63          {
64              checkConstraints = false;
65              DecoratorActionTemplate template = (DecoratorActionTemplate)iter.next();
66              //checking the constraints only on EDIT and HELP Action, as VIEW will taken care with portlet view.
67              if (template.getAction().equals(JetspeedActions.EDIT) || template.getAction().equals(JetspeedActions.HELP)) 
68                  checkConstraints = true; 
69              if (checkConstraints && checkSecurityConstraint(portlet,fragment,accessController,template.getAction()))
70              {
71                  action = createAction(rc, pw, decoration,template );
72                  if ( action != null)
73                  {
74                      actions.add(action);
75                  }
76              }
77              else if (!checkConstraints)
78              {
79                  action = createAction(rc, pw, decoration,template );
80                  if ( action != null)
81                  {
82                      actions.add(action);
83                  }
84              }            
85          }
86          return actions;
87      }
88      
89      public List getDecoratorActions(RequestContext rc, PortletApplication pa, PortletWindow pw, PortletMode pm,
90               WindowState ws, Decoration decoration, List actionTemplates)
91      {
92          DecoratorAction action;
93          ArrayList actions = new ArrayList();
94          Iterator iter = actionTemplates.iterator();
95          while (iter.hasNext())
96          {
97              action = createAction(rc, pw, decoration,(DecoratorActionTemplate)iter.next() );
98              if ( action != null)
99              {
100                 actions.add(action);
101             }
102         }
103         return actions;
104     }        
105 
106     protected DecoratorAction createAction(RequestContext rc, PortletWindow pw, Decoration decoration,
107             DecoratorActionTemplate template)
108     {
109         String actionName = template.getAction();
110 
111         PortalURL portalURL = rc.getPortalURL();
112         Boolean isAjaxRequest = (Boolean) rc
113                 .getAttribute(DecorationValve.IS_AJAX_DECORATION_REQUEST);
114 
115         WindowState ws;
116         PortletMode pm;
117         if (editMaximizesOption)
118         {
119             if (template.getAction().equals(JetspeedActions.EDIT))
120             {
121                 ws = WindowState.MAXIMIZED;
122                 pm = template.getCustomMode();
123             }
124             else if (template.getAction().equals(JetspeedActions.VIEW))
125             {
126                 ws = WindowState.NORMAL;
127                 pm = template.getCustomMode();                
128             }
129             else if (template.getAction().equals(JetspeedActions.NORMAL))
130             {
131                 pm = PortletMode.VIEW;   
132                 ws = template.getCustomState();                
133             }
134             else
135             {
136                 ws = template.getCustomState();
137                 pm = template.getCustomMode();
138             }
139         }
140         else
141         {
142             ws = template.getCustomState();
143             pm = template.getCustomMode();            
144         }
145         /////////////////////////////////////
146         
147         String actionURL = rc.getResponse().encodeURL(
148                 (isAjaxRequest == null) ? portalURL.createPortletURL(pw, pm, ws, portalURL.isSecure()).toString() 
149                         : portalURL.createNavigationalEncoding(pw, pm, ws));
150 
151         String linkURL = decoration
152                 .getResource("images/" + actionName + ".gif");
153 
154         boolean customAction = (template.getMode() != null && !template
155                 .getMode().equals(template.getCustomMode()))
156                 || (template.getState() != null && !template.getState().equals(
157                         template.getCustomState()));
158 
159         HashMap resourcesMap = (HashMap)actionResourcesMap.get();
160         ResourceBundle bundle = null;
161         String localizedName = null;
162         
163         if (resourcesMap == null)
164         {
165             resourcesMap = new HashMap();
166             actionResourcesMap.set(resourcesMap);
167             bundle = DecoratorAction.getResourceBundle(rc.getLocale());
168             resourcesMap.put(DecoratorAction.RESOURCE_BUNDLE, bundle);
169             localizedName = DecoratorAction.getResourceString(bundle, actionName, actionName);
170             resourcesMap.put(actionName,localizedName);
171         }
172         else
173         {
174             localizedName = (String)resourcesMap.get(actionName);
175             if (localizedName == null)
176             {
177                 localizedName = DecoratorAction.getResourceString(bundle, actionName, actionName);
178                 resourcesMap.put(actionName,localizedName);
179             }
180         }
181         return new DecoratorAction(actionName, localizedName, localizedName, linkURL, actionURL, customAction, template.getActionType());
182     }
183     
184     //added for checkin the constraints on actions
185     protected boolean checkSecurityConstraint(
186             PortletDefinitionComposite portlet, ContentFragment fragment,
187             SecurityAccessController accessController, String action)
188     {
189         if (fragment.getType().equals(ContentFragment.PORTLET))
190         {
191             if (accessController != null) 
192             { 
193                 return accessController
194                     .checkPortletAccess(portlet, JetspeedActions
195                             .getContainerActionMask(action)); 
196             }
197         }
198         return true;
199     }    
200     
201     public void setMaximizeOnEdit(boolean maxOnEdit)
202     {
203         this.editMaximizesOption = maxOnEdit;
204     }
205     
206     public boolean getMaximizeOnEdit()
207     {
208         return this.editMaximizesOption;
209     }
210     
211 }