001    /**
002     * Copyright (c) 2000-present 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.servlet.SessionErrors;
019    import com.liferay.portal.kernel.util.Constants;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.LocalizationUtil;
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 actionMapping, ActionForm actionForm,
068                            PortletConfig portletConfig, ActionRequest actionRequest,
069                            ActionResponse actionResponse)
070                    throws Exception {
071    
072                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
073    
074                    try {
075                            if (cmd.equals(Constants.ADD)) {
076                                    addExpando(actionRequest);
077                            }
078                            else if (cmd.equals(Constants.DELETE)) {
079                                    deleteExpando(actionRequest);
080                            }
081                            else if (cmd.equals(Constants.UPDATE)) {
082                                    updateExpando(actionRequest);
083                            }
084    
085                            sendRedirect(actionRequest, actionResponse);
086                    }
087                    catch (Exception e) {
088                            if (e instanceof NoSuchColumnException ||
089                                    e instanceof PrincipalException) {
090    
091                                    SessionErrors.add(actionRequest, e.getClass());
092    
093                                    setForward(actionRequest, "portlet.expando.error");
094                            }
095                            else if (e instanceof ColumnNameException ||
096                                             e instanceof ColumnTypeException ||
097                                             e instanceof DuplicateColumnNameException ||
098                                             e instanceof ValueDataException) {
099    
100                                    SessionErrors.add(actionRequest, e.getClass());
101                            }
102                            else {
103                                    throw e;
104                            }
105                    }
106            }
107    
108            @Override
109            public ActionForward render(
110                            ActionMapping actionMapping, ActionForm actionForm,
111                            PortletConfig portletConfig, RenderRequest renderRequest,
112                            RenderResponse renderResponse)
113                    throws Exception {
114    
115                    try {
116                            ActionUtil.getColumn(renderRequest);
117                    }
118                    catch (Exception e) {
119                            if (e instanceof NoSuchColumnException ||
120                                    e instanceof PrincipalException) {
121    
122                                    SessionErrors.add(renderRequest, e.getClass());
123    
124                                    return actionMapping.findForward("portlet.expando.error");
125                            }
126                            else {
127                                    throw e;
128                            }
129                    }
130    
131                    return actionMapping.findForward(
132                            getForward(renderRequest, "portlet.expando.edit_expando"));
133            }
134    
135            protected void addExpando(ActionRequest actionRequest) throws Exception {
136                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
137                            WebKeys.THEME_DISPLAY);
138    
139                    String modelResource = ParamUtil.getString(
140                            actionRequest, "modelResource");
141                    long resourcePrimKey = ParamUtil.getLong(
142                            actionRequest, "resourcePrimKey");
143    
144                    String name = ParamUtil.getString(actionRequest, "name");
145                    String preset = ParamUtil.getString(actionRequest, "type");
146    
147                    ExpandoBridge expandoBridge = ExpandoBridgeFactoryUtil.getExpandoBridge(
148                            themeDisplay.getCompanyId(), modelResource, resourcePrimKey);
149    
150                    if (preset.startsWith("Preset")) {
151                            addPresetExpando(expandoBridge, preset, name);
152                    }
153                    else {
154                            int type = ParamUtil.getInteger(actionRequest, "type");
155    
156                            expandoBridge.addAttribute(name, type);
157    
158                            updateProperties(actionRequest, expandoBridge, name);
159                    }
160            }
161    
162            protected int addPresetExpando(
163                            ExpandoBridge expandoBridge, String preset, String name)
164                    throws Exception {
165    
166                    int type = 0;
167    
168                    UnicodeProperties properties = null;
169    
170                    try {
171                            properties = expandoBridge.getAttributeProperties(name);
172                    }
173                    catch (Exception e) {
174                            properties = new UnicodeProperties();
175                    }
176    
177                    if (preset.equals("PresetSelectionIntegerArray()")) {
178                            type = ExpandoColumnConstants.INTEGER_ARRAY;
179    
180                            properties.setProperty(
181                                    ExpandoColumnConstants.PROPERTY_DISPLAY_TYPE,
182                                    ExpandoColumnConstants.PROPERTY_DISPLAY_TYPE_SELECTION_LIST);
183                    }
184                    else if (preset.equals("PresetSelectionDoubleArray()")) {
185                            type = ExpandoColumnConstants.DOUBLE_ARRAY;
186    
187                            properties.setProperty(
188                                    ExpandoColumnConstants.PROPERTY_DISPLAY_TYPE,
189                                    ExpandoColumnConstants.PROPERTY_DISPLAY_TYPE_SELECTION_LIST);
190                    }
191                    else if (preset.equals("PresetSelectionStringArray()")) {
192                            type = ExpandoColumnConstants.STRING_ARRAY;
193    
194                            properties.setProperty(
195                                    ExpandoColumnConstants.PROPERTY_DISPLAY_TYPE,
196                                    ExpandoColumnConstants.PROPERTY_DISPLAY_TYPE_SELECTION_LIST);
197                    }
198                    else if (preset.equals("PresetTextBox()")) {
199                            type = ExpandoColumnConstants.STRING;
200    
201                            properties.setProperty(
202                                    ExpandoColumnConstants.PROPERTY_HEIGHT, "105");
203                            properties.setProperty(
204                                    ExpandoColumnConstants.PROPERTY_WIDTH, "450");
205                    }
206                    else if (preset.equals("PresetTextBoxIndexed()")) {
207                            type = ExpandoColumnConstants.STRING;
208    
209                            properties.setProperty(
210                                    ExpandoColumnConstants.PROPERTY_HEIGHT, "105");
211                            properties.setProperty(
212                                    ExpandoColumnConstants.PROPERTY_WIDTH, "450");
213                            properties.setProperty(
214                                    ExpandoColumnConstants.INDEX_TYPE,
215                                    String.valueOf(ExpandoColumnConstants.INDEX_TYPE_TEXT));
216                    }
217                    else if (preset.equals("PresetTextFieldSecret()")) {
218                            type = ExpandoColumnConstants.STRING;
219    
220                            properties.setProperty(
221                                    ExpandoColumnConstants.PROPERTY_SECRET,
222                                    Boolean.TRUE.toString());
223                    }
224                    else {
225                            type = ExpandoColumnConstants.STRING;
226    
227                            properties.setProperty(
228                                    ExpandoColumnConstants.INDEX_TYPE,
229                                    String.valueOf(ExpandoColumnConstants.INDEX_TYPE_TEXT));
230                    }
231    
232                    expandoBridge.addAttribute(name, type);
233    
234                    expandoBridge.setAttributeProperties(name, properties);
235    
236                    return type;
237            }
238    
239            protected void deleteExpando(ActionRequest actionRequest) throws Exception {
240                    long columnId = ParamUtil.getLong(actionRequest, "columnId");
241    
242                    ExpandoColumnServiceUtil.deleteColumn(columnId);
243            }
244    
245            protected Serializable getValue(
246                            PortletRequest portletRequest, String name, int type)
247                    throws PortalException {
248    
249                    String delimiter = StringPool.COMMA;
250    
251                    Serializable value = null;
252    
253                    if (type == ExpandoColumnConstants.BOOLEAN) {
254                            value = ParamUtil.getBoolean(portletRequest, name);
255                    }
256                    else if (type == ExpandoColumnConstants.BOOLEAN_ARRAY) {
257                    }
258                    else if (type == ExpandoColumnConstants.DATE) {
259                            User user = PortalUtil.getUser(portletRequest);
260    
261                            int valueDateMonth = ParamUtil.getInteger(
262                                    portletRequest, name + "Month");
263                            int valueDateDay = ParamUtil.getInteger(
264                                    portletRequest, name + "Day");
265                            int valueDateYear = ParamUtil.getInteger(
266                                    portletRequest, name + "Year");
267                            int valueDateHour = ParamUtil.getInteger(
268                                    portletRequest, name + "Hour");
269                            int valueDateMinute = ParamUtil.getInteger(
270                                    portletRequest, name + "Minute");
271                            int valueDateAmPm = ParamUtil.getInteger(
272                                    portletRequest, name + "AmPm");
273    
274                            if (valueDateAmPm == Calendar.PM) {
275                                    valueDateHour += 12;
276                            }
277    
278                            value = PortalUtil.getDate(
279                                    valueDateMonth, valueDateDay, valueDateYear, valueDateHour,
280                                    valueDateMinute, user.getTimeZone(), ValueDataException.class);
281                    }
282                    else if (type == ExpandoColumnConstants.DATE_ARRAY) {
283                    }
284                    else if (type == ExpandoColumnConstants.DOUBLE) {
285                            value = ParamUtil.getDouble(portletRequest, name);
286                    }
287                    else if (type == ExpandoColumnConstants.DOUBLE_ARRAY) {
288                            String paramValue = ParamUtil.getString(portletRequest, name);
289    
290                            if (paramValue.contains(StringPool.NEW_LINE)) {
291                                    delimiter = StringPool.NEW_LINE;
292                            }
293    
294                            String[] values = StringUtil.split(paramValue, delimiter);
295    
296                            value = GetterUtil.getDoubleValues(values);
297                    }
298                    else if (type == ExpandoColumnConstants.FLOAT) {
299                            value = ParamUtil.getFloat(portletRequest, name);
300                    }
301                    else if (type == ExpandoColumnConstants.FLOAT_ARRAY) {
302                            String paramValue = ParamUtil.getString(portletRequest, name);
303    
304                            if (paramValue.contains(StringPool.NEW_LINE)) {
305                                    delimiter = StringPool.NEW_LINE;
306                            }
307    
308                            String[] values = StringUtil.split(paramValue, delimiter);
309    
310                            value = GetterUtil.getFloatValues(values);
311                    }
312                    else if (type == ExpandoColumnConstants.INTEGER) {
313                            value = ParamUtil.getInteger(portletRequest, name);
314                    }
315                    else if (type == ExpandoColumnConstants.INTEGER_ARRAY) {
316                            String paramValue = ParamUtil.getString(portletRequest, name);
317    
318                            if (paramValue.contains(StringPool.NEW_LINE)) {
319                                    delimiter = StringPool.NEW_LINE;
320                            }
321    
322                            String[] values = StringUtil.split(paramValue, delimiter);
323    
324                            value = GetterUtil.getIntegerValues(values);
325                    }
326                    else if (type == ExpandoColumnConstants.LONG) {
327                            value = ParamUtil.getLong(portletRequest, name);
328                    }
329                    else if (type == ExpandoColumnConstants.LONG_ARRAY) {
330                            String paramValue = ParamUtil.getString(portletRequest, name);
331    
332                            if (paramValue.contains(StringPool.NEW_LINE)) {
333                                    delimiter = StringPool.NEW_LINE;
334                            }
335    
336                            String[] values = StringUtil.split(paramValue, delimiter);
337    
338                            value = GetterUtil.getLongValues(values);
339                    }
340                    else if (type == ExpandoColumnConstants.NUMBER) {
341                            value = ParamUtil.getNumber(portletRequest, name);
342                    }
343                    else if (type == ExpandoColumnConstants.NUMBER_ARRAY) {
344                            String paramValue = ParamUtil.getString(portletRequest, name);
345    
346                            if (paramValue.contains(StringPool.NEW_LINE)) {
347                                    delimiter = StringPool.NEW_LINE;
348                            }
349    
350                            String[] values = StringUtil.split(paramValue, delimiter);
351    
352                            value = GetterUtil.getNumberValues(values);
353                    }
354                    else if (type == ExpandoColumnConstants.SHORT) {
355                            value = ParamUtil.getShort(portletRequest, name);
356                    }
357                    else if (type == ExpandoColumnConstants.SHORT_ARRAY) {
358                            String paramValue = ParamUtil.getString(portletRequest, name);
359    
360                            if (paramValue.contains(StringPool.NEW_LINE)) {
361                                    delimiter = StringPool.NEW_LINE;
362                            }
363    
364                            String[] values = StringUtil.split(paramValue, delimiter);
365    
366                            value = GetterUtil.getShortValues(values);
367                    }
368                    else if (type == ExpandoColumnConstants.STRING_ARRAY) {
369                            String paramValue = ParamUtil.getString(portletRequest, name);
370    
371                            if (paramValue.contains(StringPool.NEW_LINE)) {
372                                    delimiter = StringPool.NEW_LINE;
373                            }
374    
375                            value = StringUtil.split(paramValue, delimiter);
376                    }
377                    else if (type == ExpandoColumnConstants.STRING_LOCALIZED) {
378                            value = (Serializable)LocalizationUtil.getLocalizationMap(
379                                    portletRequest, name);
380                    }
381                    else {
382                            value = ParamUtil.getString(portletRequest, name);
383                    }
384    
385                    return value;
386            }
387    
388            protected void updateExpando(ActionRequest actionRequest) throws Exception {
389                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
390                            WebKeys.THEME_DISPLAY);
391    
392                    String modelResource = ParamUtil.getString(
393                            actionRequest, "modelResource");
394                    long resourcePrimKey = ParamUtil.getLong(
395                            actionRequest, "resourcePrimKey");
396    
397                    String name = ParamUtil.getString(actionRequest, "name");
398                    int type = ParamUtil.getInteger(actionRequest, "type");
399    
400                    Serializable defaultValue = getValue(
401                            actionRequest, "defaultValue", type);
402    
403                    ExpandoBridge expandoBridge = ExpandoBridgeFactoryUtil.getExpandoBridge(
404                            themeDisplay.getCompanyId(), modelResource, resourcePrimKey);
405    
406                    expandoBridge.setAttributeDefault(name, defaultValue);
407    
408                    updateProperties(actionRequest, expandoBridge, name);
409            }
410    
411            protected void updateProperties(
412                            ActionRequest actionRequest, ExpandoBridge expandoBridge,
413                            String name)
414                    throws Exception {
415    
416                    Enumeration<String> enu = actionRequest.getParameterNames();
417    
418                    UnicodeProperties properties = expandoBridge.getAttributeProperties(
419                            name);
420    
421                    List<String> propertyNames = new ArrayList<String>();
422    
423                    while (enu.hasMoreElements()) {
424                            String param = enu.nextElement();
425    
426                            if (param.contains("PropertyName--")) {
427                                    String propertyName = ParamUtil.getString(actionRequest, param);
428    
429                                    propertyNames.add(propertyName);
430                            }
431                    }
432    
433                    for (String propertyName : propertyNames) {
434                            String value = ParamUtil.getString(
435                                    actionRequest, "Property--" + propertyName + "--");
436    
437                            properties.setProperty(propertyName, value);
438                    }
439    
440                    expandoBridge.setAttributeProperties(name, properties);
441            }
442    
443    }