001    /**
002     * Copyright (c) 2000-present 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.exception.PortalException;
018    import com.liferay.portal.kernel.servlet.SessionErrors;
019    import com.liferay.portal.kernel.util.Constants;
020    import com.liferay.portal.kernel.util.ParamUtil;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.security.auth.PrincipalException;
023    import com.liferay.portal.service.ServiceContext;
024    import com.liferay.portal.service.ServiceContextFactory;
025    import com.liferay.portal.struts.PortletAction;
026    import com.liferay.portlet.documentlibrary.DuplicateFileException;
027    import com.liferay.portlet.documentlibrary.FileSizeException;
028    import com.liferay.portlet.dynamicdatalists.NoSuchRecordException;
029    import com.liferay.portlet.dynamicdatalists.model.DDLRecord;
030    import com.liferay.portlet.dynamicdatalists.model.DDLRecordConstants;
031    import com.liferay.portlet.dynamicdatalists.model.DDLRecordSet;
032    import com.liferay.portlet.dynamicdatalists.service.DDLRecordServiceUtil;
033    import com.liferay.portlet.dynamicdatalists.service.DDLRecordSetServiceUtil;
034    import com.liferay.portlet.dynamicdatamapping.StorageFieldRequiredException;
035    import com.liferay.portlet.dynamicdatamapping.io.DDMFormValuesJSONDeserializerUtil;
036    import com.liferay.portlet.dynamicdatamapping.model.DDMForm;
037    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
038    import com.liferay.portlet.dynamicdatamapping.storage.DDMFormValues;
039    
040    import javax.portlet.ActionRequest;
041    import javax.portlet.ActionResponse;
042    import javax.portlet.PortletConfig;
043    import javax.portlet.RenderRequest;
044    import javax.portlet.RenderResponse;
045    
046    import org.apache.struts.action.ActionForm;
047    import org.apache.struts.action.ActionForward;
048    import org.apache.struts.action.ActionMapping;
049    
050    /**
051     * @author Marcellus Tavares
052     * @author Eduardo Lundgren
053     */
054    public class EditRecordAction extends PortletAction {
055    
056            @Override
057            public void processAction(
058                            ActionMapping actionMapping, ActionForm actionForm,
059                            PortletConfig portletConfig, ActionRequest actionRequest,
060                            ActionResponse actionResponse)
061                    throws Exception {
062    
063                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
064    
065                    try {
066                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
067                                    updateRecord(actionRequest);
068                            }
069                            else if (cmd.equals(Constants.DELETE)) {
070                                    deleteRecord(actionRequest);
071                            }
072                            else if (cmd.equals(Constants.REVERT)) {
073                                    revertRecord(actionRequest);
074                            }
075    
076                            if (Validator.isNotNull(cmd)) {
077                                    sendRedirect(actionRequest, actionResponse);
078                            }
079                    }
080                    catch (Exception e) {
081                            if (e instanceof NoSuchRecordException ||
082                                    e instanceof PrincipalException) {
083    
084                                    SessionErrors.add(actionRequest, e.getClass());
085    
086                                    setForward(actionRequest, "portlet.dynamic_data_lists.error");
087                            }
088                            else if (e instanceof FileSizeException ||
089                                             e instanceof StorageFieldRequiredException) {
090    
091                                    SessionErrors.add(actionRequest, e.getClass());
092                            }
093                            else {
094                                    Throwable cause = e.getCause();
095    
096                                    if (cause instanceof DuplicateFileException) {
097                                            SessionErrors.add(
098                                                    actionRequest, DuplicateFileException.class);
099                                    }
100                                    else {
101                                            throw e;
102                                    }
103                            }
104                    }
105            }
106    
107            @Override
108            public ActionForward render(
109                            ActionMapping actionMapping, ActionForm actionForm,
110                            PortletConfig portletConfig, RenderRequest renderRequest,
111                            RenderResponse renderResponse)
112                    throws Exception {
113    
114                    try {
115                            ActionUtil.getRecord(renderRequest);
116                    }
117                    catch (Exception e) {
118                            if (e instanceof NoSuchRecordException ||
119                                    e instanceof PrincipalException) {
120    
121                                    SessionErrors.add(renderRequest, e.getClass());
122    
123                                    return actionMapping.findForward(
124                                            "portlet.dynamic_data_lists.error");
125                            }
126                            else {
127                                    throw e;
128                            }
129                    }
130    
131                    return actionMapping.findForward(
132                            getForward(
133                                    renderRequest, "portlet.dynamic_data_lists.edit_record"));
134            }
135    
136            protected void deleteRecord(ActionRequest actionRequest) throws Exception {
137                    long recordId = ParamUtil.getLong(actionRequest, "recordId");
138    
139                    DDLRecordServiceUtil.deleteRecord(recordId);
140            }
141    
142            protected DDMForm getDDMForm(ActionRequest actionRequest)
143                    throws PortalException {
144    
145                    long recordSetId = ParamUtil.getLong(actionRequest, "recordSetId");
146    
147                    DDLRecordSet recordSet = DDLRecordSetServiceUtil.getRecordSet(
148                            recordSetId);
149    
150                    DDMStructure ddmStructure = recordSet.getDDMStructure();
151    
152                    return ddmStructure.getFullHierarchyDDMForm();
153            }
154    
155            protected DDMFormValues getDDMFormValues(ActionRequest actionRequest)
156                    throws PortalException {
157    
158                    DDMForm ddmForm = getDDMForm(actionRequest);
159    
160                    String serializedDDMFormValues = ParamUtil.getString(
161                            actionRequest, "ddmFormValues");
162    
163                    return DDMFormValuesJSONDeserializerUtil.deserialize(
164                            ddmForm, serializedDDMFormValues);
165            }
166    
167            protected void revertRecord(ActionRequest actionRequest) throws Exception {
168                    long recordId = ParamUtil.getLong(actionRequest, "recordId");
169    
170                    String version = ParamUtil.getString(actionRequest, "version");
171    
172                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
173                            DDLRecord.class.getName(), actionRequest);
174    
175                    DDLRecordServiceUtil.revertRecord(recordId, version, serviceContext);
176            }
177    
178            protected DDLRecord updateRecord(ActionRequest actionRequest)
179                    throws Exception {
180    
181                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
182    
183                    long recordId = ParamUtil.getLong(actionRequest, "recordId");
184    
185                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
186                    long recordSetId = ParamUtil.getLong(actionRequest, "recordSetId");
187                    DDMFormValues ddmFormValues = getDDMFormValues(actionRequest);
188                    boolean majorVersion = ParamUtil.getBoolean(
189                            actionRequest, "majorVersion");
190    
191                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
192                            DDLRecord.class.getName(), actionRequest);
193    
194                    DDLRecord record = null;
195    
196                    if (cmd.equals(Constants.ADD)) {
197                            record = DDLRecordServiceUtil.addRecord(
198                                    groupId, recordSetId, DDLRecordConstants.DISPLAY_INDEX_DEFAULT,
199                                    ddmFormValues, serviceContext);
200                    }
201                    else {
202                            record = DDLRecordServiceUtil.updateRecord(
203                                    recordId, majorVersion,
204                                    DDLRecordConstants.DISPLAY_INDEX_DEFAULT, ddmFormValues,
205                                    serviceContext);
206                    }
207    
208                    return record;
209            }
210    
211    }