1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.expando.action;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.servlet.SessionErrors;
28  import com.liferay.portal.kernel.util.Constants;
29  import com.liferay.portal.kernel.util.GetterUtil;
30  import com.liferay.portal.kernel.util.ParamUtil;
31  import com.liferay.portal.kernel.util.StringPool;
32  import com.liferay.portal.kernel.util.StringUtil;
33  import com.liferay.portal.kernel.util.UnicodeProperties;
34  import com.liferay.portal.model.User;
35  import com.liferay.portal.security.auth.PrincipalException;
36  import com.liferay.portal.struts.PortletAction;
37  import com.liferay.portal.util.PortalUtil;
38  import com.liferay.portlet.expando.ColumnNameException;
39  import com.liferay.portlet.expando.ColumnTypeException;
40  import com.liferay.portlet.expando.DuplicateColumnNameException;
41  import com.liferay.portlet.expando.NoSuchColumnException;
42  import com.liferay.portlet.expando.ValueDataException;
43  import com.liferay.portlet.expando.model.ExpandoBridge;
44  import com.liferay.portlet.expando.model.ExpandoColumnConstants;
45  import com.liferay.portlet.expando.model.impl.ExpandoBridgeImpl;
46  import com.liferay.portlet.expando.service.ExpandoColumnServiceUtil;
47  import com.liferay.portlet.expando.util.ExpandoBridgeIndexer;
48  
49  import java.io.Serializable;
50  
51  import java.util.ArrayList;
52  import java.util.Calendar;
53  import java.util.Enumeration;
54  import java.util.List;
55  
56  import javax.portlet.ActionRequest;
57  import javax.portlet.ActionResponse;
58  import javax.portlet.PortletConfig;
59  import javax.portlet.PortletRequest;
60  import javax.portlet.RenderRequest;
61  import javax.portlet.RenderResponse;
62  
63  import org.apache.struts.action.ActionForm;
64  import org.apache.struts.action.ActionForward;
65  import org.apache.struts.action.ActionMapping;
66  
67  /**
68   * <a href="EditExpandoAction.java.html"><b><i>View Source</i></b></a>
69   *
70   * @author Raymond Augé
71   *
72   */
73  public class EditExpandoAction extends PortletAction {
74  
75      public static Serializable getValue(
76              PortletRequest portletRequest, String name, int type)
77          throws PortalException, SystemException {
78  
79          Serializable value = null;
80  
81          if (type == ExpandoColumnConstants.BOOLEAN) {
82              value = ParamUtil.getBoolean(portletRequest, name);
83          }
84          else if (type == ExpandoColumnConstants.BOOLEAN_ARRAY) {
85          }
86          else if (type == ExpandoColumnConstants.DATE) {
87              User user = PortalUtil.getUser(portletRequest);
88  
89              int valueDateMonth = ParamUtil.getInteger(
90                  portletRequest, name + "Month");
91              int valueDateDay = ParamUtil.getInteger(
92                  portletRequest, name + "Day");
93              int valueDateYear = ParamUtil.getInteger(
94                  portletRequest, name + "Year");
95              int valueDateHour = ParamUtil.getInteger(
96                  portletRequest, name + "Hour");
97              int valueDateMinute = ParamUtil.getInteger(
98                  portletRequest, name + "Minute");
99              int valueDateAmPm = ParamUtil.getInteger(
100                 portletRequest, name + "AmPm");
101 
102             if (valueDateAmPm == Calendar.PM) {
103                 valueDateHour += 12;
104             }
105 
106             value = PortalUtil.getDate(
107                 valueDateMonth, valueDateDay, valueDateYear, valueDateHour,
108                 valueDateMinute, user.getTimeZone(), new ValueDataException());
109         }
110         else if (type == ExpandoColumnConstants.DATE_ARRAY) {
111         }
112         else if (type == ExpandoColumnConstants.DOUBLE) {
113             value = ParamUtil.getDouble(portletRequest, name);
114         }
115         else if (type == ExpandoColumnConstants.DOUBLE_ARRAY) {
116             String[] values = StringUtil.split(
117                 ParamUtil.getString(portletRequest, name), StringPool.NEW_LINE);
118 
119             value = GetterUtil.getDoubleValues(values);
120         }
121         else if (type == ExpandoColumnConstants.FLOAT) {
122             value = ParamUtil.getFloat(portletRequest, name);
123         }
124         else if (type == ExpandoColumnConstants.FLOAT_ARRAY) {
125             String[] values = StringUtil.split(
126                 ParamUtil.getString(portletRequest, name), StringPool.NEW_LINE);
127 
128             value = GetterUtil.getFloatValues(values);
129         }
130         else if (type == ExpandoColumnConstants.INTEGER) {
131             value = ParamUtil.getInteger(portletRequest, name);
132         }
133         else if (type == ExpandoColumnConstants.INTEGER_ARRAY) {
134             String[] values = StringUtil.split(
135                 ParamUtil.getString(portletRequest, name), StringPool.NEW_LINE);
136 
137             value = GetterUtil.getIntegerValues(values);
138         }
139         else if (type == ExpandoColumnConstants.LONG) {
140             value = ParamUtil.getLong(portletRequest, name);
141         }
142         else if (type == ExpandoColumnConstants.LONG_ARRAY) {
143             String[] values = StringUtil.split(
144                 ParamUtil.getString(portletRequest, name), StringPool.NEW_LINE);
145 
146             value = GetterUtil.getLongValues(values);
147         }
148         else if (type == ExpandoColumnConstants.SHORT) {
149             value = ParamUtil.getShort(portletRequest, name);
150         }
151         else if (type == ExpandoColumnConstants.SHORT_ARRAY) {
152             String[] values = StringUtil.split(
153                 ParamUtil.getString(portletRequest, name), StringPool.NEW_LINE);
154 
155             value = GetterUtil.getShortValues(values);
156         }
157         else if (type == ExpandoColumnConstants.STRING_ARRAY) {
158             value = StringUtil.split(
159                 ParamUtil.getString(portletRequest, name), StringPool.NEW_LINE);
160         }
161         else {
162             value = ParamUtil.getString(portletRequest, name);
163         }
164 
165         return value;
166     }
167 
168     public void processAction(
169             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
170             ActionRequest actionRequest, ActionResponse actionResponse)
171         throws Exception {
172 
173         String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
174 
175         try {
176             if (cmd.equals(Constants.ADD)) {
177                 addExpando(actionRequest);
178             }
179             else if (cmd.equals(Constants.DELETE)) {
180                 deleteExpando(actionRequest);
181             }
182             else if (cmd.equals(Constants.UPDATE)) {
183                 updateExpando(actionRequest);
184             }
185 
186             sendRedirect(actionRequest, actionResponse);
187         }
188         catch (Exception e) {
189             if (e instanceof NoSuchColumnException ||
190                 e instanceof PrincipalException) {
191 
192                 SessionErrors.add(actionRequest, e.getClass().getName());
193 
194                 setForward(actionRequest, "portlet.expando.error");
195             }
196             else if (e instanceof ColumnNameException ||
197                      e instanceof ColumnTypeException ||
198                      e instanceof DuplicateColumnNameException ||
199                      e instanceof ValueDataException) {
200 
201                 SessionErrors.add(actionRequest, e.getClass().getName());
202             }
203             else {
204                 throw e;
205             }
206         }
207     }
208 
209     public ActionForward render(
210             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
211             RenderRequest renderRequest, RenderResponse renderResponse)
212         throws Exception {
213 
214         try {
215             ActionUtil.getColumn(renderRequest);
216         }
217         catch (Exception e) {
218             if (e instanceof NoSuchColumnException ||
219                 e instanceof PrincipalException) {
220 
221                 SessionErrors.add(renderRequest, e.getClass().getName());
222 
223                 return mapping.findForward("portlet.expando.error");
224             }
225             else {
226                 throw e;
227             }
228         }
229 
230         return mapping.findForward(
231             getForward(renderRequest, "portlet.expando.edit_expando"));
232     }
233 
234     protected void addExpando(ActionRequest actionRequest) throws Exception {
235         String modelResource = ParamUtil.getString(
236             actionRequest, "modelResource");
237         long resourcePrimKey = ParamUtil.getLong(
238             actionRequest, "resourcePrimKey");
239 
240         String name = ParamUtil.getString(actionRequest, "name");
241         String preset = ParamUtil.getString(actionRequest, "type");
242 
243         ExpandoBridge expandoBridge = new ExpandoBridgeImpl(
244             modelResource, resourcePrimKey);
245 
246         if (preset.startsWith("Preset")) {
247             addPresetExpando(actionRequest, expandoBridge, preset, name);
248         }
249         else {
250             int type = ParamUtil.getInteger(actionRequest, "type");
251 
252             expandoBridge.addAttribute(name, type);
253 
254             updateProperties(actionRequest, expandoBridge, name);
255         }
256     }
257 
258     protected void addPresetExpando(
259             ActionRequest actionRequest, ExpandoBridge expandoBridge,
260             String preset, String name)
261         throws Exception {
262 
263         int type = 0;
264         UnicodeProperties properties = expandoBridge.getAttributeProperties(
265             name);
266 
267         if (preset.equals("PresetSelectionIntegerArray()")) {
268             type = ExpandoColumnConstants.INTEGER_ARRAY;
269             properties.setProperty(
270                 ExpandoColumnConstants.PROPERTY_SELECTION,
271                 Boolean.TRUE.toString());
272         }
273         else if (preset.equals("PresetSelectionDoubleArray()")) {
274             type = ExpandoColumnConstants.DOUBLE_ARRAY;
275             properties.setProperty(
276                 ExpandoColumnConstants.PROPERTY_SELECTION,
277                 Boolean.TRUE.toString());
278         }
279         else if (preset.equals("PresetSelectionStringArray()")) {
280             type = ExpandoColumnConstants.STRING_ARRAY;
281             properties.setProperty(
282                 ExpandoColumnConstants.PROPERTY_SELECTION,
283                 Boolean.TRUE.toString());
284         }
285         else if (preset.equals("PresetTextBox()")) {
286             type = ExpandoColumnConstants.STRING;
287             properties.setProperty(
288                 ExpandoColumnConstants.PROPERTY_HEIGHT, "105");
289             properties.setProperty(
290                 ExpandoColumnConstants.PROPERTY_WIDTH, "450");
291         }
292         else if (preset.equals("PresetTextBoxIndexed()")) {
293             type = ExpandoColumnConstants.STRING;
294             properties.setProperty(
295                 ExpandoColumnConstants.PROPERTY_HEIGHT, "105");
296             properties.setProperty(
297                 ExpandoColumnConstants.PROPERTY_WIDTH, "450");
298             properties.setProperty(
299                 ExpandoBridgeIndexer.INDEXABLE, Boolean.TRUE.toString());
300         }
301         else if (preset.equals("PresetTextFieldSecret()")) {
302             type = ExpandoColumnConstants.STRING;
303             properties.setProperty(
304                 ExpandoColumnConstants.PROPERTY_SECRET,
305                 Boolean.TRUE.toString());
306         }
307         else {
308             type = ExpandoColumnConstants.STRING;
309             properties.setProperty(
310                 ExpandoBridgeIndexer.INDEXABLE, Boolean.TRUE.toString());
311         }
312 
313         expandoBridge.addAttribute(name, type);
314 
315         expandoBridge.setAttributeProperties(name, properties);
316     }
317 
318     protected void deleteExpando(ActionRequest actionRequest) throws Exception {
319         long columnId = ParamUtil.getLong(actionRequest, "columnId");
320 
321         ExpandoColumnServiceUtil.deleteColumn(columnId);
322     }
323 
324     protected void updateExpando(ActionRequest actionRequest) throws Exception {
325         String modelResource = ParamUtil.getString(
326             actionRequest, "modelResource");
327         long resourcePrimKey = ParamUtil.getLong(
328             actionRequest, "resourcePrimKey");
329 
330         String name = ParamUtil.getString(actionRequest, "name");
331         int type = ParamUtil.getInteger(actionRequest, "type");
332 
333         Serializable defaultValue = getValue(
334             actionRequest, "defaultValue", type);
335 
336         ExpandoBridge expandoBridge = new ExpandoBridgeImpl(
337             modelResource, resourcePrimKey);
338 
339         expandoBridge.setAttributeDefault(name, defaultValue);
340 
341         updateProperties(actionRequest, expandoBridge, name);
342     }
343 
344     protected void updateProperties(
345             ActionRequest actionRequest, ExpandoBridge expandoBridge,
346             String name)
347         throws Exception {
348 
349         Enumeration<String> enu = actionRequest.getParameterNames();
350 
351         UnicodeProperties properties = expandoBridge.getAttributeProperties(
352             name);
353 
354         List<String> propertyNames = new ArrayList<String>();
355 
356         while (enu.hasMoreElements()) {
357             String param = enu.nextElement();
358 
359             if (param.indexOf("PropertyName(") != -1) {
360                 String propertyName = ParamUtil.getString(actionRequest, param);
361 
362                 propertyNames.add(propertyName);
363             }
364         }
365 
366         for (String propertyName : propertyNames) {
367             String value = ParamUtil.getString(
368                 actionRequest, "Property(" + propertyName + ")");
369 
370             properties.setProperty(propertyName, value);
371         }
372 
373         expandoBridge.setAttributeProperties(name, properties);
374     }
375 
376 }