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.util.Constants;
019    import com.liferay.portal.kernel.util.LocalizationUtil;
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.service.WorkflowDefinitionLinkLocalServiceUtil;
026    import com.liferay.portal.struts.PortletAction;
027    import com.liferay.portlet.dynamicdatalists.NoSuchRecordSetException;
028    import com.liferay.portlet.dynamicdatalists.RecordSetDDMStructureIdException;
029    import com.liferay.portlet.dynamicdatalists.RecordSetNameException;
030    import com.liferay.portlet.dynamicdatalists.model.DDLRecordSet;
031    import com.liferay.portlet.dynamicdatalists.model.DDLRecordSetConstants;
032    import com.liferay.portlet.dynamicdatalists.service.DDLRecordSetServiceUtil;
033    
034    import java.util.Locale;
035    import java.util.Map;
036    
037    import javax.portlet.ActionRequest;
038    import javax.portlet.ActionResponse;
039    import javax.portlet.PortletConfig;
040    import javax.portlet.PortletPreferences;
041    import javax.portlet.RenderRequest;
042    import javax.portlet.RenderResponse;
043    
044    import org.apache.struts.action.ActionForm;
045    import org.apache.struts.action.ActionForward;
046    import org.apache.struts.action.ActionMapping;
047    
048    /**
049     * @author Marcellus Tavares
050     */
051    public class EditRecordSetAction extends PortletAction {
052    
053            @Override
054            public void processAction(
055                            ActionMapping actionMapping, ActionForm actionForm,
056                            PortletConfig portletConfig, ActionRequest actionRequest,
057                            ActionResponse actionResponse)
058                    throws Exception {
059    
060                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
061    
062                    try {
063                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
064                                    updateRecordSet(actionRequest);
065                            }
066                            else if (cmd.equals(Constants.DELETE)) {
067                                    deleteRecordSet(actionRequest);
068                            }
069    
070                            if (Validator.isNotNull(cmd)) {
071                                    sendRedirect(actionRequest, actionResponse);
072                            }
073                    }
074                    catch (Exception e) {
075                            if (e instanceof NoSuchRecordSetException ||
076                                    e instanceof PrincipalException) {
077    
078                                    SessionErrors.add(actionRequest, e.getClass());
079    
080                                    setForward(actionRequest, "portlet.dynamic_data_lists.error");
081                            }
082                            else if (e instanceof RecordSetDDMStructureIdException ||
083                                             e instanceof RecordSetNameException) {
084    
085                                    SessionErrors.add(actionRequest, e.getClass());
086                            }
087                            else {
088                                    throw e;
089                            }
090                    }
091            }
092    
093            @Override
094            public ActionForward render(
095                            ActionMapping actionMapping, ActionForm actionForm,
096                            PortletConfig portletConfig, RenderRequest renderRequest,
097                            RenderResponse renderResponse)
098                    throws Exception {
099    
100                    try {
101                            String cmd = ParamUtil.getString(renderRequest, Constants.CMD);
102    
103                            if (!cmd.equals(Constants.ADD)) {
104                                    ActionUtil.getRecordSet(renderRequest);
105                            }
106                    }
107                    catch (NoSuchRecordSetException nsrse) {
108    
109                            // Let this slide because the user can manually input an record set
110                            // key for a new record set that does not yet exist
111    
112                    }
113                    catch (Exception e) {
114                            if (e instanceof PrincipalException) {
115                                    SessionErrors.add(renderRequest, e.getClass());
116    
117                                    return actionMapping.findForward(
118                                            "portlet.dynamic_data_lists.error");
119                            }
120                            else {
121                                    throw e;
122                            }
123                    }
124    
125                    return actionMapping.findForward(
126                            getForward(
127                                    renderRequest, "portlet.dynamic_data_lists.edit_record_set"));
128            }
129    
130            protected void deleteRecordSet(ActionRequest actionRequest)
131                    throws Exception {
132    
133                    long recordSetId = ParamUtil.getLong(actionRequest, "recordSetId");
134    
135                    DDLRecordSetServiceUtil.deleteRecordSet(recordSetId);
136            }
137    
138            protected DDLRecordSet updateRecordSet(ActionRequest actionRequest)
139                    throws Exception {
140    
141                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
142    
143                    long recordSetId = ParamUtil.getLong(actionRequest, "recordSetId");
144    
145                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
146                    long ddmStructureId = ParamUtil.getLong(
147                            actionRequest, "ddmStructureId");
148                    Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap(
149                            actionRequest, "name");
150                    Map<Locale, String> descriptionMap =
151                            LocalizationUtil.getLocalizationMap(actionRequest, "description");
152                    int scope = ParamUtil.getInteger(actionRequest, "scope");
153    
154                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
155                            DDLRecordSet.class.getName(), actionRequest);
156    
157                    DDLRecordSet recordSet = null;
158    
159                    if (cmd.equals(Constants.ADD)) {
160                            recordSet = DDLRecordSetServiceUtil.addRecordSet(
161                                    groupId, ddmStructureId, null, nameMap, descriptionMap,
162                                    DDLRecordSetConstants.MIN_DISPLAY_ROWS_DEFAULT, scope,
163                                    serviceContext);
164                    }
165                    else {
166                            recordSet = DDLRecordSetServiceUtil.updateRecordSet(
167                                    recordSetId, ddmStructureId, nameMap, descriptionMap,
168                                    DDLRecordSetConstants.MIN_DISPLAY_ROWS_DEFAULT, serviceContext);
169                    }
170    
171                    String workflowDefinition = ParamUtil.getString(
172                            actionRequest, "workflowDefinition");
173    
174                    WorkflowDefinitionLinkLocalServiceUtil.updateWorkflowDefinitionLink(
175                            serviceContext.getUserId(), serviceContext.getCompanyId(), groupId,
176                            DDLRecordSet.class.getName(), recordSet.getRecordSetId(), 0,
177                            workflowDefinition);
178    
179                    PortletPreferences portletPreferences = getStrictPortletSetup(
180                            actionRequest);
181    
182                    if (portletPreferences != null) {
183                            portletPreferences.reset("displayDDMTemplateId");
184                            portletPreferences.reset("editable");
185                            portletPreferences.reset("formDDMTemplateId");
186                            portletPreferences.reset("spreadsheet");
187    
188                            portletPreferences.setValue(
189                                    "recordSetId", String.valueOf(recordSet.getRecordSetId()));
190    
191                            portletPreferences.store();
192                    }
193    
194                    return recordSet;
195            }
196    
197    }