001
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
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 UnicodeProperties properties = expandoBridge.getAttributeProperties(
168 name);
169
170 if (preset.equals("PresetSelectionIntegerArray()")) {
171 type = ExpandoColumnConstants.INTEGER_ARRAY;
172
173 properties.setProperty(
174 ExpandoColumnConstants.PROPERTY_DISPLAY_TYPE,
175 ExpandoColumnConstants.PROPERTY_DISPLAY_TYPE_SELECTION_LIST);
176 }
177 else if (preset.equals("PresetSelectionDoubleArray()")) {
178 type = ExpandoColumnConstants.DOUBLE_ARRAY;
179
180 properties.setProperty(
181 ExpandoColumnConstants.PROPERTY_DISPLAY_TYPE,
182 ExpandoColumnConstants.PROPERTY_DISPLAY_TYPE_SELECTION_LIST);
183 }
184 else if (preset.equals("PresetSelectionStringArray()")) {
185 type = ExpandoColumnConstants.STRING_ARRAY;
186
187 properties.setProperty(
188 ExpandoColumnConstants.PROPERTY_DISPLAY_TYPE,
189 ExpandoColumnConstants.PROPERTY_DISPLAY_TYPE_SELECTION_LIST);
190 }
191 else if (preset.equals("PresetTextBox()")) {
192 type = ExpandoColumnConstants.STRING;
193
194 properties.setProperty(
195 ExpandoColumnConstants.PROPERTY_HEIGHT, "105");
196 properties.setProperty(
197 ExpandoColumnConstants.PROPERTY_WIDTH, "450");
198 }
199 else if (preset.equals("PresetTextBoxIndexed()")) {
200 type = ExpandoColumnConstants.STRING;
201
202 properties.setProperty(
203 ExpandoColumnConstants.PROPERTY_HEIGHT, "105");
204 properties.setProperty(
205 ExpandoColumnConstants.PROPERTY_WIDTH, "450");
206 properties.setProperty(
207 ExpandoColumnConstants.INDEX_TYPE,
208 String.valueOf(ExpandoColumnConstants.INDEX_TYPE_TEXT));
209 }
210 else if (preset.equals("PresetTextFieldSecret()")) {
211 type = ExpandoColumnConstants.STRING;
212
213 properties.setProperty(
214 ExpandoColumnConstants.PROPERTY_SECRET,
215 Boolean.TRUE.toString());
216 }
217 else {
218 type = ExpandoColumnConstants.STRING;
219
220 properties.setProperty(
221 ExpandoColumnConstants.INDEX_TYPE,
222 String.valueOf(ExpandoColumnConstants.INDEX_TYPE_TEXT));
223 }
224
225 expandoBridge.addAttribute(name, type);
226
227 expandoBridge.setAttributeProperties(name, properties);
228
229 return type;
230 }
231
232 protected void deleteExpando(ActionRequest actionRequest) throws Exception {
233 long columnId = ParamUtil.getLong(actionRequest, "columnId");
234
235 ExpandoColumnServiceUtil.deleteColumn(columnId);
236 }
237
238 protected Serializable getValue(
239 PortletRequest portletRequest, String name, int type)
240 throws PortalException, SystemException {
241
242 String delimiter = StringPool.COMMA;
243
244 Serializable value = null;
245
246 if (type == ExpandoColumnConstants.BOOLEAN) {
247 value = ParamUtil.getBoolean(portletRequest, name);
248 }
249 else if (type == ExpandoColumnConstants.BOOLEAN_ARRAY) {
250 }
251 else if (type == ExpandoColumnConstants.DATE) {
252 User user = PortalUtil.getUser(portletRequest);
253
254 int valueDateMonth = ParamUtil.getInteger(
255 portletRequest, name + "Month");
256 int valueDateDay = ParamUtil.getInteger(
257 portletRequest, name + "Day");
258 int valueDateYear = ParamUtil.getInteger(
259 portletRequest, name + "Year");
260 int valueDateHour = ParamUtil.getInteger(
261 portletRequest, name + "Hour");
262 int valueDateMinute = ParamUtil.getInteger(
263 portletRequest, name + "Minute");
264 int valueDateAmPm = ParamUtil.getInteger(
265 portletRequest, name + "AmPm");
266
267 if (valueDateAmPm == Calendar.PM) {
268 valueDateHour += 12;
269 }
270
271 value = PortalUtil.getDate(
272 valueDateMonth, valueDateDay, valueDateYear, valueDateHour,
273 valueDateMinute, user.getTimeZone(), ValueDataException.class);
274 }
275 else if (type == ExpandoColumnConstants.DATE_ARRAY) {
276 }
277 else if (type == ExpandoColumnConstants.DOUBLE) {
278 value = ParamUtil.getDouble(portletRequest, name);
279 }
280 else if (type == ExpandoColumnConstants.DOUBLE_ARRAY) {
281 String paramValue = ParamUtil.getString(portletRequest, name);
282
283 if (paramValue.contains(StringPool.NEW_LINE)) {
284 delimiter = StringPool.NEW_LINE;
285 }
286
287 String[] values = StringUtil.split(paramValue, delimiter);
288
289 value = GetterUtil.getDoubleValues(values);
290 }
291 else if (type == ExpandoColumnConstants.FLOAT) {
292 value = ParamUtil.getFloat(portletRequest, name);
293 }
294 else if (type == ExpandoColumnConstants.FLOAT_ARRAY) {
295 String paramValue = ParamUtil.getString(portletRequest, name);
296
297 if (paramValue.contains(StringPool.NEW_LINE)) {
298 delimiter = StringPool.NEW_LINE;
299 }
300
301 String[] values = StringUtil.split(paramValue, delimiter);
302
303 value = GetterUtil.getFloatValues(values);
304 }
305 else if (type == ExpandoColumnConstants.INTEGER) {
306 value = ParamUtil.getInteger(portletRequest, name);
307 }
308 else if (type == ExpandoColumnConstants.INTEGER_ARRAY) {
309 String paramValue = ParamUtil.getString(portletRequest, name);
310
311 if (paramValue.contains(StringPool.NEW_LINE)) {
312 delimiter = StringPool.NEW_LINE;
313 }
314
315 String[] values = StringUtil.split(paramValue, delimiter);
316
317 value = GetterUtil.getIntegerValues(values);
318 }
319 else if (type == ExpandoColumnConstants.LONG) {
320 value = ParamUtil.getLong(portletRequest, name);
321 }
322 else if (type == ExpandoColumnConstants.LONG_ARRAY) {
323 String paramValue = ParamUtil.getString(portletRequest, name);
324
325 if (paramValue.contains(StringPool.NEW_LINE)) {
326 delimiter = StringPool.NEW_LINE;
327 }
328
329 String[] values = StringUtil.split(paramValue, delimiter);
330
331 value = GetterUtil.getLongValues(values);
332 }
333 else if (type == ExpandoColumnConstants.NUMBER) {
334 value = ParamUtil.getNumber(portletRequest, name);
335 }
336 else if (type == ExpandoColumnConstants.NUMBER_ARRAY) {
337 String paramValue = ParamUtil.getString(portletRequest, name);
338
339 if (paramValue.contains(StringPool.NEW_LINE)) {
340 delimiter = StringPool.NEW_LINE;
341 }
342
343 String[] values = StringUtil.split(paramValue, delimiter);
344
345 value = GetterUtil.getNumberValues(values);
346 }
347 else if (type == ExpandoColumnConstants.SHORT) {
348 value = ParamUtil.getShort(portletRequest, name);
349 }
350 else if (type == ExpandoColumnConstants.SHORT_ARRAY) {
351 String paramValue = ParamUtil.getString(portletRequest, name);
352
353 if (paramValue.contains(StringPool.NEW_LINE)) {
354 delimiter = StringPool.NEW_LINE;
355 }
356
357 String[] values = StringUtil.split(paramValue, delimiter);
358
359 value = GetterUtil.getShortValues(values);
360 }
361 else if (type == ExpandoColumnConstants.STRING_ARRAY) {
362 String paramValue = ParamUtil.getString(portletRequest, name);
363
364 if (paramValue.contains(StringPool.NEW_LINE)) {
365 delimiter = StringPool.NEW_LINE;
366 }
367
368 value = StringUtil.split(paramValue, delimiter);
369 }
370 else {
371 value = ParamUtil.getString(portletRequest, name);
372 }
373
374 return value;
375 }
376
377 protected void updateExpando(ActionRequest actionRequest) throws Exception {
378 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
379 WebKeys.THEME_DISPLAY);
380
381 String modelResource = ParamUtil.getString(
382 actionRequest, "modelResource");
383 long resourcePrimKey = ParamUtil.getLong(
384 actionRequest, "resourcePrimKey");
385
386 String name = ParamUtil.getString(actionRequest, "name");
387 int type = ParamUtil.getInteger(actionRequest, "type");
388
389 Serializable defaultValue = getValue(
390 actionRequest, "defaultValue", type);
391
392 ExpandoBridge expandoBridge = ExpandoBridgeFactoryUtil.getExpandoBridge(
393 themeDisplay.getCompanyId(), modelResource, resourcePrimKey);
394
395 expandoBridge.setAttributeDefault(name, defaultValue);
396
397 updateProperties(actionRequest, expandoBridge, name);
398 }
399
400 protected void updateProperties(
401 ActionRequest actionRequest, ExpandoBridge expandoBridge,
402 String name)
403 throws Exception {
404
405 Enumeration<String> enu = actionRequest.getParameterNames();
406
407 UnicodeProperties properties = expandoBridge.getAttributeProperties(
408 name);
409
410 List<String> propertyNames = new ArrayList<String>();
411
412 while (enu.hasMoreElements()) {
413 String param = enu.nextElement();
414
415 if (param.contains("PropertyName--")) {
416 String propertyName = ParamUtil.getString(actionRequest, param);
417
418 propertyNames.add(propertyName);
419 }
420 }
421
422 for (String propertyName : propertyNames) {
423 String value = ParamUtil.getString(
424 actionRequest, "Property--" + propertyName + "--");
425
426 properties.setProperty(propertyName, value);
427 }
428
429 expandoBridge.setAttributeProperties(name, properties);
430 }
431
432 }