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.portlets.pam;
18  
19  import java.io.IOException;
20  import java.util.ArrayList;
21  import java.util.Iterator;
22  import java.util.LinkedHashMap;
23  import java.util.List;
24  import java.util.Locale;
25  import java.util.StringTokenizer;
26  
27  import javax.portlet.ActionRequest;
28  import javax.portlet.ActionResponse;
29  import javax.portlet.PortletConfig;
30  import javax.portlet.PortletContext;
31  import javax.portlet.PortletException;
32  import javax.portlet.PortletMode;
33  import javax.portlet.PortletSession;
34  import javax.portlet.RenderRequest;
35  import javax.portlet.RenderResponse;
36  
37  import org.apache.jetspeed.CommonPortletServices;
38  import org.apache.jetspeed.components.portletregistry.FailedToStorePortletDefinitionException;
39  import org.apache.jetspeed.components.portletregistry.PortletRegistry;
40  import org.apache.jetspeed.components.portletregistry.RegistryException;
41  import org.apache.jetspeed.om.common.GenericMetadata;
42  import org.apache.jetspeed.om.common.LocalizedField;
43  import org.apache.jetspeed.om.common.MutableDescription;
44  import org.apache.jetspeed.om.common.MutableDisplayName;
45  import org.apache.jetspeed.om.common.MutableLanguage;
46  import org.apache.jetspeed.om.common.ParameterComposite;
47  import org.apache.jetspeed.om.common.SecurityRoleRefComposite;
48  import org.apache.jetspeed.om.common.UserAttribute;
49  import org.apache.jetspeed.om.common.portlet.MutablePortletApplication;
50  import org.apache.jetspeed.om.common.portlet.PortletDefinitionComposite;
51  import org.apache.jetspeed.om.common.preference.PreferenceComposite;
52  import org.apache.jetspeed.page.PageManager;
53  import org.apache.jetspeed.portlets.pam.beans.PortletApplicationBean;
54  import org.apache.jetspeed.search.SearchEngine;
55  import org.apache.pluto.om.common.SecurityRoleRef;
56  import org.apache.pluto.om.portlet.ContentType;
57  import org.apache.portals.bridges.beans.TabBean;
58  import org.apache.portals.bridges.common.GenericServletPortlet;
59  
60  /***
61   * This portlet is a tabbed editor user interface for editing both portlet defintions
62   * and portlet applications.
63   *
64   * @author <a href="mailto:jford@apache.com">Jeremy Ford</a>
65   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
66   * @version $Id: PortletApplicationDetail.java 348264 2005-11-22 22:06:45Z taylor $
67   */
68  public class PortletApplicationDetail extends GenericServletPortlet
69  {
70      private static final String PORTLET_ACTION = "portlet_action";
71      private final String VIEW_PA = "portletApplication"; 
72      private final String VIEW_PD = "portletDefinition";
73      
74      private static final String PORTLET_APP_ACTION_PREFIX = "portlet_app.";
75      private static final String PORTLET_ACTION_PREFIX = "portlet.";
76  
77      private PortletContext context;
78      private PortletRegistry registry;
79      private PageManager pageManager;
80      private SearchEngine searchEngine;
81      private LinkedHashMap paTabMap = new LinkedHashMap();
82      private LinkedHashMap pdTabMap = new LinkedHashMap();
83      
84      public void init(PortletConfig config)
85      throws PortletException 
86      {
87          super.init(config);
88          context = getPortletContext();
89          registry = (PortletRegistry)context.getAttribute(CommonPortletServices.CPS_REGISTRY_COMPONENT);
90          searchEngine = (SearchEngine) context.getAttribute(CommonPortletServices.CPS_SEARCH_COMPONENT);
91          pageManager = (PageManager) context.getAttribute(CommonPortletServices.CPS_PAGE_MANAGER_COMPONENT);
92          if (null == registry)
93          {
94              throw new PortletException("Failed to find the Portlet Registry on portlet initialization");
95          }
96          
97          TabBean tb1 = new TabBean("pa_details");
98          TabBean tb2 = new TabBean("pa_metadata");
99          TabBean tb3 = new TabBean("pa_portlets");
100         TabBean tb4 = new TabBean("pa_user_attribtues");
101         
102         paTabMap.put(tb1.getId(), tb1);
103         paTabMap.put(tb2.getId(), tb2);
104         paTabMap.put(tb3.getId(), tb3);
105         paTabMap.put(tb4.getId(), tb4);
106         
107         TabBean tb_1 = new TabBean("pd_details");
108         TabBean tb_2 = new TabBean("pd_metadata");
109         TabBean tb_3 = new TabBean("pd_preferences");
110         TabBean tb_4 = new TabBean("pd_languages");
111         TabBean tb_5 = new TabBean("pd_parameters");
112         TabBean tb_6 = new TabBean("pd_security");
113         TabBean tb_7 = new TabBean("pd_content_type");
114         
115         pdTabMap.put(tb_1.getId(), tb_1);
116         pdTabMap.put(tb_2.getId(), tb_2);
117         pdTabMap.put(tb_3.getId(), tb_3);
118         pdTabMap.put(tb_4.getId(), tb_4);
119         pdTabMap.put(tb_5.getId(), tb_5);
120         pdTabMap.put(tb_6.getId(), tb_6);
121         pdTabMap.put(tb_7.getId(), tb_7);
122     }
123     
124     public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException
125 
126     {
127         response.setContentType("text/html");
128         
129         String paName = (String)
130             request.getPortletSession().getAttribute(PortletApplicationResources.PAM_CURRENT_PA, 
131                                              PortletSession.APPLICATION_SCOPE);
132         
133         MutablePortletApplication pa = registry.getPortletApplication(paName);
134         
135         if (null != pa)
136         {
137             request.setAttribute(VIEW_PA, new PortletApplicationBean(pa));
138             
139             String pdefName = (String) request.getPortletSession().getAttribute(PortletApplicationResources.REQUEST_SELECT_PORTLET, PortletSession.APPLICATION_SCOPE);
140             PortletDefinitionComposite pdef = (PortletDefinitionComposite) pa.getPortletDefinitionByName(pdefName);
141             
142             request.setAttribute(VIEW_PD, pdef);
143             
144             request.setAttribute("tabs", paTabMap.values());
145             request.setAttribute("portlet_tabs", pdTabMap.values());
146             
147             TabBean selectedTab = (TabBean) request.getPortletSession().getAttribute(PortletApplicationResources.REQUEST_SELECT_TAB, PortletSession.APPLICATION_SCOPE);
148             if(selectedTab == null)
149             {
150                 selectedTab = (TabBean) paTabMap.values().iterator().next();
151             }
152             
153             //this supports tabs for portlets
154             if(selectedTab.getId().equals("pa_portlets"))
155             {
156                 TabBean selectedPortletTab = (TabBean) request.getPortletSession().getAttribute("selected_portlet_tab");
157                 if(selectedPortletTab == null)
158                 {
159                     selectedPortletTab = (TabBean) pdTabMap.values().iterator().next();
160                 }
161                 if(selectedPortletTab.getId().equals("pd_security")) {
162                     setupSecurityContraintContext(request, null, pdef);
163                 }
164                 request.setAttribute("selected_portlet_tab", selectedPortletTab);
165             }
166             else if(selectedTab.getId().equals("pa_details"))
167             {
168                 setupSecurityContraintContext(request, pa, null);
169             }
170             
171             request.setAttribute(PortletApplicationResources.REQUEST_SELECT_TAB, selectedTab);
172         }
173 
174         super.doView(request, response);
175     }
176 
177     private void setupSecurityContraintContext(RenderRequest request, MutablePortletApplication pa, PortletDefinitionComposite pdef) throws PortletException
178     {
179         try
180         {
181             List securityContraintRefList = pageManager.getPageSecurity().getSecurityConstraintsDefs();
182             request.setAttribute("securityContraintRefList", securityContraintRefList);
183             if(pdef == null)
184             {
185                 request.setAttribute("currentSecurityConstraintRef", pa.getJetspeedSecurityConstraint());
186             }
187             else
188             {
189                 request.setAttribute("currentSecurityConstraintRef", pdef.getJetspeedSecurityConstraint());
190             }
191         }
192         catch (Exception e)
193         {
194             throw new PortletException("Failed to retrieve security constraint references from "
195                     + (pdef == null ? "portlet application " + pa.getName() : "portlet definition " + pdef.getName()));
196         }
197     }
198 
199     
200     public void processAction(ActionRequest actionRequest, ActionResponse actionResponse) throws PortletException, IOException
201     {
202         String paName = (String)
203         actionRequest.getPortletSession().getAttribute(PortletApplicationResources.PAM_CURRENT_PA, 
204                                              PortletSession.APPLICATION_SCOPE);
205                 
206         String selectedPortlet = actionRequest.getParameter(PortletApplicationResources.REQUEST_SELECT_PORTLET);
207         if(selectedPortlet != null)
208         {
209             actionRequest.getPortletSession().setAttribute(PortletApplicationResources.REQUEST_SELECT_PORTLET, selectedPortlet, PortletSession.APPLICATION_SCOPE);
210         }
211         
212         String selectedTab = actionRequest.getParameter(PortletApplicationResources.REQUEST_SELECT_TAB);
213         if(selectedTab != null)
214         {
215             TabBean tab = (TabBean) paTabMap.get(selectedTab);
216             actionRequest.getPortletSession().setAttribute(PortletApplicationResources.REQUEST_SELECT_TAB, tab, PortletSession.APPLICATION_SCOPE);
217         }
218         
219         String selectedPortletTab = actionRequest.getParameter("selected_portlet_tab");
220         if(selectedPortletTab != null)
221         {
222             TabBean tab = (TabBean) pdTabMap.get(selectedPortletTab);
223             actionRequest.getPortletSession().setAttribute("selected_portlet_tab", tab);
224         }
225         
226         String action = actionRequest.getParameter(PORTLET_ACTION);
227         if(action != null)
228         {
229             MutablePortletApplication pa = registry.getPortletApplication(paName);
230             if(isAppAction(action))
231             {
232                 action = getAction(PORTLET_APP_ACTION_PREFIX, action);
233                 
234                 if(action.endsWith("metadata"))
235                 {
236                     processMetadataAction(actionRequest, actionResponse, pa, null, action);
237                 }
238                 else if(action.endsWith("user_attribute"))
239                 {
240                     processUserAttributeAction(actionRequest, actionResponse, pa, action);
241                 }
242                 else if(action.endsWith("edit_security_constraint"))
243                 {
244                     processSecurityRef(actionRequest, actionResponse, pa, null, action);
245                 }
246                 searchEngine.update(pa);
247             }
248             else if(isPortletAction(action))
249             {
250                 action = getAction(PORTLET_ACTION_PREFIX, action);
251                 String pdefName = (String) actionRequest.getPortletSession().getAttribute(PortletApplicationResources.REQUEST_SELECT_PORTLET, PortletSession.APPLICATION_SCOPE);
252                 
253                 try
254                 {
255                     PortletDefinitionComposite pdef = (PortletDefinitionComposite) pa.getPortletDefinitionByName(pdefName);
256                     if(action.endsWith("metadata"))
257                     {
258                         processMetadataAction(actionRequest, actionResponse, null, pdef, action);
259                     }
260                     else if(action.endsWith("portlet"))
261                     {
262                         processPortletAction(actionRequest, actionResponse, pa, pdef, action);
263                     }
264                     else if(action.endsWith("preference"))
265                     {
266                         processPreferenceAction(actionRequest, actionResponse, pa, pdef, action);
267                     }
268                     else if(action.endsWith("language"))
269                     {
270                         processLanguage(actionRequest, actionResponse, pa, pdef, action);
271                     }
272                     else if(action.endsWith("parameter"))
273                     {
274                         processParameter(actionRequest, actionResponse, pa, pdef, action);
275                     }
276                     else if(action.endsWith("security"))
277                     {
278                         processSecurity(actionRequest, actionResponse, pa, pdef, action);
279                     }
280                     else if(action.endsWith("content_type"))
281                     {
282                         processContentType(actionRequest, actionResponse, pa, pdef, action);
283                     }
284                     else if(action.endsWith("edit_security_constraint")) {
285                         processSecurityRef(actionRequest, actionResponse, null, pdef, action);
286                     }
287                     searchEngine.update(pdef);
288                 }
289                 catch (RegistryException e)
290                 {                    
291                     throw new PortletException("A Registry action has failed.  "+e.getMessage());                    
292                 }
293             }
294         }
295     }
296 
297     private boolean isAppAction(String action)
298     {
299         return action.startsWith(PORTLET_APP_ACTION_PREFIX);
300     }
301     
302     private boolean isPortletAction(String action)
303     {
304         return action.startsWith(PORTLET_ACTION_PREFIX);
305     }
306     
307     private String getAction(String prefix, String action)
308     {
309         return action.substring(prefix.length());
310     }
311     
312     /***
313      * @param actionRequest
314      * @param actionResponse
315      * @param pa
316      * @param action
317      */
318     private void processUserAttributeAction(ActionRequest actionRequest, ActionResponse actionResponse, MutablePortletApplication mpa, String action) 
319     throws PortletException, IOException
320     {
321         boolean modified = false;
322         if(action.equals("edit_user_attribute"))
323         {
324             String userAttrName = "";
325             
326             Iterator userAttrIter = mpa.getUserAttributes().iterator();
327             while (userAttrIter.hasNext())
328             {
329                 UserAttribute userAttr = (UserAttribute) userAttrIter.next();
330                 
331                 userAttrName = userAttr.getName();
332                 String description = actionRequest.getParameter(userAttrName + ":description");
333                 if(!userAttr.getDescription().equals(description))
334                 {
335                     userAttr.setDescription(description);
336                     modified = true;
337                 }
338             }
339         }
340         else if(action.equals("add_user_attribute"))
341         {
342             String userAttrName = actionRequest.getParameter("user_attr_name");
343             String userAttrDesc = actionRequest.getParameter("user_attr_desc");
344             if (userAttrName != null && userAttrName.trim().length() > 0)                
345             {
346                 mpa.addUserAttribute(userAttrName.trim(), userAttrDesc);
347                 modified = true;
348             }
349         }
350         else if(action.equals("remove_user_attribute"))
351         {
352             String[] userAttrNames = actionRequest.getParameterValues("user_attr_id");
353             if(userAttrNames != null)
354             {
355                 String userAttrName = "";
356                 int count = 0;
357                 Iterator userAttrIter = mpa.getUserAttributes().iterator();
358                 while (userAttrIter.hasNext())
359                 {
360                     UserAttribute userAttr = (UserAttribute) userAttrIter.next();
361                     for(int ix = 0; ix < userAttrNames.length; ix++)
362                     {
363                         userAttrName = userAttrNames[ix];
364                         if(userAttr.getName().equals(userAttrName))
365                         {
366                             userAttrIter.remove();
367                             count++;                                
368                             break;
369                         }
370                     }
371                 }                    
372                 modified = count > 0;
373             }
374         }
375         
376         if(modified) 
377         {
378             try 
379             {
380                 registry.updatePortletApplication(mpa);
381             } 
382             catch(RegistryException e)
383             {
384                 throw new PortletException("Failed to update portlet application while performing action " + action, e);
385             }
386         }
387     }
388 
389     /***
390      * @param actionRequest
391      * @param actionResponse
392      * @param pa
393      * @param action
394      * @throws PortletException
395      * @throws IOException
396      */
397     private void processMetadataAction(ActionRequest actionRequest, 
398                                        ActionResponse actionResponse, 
399                                        MutablePortletApplication pa, 
400                                        PortletDefinitionComposite  pd,
401                                        String action)
402     throws PortletException, IOException
403     {
404         GenericMetadata meta = null;                
405 
406         if (pd != null)
407         {
408             meta = pd.getMetadata();
409         }
410         else if(pa != null)
411         {
412             meta = pa.getMetadata();
413         }
414         
415         if (meta == null)
416         {
417             return;
418         }
419         
420         boolean modified = false;
421         if(action.equals("edit_metadata"))
422         {               
423             Iterator fieldsIter = meta.getFields().iterator();            
424             while (fieldsIter.hasNext())
425             {
426                 LocalizedField field = (LocalizedField) fieldsIter.next();
427                 String id = field.getId().toString();
428                 String value = actionRequest.getParameter(id + ":value");
429                 if (value != null)
430                 {
431                     if (!value.equals(field.getValue()))
432                     {
433                         field.setValue(value);
434                         modified = true;
435                     }
436                 }
437             }
438         }
439         else if (action.equals("remove_metadata"))
440         {
441             String[] ids = actionRequest.getParameterValues("metadata_id");            
442             if (ids != null)
443             {
444                 Iterator fieldsIter = meta.getFields().iterator();
445                 int count = 0;                        
446                 while (fieldsIter.hasNext())
447                 {
448                     LocalizedField field = (LocalizedField) fieldsIter.next();
449                     String id = field.getId().toString();
450 
451                     for(int i=0; i<ids.length; i++)
452                     {
453                         String mid = ids[i];
454                         if(mid.equals(id))
455                         {
456                             fieldsIter.remove();
457                             count++;
458                             break;
459                         }
460                     }
461                 }
462                 modified = count > 0;
463             }
464         }
465         else if(action.equals("add_metadata"))
466         {
467             String name = actionRequest.getParameter("name");
468             String value = actionRequest.getParameter("value");
469             String localeParam = actionRequest.getParameter("locale");
470             
471             if(localeParam == null || name.trim().length() == 0)
472             {
473                 localeParam = "en"; //need to default better
474             }
475             Locale locale = new Locale(localeParam);
476             
477             if (name != null && name.trim().length() > 0)                
478             {
479                 meta.addField(locale, name, value);   
480                 modified = true;                             
481             }
482         }
483         
484         if (modified)
485         {
486         	try {
487 	            if (pd == null)
488 	            {                        
489 	                registry.updatePortletApplication(pa);
490 	            }
491 	            else
492 	            {                        
493 	                registry.savePortletDefinition(pd);
494 	            }    
495             }
496             catch(RegistryException e)
497             {
498         	    throw new PortletException("Failed to perform action " + action + " on " 
499         	    		+ (pd == null ? "portlet application " + pa.getName() : "portlet definition " + pd.getName()) );
500             }
501         }
502     }
503         
504     private void processPortletAction(ActionRequest actionRequest, ActionResponse actionResponse, MutablePortletApplication pa, 
505     		PortletDefinitionComposite portlet, String action) throws RegistryException
506     {
507         if(action.equals("edit_portlet"))
508         {
509             String displayNameParam = actionRequest.getParameter("display_name");
510             if(displayNameParam == null)
511             {            
512                 int index = 0;
513                 Iterator displayNameIter = portlet.getDisplayNameSet().iterator();
514                 while (displayNameIter.hasNext())
515                 {
516                     MutableDisplayName displayName = (MutableDisplayName) displayNameIter.next();
517                     displayNameParam = actionRequest.getParameter("display_name:" + index);
518                     
519                     //this should never happen
520                     if(displayNameParam != null)
521                     {
522                         if(displayNameParam.length() == 0)
523                         {
524                             displayNameIter.remove();
525                         }
526                         else if(!displayNameParam.equals(displayName.getDisplayName()))
527                         {
528                             displayName.setDisplayName(displayNameParam);
529                         }
530                     }
531                     index++;
532                 }
533                 String expirationCache = actionRequest.getParameter("expirationCache");
534                 if (expirationCache != null)
535                 {
536                     try
537                     {
538                         Integer.parseInt(expirationCache);
539                         portlet.setExpirationCache(expirationCache);
540                     }
541                     catch (NumberFormatException e)
542                     {}
543                 }
544             }
545             else
546             {
547                 String locale = actionRequest.getParameter("locale");
548                 portlet.addDisplayName(new Locale(locale), displayNameParam);
549             }            
550         }
551         else if(action.equals("remove_portlet"))
552         {
553             //TODO should this be allowed??
554         }
555         else if(action.equals("add_portlet"))
556         {
557             
558         }
559         registry.savePortletDefinition(portlet);
560     }
561     
562     /***
563      * @param actionRequest
564      * @param actionResponse
565      * @param pa
566      * @param pdef
567      * @param action
568      * @throws RegistryException
569      */
570     private void processPreferenceAction(ActionRequest actionRequest, ActionResponse actionResponse, MutablePortletApplication pa, PortletDefinitionComposite portlet, String action) throws RegistryException
571     {
572         if(action.equals("add_preference"))
573         {
574             String name = actionRequest.getParameter("name");
575             String value = actionRequest.getParameter("value");
576             
577             PreferenceComposite pref = (PreferenceComposite) portlet.getPreferenceSet().get(name);
578             if(pref == null)
579             {
580                 portlet.addPreference(name, new String[] { value });
581             }
582             else
583             {
584                 pref.addValue(value);
585             }
586         }
587         else if(action.equals("edit_preference"))
588         {
589             String[] prefNames = actionRequest.getParameterValues("pref_edit_id");
590             for (int i = 0; i < prefNames.length; i++)
591             {
592                 String prefName = prefNames[i];
593                 PreferenceComposite prefComp = (PreferenceComposite) portlet.getPreferenceSet().get(prefName);
594                 String[] values = prefComp.getValueArray();
595                 for (int j = 0; j < values.length; j++)
596                 {
597                     String value = values[j];
598                     String newValue = actionRequest.getParameter(prefName + ":" + j);
599                     if(!value.equals(newValue))
600                     {
601                         prefComp.setValueAt(j, newValue);
602                     }
603                 }
604             }
605         }
606         else if(action.equals("remove_preference"))
607         {
608             String[] prefNames = actionRequest.getParameterValues("pref_remove_id");
609             
610             Iterator prefIter = portlet.getPreferenceSet().iterator();
611             while (prefIter.hasNext())
612             {
613                 PreferenceComposite pref = (PreferenceComposite) prefIter.next();
614                 String name = pref.getName();
615                 
616                 for(int i=0; i<prefNames.length; i++)
617                 {
618                     String prefName = prefNames[i];
619                     if(name.equals(prefName))
620                     {
621                         prefIter.remove();
622                         break;
623                     }
624                 }
625             }
626          // registry.getPersistenceStore().getTransaction().commit();
627         }
628         registry.savePortletDefinition(portlet);
629     }
630     
631     /***
632      * @param actionRequest
633      * @param actionResponse
634      * @param pa
635      * @param pdef
636      * @param action
637      * @throws RegistryException
638      */
639     private void processLanguage(ActionRequest actionRequest, ActionResponse actionResponse, MutablePortletApplication pa, PortletDefinitionComposite portlet, String action) throws RegistryException
640     {
641          if(action.equals("add_language"))
642          {
643              String title = actionRequest.getParameter("title");
644              String shortTitle = actionRequest.getParameter("short_title");
645              String keywords = actionRequest.getParameter("keyword");
646              String locale = actionRequest.getParameter("locale");
647 
648              portlet.addLanguage(title, shortTitle, keywords, new Locale(locale));
649          }
650          else if(action.equals("remove_language"))
651          {
652              String[] removeIds = actionRequest.getParameterValues("language_remove_id");
653              if(removeIds != null)
654              {
655                  int id = 0;
656                  Iterator langIter = portlet.getLanguageSet().iterator();
657                  while (langIter.hasNext())
658                  {
659                      langIter.next();
660 
661                      int currentId = id++;
662                      for(int i=0; i<removeIds.length; i++)
663                      {
664                          String removeId = removeIds[i];
665                          String tempId = "" + currentId;
666                          if(removeId.equals(tempId))
667                          {
668                              langIter.remove();
669                              break;
670                          }
671                      }
672                  }
673              }
674          }
675          else if(action.equals("edit_language"))
676          {
677              String[] editIds = actionRequest.getParameterValues("language_edit_id");
678              if(editIds != null)
679              {
680                  //technically, the size and set of edit ids should be 
681                  //equal to the size and set of the language set
682 
683                  String id;
684                  int index=0;
685                  Iterator langIter = portlet.getLanguageSet().iterator();
686                  while (langIter.hasNext())
687                  {
688                      id = editIds[index];
689                      
690                      String title = actionRequest.getParameter("title:" + id);
691                      String shortTitle = actionRequest.getParameter("short_title:" + id);
692 
693                      //must cast to interface to avoid class loader issues
694                      MutableLanguage lang = (MutableLanguage) langIter.next();
695 
696                      if(!lang.getTitle().equals(title))
697                      {
698                          lang.setTitle(title);
699                      }
700 
701                      Iterator keywordIter = lang.getKeywords();
702                      int keywordIndex = 0;
703                      ArrayList keywordList = new ArrayList();
704                      
705                      while (keywordIter.hasNext())
706                      {
707                          keywordIter.next(); //retrieve the next keyword
708                          String keywordParam = actionRequest.getParameter("keyword:" + id + ":" + keywordIndex);
709 
710                          if(keywordParam != null && keywordParam.length() > 0)
711                          {
712                              keywordList.add(keywordParam);
713                          }
714 
715                          keywordIndex++;
716                      }
717 
718                      lang.setKeywords(keywordList);
719                      if(lang.getShortTitle() == null || !lang.getShortTitle().equals(shortTitle))
720                      {
721                          lang.setShortTitle(shortTitle);
722                      }
723                      index++;
724                  }
725              }
726          }
727          
728          registry.savePortletDefinition(portlet);
729     }
730     
731     /***
732      * @param actionRequest
733      * @param actionResponse
734      * @param pa
735      * @param pdef
736      * @param action
737      * @throws FailedToStorePortletDefinitionException
738      */
739     private void processParameter(ActionRequest actionRequest, ActionResponse actionResponse, MutablePortletApplication pa, PortletDefinitionComposite portlet, String action) throws FailedToStorePortletDefinitionException
740     {
741         if(action.equals("add_parameter"))
742         {
743             String name = actionRequest.getParameter("name");
744             if(name != null)
745             {
746                 String description = actionRequest.getParameter("description");
747                 String locale = actionRequest.getParameter("locale");
748                 
749                 ParameterComposite parameter = (ParameterComposite)portlet.getInitParameterSet().get(name);
750                 if(parameter == null)
751                 {
752                     String value = actionRequest.getParameter("value");
753                     parameter = portlet.addInitParameter(name, value, description, new Locale(locale));
754                 }
755                 else
756                 {
757                     parameter.addDescription(new Locale(locale), description);
758                 }
759             }
760         }
761         else if(action.equals("edit_parameter"))
762         {
763             String[] paramIds = actionRequest.getParameterValues("parameter_edit_id");
764             
765             if(paramIds != null)
766             {
767                 for(int i=0; i<paramIds.length; i++)
768                 {
769                     String paramId = paramIds[i];
770                     ParameterComposite param = (ParameterComposite) portlet.getInitParameterSet().get(paramId);
771                     
772                     String value = actionRequest.getParameter(paramId + ":value");
773                     param.setValue(value);
774                     
775                     int index = 0;
776                     Iterator descIter = param.getDescriptionSet().iterator();
777                     while (descIter.hasNext())
778                     {
779                         MutableDescription description = (MutableDescription) descIter.next();
780                         String descParam = actionRequest.getParameter(paramId + ":description:" + index);
781                         //changing locale not allowed.
782                         
783                         if(descParam != null)
784                         {
785                             if(descParam.length() == 0)
786                             {
787                                 descIter.remove();
788                             }
789                             else if(!descParam.equals(description.getDescription()))
790                             {
791                                 description.setDescription(descParam);
792                             }
793                         }
794                         index++;
795                     }
796                 }
797             }
798         }
799         else if(action.equals("remove_parameter"))
800         {
801             String[] paramIds = actionRequest.getParameterValues("parameter_remove_id");
802             
803             if(paramIds != null)
804             {
805                 Iterator paramIter = portlet.getInitParameterSet().iterator();
806                 while (paramIter.hasNext())
807                 {
808                     ParameterComposite param = (ParameterComposite) paramIter.next();
809                     
810                     for(int i=0; i<paramIds.length; i++)
811                     {
812                         String paramId = paramIds[i];
813                         if(param.getName().equals(paramId))
814                         {
815                             paramIter.remove();
816                             break;
817                         }
818                     }
819                 }
820             }
821         }
822         registry.savePortletDefinition(portlet);
823     }
824     
825     /***
826      * @param actionRequest
827      * @param actionResponse
828      * @param pa
829      * @param pdef
830      * @param action
831      * @throws FailedToStorePortletDefinitionException
832      */
833     private void processSecurity(ActionRequest actionRequest, ActionResponse actionResponse, MutablePortletApplication pa, PortletDefinitionComposite portlet, String action) throws FailedToStorePortletDefinitionException
834     {
835         if(action.equals("add_security"))
836         {
837             String name = actionRequest.getParameter("name");
838             
839             if(name != null)
840             {
841                 String link = actionRequest.getParameter("link");
842                 
843                 SecurityRoleRefComposite securityRoleRef = (SecurityRoleRefComposite) portlet.getInitSecurityRoleRefSet().get(name);
844                 if(securityRoleRef == null && link != null)
845                 {
846                     securityRoleRef = (SecurityRoleRefComposite) portlet.addSecurityRoleRef(name, link);
847                 }
848                 
849                 if(securityRoleRef != null)
850                 {
851                     String description = actionRequest.getParameter("description");
852                     if(description != null && description.length() > 0)
853                     {
854                         String locale = actionRequest.getParameter("locale");
855                         if(locale == null)
856                         {
857                             locale = "en";
858                         }
859                         securityRoleRef.addDescription(new Locale(locale), description);
860                     }
861                 }
862             }
863         }
864         else if(action.equals("edit_security"))
865         {
866             Iterator securityIter = portlet.getInitSecurityRoleRefSet().iterator();
867             while (securityIter.hasNext())
868             {
869                 SecurityRoleRefComposite secRef = (SecurityRoleRefComposite) securityIter.next();
870                 String name = secRef.getRoleName();
871                 
872                 //TODO:  should this be editable
873 //                String newName = actionRequest.getParameter(name + ":name");
874                 String link = actionRequest.getParameter(name + ":link");
875                 
876                 if(!secRef.getRoleLink().equals(link))
877                 {
878                     secRef.setRoleLink(link);
879                 }
880                 
881                 int index = 0;
882                 Iterator descIter = secRef.getDescriptionSet().iterator();
883                 while (descIter.hasNext())
884                 {
885                     MutableDescription description = (MutableDescription) descIter.next();
886                     String descParam = actionRequest.getParameter(name + ":description:" + index);
887                     //changing locale not allowed.
888                     
889                     if(descParam != null)
890                     {
891                         if(descParam.length() == 0)
892                         {
893                             descIter.remove();
894                         }
895                         else if(!descParam.equals(description.getDescription()))
896                         {
897                             description.setDescription(descParam);
898                         }
899                     }
900                     
901                     index++;
902                 }
903             }
904         }
905         else if(action.equals("remove_security"))
906         {
907             String[] securityIds = actionRequest.getParameterValues("security_remove_id");
908             if(securityIds != null)
909             {
910                 for(int i=0; i<securityIds.length; i++)
911                 {
912                     String id = securityIds[i];
913                     SecurityRoleRef secRef = portlet.getInitSecurityRoleRefSet().get(id);
914                     portlet.getInitSecurityRoleRefSet().remove(secRef);
915                 }
916                 /*
917                 Iterator securityIter = portlet.getInitSecurityRoleRefSet()..iterator();
918                 while (securityIter.hasNext())
919                 {
920                     SecurityRoleRefComposite secRef = (SecurityRoleRefComposite) securityIter.next();
921                     for(int i=0; i<securityIds.length; i++)
922                     {
923                         String id = securityIds[i];
924                         if(secRef.getRoleName().equals(id))
925                         {
926                             securityIter.remove();
927                             break;
928                         }
929                     }
930                 }
931                 */
932             }
933         }
934         registry.savePortletDefinition(portlet);
935     }
936     
937     private void processSecurityRef(ActionRequest actionRequest, ActionResponse actionResponse, MutablePortletApplication pa, PortletDefinitionComposite pdef, String action) throws PortletException
938     {
939         String ref = actionRequest.getParameter("security-constraint-ref");
940         String currentRef = "";
941         if(pa != null)
942         {
943             currentRef = pa.getJetspeedSecurityConstraint();
944         }
945         else
946         {
947             currentRef = pdef.getJetspeedSecurityConstraint();
948         }
949         if(currentRef == null) {
950             currentRef = "";
951         }
952         if(!currentRef.equals(ref)) {
953             if(ref.length() == 0) {
954                 ref = null;
955             }
956             
957             try
958             {
959                 if(pa != null)
960                 {
961                     pa.setJetspeedSecurityConstraint(ref);
962                     registry.updatePortletApplication(pa);
963                 }
964                 else
965                 {
966                     pdef.setJetspeedSecurityConstraint(ref);
967                     registry.savePortletDefinition(pdef);
968                 }
969             }
970             catch(RegistryException e)
971             {
972                 throw new PortletException("Failed to perform action " + action + " on " 
973                         + (pdef == null ? "portlet application " + pa.getName() : "portlet definition " + pdef.getName()) );
974             }            
975         }
976     }
977     
978     /***
979      * @param actionRequest
980      * @param actionResponse
981      * @param pa
982      * @param pdef
983      * @param action
984      * @throws FailedToStorePortletDefinitionException
985      */
986     private void processContentType(ActionRequest actionRequest, ActionResponse actionResponse, 
987     		MutablePortletApplication pa, PortletDefinitionComposite portlet, String action) throws FailedToStorePortletDefinitionException
988     {
989         if(action.equals("add_content_type"))
990         {
991             String contentType = actionRequest.getParameter("content_type");
992             if(contentType != null)
993             {
994                 ArrayList allModes = new ArrayList();
995                 String[] modes = actionRequest.getParameterValues("mode");
996                 if(modes != null)
997                 {
998                     for(int i=0; i<modes.length; i++)
999                     {
1000                         String mode = modes[i];
1001                         //contentTypeImpl.addPortletMode(mode);
1002                         allModes.add(new PortletMode(mode));
1003                     }
1004                 }
1005 
1006                 String customModes = actionRequest.getParameter("custom_modes");
1007                 StringTokenizer tok = new StringTokenizer(customModes, ",");
1008                 while (tok.hasMoreTokens())
1009                 {
1010                     //contentTypeImpl.addPortletMode(tok.nextToken());
1011                     allModes.add(tok.nextToken());
1012                 }
1013                 
1014                 portlet.addContentType(contentType, allModes);
1015             }
1016         }
1017         else if(action.equals("remove_content_type"))
1018         {
1019             String[] contentIds = actionRequest.getParameterValues("content_type_remove_id");
1020             if(contentIds != null)
1021             {
1022                 Iterator contentIter = portlet.getContentTypeSet().iterator();
1023                 while (contentIter.hasNext())
1024                 {
1025                     ContentType contentType = (ContentType) contentIter.next();
1026                     for(int i=0; i<contentIds.length; i++)
1027                     {
1028                         String id = contentIds[i];
1029                         if(contentType.getContentType().equals(id))
1030                         {
1031                             contentIter.remove();
1032                             break;
1033                         }
1034                     }
1035                 }
1036             }
1037         }
1038         registry.savePortletDefinition(portlet);
1039 //      registry.getPersistenceStore().getTransaction().commit();
1040     }
1041     
1042     /*
1043     private String createXml(MutablePortletApplication pa)
1044     {
1045         StringBuffer buffer = new StringBuffer();
1046         
1047         buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
1048         
1049         //TODO:  add namespace
1050         buffer.append("<portlet-app id=\"");
1051         buffer.append(pa.getApplicationIdentifier());
1052         buffer.append("\" version=\"");
1053         buffer.append(pa.getVersion());
1054         buffer.append("\">\n");
1055         
1056         Iterator portletDefsIter = pa.getPortletDefinitions().iterator();
1057         while (portletDefsIter.hasNext())
1058         {
1059             PortletDefinitionComposite pDef = (PortletDefinitionComposite) portletDefsIter.next();
1060             buffer.append(createPortletDefinitionXml(pDef));
1061         }
1062         
1063         buffer.append("</portlet-app>\n");
1064         
1065         return buffer.toString();
1066     }
1067     */
1068     
1069     /*
1070     private String createPortletDefinitionXml(PortletDefinitionComposite pDef)
1071     {
1072         StringBuffer buffer = new StringBuffer();
1073         
1074         buffer.append("<portlet id=\"");
1075         buffer.append(pDef.getPortletIdentifier());
1076         buffer.append("\">\n");
1077         
1078         Iterator paramIter = pDef.getInitParameterSet().iterator();
1079         while (paramIter.hasNext())
1080         {
1081             ParameterComposite param = (ParameterComposite) paramIter.next();
1082             buffer.append("<init-param>\n");
1083             
1084             addDescriptions(buffer, param.getDescriptionSet());
1085             
1086             buffer.append("\t<name>");
1087             buffer.append(param.getName());
1088             buffer.append("</name>\n");
1089             buffer.append("\t<value>");
1090             buffer.append(param.getValue());
1091             buffer.append("</value>\n");
1092             buffer.append("</init-param>\n");
1093         }
1094         
1095         buffer.append("\t<portlet-name>");
1096         buffer.append(pDef.getName());
1097         buffer.append("</portlet-name>\n");
1098         
1099         Iterator displayNameIter = pDef.getDisplayNameSet().iterator();
1100         while (displayNameIter.hasNext())
1101         {
1102             MutableDisplayName displayName = (MutableDisplayName) displayNameIter.next();
1103             buffer.append("\t<display-name");
1104             if(displayName.getLocale() != null)
1105             {
1106                 buffer.append(" xml:lang=\"");
1107                 buffer.append(displayName.getLocale().getCountry());
1108                 buffer.append("\"");
1109             }
1110             buffer.append(">");
1111             buffer.append(displayName.getDisplayName());
1112             buffer.append("</display-name>\n");
1113         }
1114         
1115         addDescriptions(buffer, pDef.getDescriptionSet());
1116         
1117         buffer.append("\t<portlet-class>");
1118         buffer.append(pDef.getClassName());
1119         buffer.append("</portlet-class>\n");
1120         
1121         buffer.append("\t<expiration-cache>");
1122         buffer.append(pDef.getExpirationCache());
1123         buffer.append("</expiration-cache>\n");
1124         
1125         
1126         Iterator contentTypeIter = pDef.getContentTypeSet().iterator();
1127         while (contentTypeIter.hasNext())
1128         {
1129             buffer.append("\t<supports>\n");
1130             ContentType contentType = (ContentType) contentTypeIter.next();
1131             buffer.append("\t\t<mime-type>\n");
1132             buffer.append(contentType.getContentType());
1133             buffer.append("</mime-type>\n");
1134             
1135             Iterator modeIter = contentType.getPortletModes();
1136             while (modeIter.hasNext())
1137             {
1138                 PortletMode mode = (PortletMode) modeIter.next();
1139                 buffer.append("\t\t<portlet-mode>");
1140                 buffer.append(mode.toString());
1141                 buffer.append("</portlet-mode>\n");
1142             }
1143             
1144             buffer.append("</supports>");
1145         }
1146         
1147         
1148         
1149         String resourceBundle = pDef.getResourceBundle();
1150         if(resourceBundle == null)
1151         {
1152 	        //<portlet-info>
1153 	        //StringBuffer supportedLocaleBuffer = new StringBuffer();
1154 	        StringBuffer portletInfoBuffer = new StringBuffer();
1155 	        
1156 	        Iterator langIter = pDef.getLanguageSet().iterator();
1157 	        while (langIter.hasNext())
1158 	        {
1159 	            MutableLanguage lang = (MutableLanguage) langIter.next();
1160 	            
1161 	            //supportedLocaleBuffer.append("\t<supported-locale>");
1162 	            //supportedLocaleBuffer.append(lang.getLocale().getCountry());
1163 	            //supportedLocaleBuffer.append("</supported-locale>\n");
1164 	            
1165 	            
1166 	            //lang.
1167 	            portletInfoBuffer.append("\t<portlet-info>\n");
1168 	            portletInfoBuffer.append("\t\t<title>");
1169 	            portletInfoBuffer.append(lang.getTitle());
1170 	            portletInfoBuffer.append("</title>\n");
1171 	            if(lang.getShortTitle() != null)
1172 	            {
1173 	                portletInfoBuffer.append("\t\t<short-title>");
1174 	                portletInfoBuffer.append(lang.getShortTitle());
1175 	                portletInfoBuffer.append("</short-title>\n");
1176 	            }
1177 	            Iterator keywordIter = lang.getKeywords();
1178 	            if(keywordIter.hasNext())
1179 	            {
1180 	                portletInfoBuffer.append("\t\t<keywords>");
1181 		            while (keywordIter.hasNext())
1182 		            {
1183 		                String keyword = (String) keywordIter.next();
1184 		                portletInfoBuffer.append(keyword);
1185 		                if(keywordIter.hasNext())
1186 		                {
1187 		                    portletInfoBuffer.append(",");
1188 		                }
1189 		            }
1190 		            portletInfoBuffer.append("</keywords>\n");
1191 	            }
1192 	            portletInfoBuffer.append("\t</portlet-info>\n");
1193 	        }
1194 	        
1195 //	        buffer.append(supportedLocaleBuffer);
1196 	        buffer.append(portletInfoBuffer);
1197         }
1198         else
1199         {
1200             Iterator supportIter = pDef.getSupportedLocales().iterator();
1201             while (supportIter.hasNext())
1202             {
1203                 Locale locale = (Locale) supportIter.next();
1204                 buffer.append("\t<supported-locale>");
1205                 buffer.append(locale.getCountry());
1206                 buffer.append("<supported-locale>\n");
1207             }
1208         }
1209         
1210         buffer.append("\t<portlet-preferences>\n");
1211         Iterator prefIter = pDef.getPreferenceSet().iterator();
1212         while (prefIter.hasNext())
1213         {
1214             PreferenceComposite pref = (PreferenceComposite) prefIter.next();
1215             buffer.append("\t\t<preference>\n");
1216             buffer.append("\t\t\t<name>);");
1217             buffer.append(pref.getName());
1218             buffer.append("</name>\n");
1219             String[] values = pref.getValueArray();
1220             for (int i = 0; i < values.length; i++)
1221             {
1222                 String value = values[i];
1223                 buffer.append("\t\t\t<value>");
1224                 buffer.append(value);
1225                 buffer.append("</value>\n");
1226             }
1227             
1228             buffer.append("\t\t</preference>\n");
1229         }
1230         buffer.append("</portlet-preferences>");
1231         
1232         buffer.append("</portlet>\n");
1233         
1234         return buffer.toString();
1235     }
1236     */
1237     
1238     /*
1239     private void addDescriptions(StringBuffer buffer, DescriptionSet descriptions)
1240     {
1241         Iterator descIter = descriptions.iterator();
1242         MutableDescription desc = (MutableDescription) descIter.next();
1243         buffer.append("\t<description");
1244         if(desc.getLocale() != null)
1245         {
1246             buffer.append(" xml:lang=\"");
1247             buffer.append(desc.getLocale().getCountry());
1248             buffer.append("\"");
1249         }
1250         buffer.append(">");
1251         buffer.append(desc.getDescription());
1252         buffer.append("</description>\n");
1253     }
1254     */
1255 }