001    /**
002     * Copyright (c) 2000-2013 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.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.portal.util.PortalUtil;
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.service.DDLRecordServiceUtil;
031    import com.liferay.portlet.dynamicdatalists.util.DDLUtil;
032    import com.liferay.portlet.dynamicdatamapping.StorageFieldRequiredException;
033    
034    import javax.portlet.ActionRequest;
035    import javax.portlet.ActionResponse;
036    import javax.portlet.PortletConfig;
037    import javax.portlet.RenderRequest;
038    import javax.portlet.RenderResponse;
039    
040    import org.apache.struts.action.ActionForm;
041    import org.apache.struts.action.ActionForward;
042    import org.apache.struts.action.ActionMapping;
043    
044    /**
045     * @author Marcellus Tavares
046     * @author Eduardo Lundgren
047     */
048    public class EditRecordAction extends PortletAction {
049    
050            @Override
051            public void processAction(
052                            ActionMapping actionMapping, ActionForm actionForm,
053                            PortletConfig portletConfig, ActionRequest actionRequest,
054                            ActionResponse actionResponse)
055                    throws Exception {
056    
057                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
058    
059                    try {
060                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
061                                    updateRecord(actionRequest);
062                            }
063                            else if (cmd.equals(Constants.DELETE)) {
064                                    deleteRecord(actionRequest);
065                            }
066                            else if (cmd.equals(Constants.REVERT)) {
067                                    revertRecordVersion(actionRequest);
068                            }
069                            else if (cmd.equals(Constants.TRANSLATE)) {
070                                    updateRecord(actionRequest);
071    
072                                    setForward(
073                                            actionRequest,
074                                            "portlet.dynamic_data_lists.update_translation_redirect");
075                            }
076    
077                            if (Validator.isNotNull(cmd) && !cmd.equals(Constants.TRANSLATE)) {
078                                    sendRedirect(actionRequest, actionResponse);
079                            }
080                    }
081                    catch (Exception e) {
082                            if (e instanceof NoSuchRecordException ||
083                                    e instanceof PrincipalException) {
084    
085                                    SessionErrors.add(actionRequest, e.getClass());
086    
087                                    setForward(actionRequest, "portlet.dynamic_data_lists.error");
088                            }
089                            else if (e instanceof FileSizeException ||
090                                             e instanceof StorageFieldRequiredException) {
091    
092                                    SessionErrors.add(actionRequest, e.getClass());
093                            }
094                            else {
095                                    throw e;
096                            }
097                    }
098            }
099    
100            @Override
101            public ActionForward render(
102                            ActionMapping actionMapping, ActionForm actionForm,
103                            PortletConfig portletConfig, RenderRequest renderRequest,
104                            RenderResponse renderResponse)
105                    throws Exception {
106    
107                    try {
108                            ActionUtil.getRecord(renderRequest);
109                    }
110                    catch (Exception e) {
111                            if (e instanceof NoSuchRecordException ||
112                                    e instanceof PrincipalException) {
113    
114                                    SessionErrors.add(renderRequest, e.getClass());
115    
116                                    return actionMapping.findForward(
117                                            "portlet.dynamic_data_lists.error");
118                            }
119                            else {
120                                    throw e;
121                            }
122                    }
123    
124                    return actionMapping.findForward(
125                            getForward(
126                                    renderRequest, "portlet.dynamic_data_lists.edit_record"));
127            }
128    
129            protected void deleteRecord(ActionRequest actionRequest) throws Exception {
130                    long recordId = ParamUtil.getLong(actionRequest, "recordId");
131    
132                    DDLRecordServiceUtil.deleteRecord(recordId);
133            }
134    
135            protected void revertRecordVersion(ActionRequest actionRequest)
136                    throws Exception {
137    
138                    long recordId = ParamUtil.getLong(actionRequest, "recordId");
139    
140                    String version = ParamUtil.getString(actionRequest, "version");
141    
142                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
143                            DDLRecord.class.getName(), actionRequest);
144    
145                    DDLRecordServiceUtil.revertRecordVersion(
146                            recordId, version, serviceContext);
147            }
148    
149            protected DDLRecord updateRecord(ActionRequest actionRequest)
150                    throws Exception {
151    
152                    UploadPortletRequest uploadPortletRequest =
153                            PortalUtil.getUploadPortletRequest(actionRequest);
154    
155                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
156                            DDLRecord.class.getName(), uploadPortletRequest);
157    
158                    long recordId = ParamUtil.getLong(serviceContext, "recordId");
159    
160                    long recordSetId = ParamUtil.getLong(serviceContext, "recordSetId");
161    
162                    return DDLUtil.updateRecord(
163                            recordId, recordSetId, true, serviceContext);
164            }
165    
166    }