1
14
15 package com.liferay.portlet.expando.action;
16
17 import com.liferay.portal.kernel.exception.PortalException;
18 import com.liferay.portal.kernel.exception.SystemException;
19 import com.liferay.portal.kernel.servlet.SessionErrors;
20 import com.liferay.portal.kernel.util.Constants;
21 import com.liferay.portal.kernel.util.GetterUtil;
22 import com.liferay.portal.kernel.util.ParamUtil;
23 import com.liferay.portal.kernel.util.StringPool;
24 import com.liferay.portal.kernel.util.StringUtil;
25 import com.liferay.portal.kernel.util.UnicodeProperties;
26 import com.liferay.portal.kernel.util.WebKeys;
27 import com.liferay.portal.model.User;
28 import com.liferay.portal.security.auth.PrincipalException;
29 import com.liferay.portal.struts.PortletAction;
30 import com.liferay.portal.theme.ThemeDisplay;
31 import com.liferay.portal.util.PortalUtil;
32 import com.liferay.portlet.expando.ColumnNameException;
33 import com.liferay.portlet.expando.ColumnTypeException;
34 import com.liferay.portlet.expando.DuplicateColumnNameException;
35 import com.liferay.portlet.expando.NoSuchColumnException;
36 import com.liferay.portlet.expando.ValueDataException;
37 import com.liferay.portlet.expando.model.ExpandoBridge;
38 import com.liferay.portlet.expando.model.ExpandoColumnConstants;
39 import com.liferay.portlet.expando.service.ExpandoColumnServiceUtil;
40 import com.liferay.portlet.expando.util.ExpandoBridgeFactoryUtil;
41 import com.liferay.portlet.expando.util.ExpandoBridgeIndexer;
42
43 import java.io.Serializable;
44
45 import java.util.ArrayList;
46 import java.util.Calendar;
47 import java.util.Enumeration;
48 import java.util.List;
49
50 import javax.portlet.ActionRequest;
51 import javax.portlet.ActionResponse;
52 import javax.portlet.PortletConfig;
53 import javax.portlet.PortletRequest;
54 import javax.portlet.RenderRequest;
55 import javax.portlet.RenderResponse;
56
57 import org.apache.struts.action.ActionForm;
58 import org.apache.struts.action.ActionForward;
59 import org.apache.struts.action.ActionMapping;
60
61
66 public class EditExpandoAction extends PortletAction {
67
68 public static Serializable getValue(
69 PortletRequest portletRequest, String name, int type)
70 throws PortalException, SystemException {
71
72 Serializable value = null;
73
74 if (type == ExpandoColumnConstants.BOOLEAN) {
75 value = ParamUtil.getBoolean(portletRequest, name);
76 }
77 else if (type == ExpandoColumnConstants.BOOLEAN_ARRAY) {
78 }
79 else if (type == ExpandoColumnConstants.DATE) {
80 User user = PortalUtil.getUser(portletRequest);
81
82 int valueDateMonth = ParamUtil.getInteger(
83 portletRequest, name + "Month");
84 int valueDateDay = ParamUtil.getInteger(
85 portletRequest, name + "Day");
86 int valueDateYear = ParamUtil.getInteger(
87 portletRequest, name + "Year");
88 int valueDateHour = ParamUtil.getInteger(
89 portletRequest, name + "Hour");
90 int valueDateMinute = ParamUtil.getInteger(
91 portletRequest, name + "Minute");
92 int valueDateAmPm = ParamUtil.getInteger(
93 portletRequest, name + "AmPm");
94
95 if (valueDateAmPm == Calendar.PM) {
96 valueDateHour += 12;
97 }
98
99 value = PortalUtil.getDate(
100 valueDateMonth, valueDateDay, valueDateYear, valueDateHour,
101 valueDateMinute, user.getTimeZone(), new ValueDataException());
102 }
103 else if (type == ExpandoColumnConstants.DATE_ARRAY) {
104 }
105 else if (type == ExpandoColumnConstants.DOUBLE) {
106 value = ParamUtil.getDouble(portletRequest, name);
107 }
108 else if (type == ExpandoColumnConstants.DOUBLE_ARRAY) {
109 String[] values = StringUtil.split(
110 ParamUtil.getString(portletRequest, name), StringPool.NEW_LINE);
111
112 value = GetterUtil.getDoubleValues(values);
113 }
114 else if (type == ExpandoColumnConstants.FLOAT) {
115 value = ParamUtil.getFloat(portletRequest, name);
116 }
117 else if (type == ExpandoColumnConstants.FLOAT_ARRAY) {
118 String[] values = StringUtil.split(
119 ParamUtil.getString(portletRequest, name), StringPool.NEW_LINE);
120
121 value = GetterUtil.getFloatValues(values);
122 }
123 else if (type == ExpandoColumnConstants.INTEGER) {
124 value = ParamUtil.getInteger(portletRequest, name);
125 }
126 else if (type == ExpandoColumnConstants.INTEGER_ARRAY) {
127 String[] values = StringUtil.split(
128 ParamUtil.getString(portletRequest, name), StringPool.NEW_LINE);
129
130 value = GetterUtil.getIntegerValues(values);
131 }
132 else if (type == ExpandoColumnConstants.LONG) {
133 value = ParamUtil.getLong(portletRequest, name);
134 }
135 else if (type == ExpandoColumnConstants.LONG_ARRAY) {
136 String[] values = StringUtil.split(
137 ParamUtil.getString(portletRequest, name), StringPool.NEW_LINE);
138
139 value = GetterUtil.getLongValues(values);
140 }
141 else if (type == ExpandoColumnConstants.SHORT) {
142 value = ParamUtil.getShort(portletRequest, name);
143 }
144 else if (type == ExpandoColumnConstants.SHORT_ARRAY) {
145 String[] values = StringUtil.split(
146 ParamUtil.getString(portletRequest, name), StringPool.NEW_LINE);
147
148 value = GetterUtil.getShortValues(values);
149 }
150 else if (type == ExpandoColumnConstants.STRING_ARRAY) {
151 value = StringUtil.split(
152 ParamUtil.getString(portletRequest, name), StringPool.NEW_LINE);
153 }
154 else {
155 value = ParamUtil.getString(portletRequest, name);
156 }
157
158 return value;
159 }
160
161 public void processAction(
162 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
163 ActionRequest actionRequest, ActionResponse actionResponse)
164 throws Exception {
165
166 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
167
168 try {
169 if (cmd.equals(Constants.ADD)) {
170 addExpando(actionRequest);
171 }
172 else if (cmd.equals(Constants.DELETE)) {
173 deleteExpando(actionRequest);
174 }
175 else if (cmd.equals(Constants.UPDATE)) {
176 updateExpando(actionRequest);
177 }
178
179 sendRedirect(actionRequest, actionResponse);
180 }
181 catch (Exception e) {
182 if (e instanceof NoSuchColumnException ||
183 e instanceof PrincipalException) {
184
185 SessionErrors.add(actionRequest, e.getClass().getName());
186
187 setForward(actionRequest, "portlet.expando.error");
188 }
189 else if (e instanceof ColumnNameException ||
190 e instanceof ColumnTypeException ||
191 e instanceof DuplicateColumnNameException ||
192 e instanceof ValueDataException) {
193
194 SessionErrors.add(actionRequest, e.getClass().getName());
195 }
196 else {
197 throw e;
198 }
199 }
200 }
201
202 public ActionForward render(
203 ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
204 RenderRequest renderRequest, RenderResponse renderResponse)
205 throws Exception {
206
207 try {
208 ActionUtil.getColumn(renderRequest);
209 }
210 catch (Exception e) {
211 if (e instanceof NoSuchColumnException ||
212 e instanceof PrincipalException) {
213
214 SessionErrors.add(renderRequest, e.getClass().getName());
215
216 return mapping.findForward("portlet.expando.error");
217 }
218 else {
219 throw e;
220 }
221 }
222
223 return mapping.findForward(
224 getForward(renderRequest, "portlet.expando.edit_expando"));
225 }
226
227 protected void addExpando(ActionRequest actionRequest) throws Exception {
228 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
229 WebKeys.THEME_DISPLAY);
230
231 String modelResource = ParamUtil.getString(
232 actionRequest, "modelResource");
233 long resourcePrimKey = ParamUtil.getLong(
234 actionRequest, "resourcePrimKey");
235
236 String name = ParamUtil.getString(actionRequest, "name");
237 String preset = ParamUtil.getString(actionRequest, "type");
238
239 ExpandoBridge expandoBridge = ExpandoBridgeFactoryUtil.getExpandoBridge(
240 themeDisplay.getCompanyId(), modelResource, resourcePrimKey);
241
242 if (preset.startsWith("Preset")) {
243 addPresetExpando(actionRequest, expandoBridge, preset, name);
244 }
245 else {
246 int type = ParamUtil.getInteger(actionRequest, "type");
247
248 expandoBridge.addAttribute(name, type);
249
250 updateProperties(actionRequest, expandoBridge, name);
251 }
252 }
253
254 protected void addPresetExpando(
255 ActionRequest actionRequest, ExpandoBridge expandoBridge,
256 String preset, String name)
257 throws Exception {
258
259 int type = 0;
260 UnicodeProperties properties = expandoBridge.getAttributeProperties(
261 name);
262
263 if (preset.equals("PresetSelectionIntegerArray()")) {
264 type = ExpandoColumnConstants.INTEGER_ARRAY;
265 properties.setProperty(
266 ExpandoColumnConstants.PROPERTY_SELECTION,
267 Boolean.TRUE.toString());
268 }
269 else if (preset.equals("PresetSelectionDoubleArray()")) {
270 type = ExpandoColumnConstants.DOUBLE_ARRAY;
271 properties.setProperty(
272 ExpandoColumnConstants.PROPERTY_SELECTION,
273 Boolean.TRUE.toString());
274 }
275 else if (preset.equals("PresetSelectionStringArray()")) {
276 type = ExpandoColumnConstants.STRING_ARRAY;
277 properties.setProperty(
278 ExpandoColumnConstants.PROPERTY_SELECTION,
279 Boolean.TRUE.toString());
280 }
281 else if (preset.equals("PresetTextBox()")) {
282 type = ExpandoColumnConstants.STRING;
283 properties.setProperty(
284 ExpandoColumnConstants.PROPERTY_HEIGHT, "105");
285 properties.setProperty(
286 ExpandoColumnConstants.PROPERTY_WIDTH, "450");
287 }
288 else if (preset.equals("PresetTextBoxIndexed()")) {
289 type = ExpandoColumnConstants.STRING;
290 properties.setProperty(
291 ExpandoColumnConstants.PROPERTY_HEIGHT, "105");
292 properties.setProperty(
293 ExpandoColumnConstants.PROPERTY_WIDTH, "450");
294 properties.setProperty(
295 ExpandoBridgeIndexer.INDEXABLE, Boolean.TRUE.toString());
296 }
297 else if (preset.equals("PresetTextFieldSecret()")) {
298 type = ExpandoColumnConstants.STRING;
299 properties.setProperty(
300 ExpandoColumnConstants.PROPERTY_SECRET,
301 Boolean.TRUE.toString());
302 }
303 else {
304 type = ExpandoColumnConstants.STRING;
305 properties.setProperty(
306 ExpandoBridgeIndexer.INDEXABLE, Boolean.TRUE.toString());
307 }
308
309 expandoBridge.addAttribute(name, type);
310
311 expandoBridge.setAttributeProperties(name, properties);
312 }
313
314 protected void deleteExpando(ActionRequest actionRequest) throws Exception {
315 long columnId = ParamUtil.getLong(actionRequest, "columnId");
316
317 ExpandoColumnServiceUtil.deleteColumn(columnId);
318 }
319
320 protected void updateExpando(ActionRequest actionRequest) throws Exception {
321 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
322 WebKeys.THEME_DISPLAY);
323
324 String modelResource = ParamUtil.getString(
325 actionRequest, "modelResource");
326 long resourcePrimKey = ParamUtil.getLong(
327 actionRequest, "resourcePrimKey");
328
329 String name = ParamUtil.getString(actionRequest, "name");
330 int type = ParamUtil.getInteger(actionRequest, "type");
331
332 Serializable defaultValue = getValue(
333 actionRequest, "defaultValue", type);
334
335 ExpandoBridge expandoBridge = ExpandoBridgeFactoryUtil.getExpandoBridge(
336 themeDisplay.getCompanyId(), modelResource, resourcePrimKey);
337
338 expandoBridge.setAttributeDefault(name, defaultValue);
339
340 updateProperties(actionRequest, expandoBridge, name);
341 }
342
343 protected void updateProperties(
344 ActionRequest actionRequest, ExpandoBridge expandoBridge,
345 String name)
346 throws Exception {
347
348 Enumeration<String> enu = actionRequest.getParameterNames();
349
350 UnicodeProperties properties = expandoBridge.getAttributeProperties(
351 name);
352
353 List<String> propertyNames = new ArrayList<String>();
354
355 while (enu.hasMoreElements()) {
356 String param = enu.nextElement();
357
358 if (param.indexOf("PropertyName(") != -1) {
359 String propertyName = ParamUtil.getString(actionRequest, param);
360
361 propertyNames.add(propertyName);
362 }
363 }
364
365 for (String propertyName : propertyNames) {
366 String value = ParamUtil.getString(
367 actionRequest, "Property(" + propertyName + ")");
368
369 properties.setProperty(propertyName, value);
370 }
371
372 expandoBridge.setAttributeProperties(name, properties);
373 }
374
375 }