KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.apache.jetspeed.modules.actions.portlets;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.Iterator JavaDoc;
21
22 import org.apache.jetspeed.modules.actions.portlets.security.SecurityConstants;
23 import org.apache.jetspeed.om.BaseSecurityReference;
24 import org.apache.jetspeed.om.SecurityReference;
25 import org.apache.jetspeed.om.registry.CapabilityMap;
26 import org.apache.jetspeed.om.registry.ClientEntry;
27 import org.apache.jetspeed.om.registry.MediaTypeEntry;
28 import org.apache.jetspeed.om.registry.Parameter;
29 import org.apache.jetspeed.om.registry.PortletEntry;
30 import org.apache.jetspeed.om.registry.PortletInfoEntry;
31 import org.apache.jetspeed.om.registry.RegistryEntry;
32 import org.apache.jetspeed.om.registry.base.BaseCachedParameter;
33 import org.apache.jetspeed.om.registry.base.BaseParameter;
34 import org.apache.jetspeed.om.registry.base.BaseSecurity;
35 import org.apache.jetspeed.portal.portlets.VelocityPortlet;
36 import org.apache.jetspeed.services.Registry;
37 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
38 import org.apache.jetspeed.services.logging.JetspeedLogger;
39 import org.apache.jetspeed.util.template.JetspeedLink;
40 import org.apache.jetspeed.util.template.JetspeedLinkFactory;
41 import org.apache.turbine.util.DynamicURI;
42 import org.apache.turbine.util.RunData;
43 import org.apache.turbine.util.TurbineException;
44 import org.apache.turbine.util.security.EntityExistsException;
45 import org.apache.velocity.context.Context;
46
47 /**
48  * An abstract base class with default actions for many of the common
49  * fields and parameters shared by the registry entries. To add a new registry
50  * update action, simply derive from this class and override the resetForm,
51  * clearUserData, and updateRegistry functions. If you need to provide more
52  * actions that those that are provided, simply create them in your derived class.
53  *
54  * @author <a HREF="mailto:caius1440@hotmail.com">Jeremy Ford</a>
55  * @version $Id: RegistryUpdateAction.java,v 1.10 2004/03/31 04:49:10 morciuch Exp $
56  */

