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.service.ExpandoColumnServiceUtil;
46  import com.liferay.portlet.expando.util.ExpandoBridgeFactoryUtil;
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  public class EditExpandoAction extends PortletAction {
73  
74      public static Serializable getValue(
75              PortletRequest portletRequest, String name, int type)
76          throws PortalException, SystemException {
77  
78          Serializable value = null;
79  
80          if (type == ExpandoColumnConstants.BOOLEAN) {
81              value = ParamUtil.getBoolean(portletRequest, name);
82          }
83          else if (type == ExpandoColumnConstants.BOOLEAN_ARRAY) {
84          }
85          else if (type == ExpandoColumnConstants.DATE) {
86              User user = PortalUtil.getUser(portletRequest);
87  
88              int valueDateMonth = ParamUtil.getInteger(
89                  portletRequest, name + "Month");
90              int valueDateDay = ParamUtil.getInteger(
91                  portletRequest, name + "Day");
92              int valueDateYear = ParamUtil.getInteger(
93                  portletRequest, name + "Year");
94              int valueDateHour = ParamUtil.getInteger(
95                  portletRequest, name + "Hour");
96              int valueDateMinute = ParamUtil.getInteger(
97                  portletRequest, name + "Minute");
98              int valueDateAmPm = ParamUtil.getInteger(
99                  portletRequest, name + "AmPm");
100 
101             if (valueDateAmPm == Calendar.PM) {
102                 valueDateHour += 12;
103             }
104 
105             value = PortalUtil.getDate(
106                 valueDateMonth, valueDateDay, valueDateYear, valueDateHour,
107                 valueDateMinute, user.getTimeZone(), new ValueDataException());
108         }
109         else if (type == ExpandoColumnConstants.DATE_ARRAY) {
110         }
111         else if (type == ExpandoColumnConstants.DOUBLE) {
112             value = ParamUtil.getDouble(portletRequest, name);
113         }
114         else if (type == ExpandoColumnConstants.DOUBLE_ARRAY) {
115             String[] values = StringUtil.split(
116                 ParamUtil.getString(portletRequest, name), StringPool.NEW_LINE);
117 
118             value = GetterUtil.getDoubleValues(values);
119         }
120         else if (type == ExpandoColumnConstants.FLOAT) {
121             value = ParamUtil.getFloat(portletRequest, name);
122         }
123         else if (type == ExpandoColumnConstants.FLOAT_ARRAY) {
124             String[] values = StringUtil.split(
125                 ParamUtil.getString(portletRequest, name), StringPool.NEW_LINE);
126 
127             value = GetterUtil.getFloatValues(values);
128         }
129         else if (type == ExpandoColumnConstants.INTEGER) {
130             value = ParamUtil.getInteger(portletRequest, name);
131         }
132         else if (type == ExpandoColumnConstants.INTEGER_ARRAY) {
133             String[] values = StringUtil.split(
134                 ParamUtil.getString(portletRequest, name), StringPool.NEW_LINE);
135 
136             value = GetterUtil.getIntegerValues(values);
137         }
138         else if (type == ExpandoColumnConstants.LONG) {
139             value = ParamUtil.getLong(portletRequest, name);
140         }
141         else if (type == ExpandoColumnConstants.LONG_ARRAY) {
142             String[] values = StringUtil.split(
143                 ParamUtil.getString(portletRequest, name), StringPool.NEW_LINE);
144 
145             value = GetterUtil.getLongValues(values);
146         }
147         else if (type == ExpandoColumnConstants.SHORT) {
148             value = ParamUtil.getShort(portletRequest, name);
149         }
150         else if (type == ExpandoColumnConstants.SHORT_ARRAY) {
151             String[] values = StringUtil.split(
152                 ParamUtil.getString(portletRequest, name), StringPool.NEW_LINE);
153 
154             value = GetterUtil.getShortValues(values);
155         }
156         else if (type == ExpandoColumnConstants.STRING_ARRAY) {
157             value = StringUtil.split(
158                 ParamUtil.getString(portletRequest, name), StringPool.NEW_LINE);
159         }
160         else {
161             value = ParamUtil.getString(portletRequest, name);
162         }
163 
164         return value;
165     }
166 
167     public void processAction(
168             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
169             ActionRequest actionRequest, ActionResponse actionResponse)
170         throws Exception {
171 
172         String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
173 
174         try {
175             if (cmd.equals(Constants.ADD)) {
176                 addExpando(actionRequest);
177             }
178             else if (cmd.equals(Constants.DELETE)) {
179                 deleteExpando(actionRequest);
180             }
181             else if (cmd.equals(Constants.UPDATE)) {
182                 updateExpando(actionRequest);
183             }
184 
185             sendRedirect(actionRequest, actionResponse);
186         }
187         catch (Exception e) {
188             if (e instanceof NoSuchColumnException ||
189                 e instanceof PrincipalException) {
190 
191                 SessionErrors.add(actionRequest, e.getClass().getName());
192 
193                 setForward(actionRequest, "portlet.expando.error");
194             }
195             else if (e instanceof ColumnNameException ||
196                      e instanceof ColumnTypeException ||
197                      e instanceof DuplicateColumnNameException ||
198                      e instanceof ValueDataException) {
199 
200                 SessionErrors.add(actionRequest, e.getClass().getName());
201             }
202             else {
203                 throw e;
204             }
205         }
206     }
207 
208     public ActionForward render(
209             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
210             RenderRequest renderRequest, RenderResponse renderResponse)
211         throws Exception {
212 
213         try {
214             ActionUtil.getColumn(renderRequest);
215         }
216         catch (Exception e) {
217             if (e instanceof NoSuchColumnException ||
218                 e instanceof PrincipalException) {
219 
220                 SessionErrors.add(renderRequest, e.getClass().getName());
221 
222                 return mapping.findForward("portlet.expando.error");
223             }
224             else {
225                 throw e;
226             }
227         }
228 
229         return mapping.findForward(
230             getForward(renderRequest, "portlet.expando.edit_expando"));
231     }
232 
233     protected void addExpando(ActionRequest actionRequest) throws Exception {
234         String modelResource = ParamUtil.getString(
235             actionRequest, "modelResource");
236         long resourcePrimKey = ParamUtil.getLong(
237             actionRequest, "resourcePrimKey");
238 
239         String name = ParamUtil.getString(actionRequest, "name");
240         String preset = ParamUtil.getString(actionRequest, "type");
241 
242         ExpandoBridge expandoBridge = ExpandoBridgeFactoryUtil.getExpandoBridge(
243             modelResource, resourcePrimKey);
244 
245         if (preset.startsWith("Preset")) {
246             addPresetExpando(actionRequest, expandoBridge, preset, name);
247         }
248         else {
249             int type = ParamUtil.getInteger(actionRequest, "type");
250 
251             expandoBridge.addAttribute(name, type);
252 
253             updateProperties(actionRequest, expandoBridge, name);
254         }
255     }
256 
257     protected void addPresetExpando(
258             ActionRequest actionRequest, ExpandoBridge expandoBridge,
259             String preset, String name)
260         throws Exception {
261 
262         int type = 0;
263         UnicodeProperties properties = expandoBridge.getAttributeProperties(
264             name);
265 
266         if (preset.equals("PresetSelectionIntegerArray()")) {
267             type = ExpandoColumnConstants.INTEGER_ARRAY;
268             properties.setProperty(
269                 ExpandoColumnConstants.PROPERTY_SELECTION,
270                 Boolean.TRUE.toString());
271         }
272         else if (preset.equals("PresetSelectionDoubleArray()")) {
273             type = ExpandoColumnConstants.DOUBLE_ARRAY;
274             properties.setProperty(
275                 ExpandoColumnConstants.PROPERTY_SELECTION,
276                 Boolean.TRUE.toString());
277         }
278         else if (preset.equals("PresetSelectionStringArray()")) {
279             type = ExpandoColumnConstants.STRING_ARRAY;
280             properties.setProperty(
281                 ExpandoColumnConstants.PROPERTY_SELECTION,
282                 Boolean.TRUE.toString());
283         }
284         else if (preset.equals("PresetTextBox()")) {
285             type = ExpandoColumnConstants.STRING;
286             properties.setProperty(
287                 ExpandoColumnConstants.PROPERTY_HEIGHT, "105");
288             properties.setProperty(
289                 ExpandoColumnConstants.PROPERTY_WIDTH, "450");
290         }
291         else if (preset.equals("PresetTextBoxIndexed()")) {
292             type = ExpandoColumnConstants.STRING;
293             properties.setProperty(
294                 ExpandoColumnConstants.PROPERTY_HEIGHT, "105");
295             properties.setProperty(
296                 ExpandoColumnConstants.PROPERTY_WIDTH, "450");
297             properties.setProperty(
298                 ExpandoBridgeIndexer.INDEXABLE, Boolean.TRUE.toString());
299         }
300         else if (preset.equals("PresetTextFieldSecret()")) {
301             type = ExpandoColumnConstants.STRING;
302             properties.setProperty(
303                 ExpandoColumnConstants.PROPERTY_SECRET,
304                 Boolean.TRUE.toString());
305         }
306         else {
307             type = ExpandoColumnConstants.STRING;
308             properties.setProperty(
309                 ExpandoBridgeIndexer.INDEXABLE, Boolean.TRUE.toString());
310         }
311 
312         expandoBridge.addAttribute(name, type);
313 
314         expandoBridge.setAttributeProperties(name, properties);
315     }
316 
317     protected void deleteExpando(ActionRequest actionRequest) throws Exception {
318         long columnId = ParamUtil.getLong(actionRequest, "columnId");
319 
320         ExpandoColumnServiceUtil.deleteColumn(columnId);
321     }
322 
323     protected void updateExpando(ActionRequest actionRequest) throws Exception {
324         String modelResource = ParamUtil.getString(
325             actionRequest, "modelResource");
326         long resourcePrimKey = ParamUtil.getLong(
327             actionRequest, "resourcePrimKey");
328 
329         String name = ParamUtil.getString(actionRequest, "name");
330         int type = ParamUtil.getInteger(actionRequest, "type");
331 
332         Serializable defaultValue = getValue(
333             actionRequest, "defaultValue", type);
334 
335         ExpandoBridge expandoBridge = ExpandoBridgeFactoryUtil.getExpandoBridge(
336             modelResource, resourcePrimKey);
337 
338         expandoBridge.setAttributeDefault(name, defaultValue);
339 
340         updateProperties(actionRequest, expandoBridge, name);
341     }
342 
343     protected void updateProperties(
344             ActionRequest actionRequest, ExpandoBridge expandoBridge,
345             String name)
346         throws Exception {
347 
348         Enumeration<String> enu = actionRequest.getParameterNames();
349 
350         UnicodeProperties properties = expandoBridge.getAttributeProperties(
351             name);
352 
353         List<String> propertyNames = new ArrayList<String>();
354 
355         while (enu.hasMoreElements()) {
356             String param = enu.nextElement();
357 
358             if (param.indexOf("PropertyName(") != -1) {
359                 String propertyName = ParamUtil.getString(actionRequest, param);
360 
361                 propertyNames.add(propertyName);
362             }
363         }
364 
365         for (String propertyName : propertyNames) {
366             String value = ParamUtil.getString(
367                 actionRequest, "Property(" + propertyName + ")");
368 
369             properties.setProperty(propertyName, value);
370         }
371 
372         expandoBridge.setAttributeProperties(name, properties);
373     }
374 
375 }