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;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.portlet.bridges.mvc.MVCPortlet;
019    import com.liferay.portal.kernel.servlet.SessionErrors;
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.theme.ThemeDisplay;
030    import com.liferay.portal.util.PortalUtil;
031    import com.liferay.portlet.expando.model.ExpandoBridge;
032    import com.liferay.portlet.expando.model.ExpandoColumnConstants;
033    import com.liferay.portlet.expando.service.ExpandoColumnServiceUtil;
034    import com.liferay.portlet.expando.util.ExpandoBridgeFactoryUtil;
035    import com.liferay.portlet.expando.util.ExpandoPresetUtil;
036    
037    import java.io.IOException;
038    import java.io.Serializable;
039    
040    import java.util.ArrayList;
041    import java.util.Calendar;
042    import java.util.Enumeration;
043    import java.util.List;
044    
045    import javax.portlet.ActionRequest;
046    import javax.portlet.ActionResponse;
047    import javax.portlet.PortletException;
048    import javax.portlet.PortletRequest;
049    import javax.portlet.RenderRequest;
050    import javax.portlet.RenderResponse;
051    
052    /**
053     * @author Raymond Aug??
054     * @author Drew Brokke
055     */
056    public class ExpandoPortlet extends MVCPortlet {
057    
058            public void addExpando(
059                            ActionRequest actionRequest, ActionResponse actionResponse)
060                    throws Exception {
061    
062                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
063                            WebKeys.THEME_DISPLAY);
064    
065                    String modelResource = ParamUtil.getString(
066                            actionRequest, "modelResource");
067                    long resourcePrimKey = ParamUtil.getLong(
068                            actionRequest, "resourcePrimKey");
069    
070                    String name = ParamUtil.getString(actionRequest, "name");
071                    String preset = ParamUtil.getString(actionRequest, "type");
072    
073                    ExpandoBridge expandoBridge = ExpandoBridgeFactoryUtil.getExpandoBridge(
074                            themeDisplay.getCompanyId(), modelResource, resourcePrimKey);
075    
076                    if (preset.startsWith("Preset")) {
077                            ExpandoPresetUtil.addPresetExpando(expandoBridge, preset, name);
078                    }
079                    else {
080                            int type = ParamUtil.getInteger(actionRequest, "type");
081    
082                            expandoBridge.addAttribute(name, type);
083    
084                            updateProperties(actionRequest, expandoBridge, name);
085                    }
086            }
087    
088            public void deleteExpando(
089                            ActionRequest actionRequest, ActionResponse actionResponse)
090                    throws Exception {
091    
092                    long columnId = ParamUtil.getLong(actionRequest, "columnId");
093    
094                    ExpandoColumnServiceUtil.deleteColumn(columnId);
095            }
096    
097            public void updateExpando(
098                            ActionRequest actionRequest, ActionResponse actionResponse)
099                    throws Exception {
100    
101                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
102                            WebKeys.THEME_DISPLAY);
103    
104                    String modelResource = ParamUtil.getString(
105                            actionRequest, "modelResource");
106                    long resourcePrimKey = ParamUtil.getLong(
107                            actionRequest, "resourcePrimKey");
108    
109                    String name = ParamUtil.getString(actionRequest, "name");
110                    int type = ParamUtil.getInteger(actionRequest, "type");
111    
112                    Serializable defaultValue = getValue(
113                            actionRequest, "defaultValue", type);
114    
115                    ExpandoBridge expandoBridge = ExpandoBridgeFactoryUtil.getExpandoBridge(
116                            themeDisplay.getCompanyId(), modelResource, resourcePrimKey);
117    
118                    expandoBridge.setAttributeDefault(name, defaultValue);
119    
120                    updateProperties(actionRequest, expandoBridge, name);
121            }
122    
123            @Override
124            protected void doDispatch(
125                            RenderRequest renderRequest, RenderResponse renderResponse)
126                    throws IOException, PortletException {
127    
128                    if (SessionErrors.contains(
129                                    renderRequest, ColumnNameException.class.getName()) ||
130                            SessionErrors.contains(
131                                    renderRequest, ColumnTypeException.class.getName()) ||
132                            SessionErrors.contains(
133                                    renderRequest, DuplicateColumnNameException.class.getName()) ||
134                            SessionErrors.contains(
135                                    renderRequest, ValueDataException.class.getName())) {
136    
137                            include(
138                                    "/html/portlet/expando/edit_expando.jsp", renderRequest,
139                                    renderResponse);
140                    }
141    
142                    if (SessionErrors.contains(
143                                    renderRequest, NoSuchColumnException.class.getName()) ||
144                            SessionErrors.contains(
145                                    renderRequest, PrincipalException.class.getName())) {
146    
147                            include(
148                                    "/html/portlet/expando/error.jsp", renderRequest,
149                                    renderResponse);
150                    }
151                    else {
152                            super.doDispatch(renderRequest, renderResponse);
153                    }
154            }
155    
156            protected Serializable getValue(
157                            PortletRequest portletRequest, String name, int type)
158                    throws PortalException {
159    
160                    String delimiter = StringPool.COMMA;
161    
162                    Serializable value = null;
163    
164                    if (type == ExpandoColumnConstants.BOOLEAN) {
165                            value = ParamUtil.getBoolean(portletRequest, name);
166                    }
167                    else if (type == ExpandoColumnConstants.BOOLEAN_ARRAY) {
168                    }
169                    else if (type == ExpandoColumnConstants.DATE) {
170                            User user = PortalUtil.getUser(portletRequest);
171    
172                            int valueDateMonth = ParamUtil.getInteger(
173                                    portletRequest, name + "Month");
174                            int valueDateDay = ParamUtil.getInteger(
175                                    portletRequest, name + "Day");
176                            int valueDateYear = ParamUtil.getInteger(
177                                    portletRequest, name + "Year");
178                            int valueDateHour = ParamUtil.getInteger(
179                                    portletRequest, name + "Hour");
180                            int valueDateMinute = ParamUtil.getInteger(
181                                    portletRequest, name + "Minute");
182                            int valueDateAmPm = ParamUtil.getInteger(
183                                    portletRequest, name + "AmPm");
184    
185                            if (valueDateAmPm == Calendar.PM) {
186                                    valueDateHour += 12;
187                            }
188    
189                            value = PortalUtil.getDate(
190                                    valueDateMonth, valueDateDay, valueDateYear, valueDateHour,
191                                    valueDateMinute, user.getTimeZone(), ValueDataException.class);
192                    }
193                    else if (type == ExpandoColumnConstants.DATE_ARRAY) {
194                    }
195                    else if (type == ExpandoColumnConstants.DOUBLE) {
196                            value = ParamUtil.getDouble(portletRequest, name);
197                    }
198                    else if (type == ExpandoColumnConstants.DOUBLE_ARRAY) {
199                            String paramValue = ParamUtil.getString(portletRequest, name);
200    
201                            if (paramValue.contains(StringPool.NEW_LINE)) {
202                                    delimiter = StringPool.NEW_LINE;
203                            }
204    
205                            String[] values = StringUtil.split(paramValue, delimiter);
206    
207                            value = GetterUtil.getDoubleValues(values);
208                    }
209                    else if (type == ExpandoColumnConstants.FLOAT) {
210                            value = ParamUtil.getFloat(portletRequest, name);
211                    }
212                    else if (type == ExpandoColumnConstants.FLOAT_ARRAY) {
213                            String paramValue = ParamUtil.getString(portletRequest, name);
214    
215                            if (paramValue.contains(StringPool.NEW_LINE)) {
216                                    delimiter = StringPool.NEW_LINE;
217                            }
218    
219                            String[] values = StringUtil.split(paramValue, delimiter);
220    
221                            value = GetterUtil.getFloatValues(values);
222                    }
223                    else if (type == ExpandoColumnConstants.INTEGER) {
224                            value = ParamUtil.getInteger(portletRequest, name);
225                    }
226                    else if (type == ExpandoColumnConstants.INTEGER_ARRAY) {
227                            String paramValue = ParamUtil.getString(portletRequest, name);
228    
229                            if (paramValue.contains(StringPool.NEW_LINE)) {
230                                    delimiter = StringPool.NEW_LINE;
231                            }
232    
233                            String[] values = StringUtil.split(paramValue, delimiter);
234    
235                            value = GetterUtil.getIntegerValues(values);
236                    }
237                    else if (type == ExpandoColumnConstants.LONG) {
238                            value = ParamUtil.getLong(portletRequest, name);
239                    }
240                    else if (type == ExpandoColumnConstants.LONG_ARRAY) {
241                            String paramValue = ParamUtil.getString(portletRequest, name);
242    
243                            if (paramValue.contains(StringPool.NEW_LINE)) {
244                                    delimiter = StringPool.NEW_LINE;
245                            }
246    
247                            String[] values = StringUtil.split(paramValue, delimiter);
248    
249                            value = GetterUtil.getLongValues(values);
250                    }
251                    else if (type == ExpandoColumnConstants.NUMBER) {
252                            value = ParamUtil.getNumber(portletRequest, name);
253                    }
254                    else if (type == ExpandoColumnConstants.NUMBER_ARRAY) {
255                            String paramValue = ParamUtil.getString(portletRequest, name);
256    
257                            if (paramValue.contains(StringPool.NEW_LINE)) {
258                                    delimiter = StringPool.NEW_LINE;
259                            }
260    
261                            String[] values = StringUtil.split(paramValue, delimiter);
262    
263                            value = GetterUtil.getNumberValues(values);
264                    }
265                    else if (type == ExpandoColumnConstants.SHORT) {
266                            value = ParamUtil.getShort(portletRequest, name);
267                    }
268                    else if (type == ExpandoColumnConstants.SHORT_ARRAY) {
269                            String paramValue = ParamUtil.getString(portletRequest, name);
270    
271                            if (paramValue.contains(StringPool.NEW_LINE)) {
272                                    delimiter = StringPool.NEW_LINE;
273                            }
274    
275                            String[] values = StringUtil.split(paramValue, delimiter);
276    
277                            value = GetterUtil.getShortValues(values);
278                    }
279                    else if (type == ExpandoColumnConstants.STRING_ARRAY) {
280                            String paramValue = ParamUtil.getString(portletRequest, name);
281    
282                            if (paramValue.contains(StringPool.NEW_LINE)) {
283                                    delimiter = StringPool.NEW_LINE;
284                            }
285    
286                            value = StringUtil.split(paramValue, delimiter);
287                    }
288                    else if (type == ExpandoColumnConstants.STRING_LOCALIZED) {
289                            value = (Serializable)LocalizationUtil.getLocalizationMap(
290                                    portletRequest, name);
291                    }
292                    else {
293                            value = ParamUtil.getString(portletRequest, name);
294                    }
295    
296                    return value;
297            }
298    
299            @Override
300            protected boolean isSessionErrorException(Throwable cause) {
301                    if (cause instanceof ColumnNameException ||
302                            cause instanceof ColumnTypeException ||
303                            cause instanceof DuplicateColumnNameException ||
304                            cause instanceof NoSuchColumnException ||
305                            cause instanceof PrincipalException ||
306                            cause instanceof ValueDataException) {
307    
308                            return true;
309                    }
310    
311                    return false;
312            }
313    
314            protected void updateProperties(
315                            ActionRequest actionRequest, ExpandoBridge expandoBridge,
316                            String name)
317                    throws Exception {
318    
319                    Enumeration<String> enu = actionRequest.getParameterNames();
320    
321                    UnicodeProperties properties = expandoBridge.getAttributeProperties(
322                            name);
323    
324                    List<String> propertyNames = new ArrayList<>();
325    
326                    while (enu.hasMoreElements()) {
327                            String param = enu.nextElement();
328    
329                            if (param.contains("PropertyName--")) {
330                                    String propertyName = ParamUtil.getString(actionRequest, param);
331    
332                                    propertyNames.add(propertyName);
333                            }
334                    }
335    
336                    for (String propertyName : propertyNames) {
337                            String value = ParamUtil.getString(
338                                    actionRequest, "Property--" + propertyName + "--");
339    
340                            properties.setProperty(propertyName, value);
341                    }
342    
343                    expandoBridge.setAttributeProperties(name, properties);
344            }
345    
346    }