57 public abstract class RegistryUpdateAction extends SecureVelocityPortletAction
58 {
59     protected String JavaDoc registryEntryName = "";
60     protected String JavaDoc registry = "";
61     protected String JavaDoc pane = "";
62
63     private static final String JavaDoc REASON = "reason";
64
65     /**
66      * Static initialization of the logger for this class
67      */

68     private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(RegistryUpdateAction.class.getName());
69     
70     protected void buildNormalContext(
71         VelocityPortlet portlet,
72         Context context,
73         RunData rundata)
74         throws Exception JavaDoc
75     {
76         String JavaDoc msgid =
77             rundata.getParameters().getString(SecurityConstants.PARAM_MSGID);
78         if (msgid != null)
79         {
80             int id = Integer.parseInt(msgid);
81             if (id < SecurityConstants.MESSAGES.length)
82                 context.put(
83                     SecurityConstants.PARAM_MSG,
84                     SecurityConstants.MESSAGES[id]);
85         }
86
87         String JavaDoc mode =
88             rundata.getParameters().getString(SecurityConstants.PARAM_MODE);
89         context.put(SecurityConstants.PARAM_MODE, mode);
90
91         String JavaDoc reason = rundata.getParameters().getString(REASON);
92         context.put(REASON, reason);
93     }
94
95     /**
96      * Insert a registry entry into the registry
97      * @param rundata The turbine rundata context for this request.
98      * @param context The velocity context for this request.
99      * @throws Exception
100      */

101     public void doInsert(RunData rundata, Context context) throws Exception JavaDoc
102     {
103         try
104         {
105             String JavaDoc entryName =
106                 rundata.getParameters().getString(registryEntryName);
107
108             if (entryName == null || entryName.length() == 0)
109             {
110                 DynamicURI duri =
111                     redirect(
112                         rundata,
113                         SecurityConstants.PARAM_MODE_INSERT,
114                         SecurityConstants.MID_INVALID_ENTITY_NAME);
115                 rundata.setRedirectURI(duri.toString());
116                 resetForm(rundata);
117             }
118             else
119             {
120                 //check if profile to be created already exists
121
RegistryEntry existingEntry =
122                     Registry.getEntry(registry, entryName);
123                 if (existingEntry != null)
124                 {
125                     throw new EntityExistsException(
126                         "RegistryEntry: " + entryName + " Already Exists!");
127                 }
128
129                 RegistryEntry registryEntry = Registry.createEntry(registry);
130                 registryEntry.setName(entryName);
131
132                 updateRegistryEntry(rundata, registryEntry);
133
134                 Registry.addEntry(registry, registryEntry);
135
136                 clearUserData(rundata);
137                 
138                 rundata.getUser().setTemp(RegistryBrowseAction.PREFIX + registry + ":" + RegistryBrowseAction.REFRESH, Boolean.TRUE);
139             }
140         }
141         catch (EntityExistsException e)
142         {
143             //
144
// dup key found - display error message - bring back to same screen
145
//
146
DynamicURI duri =
147                 redirect(
148                     rundata,
149                     SecurityConstants.PARAM_MODE_INSERT,
150                     SecurityConstants.MID_ENTITY_ALREADY_EXISTS);
151             rundata.setRedirectURI(duri.toString());
152             resetForm(rundata);
153
154             logger.error(
155                 this.getClass().getName()
156                     + ": Trying to create duplicate entry");
157         }
158         catch (Exception JavaDoc e)
159         {
160             DynamicURI duri =
161                 redirect(
162                     rundata,
163                     SecurityConstants.PARAM_MODE_INSERT,
164                     SecurityConstants.MID_UPDATE_FAILED);
165             duri = duri.addQueryData(REASON, e.getMessage());
166             rundata.setRedirectURI(duri.toString());
167             resetForm(rundata);
168
169             logger.error("Exception", e);
170         }
171     }
172
173     /**
174      * Update a registry entry
175      * @param rundata The turbine rundata context for this request.
176      * @param context The velocity context for this request.
177      * @throws Exception
178      */

179     public void doUpdate(RunData rundata, Context context) throws Exception JavaDoc
180     {
181         try
182         {
183             String JavaDoc entryName =
184                 rundata.getParameters().getString(registryEntryName);
185             RegistryEntry registryEntry =
186                 Registry.getEntry(registry, entryName);
187             if (registryEntry != null)
188             {
189                 updateRegistryEntry(rundata, registryEntry);
190
191                 Registry.addEntry(registry, registryEntry);
192                 clearUserData(rundata);
193             }
194             else
195             {
196                 DynamicURI duri =
197                     redirect(
198                         rundata,
199                         SecurityConstants.PARAM_MODE_UPDATE,
200                         SecurityConstants.MID_INVALID_ENTITY_NAME);
201                 rundata.setRedirectURI(duri.toString());
202                 resetForm(rundata);
203
204                 logger.error(
205                     this.getClass().getName()
206                         + ": Failed to find registry entry for updating");
207             }
208         }
209         catch (Exception JavaDoc e)
210         {
211             DynamicURI duri =
212                 redirect(
213                     rundata,
214                     SecurityConstants.PARAM_MODE_UPDATE,
215                     SecurityConstants.MID_UPDATE_FAILED);
216             duri = duri.addQueryData(REASON, e.getMessage());
217             rundata.setRedirectURI(duri.toString());
218             resetForm(rundata);
219
220             logger.error("Exception", e);
221         }
222     }
223
224     /**
225      * Delete a registry entry
226      * @param rundata The turbine rundata context for this request.
227      * @param context The velocity context for this request.
228      * @throws Exception
229      */

230     public void doDelete(RunData rundata, Context context) throws Exception JavaDoc
231     {
232         try
233         {
234             String JavaDoc entryName =
235                 rundata.getParameters().getString(registryEntryName);
236
237             if (entryName == null || entryName.length() == 0)
238             {
239                 DynamicURI duri =
240                     redirect(
241                         rundata,
242                         SecurityConstants.PARAM_MODE_DELETE,
243                         SecurityConstants.MID_INVALID_ENTITY_NAME);
244                 rundata.setRedirectURI(duri.toString());
245                 resetForm(rundata);
246
247                 logger.error(
248                     this.getClass().getName()
249                         + ": Failed to find registry entry for deleting");
250             }
251             else
252             {
253                 Registry.removeEntry(registry, entryName);
254                 clearUserData(rundata);
255                 
256                 rundata.getUser().setTemp(RegistryBrowseAction.PREFIX + registry + ":" + RegistryBrowseAction.REFRESH, Boolean.TRUE);
257             }
258         }
259         catch (Exception JavaDoc e)
260         {
261             DynamicURI duri =
262                 redirect(
263                     rundata,
264                     SecurityConstants.PARAM_MODE_DELETE,
265                     SecurityConstants.MID_DELETE_FAILED);
266             duri = duri.addQueryData(REASON, e.getMessage());
267             rundata.setRedirectURI(duri.toString());
268             resetForm(rundata);
269
270             logger.error("Exception", e);
271         }
272     }
273
274     /**
275      * Cleanup method
276      * @param rundata The turbine rundata context for this request.
277      * @param context The velocity context for this request.
278      * @throws Exception
279      */

280     public void doCancel(RunData rundata, Context context) throws Exception JavaDoc
281     {
282         clearUserData(rundata);
283     }
284
285     /**
286      * Basic implementation of a method to update a registry entry. The fields that
287      * are common to all registry entries can simply be added below.
288      * @param rundata The turbine rundata context for this request.
289      * @param registryEntry The registry entry to update
290      */

291     protected void updateRegistryEntry(
292         RunData rundata,
293         RegistryEntry registryEntry)
294         throws Exception JavaDoc
295     {
296         String JavaDoc description = rundata.getParameters().getString("description");
297         String JavaDoc title = rundata.getParameters().getString("title");
298         Boolean JavaDoc hidden = rundata.getParameters().getBool("hidden");
299         String JavaDoc securityRef = rundata.getParameters().getString("security_ref");
300
301         SecurityReference security = registryEntry.getSecurityRef();
302         String JavaDoc securityParent = null;
303         if (security != null)
304         {
305             securityParent = security.getParent();
306         }
307
308         if (hasChanged(securityParent, securityRef))
309         {
310             if (security == null)
311             {
312                 security = new BaseSecurityReference();
313             }
314             security.setParent(securityRef);
315             registryEntry.setSecurityRef(security);
316         }
317
318         if (hasChanged(registryEntry.getDescription(), description))
319         {
320             registryEntry.setDescription(description);
321         }
322         if (hasChanged(String.valueOf(registryEntry.isHidden()),
323             String.valueOf(hidden)))
324         {
325             registryEntry.setHidden(hidden.booleanValue());
326         }
327         if (hasChanged(registryEntry.getTitle(), title))
328         {
329             registryEntry.setTitle(title);
330         }
331     }
332
333     /**
334      * Determines whether a field has changed value. Used in update methods.
335      *
336      * @param oldValue The original value
337      * @param newValue The new value
338      */

339     protected boolean hasChanged(String JavaDoc oldValue, String JavaDoc newValue)
340     {
341         boolean result = false;
342
343         if (oldValue == null && newValue == null)
344         {
345             result = false;
346         }
347         else if (
348             oldValue == null && (newValue != null && newValue.length() == 0))
349         {
350             result = false;
351         }
352         else if (oldValue == null && (newValue != null))
353         {
354             result = true;
355         }
356         else if (oldValue != null && newValue == null)
357         {
358             result = true;
359         }
360         else if (!oldValue.equals(newValue))
361         {
362             result = true;
363         }
364
365         return result;
366     }
367
368     /**
369      * Add a parameter to a registry entry
370      * @param rundata The turbine rundata context for this request.
371      * @param context The velocity context for this request.
372      * @throws Exception
373      */

374     public void doAddparameter(RunData rundata, Context context)
375         throws Exception JavaDoc
376     {
377         try
378         {
379             String JavaDoc entryName =
380                 rundata.getParameters().getString(registryEntryName);
381             PortletInfoEntry regEntry =
382                 (PortletInfoEntry) Registry.getEntry(registry, entryName);
383             if (regEntry != null)
384             {
385                 String JavaDoc parameterName =
386                     rundata.getParameters().getString("parameter_name");
387                 if (parameterName != null && parameterName.length() > 0)
388                 {
389                     Parameter parameter = null;
390                     if (regEntry instanceof PortletEntry)
391                     {
392                         parameter = new BaseCachedParameter();
393
394                         boolean isCachedOnName =
395                             rundata.getParameters().getBoolean(
396                                 "cached_on_name",
397                                 false);
398                         boolean isCachedOnValue =
399                             rundata.getParameters().getBoolean(
400                                 "cached_on_value",
401                                 false);
402
403                         ((BaseCachedParameter) parameter).setCachedOnName(
404                             isCachedOnName);
405                         ((BaseCachedParameter) parameter).setCachedOnValue(
406                             isCachedOnValue);
407                     }
408                     else
409                     {
410                         parameter = new BaseParameter();
411                     }
412
413                     String JavaDoc parameterValue =
414                         rundata.getParameters().getString("parameter_value");
415                     boolean isHidden =
416                         rundata.getParameters().getBoolean("is_hidden", false);
417                     String JavaDoc description =
418                         rundata.getParameters().getString("description");
419                     String JavaDoc title = rundata.getParameters().getString("title");
420                     String JavaDoc type = rundata.getParameters().getString("type");
421
422                     parameter.setName(parameterName);
423                     parameter.setValue(parameterValue);
424                     parameter.setHidden(isHidden);
425                     parameter.setDescription(description);
426                     parameter.setTitle(title);
427                     parameter.setType(type);
428
429                     String JavaDoc securityRole =
430                         rundata.getParameters().getString("security_role");
431                     String JavaDoc securityRef =
432                         rundata.getParameters().getString("security_ref");
433
434                     if (securityRole != null && securityRole.length() > 0)
435                     {
436                         BaseSecurity paramSecurity = new BaseSecurity();
437                         paramSecurity.setRole(securityRole);
438                         parameter.setSecurity(paramSecurity);
439                     }
440
441                     if (securityRef != null && securityRef.length() > 0)
442                     {
443                         BaseSecurityReference paramSecurityRef =
444                             new BaseSecurityReference();
445                         paramSecurityRef.setParent(securityRef);
446                         parameter.setSecurityRef(paramSecurityRef);
447                     }
448
449                     regEntry.addParameter(parameter);
450
451                     Registry.addEntry(registry, regEntry);
452
453                     clearUserData(rundata);
454                 }
455                 else
456                 {
457                     DynamicURI duri =
458                         redirect(
459                             rundata,
460                             SecurityConstants.PARAM_MODE_UPDATE,
461                             SecurityConstants.MID_MISSING_PARAMETER);
462                     rundata.setRedirectURI(duri.toString());
463
464                     resetForm(rundata);
465                 }
466             }
467             else
468             {
469                 DynamicURI duri =
470                     redirect(
471                         rundata,
472                         SecurityConstants.PARAM_MODE_UPDATE,
473                         SecurityConstants.MID_INVALID_ENTITY_NAME);
474                 rundata.setRedirectURI(duri.toString());
475
476                 resetForm(rundata);
477
478                 logger.error(
479                     this.getClass().getName()
480                         + ": Failed to find registry entry while trying to add a parameter");
481             }
482         }
483         catch (Exception JavaDoc e)
484         {
485             DynamicURI duri =
486                 redirect(
487                     rundata,
488                     SecurityConstants.PARAM_MODE_UPDATE,
489                     SecurityConstants.MID_INVALID_ENTITY_NAME);
490             rundata.setRedirectURI(duri.toString());
491
492             resetForm(rundata);
493
494             logger.error("Exception", e);
495         }
496     }
497
498     /**
499      * Update parameters of a registry entry
500      * @param rundata The turbine rundata context for this request.
501      * @param context The velocity context for this request.
502      * @throws Exception
503      */

504     public void doUpdateparameters(RunData rundata, Context context)
505         throws Exception JavaDoc
506     {
507         try
508         {
509             String JavaDoc entryName =
510                 rundata.getParameters().getString(registryEntryName);
511             PortletInfoEntry regEntry =
512                 (PortletInfoEntry) Registry.getEntry(registry, entryName);
513             if (regEntry != null)
514             {
515                 String JavaDoc[] parameters =
516                     rundata.getParameters().getStrings("update_parameter_name");
517                 if (parameters != null && parameters.length > 0)
518                 {
519                     for (int i = 0; i < parameters.length; i++)
520                     {
521                         String JavaDoc parameterName = parameters[i];
522                         Parameter parameter = regEntry.getParameter(parameterName);
523                         if (regEntry instanceof PortletEntry)
524                         {
525                             if(parameter == null)
526                             {
527                                 parameter = new BaseCachedParameter();
528                                 regEntry.addParameter(parameter);
529                             }
530
531                             boolean isCachedOnName =
532                                 rundata.getParameters().getBoolean(
533                                     parameterName +".cached_on_name",
534                                     false);
535                             boolean isCachedOnValue =
536                                 rundata.getParameters().getBoolean(
537                                     parameterName +".cached_on_value",
538                                     false);
539
540                             ((BaseCachedParameter) parameter).setCachedOnName(
541                                 isCachedOnName);
542                             ((BaseCachedParameter) parameter).setCachedOnValue(
543                                 isCachedOnValue);
544                         }
545                         else if(parameter == null)
546                         {
547                             parameter = new BaseParameter();
548                             regEntry.addParameter(parameter);
549                         }
550                         
551                         String JavaDoc parameterValue =
552                             rundata.getParameters().getString(
553                                 parameterName + ".parameter_value");
554                         String JavaDoc description =
555                             rundata.getParameters().getString(
556                                 parameterName + ".description");
557                         String JavaDoc title =
558                             rundata.getParameters().getString(
559                                 parameterName + ".title");
560                         String JavaDoc securityRole =
561                             rundata.getParameters().getString(
562                                 parameterName + ".security_role");
563                         String JavaDoc securityRef =
564                             rundata.getParameters().getString(
565                                 parameterName + ".security_ref");
566                         String JavaDoc type =
567                             rundata.getParameters().getString(
568                                 parameterName + ".type");
569                         boolean isHidden =
570                             rundata.getParameters().getBoolean(
571                                 parameterName + ".is_hidden",
572                                 false);
573
574                         parameter.setName(parameterName);
575                         parameter.setValue(parameterValue);
576                         parameter.setHidden(isHidden);
577                         parameter.setDescription(description);
578                         parameter.setTitle(title);
579                         parameter.setType(type);
580
581                         if (securityRef != null && securityRef.length() > 0)
582                         {
583                             BaseSecurityReference paramSecurityRef =
584                                 new BaseSecurityReference();
585                             paramSecurityRef.setParent(securityRef);
586                             parameter.setSecurityRef(paramSecurityRef);
587                         }
588                     }
589
590                     Registry.addEntry(registry, regEntry);
591                     clearUserData(rundata);
592                 }
593                 else
594                 {
595                     DynamicURI duri =
596                         redirect(
597                             rundata,
598                             SecurityConstants.PARAM_MODE_UPDATE,
599                             SecurityConstants.MID_MISSING_PARAMETER);
600                     rundata.setRedirectURI(duri.toString());
601
602                     resetForm(rundata);
603                 }
604             }
605             else
606             {
607                 DynamicURI duri =
608                     redirect(
609                         rundata,
610                         SecurityConstants.PARAM_MODE_UPDATE,
611                         SecurityConstants.MID_INVALID_ENTITY_NAME);
612                 rundata.setRedirectURI(duri.toString());
613                 resetForm(rundata);
614
615                 logger.error(
616                     this.getClass().getName()
617                         + ": Failed to find registry entry while trying to update parameters");
618             }
619         }
620         catch (Exception JavaDoc e)
621         {
622             DynamicURI duri =
623                 redirect(
624                     rundata,
625                     SecurityConstants.PARAM_MODE_UPDATE,
626                     SecurityConstants.MID_UPDATE_FAILED);
627             rundata.setRedirectURI(duri.toString());
628
629             resetForm(rundata);
630
631             logger.error("Exception", e);
632         }
633     }
634
635     /**
636      * Update parameter's values of a registry entry
637      * @param rundata The turbine rundata context for this request.
638      * @param context The velocity context for this request.
639      * @throws Exception
640      */

641     public void doUpdateparametervalues(RunData rundata, Context context)
642         throws Exception JavaDoc
643     {
644         try
645         {
646             String JavaDoc entryName =
647                 rundata.getParameters().getString(registryEntryName);
648             PortletInfoEntry regEntry =
649                 (PortletInfoEntry) Registry.getEntry(registry, entryName);
650             if (regEntry != null)
651             {
652                 String JavaDoc[] parameters =
653                     rundata.getParameters().getStrings("update_parameter_name");
654                 if (parameters != null && parameters.length > 0)
655                 {
656                     for (int i = 0; i < parameters.length; i++)
657                     {
658                         String JavaDoc parameterName = parameters[i];
659                         String JavaDoc parameterValue =
660                             rundata.getParameters().getString(
661                                 parameterName + ".parameter_value");
662
663                         Parameter parameter =
664                             regEntry.getParameter(parameterName);
665
666                         if (parameter == null)
667                         {
668                             if (regEntry instanceof PortletEntry)
669                             {
670                                 parameter = new BaseCachedParameter();
671                             }
672                             else
673                             {
674                                 parameter = new BaseParameter();
675                             }
676                             
677                             parameter.setName(parameterName);
678                             regEntry.addParameter(parameter);
679                         }
680
681                         if (parameter != null)
682                         {
683                             parameter.setValue(parameterValue);
684                         }
685                     }
686
687                     Registry.addEntry(registry, regEntry);
688                     clearUserData(rundata);
689                 }
690                 else
691                 {
692
693                     DynamicURI duri =
694                         redirect(
695                             rundata,
696                             SecurityConstants.PARAM_MODE_UPDATE,
697                             SecurityConstants.MID_MISSING_PARAMETER);
698                     rundata.setRedirectURI(duri.toString());
699                     resetForm(rundata);
700                 }
701             }
702             else
703             {
704                 DynamicURI duri =
705                     redirect(
706                         rundata,
707                         SecurityConstants.PARAM_MODE_UPDATE,
708                         SecurityConstants.MID_INVALID_ENTITY_NAME);
709                 rundata.setRedirectURI(duri.toString());
710                 resetForm(rundata);
711
712                 logger.error(
713                     this.getClass().getName()
714                         + ": Failed to find registry entry while trying to update parameters");
715             }
716         }
717         catch (Exception JavaDoc e)
718         {
719             DynamicURI duri =
720                 redirect(
721                     rundata,
722                     SecurityConstants.PARAM_MODE_UPDATE,
723                     SecurityConstants.MID_UPDATE_FAILED);
724             rundata.setRedirectURI(duri.toString());
725             resetForm(rundata);
726
727             logger.error("Exception", e);
728         }
729     }
730
731     /**
732      * Remove parameters from a registry entry
733      * @param rundata The turbine rundata context for this request.
734      * @param context The velocity context for this request.
735      * @throws Exception
736      */

737     public void doRemoveparameters(RunData rundata, Context context)
738         throws Exception JavaDoc
739     {
740         try
741         {
742             String JavaDoc entryName =
743                 rundata.getParameters().getString(registryEntryName);
744             PortletInfoEntry portletEntry =
745                 (PortletInfoEntry) Registry.getEntry(registry, entryName);
746             if (portletEntry != null)
747             {
748                 String JavaDoc[] parameters =
749                     rundata.getParameters().getStrings("parameter_name");
750                 if (parameters != null && parameters.length > 0)
751                 {
752                     for (int i = 0; i < parameters.length; i++)
753                     {
754                         String JavaDoc parameterName = parameters[i];
755                         portletEntry.removeParameter(parameterName);
756                     }
757
758                     Registry.addEntry(registry, portletEntry);
759                     clearUserData(rundata);
760                 }
761                 else
762                 {
763                     DynamicURI duri =
764                         redirect(
765                             rundata,
766                             SecurityConstants.PARAM_MODE_UPDATE,
767                             SecurityConstants.MID_MISSING_PARAMETER);
768                     rundata.setRedirectURI(duri.toString());
769                     resetForm(rundata);
770                 }
771             }
772             else
773             {
774                 DynamicURI duri =
775                     redirect(
776                         rundata,
777                         SecurityConstants.PARAM_MODE_UPDATE,
778                         SecurityConstants.MID_INVALID_ENTITY_NAME);
779                 rundata.setRedirectURI(duri.toString());
780                 resetForm(rundata);
781
782                 logger.error(
783                     this.getClass().getName()
784                         + ": Failed to find registry entry while trying to remove parameters");
785             }
786         }
787         catch (Exception JavaDoc e)
788         {
789             DynamicURI duri =
790                 redirect(
791                     rundata,
792                     SecurityConstants.PARAM_MODE_UPDATE,
793                     SecurityConstants.MID_UPDATE_FAILED);
794             rundata.setRedirectURI(duri.toString());
795             resetForm(rundata);
796
797             logger.error("Exception", e);
798         }
799     }
800
801     /**
802      * Add a media type to a registry entry
803      * @param rundata The turbine rundata context for this request.
804      * @param context The velocity context for this request.
805      * @throws Exception
806      */

807     public void doAddmediatype(RunData rundata, Context context)
808         throws Exception JavaDoc
809     {
810         try
811         {
812             String JavaDoc entryName =
813                 rundata.getParameters().getString(registryEntryName);
814             PortletInfoEntry portletEntry =
815                 (PortletInfoEntry) Registry.getEntry(registry, entryName);
816             if (portletEntry != null)
817             {
818                 String JavaDoc mediaType =
819                     rundata.getParameters().getString("media_type");
820                 if (mediaType != null && mediaType.length() > 0)
821                 {
822                     portletEntry.addMediaType(mediaType);
823                     Registry.addEntry(registry, portletEntry);
824                     clearUserData(rundata);
825                 }
826                 else
827                 {
828                     DynamicURI duri =
829                         redirect(
830                             rundata,
831                             SecurityConstants.PARAM_MODE_UPDATE,
832                             SecurityConstants.MID_MISSING_PARAMETER);
833                     rundata.setRedirectURI(duri.toString());
834                     resetForm(rundata);
835                 }
836             }
837             else
838             {
839                 DynamicURI duri =
840                     redirect(
841                         rundata,
842                         SecurityConstants.PARAM_MODE_UPDATE,
843                         SecurityConstants.MID_INVALID_ENTITY_NAME);
844                 rundata.setRedirectURI(duri.toString());
845                 resetForm(rundata);
846
847                 logger.error(
848                     this.getClass().getName()
849                         + ": Failed to find registry entry while trying to add media type");
850             }
851         }
852         catch (Exception JavaDoc e)
853         {
854             DynamicURI duri =
855                 redirect(
856                     rundata,
857                     SecurityConstants.PARAM_MODE_UPDATE,
858                     SecurityConstants.MID_UPDATE_FAILED);
859             rundata.setRedirectURI(duri.toString());
860             resetForm(rundata);
861
862             logger.error("Exception", e);
863         }
864     }
865
866     /**
867      * Remove media types from a registry entry
868      * @param rundata The turbine rundata context for this request.
869      * @param context The velocity context for this request.
870      * @throws Exception
871      */

872     public void doRemovemediatypes(RunData rundata, Context context)
873         throws Exception JavaDoc
874     {
875         try
876         {
877             String JavaDoc entryName =
878                 rundata.getParameters().getString(registryEntryName);
879             PortletInfoEntry portletEntry =
880                 (PortletInfoEntry) Registry.getEntry(registry, entryName);
881             if (portletEntry != null)
882             {
883                 String JavaDoc[] mediaTypes =
884                     rundata.getParameters().getStrings("media_type");
885                 if (mediaTypes != null && mediaTypes.length > 0)
886                 {
887                     for (int i = 0; i < mediaTypes.length; i++)
888                     {
889                         String JavaDoc mediaType = mediaTypes[i];
890                         portletEntry.removeMediaType(mediaType);
891                     }
892
893                     Registry.addEntry(registry, portletEntry);
894                     clearUserData(rundata);
895                 }
896                 else
897                 {
898                     DynamicURI duri =
899                         redirect(
900                             rundata,
901                             SecurityConstants.PARAM_MODE_UPDATE,
902                             SecurityConstants.MID_MISSING_PARAMETER);
903                     rundata.setRedirectURI(duri.toString());
904                     resetForm(rundata);
905                 }
906
907             }
908             else
909             {
910                 DynamicURI duri =
911                     redirect(
912                         rundata,
913                         SecurityConstants.PARAM_MODE_UPDATE,
914                         SecurityConstants.MID_INVALID_ENTITY_NAME);
915                 rundata.setRedirectURI(duri.toString());
916                 resetForm(rundata);
917
918                 logger.error(
919                     this.getClass().getName()
920                         + ": Failed to find registry entry while trying to remove media types");
921             }
922         }
923         catch (Exception JavaDoc e)
924         {
925             DynamicURI duri =
926                 redirect(
927                     rundata,
928                     SecurityConstants.PARAM_MODE_UPDATE,
929                     SecurityConstants.MID_UPDATE_FAILED);
930             rundata.setRedirectURI(duri.toString());
931             resetForm(rundata);
932
933             logger.error("Exception", e);
934         }
935     }
936
937     /**
938      * Add capabilities to a registry entry
939      * @param rundata The turbine rundata context for this request.
940      * @param context The velocity context for this request.
941      * @throws Exception
942      */

943     public void doAddcapability(RunData rundata, Context context)
944         throws Exception JavaDoc
945     {
946         try
947         {
948             String JavaDoc entryName =
949                 rundata.getParameters().getString(registryEntryName);
950             RegistryEntry regEntry = Registry.getEntry(registry, entryName);
951
952             if (regEntry != null)
953             {
954                 CapabilityMap cm = null;
955                 if (regEntry instanceof MediaTypeEntry)
956                 {
957                     MediaTypeEntry mediaTypeEntry = (MediaTypeEntry) regEntry;
958                     cm = mediaTypeEntry.getCapabilityMap();
959                 }
960                 else if (regEntry instanceof ClientEntry)
961                 {
962                     ClientEntry clientEntry = (ClientEntry) regEntry;
963                     cm = clientEntry.getCapabilityMap();
964                 }
965                 else
966                 {
967                     //TODO: throw error
968
}
969
970                 if (cm != null)
971                 {
972                     String JavaDoc[] capabilities =
973                         rundata.getParameters().getStrings("capability");
974                     if (capabilities != null && capabilities.length > 0)
975                     {
976                         for (int i = 0; i < capabilities.length; i++)
977                         {
978                             String JavaDoc capability = capabilities[i];
979                             if (cm.contains(capability))
980                             {
981                             }
982                             else
983                             {
984                                 if (capability != null
985                                     && capability.length() > 0)
986                                 {
987                                     cm.addCapability(capability);
988                                 }
989                             }
990                         }
991                     }
992
993                     Registry.addEntry(registry, regEntry);
994                     clearUserData(rundata);
995                 }
996                 else
997                 {
998                     DynamicURI duri =
999                         redirect(
1000                            rundata,
1001                            SecurityConstants.PARAM_MODE_UPDATE,
1002                            SecurityConstants.MID_UPDATE_FAILED);
1003                    rundata.setRedirectURI(duri.toString());
1004                    resetForm(rundata);
1005                }
1006            }
1007            else
1008            {
1009                DynamicURI duri =
1010                    redirect(
1011                        rundata,
1012                        SecurityConstants.PARAM_MODE_UPDATE,
1013                        SecurityConstants.MID_INVALID_ENTITY_NAME);
1014                rundata.setRedirectURI(duri.toString());
1015                resetForm(rundata);
1016
1017                logger.error(
1018                    this.getClass().getName()
1019                        + ": Failed to find registry entry while trying to add capabilities");
1020            }
1021        }
1022        catch (Exception JavaDoc e)
1023        {
1024            DynamicURI duri =
1025                redirect(
1026                    rundata,
1027                    SecurityConstants.PARAM_MODE_UPDATE,
1028                    SecurityConstants.MID_UPDATE_FAILED);
1029            rundata.setRedirectURI(duri.toString());
1030            resetForm(rundata);
1031
1032            logger.error("Exception", e);
1033        }
1034    }
1035
1036    /**
1037     * Remove capabilites from a registry entry
1038     * @param rundata The turbine rundata context for this request.
1039     * @param context The velocity context for this request.
1040     * @throws Exception
1041     */

1042    public void doRemovecapability(RunData rundata, Context context)
1043        throws Exception JavaDoc
1044    {
1045        try
1046        {
1047            String JavaDoc entryName =
1048                rundata.getParameters().getString(registryEntryName);
1049            RegistryEntry regEntry = Registry.getEntry(registry, entryName);
1050            if (regEntry != null)
1051            {
1052                CapabilityMap cm = null;
1053                if (regEntry instanceof MediaTypeEntry)
1054                {
1055                    MediaTypeEntry mediaTypeEntry = (MediaTypeEntry) regEntry;
1056                    cm = mediaTypeEntry.getCapabilityMap();
1057                }
1058                else if (regEntry instanceof ClientEntry)
1059                {
1060                    ClientEntry clientEntry = (ClientEntry) regEntry;
1061                    cm = clientEntry.getCapabilityMap();
1062                }
1063                else
1064                {
1065                    //TODO: throw error
1066
}
1067
1068                if (cm != null)
1069                {
1070                    String JavaDoc[] capabilities =
1071                        rundata.getParameters().getStrings("capability");
1072                    if (capabilities != null && capabilities.length > 0)
1073                    {
1074                        for (int i = 0; i < capabilities.length; i++)
1075                        {
1076                            String JavaDoc capability = capabilities[i];
1077
1078                            cm.removeCapability(capability);
1079                        }
1080
1081                        Registry.addEntry(registry, regEntry);
1082                        clearUserData(rundata);
1083                    }
1084
1085                    Registry.addEntry(registry, regEntry);
1086                    clearUserData(rundata);
1087                }
1088                else
1089                {
1090                    DynamicURI duri =
1091                        redirect(
1092                            rundata,
1093                            SecurityConstants.PARAM_MODE_UPDATE,
1094                            SecurityConstants.MID_UPDATE_FAILED);
1095                    rundata.setRedirectURI(duri.toString());
1096                    resetForm(rundata);
1097                }
1098            }
1099            else
1100            {
1101                DynamicURI duri =
1102                    redirect(
1103                        rundata,
1104                        SecurityConstants.PARAM_MODE_UPDATE,
1105                        SecurityConstants.MID_INVALID_ENTITY_NAME);
1106                rundata.setRedirectURI(duri.toString());
1107                resetForm(rundata);
1108
1109                logger.error(
1110                    this.getClass().getName()
1111                        + ": Failed to find registry entry while trying to remove capabilities");
1112            }
1113        }
1114        catch (Exception JavaDoc e)
1115        {
1116            DynamicURI duri =
1117                redirect(
1118                    rundata,
1119                    SecurityConstants.PARAM_MODE_UPDATE,
1120                    SecurityConstants.MID_UPDATE_FAILED);
1121            rundata.setRedirectURI(duri.toString());
1122            resetForm(rundata);
1123
1124            logger.error("Exception", e);
1125        }
1126    }
1127
1128    /**
1129     * Method that sets up a redirect link given the rundata, the mode, and a reason
1130     * @param rundata
1131     * @param mode
1132     * @param reason
1133     * @return
1134     * @throws TurbineException
1135     */

1136    protected DynamicURI redirect(RunData rundata, String JavaDoc mode, int reason)
1137        throws TurbineException
1138    {
1139        JetspeedLink link = JetspeedLinkFactory.getInstance(rundata);
1140        DynamicURI duri =
1141            link
1142                .getPaneByName(pane)
1143                .addPathInfo(SecurityConstants.PARAM_MODE, mode)
1144                .addPathInfo(SecurityConstants.PARAM_MSGID, reason);
1145
1146        String JavaDoc entryName = rundata.getParameters().getString(registryEntryName);
1147        if (entryName != null && entryName.length() > 0)
1148        {
1149            duri.addQueryData(registryEntryName, entryName);
1150        }
1151
1152        JetspeedLinkFactory.putInstance(link);
1153
1154        return duri;
1155    }
1156
1157    /**
1158     * Remove any data that was added to the user's temporary storage
1159     * @param rundata
1160     */

1161    protected void clearUserData(RunData rundata)
1162    {
1163        rundata.getUser().removeTemp(registryEntryName);
1164        rundata.getUser().removeTemp("title");
1165        rundata.getUser().removeTemp("description");
1166    }
1167
1168    /**
1169     * Method to reset data entered into the forms
1170     * @param rundata
1171     */

1172    protected void resetForm(RunData rundata)
1173    {
1174        String JavaDoc entryName = rundata.getParameters().getString(registryEntryName);
1175        String JavaDoc title = rundata.getParameters().getString("title");
1176        String JavaDoc description = rundata.getParameters().getString("description");
1177
1178        rundata.getUser().setTemp(registryEntryName, entryName);
1179        rundata.getUser().setTemp("title", title);
1180        rundata.getUser().setTemp("description", description);
1181    }
1182
1183    /**
1184     * Turns an iterator into a collection
1185     *
1186     * @param iter An iterator
1187     * @return the collection
1188     */

1189    protected Collection JavaDoc iteratorToCollection(Iterator JavaDoc iter)
1190    {
1191        Collection JavaDoc collection = new ArrayList JavaDoc();
1192        while (iter.hasNext())
1193        {
1194            collection.add(iter.next());
1195        }
1196        return collection;
1197    }
1198}
1199
Popular Tags