001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.dynamicdatalists.action;
016    
017    import com.liferay.portal.kernel.servlet.SessionErrors;
018    import com.liferay.portal.kernel.upload.UploadPortletRequest;
019    import com.liferay.portal.kernel.util.Constants;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.kernel.util.StringUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.security.auth.PrincipalException;
025    import com.liferay.portal.service.ServiceContext;
026    import com.liferay.portal.service.ServiceContextFactory;
027    import com.liferay.portal.struts.PortletAction;
028    import com.liferay.portal.theme.ThemeDisplay;
029    import com.liferay.portal.util.PortalUtil;
030    import com.liferay.portal.util.WebKeys;
031    import com.liferay.portlet.documentlibrary.FileSizeException;
032    import com.liferay.portlet.dynamicdatalists.NoSuchRecordException;
033    import com.liferay.portlet.dynamicdatalists.model.DDLRecord;
034    import com.liferay.portlet.dynamicdatalists.model.DDLRecordConstants;
035    import com.liferay.portlet.dynamicdatalists.model.DDLRecordSet;
036    import com.liferay.portlet.dynamicdatalists.service.DDLRecordLocalServiceUtil;
037    import com.liferay.portlet.dynamicdatalists.service.DDLRecordServiceUtil;
038    import com.liferay.portlet.dynamicdatalists.service.DDLRecordSetLocalServiceUtil;
039    import com.liferay.portlet.dynamicdatalists.util.DDLUtil;
040    import com.liferay.portlet.dynamicdatamapping.StorageFieldRequiredException;
041    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
042    import com.liferay.portlet.dynamicdatamapping.storage.Field;
043    import com.liferay.portlet.dynamicdatamapping.storage.FieldConstants;
044    import com.liferay.portlet.dynamicdatamapping.storage.Fields;
045    
046    import java.io.Serializable;
047    
048    import javax.portlet.ActionRequest;
049    import javax.portlet.ActionResponse;
050    import javax.portlet.PortletConfig;
051    import javax.portlet.RenderRequest;
052    import javax.portlet.RenderResponse;
053    
054    import org.apache.struts.action.ActionForm;
055    import org.apache.struts.action.ActionForward;
056    import org.apache.struts.action.ActionMapping;
057    
058    /**
059     * @author Marcellus Tavares
060     * @author Eduardo Lundgren
061     */
062    public class EditRecordAction extends PortletAction {
063    
064            @Override
065            public void processAction(
066                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
067                            ActionRequest actionRequest, ActionResponse actionResponse)
068                    throws Exception {
069    
070                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
071    
072                    try {
073                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
074                                    updateRecord(actionRequest);
075                            }
076                            else if (cmd.equals(Constants.DELETE)) {
077                                    deleteRecord(actionRequest);
078                            }
079                            else if (cmd.equals(Constants.REVERT)) {
080                                    revertRecordVersion(actionRequest);
081                            }
082    
083                            if (Validator.isNotNull(cmd)) {
084                                    sendRedirect(actionRequest, actionResponse);
085                            }
086                    }
087                    catch (Exception e) {
088                            if (e instanceof NoSuchRecordException ||
089                                    e instanceof PrincipalException) {
090    
091                                    SessionErrors.add(actionRequest, e.getClass().getName());
092    
093                                    setForward(actionRequest, "portlet.dynamic_data_lists.error");
094                            }
095                            else if (e instanceof FileSizeException ||
096                                             e instanceof StorageFieldRequiredException) {
097    
098                                    SessionErrors.add(actionRequest, e.getClass().getName());
099                            }
100                            else {
101                                    throw e;
102                            }
103                    }
104            }
105    
106            @Override
107            public ActionForward render(
108                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
109                            RenderRequest renderRequest, RenderResponse renderResponse)
110                    throws Exception {
111    
112                    try {
113                            ActionUtil.getRecord(renderRequest);
114                    }
115                    catch (Exception e) {
116                            if (e instanceof NoSuchRecordException ||
117                                    e instanceof PrincipalException) {
118    
119                                    SessionErrors.add(renderRequest, e.getClass().getName());
120    
121                                    return mapping.findForward("portlet.dynamic_data_lists.error");
122                            }
123                            else {
124                                    throw e;
125                            }
126                    }
127    
128                    return mapping.findForward(
129                            getForward(
130                                    renderRequest, "portlet.dynamic_data_lists.edit_record"));
131            }
132    
133            protected void deleteRecord(ActionRequest actionRequest)
134                    throws Exception {
135    
136                    long recordId = ParamUtil.getLong(actionRequest, "recordId");
137    
138                    DDLRecordLocalServiceUtil.deleteRecord(recordId);
139            }
140    
141            protected void revertRecordVersion(ActionRequest actionRequest)
142                    throws Exception {
143    
144                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
145                            WebKeys.THEME_DISPLAY);
146    
147                    long recordId = ParamUtil.getLong(actionRequest, "recordId");
148    
149                    String version = ParamUtil.getString(actionRequest, "version");
150    
151                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
152                            DDLRecord.class.getName(), actionRequest);
153    
154                    DDLRecordLocalServiceUtil.revertRecordVersion(
155                            themeDisplay.getUserId(), recordId, version, serviceContext);
156            }
157    
158            protected DDLRecord updateRecord(ActionRequest actionRequest)
159                    throws Exception {
160    
161                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
162                            WebKeys.THEME_DISPLAY);
163    
164                    long recordId = ParamUtil.getLong(actionRequest, "recordId");
165    
166                    long recordSetId = ParamUtil.getLong(actionRequest, "recordSetId");
167                    boolean majorVersion = ParamUtil.getBoolean(
168                            actionRequest, "majorVersion");
169    
170                    DDLRecord record = DDLRecordLocalServiceUtil.fetchRecord(recordId);
171    
172                    DDLRecordSet recordSet = DDLRecordSetLocalServiceUtil.getRecordSet(
173                            recordSetId);
174    
175                    DDMStructure ddmStructure = recordSet.getDDMStructure();
176    
177                    Fields fields = new Fields();
178    
179                    for (String fieldName : ddmStructure.getFieldNames()) {
180                            String fieldDataType = ddmStructure.getFieldDataType(fieldName);
181                            String fieldType = ddmStructure.getFieldType(fieldName);
182                            String fieldValue = ParamUtil.getString(actionRequest, fieldName);
183    
184                            if (fieldDataType.equals(FieldConstants.FILE_UPLOAD)) {
185                                    continue;
186                            }
187    
188                            if (fieldType.equals("select")) {
189                                    boolean multiple = GetterUtil.getBoolean(
190                                            ddmStructure.getFieldProperty(fieldName, "multiple"));
191    
192                                    if (multiple) {
193                                            String[] fieldValues = ParamUtil.getParameterValues(
194                                                    actionRequest, fieldName);
195    
196                                            fieldValue = StringUtil.merge(fieldValues);
197                                    }
198                            }
199    
200                            Serializable fieldValueSerializable =
201                                    FieldConstants.getSerializable(fieldDataType, fieldValue);
202    
203                            Field field = new Field(
204                                    ddmStructure.getStructureId(), fieldName,
205                                    fieldValueSerializable);
206    
207                            fields.put(field);
208                    }
209    
210                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
211                            DDLRecord.class.getName(), actionRequest);
212    
213                    if (record != null) {
214                            record = DDLRecordServiceUtil.updateRecord(
215                                    recordId, majorVersion,
216                                    DDLRecordConstants.DISPLAY_INDEX_DEFAULT, fields, false,
217                                    serviceContext);
218                    }
219                    else {
220                            record = DDLRecordServiceUtil.addRecord(
221                                    themeDisplay.getScopeGroupId(), recordSetId,
222                                    DDLRecordConstants.DISPLAY_INDEX_DEFAULT, fields,
223                                    serviceContext);
224                    }
225    
226                    UploadPortletRequest uploadPortletRequest =
227                            PortalUtil.getUploadPortletRequest(actionRequest);
228    
229                    DDLUtil.uploadRecordFieldFiles(
230                            record, uploadPortletRequest, serviceContext);
231    
232                    return record;
233            }
234    
235    }