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