View Javadoc

1   /*
2    * Copyright 2000-2001,2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.jetspeed.modules.actions.controls;
18  
19  // Turbine stuff
20  import org.apache.turbine.modules.Action;
21  import org.apache.turbine.util.RunData;
22  
23  // Jetspeed stuff
24  import org.apache.jetspeed.portal.Portlet;
25  import org.apache.jetspeed.portal.PortletSet;
26  import org.apache.jetspeed.portal.PortletControl;
27  import org.apache.jetspeed.services.rundata.JetspeedRunData;
28  import org.apache.jetspeed.om.profile.Profile;
29  import org.apache.jetspeed.om.profile.ProfileLocator;
30  import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
31  import org.apache.jetspeed.services.logging.JetspeedLogger;
32  import org.apache.jetspeed.services.Profiler;
33  import org.apache.jetspeed.services.statemanager.SessionState;
34  import org.apache.jetspeed.util.template.JetspeedLink;
35  import org.apache.jetspeed.util.template.JetspeedLinkFactory;
36  import org.apache.jetspeed.services.security.PortalResource;
37  import org.apache.jetspeed.services.JetspeedSecurity;
38  import org.apache.jetspeed.om.security.JetspeedUser;
39  
40  import java.util.Enumeration;
41  import java.util.Stack;
42  
43  /***
44   * Handle Customization requests for the current portal page
45   *
46   * @author <a href="mailto:raphael@apache.org">Raphaël Luta</a>
47   * @author <a href="mailto:paulsp@apache.org">Paul Spencer</a>
48   * @author <a href="mailto:ggolden@apache.org">Glenn R. Golden</a>
49   */
50  public class Customize extends Action
51  {
52      
53      /***
54       * Static initialization of the logger for this class
55       */
56      private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(Customize.class.getName());
57      
58      /***
59       * @param rundata The RunData object for the current request
60       */
61      public void doPerform( RunData rundata ) throws Exception
62      {
63          JetspeedRunData jdata = (JetspeedRunData)rundata;
64  
65          if (jdata.getUser() == null)
66          {
67              return;
68          }
69          if(jdata.getProfile() == null)
70          {
71              return;
72          }
73  
74          // read some parameters
75          String editMediaType = jdata.getParameters().getString ("mtype");
76          String resetStack = jdata.getParameters().getString ("reset");
77          String peid = jdata.getParameters().getString("js_peid");
78  
79          // get the customization state for this page
80          SessionState customizationState = jdata.getPageSessionState();
81  
82          // this will be the profile we are editing
83          Profile profile = null;
84  
85          // the "reset" parameter's presence signals the start of customization
86          if (    (resetStack != null)
87              &&  ((resetStack.equalsIgnoreCase("on")) || (resetStack.equalsIgnoreCase("1"))))
88          {
89              // clear out any prior customization state
90              jdata.cleanupFromCustomization();
91          }
92  
93          // if we have not yet setup for customization, do so now
94          if (jdata.getCustomizedProfile() == null)
95          {
96              ProfileLocator locator = (ProfileLocator)jdata.getProfile().clone();
97  
98              if (editMediaType != null)
99              {
100                 locator.setMediaType(editMediaType);
101             }
102 
103             // get a profile to edit
104             profile = (Profile) Profiler.getProfile(locator).clone();
105             jdata.setCustomizedProfile(profile);
106         }
107 
108         // we are continuing an on-going customization
109         else
110         {
111             // get the profile we are working on
112             profile = jdata.getCustomizedProfile();
113         }
114 
115         // Get js_peid parmameter.
116         // If it does not exist, we will customize the root of the profile
117         if ( peid == null )
118         {
119             // use the id of the root set of the profile
120             peid = profile.getRootSet().getID();
121             jdata.setJs_peid(peid);
122         }
123 
124         // find the portlet within the profile with this peid %%% isn't there a better way to do this? -ggolden
125         Portlet found = null;
126         Stack sets = new Stack();
127         sets.push(profile.getRootSet());
128 
129         while ((found==null) && (sets.size() > 0))
130         {
131             PortletSet set = (PortletSet)sets.pop();
132 
133             if (set.getID().equals(peid))
134             {
135                 found = set;
136             }
137             else
138             {
139                 Enumeration en = set.getPortlets();
140                 while((found==null) && en.hasMoreElements())
141                 {
142                     Portlet p = (Portlet)en.nextElement();
143 
144                     // unstack the controls to find the real PortletSets
145                     Portlet real = p;
146                     while (real instanceof PortletControl)
147                     {
148                         real = ((PortletControl)p).getPortlet();
149                     }
150 
151                     if (real instanceof PortletSet)
152                     {
153                         if (real.getID().equals(peid))
154                         {
155                             found=real;
156                         }
157                         else
158                         {
159                             // we'll explore this set afterwards
160                             sets.push(real);
161                         }
162                     }
163                     else if (p.getID().equals(peid))
164                     {
165                         found = p;
166                     }
167                 }
168             }
169         }
170 
171         if (found!=null)
172         {
173             PortalResource portalResource = new PortalResource(found);
174             try
175             {
176                 JetspeedLink jsLink = JetspeedLinkFactory.getInstance(rundata);
177                 portalResource.setOwner(jsLink.getUserName());
178                 JetspeedLinkFactory.putInstance(jsLink);
179             }
180             catch (Exception e)
181             {
182                 logger.warn(e.toString());
183                 portalResource.setOwner(null);
184             }
185 
186             if(!JetspeedSecurity.checkPermission((JetspeedUser) jdata.getUser(), 
187                                                  portalResource, 
188                                                  JetspeedSecurity.PERMISSION_CUSTOMIZE))
189             {
190                 logger.warn("User " + jdata.getUser().getUserName() + " has no customize permission for portlet with id " + peid);
191                 jdata.setMessage("Sorry, you have no customize permission for this portlet");
192                 return;
193             }
194             jdata.setCustomized(found);
195             jdata.setScreenTemplate("Customize");
196         }
197 
198     }
199 
200     /***
201     * Save the current customization
202     * Used by any other customizer to get this done right!
203     *
204     * @deprecated. The customizers should handle the save themselves
205     */
206     public static void save(RunData data)
207     {
208         try
209         {
210             Profile profile = ((JetspeedRunData) data).getCustomizedProfile();
211             profile.store();
212         }
213         catch (Exception e)
214         {
215             logger.error("Error while saving profile", e);
216         }
217 
218     }   // save
219 
220     /***
221     * Exit the customizer.
222     * @deprecated. Exec the controls.EndCustomize action instead
223     */
224     public static void exit(RunData data)
225     {
226         JetspeedLink jsLink = null;
227         ((JetspeedRunData) data).cleanupFromCustomization();
228 
229         // bring logged on user to homepage via HTTP redirect
230         try
231         {
232             jsLink = JetspeedLinkFactory.getInstance(data);
233             String mtype = data.getParameters().getString("mtype");
234             if (mtype != null)
235             {
236                 jsLink.setMediaType(mtype);
237                 jsLink.addQueryData("mtype", mtype);
238             }
239 
240         }
241         catch (Exception e)
242         {
243             logger.error("exit error", e);
244         }
245         data.setRedirectURI(jsLink.toString());
246         JetspeedLinkFactory.putInstance(jsLink);
247         jsLink = null;
248 
249     }   // exit
250 }
251