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 at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * 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 and14 * limitations under the License.15 */1617packageorg.apache.jetspeed.modules.actions.controls;
1819// Turbine stuff20import org.apache.turbine.modules.Action;
21import org.apache.turbine.util.RunData;
2223// Jetspeed stuff24import org.apache.jetspeed.portal.Portlet;
25import org.apache.jetspeed.portal.PortletSet;
26import org.apache.jetspeed.portal.PortletControl;
27import org.apache.jetspeed.services.rundata.JetspeedRunData;
28import org.apache.jetspeed.om.profile.Profile;
29import org.apache.jetspeed.om.profile.ProfileLocator;
30import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
31import org.apache.jetspeed.services.logging.JetspeedLogger;
32import org.apache.jetspeed.services.Profiler;
33import org.apache.jetspeed.services.statemanager.SessionState;
34import org.apache.jetspeed.util.template.JetspeedLink;
35import org.apache.jetspeed.util.template.JetspeedLinkFactory;
36import org.apache.jetspeed.services.security.PortalResource;
37import org.apache.jetspeed.services.JetspeedSecurity;
38import org.apache.jetspeed.om.security.JetspeedUser;
3940import java.util.Enumeration;
41import java.util.Stack;
4243/***44 * Handle Customization requests for the current portal page45 *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 */50publicclassCustomizeextends Action
51 {
5253/***54 * Static initialization of the logger for this class55 */56privatestaticfinalJetspeedLogger logger = JetspeedLogFactoryService.getLogger(Customize.class.getName());
5758/***59 * @param rundata The RunData object for the current request60 */61publicvoid doPerform( RunData rundata ) throws Exception
62 {
63JetspeedRunData jdata = (JetspeedRunData)rundata;
6465if (jdata.getUser() == null)
66 {
67return;
68 }
69if(jdata.getProfile() == null)
70 {
71return;
72 }
7374// read some parameters75 String editMediaType = jdata.getParameters().getString ("mtype");
76 String resetStack = jdata.getParameters().getString ("reset");
77 String peid = jdata.getParameters().getString("js_peid");
7879// get the customization state for this page80SessionState customizationState = jdata.getPageSessionState();
8182// this will be the profile we are editing83Profile profile = null;
8485// the "reset" parameter's presence signals the start of customization86if ( (resetStack != null)
87 && ((resetStack.equalsIgnoreCase("on")) || (resetStack.equalsIgnoreCase("1"))))
88 {
89// clear out any prior customization state90 jdata.cleanupFromCustomization();
91 }
9293// if we have not yet setup for customization, do so now94if (jdata.getCustomizedProfile() == null)
95 {
96ProfileLocator locator = (ProfileLocator)jdata.getProfile().clone();
9798if (editMediaType != null)
99 {
100 locator.setMediaType(editMediaType);
101 }
102103// get a profile to edit104 profile = (Profile) Profiler.getProfile(locator).clone();
105 jdata.setCustomizedProfile(profile);
106 }
107108// we are continuing an on-going customization109else110 {
111// get the profile we are working on112 profile = jdata.getCustomizedProfile();
113 }
114115// Get js_peid parmameter.116// If it does not exist, we will customize the root of the profile117if ( peid == null )
118 {
119// use the id of the root set of the profile120 peid = profile.getRootSet().getID();
121 jdata.setJs_peid(peid);
122 }
123124// find the portlet within the profile with this peid %%% isn't there a better way to do this? -ggolden125Portlet found = null;
126 Stack sets = new Stack();
127 sets.push(profile.getRootSet());
128129while ((found==null) && (sets.size() > 0))
130 {
131PortletSet set = (PortletSet)sets.pop();
132133if (set.getID().equals(peid))
134 {
135 found = set;
136 }
137else138 {
139 Enumeration en = set.getPortlets();
140while((found==null) && en.hasMoreElements())
141 {
142Portlet p = (Portlet)en.nextElement();
143144// unstack the controls to find the real PortletSets145Portlet real = p;
146while (real instanceof PortletControl)
147 {
148 real = ((PortletControl)p).getPortlet();
149 }
150151if (real instanceof PortletSet)
152 {
153if (real.getID().equals(peid))
154 {
155 found=real;
156 }
157else158 {
159// we'll explore this set afterwards160 sets.push(real);
161 }
162 }
163elseif (p.getID().equals(peid))
164 {
165 found = p;
166 }
167 }
168 }
169 }
170171if (found!=null)
172 {
173PortalResource portalResource = newPortalResource(found);
174try175 {
176JetspeedLink jsLink = JetspeedLinkFactory.getInstance(rundata);
177 portalResource.setOwner(jsLink.getUserName());
178 JetspeedLinkFactory.putInstance(jsLink);
179 }
180catch (Exception e)
181 {
182 logger.warn(e.toString());
183 portalResource.setOwner(null);
184 }
185186if(!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");
192return;
193 }
194 jdata.setCustomized(found);
195 jdata.setScreenTemplate("Customize");
196 }
197198 }
199200/***201 * Save the current customization202 * Used by any other customizer to get this done right!203 *204 * @deprecated. The customizers should handle the save themselves205 */206publicstaticvoid save(RunData data)
207 {
208try209 {
210Profile profile = ((JetspeedRunData) data).getCustomizedProfile();
211 profile.store();
212 }
213catch (Exception e)
214 {
215 logger.error("Error while saving profile", e);
216 }
217218 } // save219220/***221 * Exit the customizer.222 * @deprecated. Exec the controls.EndCustomize action instead223 */224publicstaticvoid exit(RunData data)
225 {
226JetspeedLink jsLink = null;
227 ((JetspeedRunData) data).cleanupFromCustomization();
228229// bring logged on user to homepage via HTTP redirect230try231 {
232 jsLink = JetspeedLinkFactory.getInstance(data);
233 String mtype = data.getParameters().getString("mtype");
234if (mtype != null)
235 {
236 jsLink.setMediaType(mtype);
237 jsLink.addQueryData("mtype", mtype);
238 }
239240 }
241catch (Exception e)
242 {
243 logger.error("exit error", e);
244 }
245 data.setRedirectURI(jsLink.toString());
246 JetspeedLinkFactory.putInstance(jsLink);
247 jsLink = null;
248249 } // exit250 }
251