KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > modules > actions > portlets > PortletUpdateAction


1 /*
2  * Copyright 2000-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.portlets;
18
19 import java.util.Collection JavaDoc;
20 import java.util.Iterator JavaDoc;
21
22 import org.apache.commons.lang.SerializationUtils;
23 import org.apache.jetspeed.modules.actions.portlets.security.SecurityConstants;
24 import org.apache.jetspeed.om.BaseSecurityReference;
25 import org.apache.jetspeed.om.SecurityReference;
26 import org.apache.jetspeed.om.registry.MediaTypeRegistry;
27 import org.apache.jetspeed.om.registry.PortletEntry;
28 import org.apache.jetspeed.om.registry.RegistryEntry;
29 import org.apache.jetspeed.om.registry.base.BaseParameter;
30 import org.apache.jetspeed.om.registry.base.BaseSecurity;
31 import org.apache.jetspeed.portal.portlets.VelocityPortlet;
32 import org.apache.jetspeed.services.JetspeedSecurity;
33 import org.apache.jetspeed.services.Registry;
34 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
35 import org.apache.jetspeed.services.logging.JetspeedLogger;
36 import org.apache.turbine.util.DynamicURI;
37 import org.apache.turbine.util.RunData;
38 import org.apache.turbine.util.TurbineException;
39 import org.apache.velocity.context.Context;
40
41 /**
42  * This action enables the creation and editing of portlets
43  *
44  * @author <a HREF="mailto:caius1440@hotmail.com">Jeremy Ford</a>
45  * @version $Id: PortletUpdateAction.java,v 1.8 2004/02/23 02:56:58 jford Exp $
46  */

