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