001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.expando.action;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.servlet.SessionErrors;
020    import com.liferay.portal.kernel.util.Constants;
021    import com.liferay.portal.kernel.util.GetterUtil;
022    import com.liferay.portal.kernel.util.ParamUtil;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.util.StringUtil;
025    import com.liferay.portal.kernel.util.UnicodeProperties;
026    import com.liferay.portal.kernel.util.WebKeys;
027    import com.liferay.portal.model.User;
028    import com.liferay.portal.security.auth.PrincipalException;
029    import com.liferay.portal.struts.PortletAction;
030    import com.liferay.portal.theme.ThemeDisplay;
031    import com.liferay.portal.util.PortalUtil;
032    import com.liferay.portlet.expando.ColumnNameException;
033    import com.liferay.portlet.expando.ColumnTypeException;
034    import com.liferay.portlet.expando.DuplicateColumnNameException;
035    import com.liferay.portlet.expando.NoSuchColumnException;
036    import com.liferay.portlet.expando.ValueDataException;
037    import com.liferay.portlet.expando.model.ExpandoBridge;
038    import com.liferay.portlet.expando.model.ExpandoColumnConstants;
039    import com.liferay.portlet.expando.service.ExpandoColumnServiceUtil;
040    import com.liferay.portlet.expando.util.ExpandoBridgeFactoryUtil;
041    
042    import java.io.Serializable;
043    
044    import java.util.ArrayList;
045    import java.util.Calendar;
046    import java.util.Enumeration;
047    import java.util.List;
048    
049    import javax.portlet.ActionRequest;
050    import javax.portlet.ActionResponse;
051    import javax.portlet.PortletConfig;
052    import javax.portlet.PortletRequest;
053    import javax.portlet.RenderRequest;
054    import javax.portlet.RenderResponse;
055    
056    import org.apache.struts.action.ActionForm;
057    import org.apache.struts.action.ActionForward;
058    import org.apache.struts.action.ActionMapping;
059    
060    /**
061     * @author Raymond Augé
062     */
063    public class EditExpandoAction extends PortletAction {
064    
065            @Override
066            public void processAction(
067                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
068                            ActionRequest actionRequest, ActionResponse actionResponse)
069                    throws Exception {
070    
071                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
072    
073                    try {
074                            if (cmd.equals(Constants.ADD)) {
075                                    addExpando(actionRequest);
076                            }
077                            else if (cmd.equals(Constants.DELETE)) {
078                                    deleteExpando(actionRequest);
079                            }
080                            else if (cmd.equals(Constants.UPDATE)) {
081                                    updateExpando(actionRequest);
082                            }
083    
084                            sendRedirect(actionRequest, actionResponse);
085                    }
086                    catch (Exception e) {
087                            if (e instanceof NoSuchColumnException ||
088                                    e instanceof PrincipalException) {
089    
090                                    SessionErrors.add(actionRequest, e.getClass().getName());
091    
092                                    setForward(actionRequest, "portlet.expando.error");
093                            }
094                            else if (e instanceof ColumnNameException ||
095                                             e instanceof ColumnTypeException ||
096                                             e instanceof DuplicateColumnNameException ||
097                                             e instanceof ValueDataException) {
098    
099                                    SessionErrors.add(actionRequest, e.getClass().getName());
100                            }
101                            else {
102                                    throw e;
103                            }
104                    }
105            }
106    
107            @Override
108            public ActionForward render(
109                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
110                            RenderRequest renderRequest, RenderResponse renderResponse)
111                    throws Exception {
112    
113                    try {
114                            ActionUtil.getColumn(renderRequest);
115                    }
116                    catch (Exception e) {
117                            if (e instanceof NoSuchColumnException ||
118                                    e instanceof PrincipalException) {
119    
120                                    SessionErrors.add(renderRequest, e.getClass().getName());
121    
122                                    return mapping.findForward("portlet.expando.error");
123                            }
124                            else {
125                                    throw e;
126                            }
127                    }
128    
129                    return mapping.findForward(
130                            getForward(renderRequest, "portlet.expando.edit_expando"));
131            }
132    
133            protected void addExpando(ActionRequest actionRequest) throws Exception {
134                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
135                            WebKeys.THEME_DISPLAY);
136    
137                    String modelResource = ParamUtil.getString(
138                            actionRequest, "modelResource");
139                    long resourcePrimKey = ParamUtil.getLong(
140                            actionRequest, "resourcePrimKey");
141    
142                    String name = ParamUtil.getString(actionRequest, "name");
143                    String preset = ParamUtil.getString(actionRequest, "type");
144    
145                    ExpandoBridge expandoBridge = ExpandoBridgeFactoryUtil.getExpandoBridge(
146                            themeDisplay.getCompanyId(), modelResource, resourcePrimKey);
147    
148                    if (preset.startsWith("Preset")) {
149                            addPresetExpando(expandoBridge, preset, name);
150                    }
151                    else {
152                            int type = ParamUtil.getInteger(actionRequest, "type");
153    
154                            expandoBridge.addAttribute(name, type);
155    
156                            updateProperties(actionRequest, expandoBridge, name);
157                    }
158            }
159    
160            protected int addPresetExpando(
161                            ExpandoBridge expandoBridge, String preset, String name)
162                    throws Exception {
163    
164                    int type = 0;
165                    UnicodeProperties properties = expandoBridge.getAttributeProperties(
166                            name);
167    
168                    if (preset.equals("PresetSelectionIntegerArray()")) {
169                            type = ExpandoColumnConstants.INTEGER_ARRAY;
170    
171                            properties.setProperty(
172                                    ExpandoColumnConstants.PROPERTY_DISPLAY_TYPE,
173                                    ExpandoColumnConstants.PROPERTY_DISPLAY_TYPE_SELECTION_LIST);
174                    }
175                    else if (preset.equals("PresetSelectionDoubleArray()")) {
176                            type = ExpandoColumnConstants.DOUBLE_ARRAY;
177    
178                            properties.setProperty(
179                                    ExpandoColumnConstants.PROPERTY_DISPLAY_TYPE,
180                                    ExpandoColumnConstants.PROPERTY_DISPLAY_TYPE_SELECTION_LIST);
181                    }
182                    else if (preset.equals("PresetSelectionStringArray()")) {
183                            type = ExpandoColumnConstants.STRING_ARRAY;
184    
185                            properties.setProperty(
186                                    ExpandoColumnConstants.PROPERTY_DISPLAY_TYPE,
187                                    ExpandoColumnConstants.PROPERTY_DISPLAY_TYPE_SELECTION_LIST);
188                    }
189                    else if (preset.equals("PresetTextBox()")) {
190                            type = ExpandoColumnConstants.STRING;
191    
192                            properties.setProperty(
193                                    ExpandoColumnConstants.PROPERTY_HEIGHT, "105");
194                            properties.setProperty(
195                                    ExpandoColumnConstants.PROPERTY_WIDTH, "450");
196                    }
197                    else if (preset.equals("PresetTextBoxIndexed()")) {
198                            type = ExpandoColumnConstants.STRING;
199    
200                            properties.setProperty(
201                                    ExpandoColumnConstants.PROPERTY_HEIGHT, "105");
202                            properties.setProperty(
203                                    ExpandoColumnConstants.PROPERTY_WIDTH, "450");
204                            properties.setProperty(
205                                    ExpandoColumnConstants.INDEX_TYPE,
206                                    String.valueOf(ExpandoColumnConstants.INDEX_TYPE_TEXT));
207                    }
208                    else if (preset.equals("PresetTextFieldSecret()")) {
209                            type = ExpandoColumnConstants.STRING;
210    
211                            properties.setProperty(
212                                    ExpandoColumnConstants.PROPERTY_SECRET,
213                                    Boolean.TRUE.toString());
214                    }
215                    else {
216                            type = ExpandoColumnConstants.STRING;
217    
218                            properties.setProperty(
219                                    ExpandoColumnConstants.INDEX_TYPE,
220                                    String.valueOf(ExpandoColumnConstants.INDEX_TYPE_TEXT));
221                    }
222    
223                    expandoBridge.addAttribute(name, type);
224    
225                    expandoBridge.setAttributeProperties(name, properties);
226    
227                    return type;
228            }
229    
230            protected void deleteExpando(ActionRequest actionRequest) throws Exception {
231                    long columnId = ParamUtil.getLong(actionRequest, "columnId");
232    
233                    ExpandoColumnServiceUtil.deleteColumn(columnId);
234            }
235    
236            protected Serializable getValue(
237                            PortletRequest portletRequest, String name, int type)
238                    throws PortalException, SystemException {
239    
240                    String delimiter = StringPool.COMMA;
241    
242                    Serializable value = null;
243    
244                    if (type == ExpandoColumnConstants.BOOLEAN) {
245                            value = ParamUtil.getBoolean(portletRequest, name);
246                    }
247                    else if (type == ExpandoColumnConstants.BOOLEAN_ARRAY) {
248                    }
249                    else if (type == ExpandoColumnConstants.DATE) {
250                            User user = PortalUtil.getUser(portletRequest);
251    
252                            int valueDateMonth = ParamUtil.getInteger(
253                                    portletRequest, name + "Month");
254                            int valueDateDay = ParamUtil.getInteger(
255                                    portletRequest, name + "Day");
256                            int valueDateYear = ParamUtil.getInteger(
257                                    portletRequest, name + "Year");
258                            int valueDateHour = ParamUtil.getInteger(
259                                    portletRequest, name + "Hour");
260                            int valueDateMinute = ParamUtil.getInteger(
261                                    portletRequest, name + "Minute");
262                            int valueDateAmPm = ParamUtil.getInteger(
263                                    portletRequest, name + "AmPm");
264    
265                            if (valueDateAmPm == Calendar.PM) {
266                                    valueDateHour += 12;
267                            }
268    
269                            value = PortalUtil.getDate(
270                                    valueDateMonth, valueDateDay, valueDateYear, valueDateHour,
271                                    valueDateMinute, user.getTimeZone(), new ValueDataException());
272                    }
273                    else if (type == ExpandoColumnConstants.DATE_ARRAY) {
274                    }
275                    else if (type == ExpandoColumnConstants.DOUBLE) {
276                            value = ParamUtil.getDouble(portletRequest, name);
277                    }
278                    else if (type == ExpandoColumnConstants.DOUBLE_ARRAY) {
279                            String paramValue = ParamUtil.getString(portletRequest, name);
280    
281                            if (paramValue.contains(StringPool.NEW_LINE)) {
282                                    delimiter = StringPool.NEW_LINE;
283                            }
284    
285                            String[] values = StringUtil.split(paramValue, delimiter);
286    
287                            value = GetterUtil.getDoubleValues(values);
288                    }
289                    else if (type == ExpandoColumnConstants.FLOAT) {
290                            value = ParamUtil.getFloat(portletRequest, name);
291                    }
292                    else if (type == ExpandoColumnConstants.FLOAT_ARRAY) {
293                            String paramValue = ParamUtil.getString(portletRequest, name);
294    
295                            if (paramValue.contains(StringPool.NEW_LINE)) {
296                                    delimiter = StringPool.NEW_LINE;
297                            }
298    
299                            String[] values = StringUtil.split(paramValue, delimiter);
300    
301                            value = GetterUtil.getFloatValues(values);
302                    }
303                    else if (type == ExpandoColumnConstants.INTEGER) {
304                            value = ParamUtil.getInteger(portletRequest, name);
305                    }
306                    else if (type == ExpandoColumnConstants.INTEGER_ARRAY) {
307                            String paramValue = ParamUtil.getString(portletRequest, name);
308    
309                            if (paramValue.contains(StringPool.NEW_LINE)) {
310                                    delimiter = StringPool.NEW_LINE;
311                            }
312    
313                            String[] values = StringUtil.split(paramValue, delimiter);
314    
315                            value = GetterUtil.getIntegerValues(values);
316                    }
317                    else if (type == ExpandoColumnConstants.LONG) {
318                            value = ParamUtil.getLong(portletRequest, name);
319                    }
320                    else if (type == ExpandoColumnConstants.LONG_ARRAY) {
321                            String paramValue = ParamUtil.getString(portletRequest, name);
322    
323                            if (paramValue.contains(StringPool.NEW_LINE)) {
324                                    delimiter = StringPool.NEW_LINE;
325                            }
326    
327                            String[] values = StringUtil.split(paramValue, delimiter);
328    
329                            value = GetterUtil.getLongValues(values);
330                    }
331                    else if (type == ExpandoColumnConstants.SHORT) {
332                            value = ParamUtil.getShort(portletRequest, name);
333                    }
334                    else if (type == ExpandoColumnConstants.SHORT_ARRAY) {
335                            String paramValue = ParamUtil.getString(portletRequest, name);
336    
337                            if (paramValue.contains(StringPool.NEW_LINE)) {
338                                    delimiter = StringPool.NEW_LINE;
339                            }
340    
341                            String[] values = StringUtil.split(paramValue, delimiter);
342    
343                            value = GetterUtil.getShortValues(values);
344                    }
345                    else if (type == ExpandoColumnConstants.STRING_ARRAY) {
346                            String paramValue = ParamUtil.getString(portletRequest, name);
347    
348                            if (paramValue.contains(StringPool.NEW_LINE)) {
349                                    delimiter = StringPool.NEW_LINE;
350                            }
351    
352                            value = StringUtil.split(paramValue, delimiter);
353                    }
354                    else {
355                            value = ParamUtil.getString(portletRequest, name);
356                    }
357    
358                    return value;
359            }
360    
361            protected void updateExpando(ActionRequest actionRequest) throws Exception {
362                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
363                            WebKeys.THEME_DISPLAY);
364    
365                    String modelResource = ParamUtil.getString(
366                            actionRequest, "modelResource");
367                    long resourcePrimKey = ParamUtil.getLong(
368                            actionRequest, "resourcePrimKey");
369    
370                    String name = ParamUtil.getString(actionRequest, "name");
371                    int type = ParamUtil.getInteger(actionRequest, "type");
372    
373                    Serializable defaultValue = getValue(
374                            actionRequest, "defaultValue", type);
375    
376                    ExpandoBridge expandoBridge = ExpandoBridgeFactoryUtil.getExpandoBridge(
377                            themeDisplay.getCompanyId(), modelResource, resourcePrimKey);
378    
379                    expandoBridge.setAttributeDefault(name, defaultValue);
380    
381                    updateProperties(actionRequest, expandoBridge, name);
382            }
383    
384            protected void updateProperties(
385                            ActionRequest actionRequest, ExpandoBridge expandoBridge,
386                            String name)
387                    throws Exception {
388    
389                    Enumeration<String> enu = actionRequest.getParameterNames();
390    
391                    UnicodeProperties properties = expandoBridge.getAttributeProperties(
392                            name);
393    
394                    List<String> propertyNames = new ArrayList<String>();
395    
396                    while (enu.hasMoreElements()) {
397                            String param = enu.nextElement();
398    
399                            if (param.indexOf("PropertyName--") != -1) {
400                                    String propertyName = ParamUtil.getString(actionRequest, param);
401    
402                                    propertyNames.add(propertyName);
403                            }
404                    }
405    
406                    for (String propertyName : propertyNames) {
407                            String value = ParamUtil.getString(
408                                    actionRequest, "Property--" + propertyName + "--");
409    
410                            properties.setProperty(propertyName, value);
411                    }
412    
413                    expandoBridge.setAttributeProperties(name, properties);
414            }
415    
416    }