47 public class PortletUpdateAction extends RegistryUpdateAction
48 {
49     private static final String JavaDoc PORTLET_UPDATE_PANE = "portlet-form";
50
51     private static final String JavaDoc PORTLET_NAME = "portlet_name";
52     private static final String JavaDoc TAB_PARAMETER = "tab";
53     
54     /**
55      * Static initialization of the logger for this class
56      */

57     private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(PortletUpdateAction.class.getName());
58     
59     public PortletUpdateAction()
60     {
61         registryEntryName = PORTLET_NAME;
62         registry = Registry.PORTLET;
63         pane = PORTLET_UPDATE_PANE;
64     }
65
66     /**
67      * Subclasses must override this method to provide default behavior
68      * for the portlet action
69      */

70     /**
71      * Build the normal state content for this portlet.
72      *
73      * @param portlet The velocity-based portlet that is being built.
74      * @param context The velocity context for this request.
75      * @param rundata The turbine rundata context for this request.
76      */

77     protected void buildNormalContext(
78         VelocityPortlet portlet,
79         Context context,
80         RunData rundata)
81         throws Exception JavaDoc
82     {
83         super.buildNormalContext(portlet, context, rundata);
84         
85         String JavaDoc mode =
86             rundata.getParameters().getString(SecurityConstants.PARAM_MODE);
87
88         if (mode != null && mode.equals(SecurityConstants.PARAM_MODE_INSERT))
89         {
90             context.put("parents", PortletFilter.buildParentList(PortletFilter.getAllPortlets()));
91             context.put(
92                 "securitys",
93                 CustomizeSetAction.buildList(rundata, Registry.SECURITY));
94         }
95
96         if (mode != null && mode.equals(SecurityConstants.PARAM_MODE_UPDATE))
97         {
98             String JavaDoc portletName =
99                 rundata.getParameters().getString(PORTLET_NAME);
100             PortletEntry portletEntry =
101                 (PortletEntry) Registry.getEntry(Registry.PORTLET, portletName);
102                 
103             context.put("groups", iteratorToCollection(JetspeedSecurity.getGroups()));
104             context.put("categories", PortletFilter.buildCategoryList(PortletFilter.getAllPortlets()));
105
106             String JavaDoc tab = rundata.getParameters().getString(TAB_PARAMETER);
107             context.put("currentTab", tab);
108
109             context.put(
110                 "securitys",
111                 CustomizeSetAction.buildList(rundata, Registry.SECURITY));
112
113             context.put("entry", portletEntry);
114             
115             MediaTypeRegistry mediaTypeReg = (MediaTypeRegistry)Registry.get(Registry.MEDIA_TYPE);
116             context.put("media_types", iteratorToCollection(mediaTypeReg.listEntryNames()));
117             
118             if(portletEntry.getType() != null && portletEntry.getType().equals("ref"))
119             {
120                 PortletEntry parentEntry = (PortletEntry) Registry.getEntry(Registry.PORTLET, portletEntry.getParent());
121                 if(parentEntry == null)
122                 {
123                     logger.error(this.getClass().getName() + ": Portlet " + portletName + " of type ref has no parent. This portlet will not work properly.");
124                 }
125                 else
126                 {
127                     Collection JavaDoc parentCategories = iteratorToCollection(parentEntry.listCategories());
128                     context.put("parent_categories", parentCategories);
129                     
130                     Collection JavaDoc parentMediaTypes = iteratorToCollection(parentEntry.listMediaTypes());
131                     context.put("parent_mediatypes", parentMediaTypes);
132                 }
133             }
134         }
135
136         if (mode != null && mode.equals(SecurityConstants.PARAM_MODE_DELETE))
137         {
138             String JavaDoc portletName =
139                 rundata.getParameters().getString(PORTLET_NAME);
140             PortletEntry portletEntry =
141                 (PortletEntry) Registry.getEntry(registry, portletName);
142
143             context.put("entry", portletEntry);
144         }
145     }
146     
147     public void doInsert(RunData rundata, Context context) throws Exception JavaDoc
148     {
149         super.doInsert(rundata, context);
150         
151         String JavaDoc entryName =
152                rundata.getParameters().getString(registryEntryName);
153         
154         PortletEntry portletEntry =
155                         (PortletEntry) Registry.getEntry(registry, entryName);
156         if (portletEntry == null)
157         {
158             String JavaDoc message = "Portlet entry " + entryName + " does not exist. The portlet was not added to the registry.";
159             logger.error(this.getClass().getName() + ": " + message);
160             
161             throw new IllegalStateException JavaDoc(message);
162         }
163         else
164         {
165             if(portletEntry.getType().equals("ref")) {
166                 PortletEntry parentEntry = (PortletEntry)Registry.getEntry(Registry.PORTLET, portletEntry.getParent());
167                 
168                 if(parentEntry == null)
169                 {
170                     logger.error(this.getClass().getName() + ": Portlet " + entryName + " of type ref has no parent. This portlet will not work properly.");
171                 }
172                 else
173                 {
174                     //When we create a portlet entry initially, we need to copy
175
//the parameters from the parent to the child so that
176
//a user does not end up editing his parents parameters
177
Iterator JavaDoc paramIter = portletEntry.getParameterNames();
178                     while(paramIter.hasNext())
179                     {
180                         String JavaDoc paramName = (String JavaDoc)paramIter.next();
181                         BaseParameter param = (BaseParameter) portletEntry.getParameter(paramName);
182                         BaseParameter clonedParameter = (BaseParameter)SerializationUtils.clone(param);
183                         portletEntry.addParameter(clonedParameter);
184                     }
185     
186                     Registry.addEntry(Registry.PORTLET, portletEntry);
187                 }
188             }
189         }
190     }
191     
192     /**
193      * @see org.apache.jetspeed.modules.actions.portlets.RegistryUpdateAction#updateRegistryEntry(org.apache.turbine.util.RunData, org.apache.jetspeed.om.registry.RegistryEntry)
194      */

195     protected void updateRegistryEntry(RunData rundata, RegistryEntry registryEntry) throws Exception JavaDoc
196     {
197         super.updateRegistryEntry(rundata, registryEntry);
198         setPortletEntryInfo(rundata, (PortletEntry)registryEntry);
199     }
200
201     /**
202      * Sets the portlet entry's fields
203      * @param rundata
204      * @param portletEntry
205      */

206     private void setPortletEntryInfo(
207         RunData rundata,
208         PortletEntry portletEntry)
209     {
210         String JavaDoc parent = rundata.getParameters().getString("parent");
211         String JavaDoc title = rundata.getParameters().getString("title");
212         String JavaDoc description = rundata.getParameters().getString("description");
213         String JavaDoc url = rundata.getParameters().getString("url");
214         String JavaDoc type = rundata.getParameters().getString("portlet_type");
215
216         String JavaDoc mediaType = rundata.getParameters().getString("media_type");
217
218         //meta info
219
String JavaDoc image = rundata.getParameters().getString("image");
220
221         String JavaDoc className = rundata.getParameters().getString("class_name");
222
223         boolean isApplication =
224             rundata.getParameters().getBoolean("is_application", false);
225         boolean isCachedOnURL =
226             rundata.getParameters().getBoolean("is_cached_on_url", false);
227         boolean isHidden =
228             rundata.getParameters().getBoolean("is_hidden", false);
229         boolean isAdmin = rundata.getParameters().getBoolean("is_admin", false);
230
231         String JavaDoc newSecurityParent =
232             rundata.getParameters().getString("security_ref");
233
234         String JavaDoc newSecurityRole =
235             rundata.getParameters().getString("security_role");
236
237         portletEntry.setTitle(title);
238         portletEntry.setDescription(description);
239         portletEntry.setURL(url);
240         portletEntry.setParent(parent);
241         portletEntry.setType(type);
242         portletEntry.getMetaInfo().setImage(image);
243
244         //need to build media index before add media
245
portletEntry.listMediaTypes();
246         portletEntry.addMediaType(mediaType);
247
248         if (className != null && className.length() > 0)
249         {
250             portletEntry.setClassname(className);
251         }
252
253         portletEntry.setApplication(isApplication);
254         portletEntry.setCachedOnURL(isCachedOnURL);
255         portletEntry.setHidden(isHidden);
256         //portletEntry.
257

258         if (newSecurityParent != null && newSecurityParent.length() > 0)
259         {
260             SecurityReference securityRef = new BaseSecurityReference();
261             securityRef.setParent(newSecurityParent);
262             portletEntry.setSecurityRef(securityRef);
263         }
264
265         if (newSecurityRole != null && newSecurityRole.length() > 0)
266         {
267             BaseSecurity securityRole = new BaseSecurity();
268             securityRole.setRole(newSecurityRole);
269             portletEntry.setSecurity(securityRole);
270         }
271     }
272
273     /**
274      * Add a category to a portlet
275      * @param rundata The turbine rundata context for this request.
276      * @param context The velocity context for this request.
277      * @throws Exception
278      */

279     public void doAddcategory(RunData rundata, Context context)
280         throws Exception JavaDoc
281     {
282         try
283         {
284             String JavaDoc portletName =
285                 rundata.getParameters().getString(PORTLET_NAME);
286             PortletEntry portletEntry =
287                 (PortletEntry) Registry.getEntry(Registry.PORTLET, portletName);
288             if (portletEntry != null)
289             {
290                 String JavaDoc categoryName =
291                     rundata.getParameters().getString("category_name");
292                 if (categoryName != null && categoryName.length() > 0)
293                 {
294                     String JavaDoc categoryGroup =
295                         rundata.getParameters().getString(
296                             "category_group",
297                             "Jetspeed");
298                     portletEntry.addCategory(categoryName, categoryGroup);
299
300                     Registry.addEntry(registry, portletEntry);
301
302                     clearUserData(rundata);
303                 }
304                 else
305                 {
306                     DynamicURI duri = redirect(rundata, SecurityConstants.PARAM_MODE_UPDATE, SecurityConstants.MID_MISSING_PARAMETER);
307                     rundata.setRedirectURI(duri.toString());
308
309                     resetForm(rundata);
310                 }
311             }
312             else
313             {
314                 DynamicURI duri = redirect(rundata, SecurityConstants.PARAM_MODE_UPDATE, SecurityConstants.MID_INVALID_ENTITY_NAME);
315                 rundata.setRedirectURI(duri.toString());
316                 resetForm(rundata);
317                 
318                 logger.error("Failed to find registry entry while trying to add category");
319             }
320         }
321         catch (Exception JavaDoc e)
322         {
323             DynamicURI duri = redirect(rundata, SecurityConstants.PARAM_MODE_UPDATE, SecurityConstants.MID_UPDATE_FAILED);
324             rundata.setRedirectURI(duri.toString());
325             resetForm(rundata);
326             
327             logger.error("Exception", e);
328         }
329     }
330
331     /**
332      * Remove categories from a portlet
333      * @param rundata The turbine rundata context for this request.
334      * @param context The velocity context for this request.
335      * @throws Exception
336      */

337     public void doRemovecategories(RunData rundata, Context context)
338         throws Exception JavaDoc
339     {
340         try
341         {
342             String JavaDoc portletName =
343                 rundata.getParameters().getString(PORTLET_NAME);
344             PortletEntry portletEntry =
345                 (PortletEntry) Registry.getEntry(Registry.PORTLET, portletName);
346             if (portletEntry != null)
347             {
348                 String JavaDoc[] categories =
349                     rundata.getParameters().getStrings("category_name");
350                 if (categories != null && categories.length > 0)
351                 {
352                     for (int i = 0; i < categories.length; i++)
353                     {
354                         String JavaDoc categoryName = categories[i];
355                         String JavaDoc categoryGroup =
356                             rundata.getParameters().getString(
357                                 categoryName + ".category_group",
358                                 "Jetspeed");
359                         portletEntry.removeCategory(
360                             categoryName,
361                             categoryGroup);
362                     }
363
364                     Registry.addEntry(registry, portletEntry);
365                     clearUserData(rundata);
366                 }
367                 else
368                 {
369                     DynamicURI duri = redirect(rundata, SecurityConstants.PARAM_MODE_UPDATE, SecurityConstants.MID_MISSING_PARAMETER);
370                     duri = duri.addQueryData(PORTLET_NAME, portletName);
371                     rundata.setRedirectURI(duri.toString());
372
373                     resetForm(rundata);
374                 }
375             }
376             else
377             {
378                 DynamicURI duri = redirect(rundata, SecurityConstants.PARAM_MODE_UPDATE, SecurityConstants.MID_INVALID_ENTITY_NAME);
379                 rundata.setRedirectURI(duri.toString());
380                 resetForm(rundata);
381                 
382                 logger.error("Failed to find registry entry while trying to remove categories");
383             }
384         }
385         catch (Exception JavaDoc e)
386         {
387             DynamicURI duri = redirect(rundata, SecurityConstants.PARAM_MODE_UPDATE, SecurityConstants.MID_UPDATE_FAILED);
388             rundata.setRedirectURI(duri.toString());
389             resetForm(rundata);
390             
391             logger.error("Exception", e);
392         }
393     }
394
395     /**
396      * @see org.apache.jetspeed.modules.actions.portlets.RegistryUpdateAction#resetForm(org.apache.turbine.util.RunData)
397      */

398     /**
399      * Populates the user's temp storage with form data
400      * @param rundata The turbine rundata context for this request.
401      */

402     protected void resetForm(RunData rundata)
403     {
404         super.resetForm(rundata);
405         
406         String JavaDoc parent = rundata.getParameters().getString("parent");
407         String JavaDoc title = rundata.getParameters().getString("title");
408         String JavaDoc description = rundata.getParameters().getString("description");
409         String JavaDoc url = rundata.getParameters().getString("url");
410         String JavaDoc type = rundata.getParameters().getString("portlet_type");
411
412         //meta info
413
String JavaDoc image = rundata.getParameters().getString("image");
414
415         String JavaDoc className = rundata.getParameters().getString("class_name");
416
417         String JavaDoc isApplication =
418             rundata.getParameters().getString("is_application");
419         String JavaDoc isCachedOnURL =
420             rundata.getParameters().getString("is_cached_on_url");
421         String JavaDoc isHidden = rundata.getParameters().getString("is_hidden");
422         String JavaDoc isAdmin = rundata.getParameters().getString("is_admin");
423
424         String JavaDoc newSecurityParent =
425             rundata.getParameters().getString("security_ref");
426
427         //String newSecurity = rundata.getParameters().getString("security_role");
428

429         rundata.getUser().setTemp("parent", parent);
430         rundata.getUser().setTemp("portlet_type", type);
431         rundata.getUser().setTemp("class_name", className);
432         rundata.getUser().setTemp("url", url);
433         rundata.getUser().setTemp("image", image);
434         rundata.getUser().setTemp("is_application", isApplication);
435         rundata.getUser().setTemp("is_cached_on_url", isCachedOnURL);
436         rundata.getUser().setTemp("is_hidden", isHidden);
437         rundata.getUser().setTemp("is_admin", isAdmin);
438         rundata.getUser().setTemp("security_ref", newSecurityParent);
439         //rundata.getUser().setTemp("security_role", newSecurity);
440
}
441
442     
443     /**
444      * @see org.apache.jetspeed.modules.actions.portlets.RegistryUpdateAction#clearUserData(org.apache.turbine.util.RunData)
445      */

446     protected void clearUserData(RunData rundata)
447     {
448         try
449         {
450             super.clearUserData(rundata);
451
452             rundata.getUser().removeTemp("parameter_name");
453             rundata.getUser().removeTemp("paramter_value");
454             rundata.getUser().removeTemp("parent");
455             rundata.getUser().removeTemp("portlet_type");
456             rundata.getUser().removeTemp("class_name");
457             rundata.getUser().removeTemp("url");
458             rundata.getUser().removeTemp("image");
459             rundata.getUser().removeTemp("is_application");
460             rundata.getUser().removeTemp("is_cached_on_url");
461             rundata.getUser().removeTemp("is_hidden");
462             rundata.getUser().removeTemp("is_admin");
463             rundata.getUser().removeTemp("security_ref");
464             //rundata.getUser().removeTemp("security_role");
465
}
466         catch (Exception JavaDoc e)
467         {
468             if (logger.isDebugEnabled())
469             {
470                 logger.debug("PortletUpdateAction: Failed to clear user data");
471             }
472         }
473     }
474     
475     /**
476      * @see org.apache.jetspeed.modules.actions.portlets.RegistryUpdateAction#redirect(org.apache.turbine.util.RunData, java.lang.String, int)
477      */

478     protected DynamicURI redirect(RunData rundata, String JavaDoc mode, int reason)
479             throws TurbineException
480     {
481         DynamicURI duri = super.redirect(rundata, mode, reason);
482         
483         String JavaDoc tab = rundata.getParameters().getString(TAB_PARAMETER);
484         if(tab != null && tab.length() > 0)
485         {
486             duri.addQueryData(TAB_PARAMETER, tab);
487         }
488
489         return duri;
490     }
491 }
492
Popular Tags