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.entityeditor;
18  
19  import java.io.IOException;
20  import java.util.Arrays;
21  
22  import javax.portlet.ActionRequest;
23  import javax.portlet.ActionResponse;
24  import javax.portlet.PortletConfig;
25  import javax.portlet.PortletContext;
26  import javax.portlet.PortletException;
27  
28  import org.apache.jetspeed.CommonPortletServices;
29  import org.apache.jetspeed.components.portletentity.PortletEntityAccessComponent;
30  import org.apache.jetspeed.components.portletentity.PortletEntityNotStoredException;
31  import org.apache.jetspeed.om.common.preference.PreferenceComposite;
32  import org.apache.jetspeed.om.common.preference.PreferenceSetComposite;
33  import org.apache.pluto.om.entity.PortletEntity;
34  import org.apache.portals.bridges.velocity.GenericVelocityPortlet;
35  
36  public class PortletEntityEditorPortlet extends GenericVelocityPortlet
37  {
38      
39      private PortletEntityAccessComponent entityAccess;
40      /* (non-Javadoc)
41       * @see org.apache.portals.bridges.velocity.GenericVelocityPortlet#init(javax.portlet.PortletConfig)
42       */
43      public void init(PortletConfig config) throws PortletException
44      {
45          super.init(config);
46          PortletContext context = getPortletContext();
47          entityAccess = (PortletEntityAccessComponent)context.getAttribute(CommonPortletServices.CPS_ENTITY_ACCESS_COMPONENT);
48      }
49  
50      /* (non-Javadoc)
51       * @see org.apache.portals.bridges.velocity.GenericVelocityPortlet#processAction(javax.portlet.ActionRequest, javax.portlet.ActionResponse)
52       */
53      public void processAction(ActionRequest request, ActionResponse response) throws PortletException, IOException
54      {
55          String action = request.getParameter("action");
56          
57          if(action == null)
58          {
59              throw new PortletException("This editor requires an action parameter");
60          }
61          else if(action.equals("updateValue"))
62          {
63              doUpdateValue(request, response);
64          }
65          else if(action.equals("addValue"))
66          {
67              doAddValue(request, response);
68          }
69          else if(action.equals("removeValue"))
70          {
71              doRemoveValue(request, response);
72          }
73          else if(action.equals("addPref"))
74          {
75              doAddPref(request, response);
76          }
77          else if(action.equals("removePref"))
78          {
79              doRemovePref(request, response);
80          }
81          else
82          {
83              throw new PortletException("'"+action+"' is not a valid editor action.");
84          }
85      }
86      
87      protected final void doAddPref(ActionRequest request, ActionResponse response) throws PortletException
88      {
89          PortletEntity entity = getPortletEntity(request);
90          String newName = request.getParameter("newPreferenceName");
91          if(newName == null || newName.length() < 1)
92          {
93              throw new PortletException("You must specify a name for a new preference.");
94          }
95          
96          String[] newValues = request.getParameterValues("newPreferenceValue");
97          if(newValues == null || newValues.length == 0)
98          {
99              throw new PortletException("You must specfiy a value for the new preference "+newName);
100         }
101         
102         PreferenceSetComposite prefSet = (PreferenceSetComposite) entity.getPreferenceSet();
103         prefSet.add(newName, Arrays.asList(newValues));
104         try
105         {
106             entityAccess.storePortletEntity(entity);
107         }
108         catch (PortletEntityNotStoredException e)
109         {
110             throw new PortletException(e.getMessage(), e);
111         }
112     }
113     
114     protected final void doAddValue(ActionRequest request, ActionResponse response) throws PortletException
115     {
116         PortletEntity entity = getPortletEntity(request);
117         String prefString= request.getParameter("selectedPref");
118         String newValue = request.getParameter("newPrefValue");
119         String prefName = prefString.split("::")[1];
120         PreferenceComposite pref = (PreferenceComposite) entity.getPreferenceSet().get(prefName);
121         pref.addValue(newValue);
122     }
123     
124     protected final void doRemovePref(ActionRequest request, ActionResponse response) throws PortletException
125     {
126         PortletEntity entity = getPortletEntity(request);
127         String prefString= request.getParameter("selectedPref");
128         String prefName = prefString.split("::")[1];
129         ((PreferenceSetComposite)entity.getPreferenceSet()).remove(prefName);
130         
131     }
132     
133     protected final void doUpdateValue(ActionRequest request, ActionResponse response) throws PortletException
134     {
135         PortletEntity entity = getPortletEntity(request);
136         String prefString= request.getParameter("selectedPref");
137         String updatedValue = request.getParameter("selectedPrefValue");
138         if(updatedValue.trim().length() == 0)
139         {
140             throw new PortletException("Preference values cannot be empty.");
141         }
142         String[] info = prefString.split("::");
143         String prefName = info[1];
144         int valueIndex = Integer.parseInt(info[2]);
145         PreferenceComposite pref = (PreferenceComposite) entity.getPreferenceSet().get(prefName);
146         pref.setValueAt(valueIndex, updatedValue);
147     }
148     
149     protected final void doRemoveValue(ActionRequest request, ActionResponse response) throws PortletException
150     {
151         PortletEntity entity = getPortletEntity(request);
152         String prefString= request.getParameter("selectedPref");
153         String[] info = prefString.split("::");
154         String prefName = info[1];
155         int valueIndex = Integer.parseInt(info[2]);
156         PreferenceComposite pref = (PreferenceComposite) entity.getPreferenceSet().get(prefName);
157         pref.removeValueAt(valueIndex);
158     }
159 
160     
161     protected final PortletEntity getPortletEntity(ActionRequest request) throws PortletException
162     {
163         String entityId = request.getParameter("portletEntityId");
164         if(entityId == null)
165         {
166             throw new PortletException("There was no 'entityId' parameter specified in the request.");
167         }
168         else
169         {
170            return entityAccess.getPortletEntity(entityId);            
171         }
172     }
173 }