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 import com.liferay.portlet.expando.util.ExpandoBridgeIndexer;
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 public static Serializable getValue(
067 PortletRequest portletRequest, String name, int type)
068 throws PortalException, SystemException {
069
070 Serializable value = null;
071
072 if (type == ExpandoColumnConstants.BOOLEAN) {
073 value = ParamUtil.getBoolean(portletRequest, name);
074 }
075 else if (type == ExpandoColumnConstants.BOOLEAN_ARRAY) {
076 }
077 else if (type == ExpandoColumnConstants.DATE) {
078 User user = PortalUtil.getUser(portletRequest);
079
080 int valueDateMonth = ParamUtil.getInteger(
081 portletRequest, name + "Month");
082 int valueDateDay = ParamUtil.getInteger(
083 portletRequest, name + "Day");
084 int valueDateYear = ParamUtil.getInteger(
085 portletRequest, name + "Year");
086 int valueDateHour = ParamUtil.getInteger(
087 portletRequest, name + "Hour");
088 int valueDateMinute = ParamUtil.getInteger(
089 portletRequest, name + "Minute");
090 int valueDateAmPm = ParamUtil.getInteger(
091 portletRequest, name + "AmPm");
092
093 if (valueDateAmPm == Calendar.PM) {
094 valueDateHour += 12;
095 }
096
097 value = PortalUtil.getDate(
098 valueDateMonth, valueDateDay, valueDateYear, valueDateHour,
099 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 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
227 WebKeys.THEME_DISPLAY);
228
229 String modelResource = ParamUtil.getString(
230 actionRequest, "modelResource");
231 long resourcePrimKey = ParamUtil.getLong(
232 actionRequest, "resourcePrimKey");
233
234 String name = ParamUtil.getString(actionRequest, "name");
235 String preset = ParamUtil.getString(actionRequest, "type");
236
237 ExpandoBridge expandoBridge = ExpandoBridgeFactoryUtil.getExpandoBridge(
238 themeDisplay.getCompanyId(), modelResource, resourcePrimKey);
239
240 if (preset.startsWith("Preset")) {
241 addPresetExpando(actionRequest, expandoBridge, preset, name);
242 }
243 else {
244 int type = ParamUtil.getInteger(actionRequest, "type");
245
246 expandoBridge.addAttribute(name, type);
247
248 updateProperties(actionRequest, expandoBridge, name);
249 }
250 }
251
252 protected void addPresetExpando(
253 ActionRequest actionRequest, ExpandoBridge expandoBridge,
254 String preset, String name)
255 throws Exception {
256
257 int type = 0;
258 UnicodeProperties properties = expandoBridge.getAttributeProperties(
259 name);
260
261 if (preset.equals("PresetSelectionIntegerArray()")) {
262 type = ExpandoColumnConstants.INTEGER_ARRAY;
263 properties.setProperty(
264 ExpandoColumnConstants.PROPERTY_SELECTION,
265 Boolean.TRUE.toString());
266 }
267 else if (preset.equals("PresetSelectionDoubleArray()")) {
268 type = ExpandoColumnConstants.DOUBLE_ARRAY;
269 properties.setProperty(
270 ExpandoColumnConstants.PROPERTY_SELECTION,
271 Boolean.TRUE.toString());
272 }
273 else if (preset.equals("PresetSelectionStringArray()")) {
274 type = ExpandoColumnConstants.STRING_ARRAY;
275 properties.setProperty(
276 ExpandoColumnConstants.PROPERTY_SELECTION,
277 Boolean.TRUE.toString());
278 }
279 else if (preset.equals("PresetTextBox()")) {
280 type = ExpandoColumnConstants.STRING;
281 properties.setProperty(
282 ExpandoColumnConstants.PROPERTY_HEIGHT, "105");
283 properties.setProperty(
284 ExpandoColumnConstants.PROPERTY_WIDTH, "450");
285 }
286 else if (preset.equals("PresetTextBoxIndexed()")) {
287 type = ExpandoColumnConstants.STRING;
288 properties.setProperty(
289 ExpandoColumnConstants.PROPERTY_HEIGHT, "105");
290 properties.setProperty(
291 ExpandoColumnConstants.PROPERTY_WIDTH, "450");
292 properties.setProperty(
293 ExpandoBridgeIndexer.INDEXABLE, Boolean.TRUE.toString());
294 }
295 else if (preset.equals("PresetTextFieldSecret()")) {
296 type = ExpandoColumnConstants.STRING;
297 properties.setProperty(
298 ExpandoColumnConstants.PROPERTY_SECRET,
299 Boolean.TRUE.toString());
300 }
301 else {
302 type = ExpandoColumnConstants.STRING;
303 properties.setProperty(
304 ExpandoBridgeIndexer.INDEXABLE, Boolean.TRUE.toString());
305 }
306
307 expandoBridge.addAttribute(name, type);
308
309 expandoBridge.setAttributeProperties(name, properties);
310 }
311
312 protected void deleteExpando(ActionRequest actionRequest) throws Exception {
313 long columnId = ParamUtil.getLong(actionRequest, "columnId");
314
315 ExpandoColumnServiceUtil.deleteColumn(columnId);
316 }
317
318 protected void updateExpando(ActionRequest actionRequest) throws Exception {
319 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
320 WebKeys.THEME_DISPLAY);
321
322 String modelResource = ParamUtil.getString(
323 actionRequest, "modelResource");
324 long resourcePrimKey = ParamUtil.getLong(
325 actionRequest, "resourcePrimKey");
326
327 String name = ParamUtil.getString(actionRequest, "name");
328 int type = ParamUtil.getInteger(actionRequest, "type");
329
330 Serializable defaultValue = getValue(
331 actionRequest, "defaultValue", type);
332
333 ExpandoBridge expandoBridge = ExpandoBridgeFactoryUtil.getExpandoBridge(
334 themeDisplay.getCompanyId(), modelResource, resourcePrimKey);
335
336 expandoBridge.setAttributeDefault(name, defaultValue);
337
338 updateProperties(actionRequest, expandoBridge, name);
339 }
340
341 protected void updateProperties(
342 ActionRequest actionRequest, ExpandoBridge expandoBridge,
343 String name)
344 throws Exception {
345
346 Enumeration<String> enu = actionRequest.getParameterNames();
347
348 UnicodeProperties properties = expandoBridge.getAttributeProperties(
349 name);
350
351 List<String> propertyNames = new ArrayList<String>();
352
353 while (enu.hasMoreElements()) {
354 String param = enu.nextElement();
355
356 if (param.indexOf("PropertyName--") != -1) {
357 String propertyName = ParamUtil.getString(actionRequest, param);
358
359 propertyNames.add(propertyName);
360 }
361 }
362
363 for (String propertyName : propertyNames) {
364 String value = ParamUtil.getString(
365 actionRequest, "Property--" + propertyName + "--");
366
367 properties.setProperty(propertyName, value);
368 }
369
370 expandoBridge.setAttributeProperties(name, properties);
371 }
372
373 }