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