001    /**
002     * Copyright (c) 2000-2012 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 mapping, ActionForm form, PortletConfig portletConfig,
057                            ActionRequest actionRequest, 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().getName());
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().getName());
086                            }
087                            else {
088                                    throw e;
089                            }
090                    }
091            }
092    
093            @Override
094            public ActionForward render(
095                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
096                            RenderRequest renderRequest, RenderResponse renderResponse)
097                    throws Exception {
098    
099                    try {
100                            String cmd = ParamUtil.getString(renderRequest, Constants.CMD);
101    
102                            if (!cmd.equals(Constants.ADD)) {
103                                    ActionUtil.getRecordSet(renderRequest);
104                            }
105                    }
106                    catch (NoSuchRecordSetException nsee) {
107    
108                            // Let this slide because the user can manually input an record set
109                            // key for a new record set that does not yet exist
110    
111                    }
112                    catch (Exception e) {
113                            if (e instanceof PrincipalException) {
114                                    SessionErrors.add(renderRequest, e.getClass().getName());
115    
116                                    return mapping.findForward("portlet.dynamic_data_lists.error");
117                            }
118                            else {
119                                    throw e;
120                            }
121                    }
122    
123                    return mapping.findForward(
124                            getForward(
125                                    renderRequest, "portlet.dynamic_data_lists.edit_record_set"));
126            }
127    
128            protected void deleteRecordSet(ActionRequest actionRequest)
129                    throws Exception {
130    
131                    long recordSetId = ParamUtil.getLong(actionRequest, "recordSetId");
132    
133                    DDLRecordSetServiceUtil.deleteRecordSet(recordSetId);
134            }
135    
136            protected DDLRecordSet updateRecordSet(ActionRequest actionRequest)
137                    throws Exception {
138    
139                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
140    
141                    long recordSetId = ParamUtil.getLong(actionRequest, "recordSetId");
142    
143                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
144                    long ddmStructureId = ParamUtil.getLong(
145                            actionRequest, "ddmStructureId");
146                    Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap(
147                            actionRequest, "name");
148                    Map<Locale, String> descriptionMap =
149                            LocalizationUtil.getLocalizationMap(actionRequest, "description");
150                    int scope = ParamUtil.getInteger(actionRequest, "scope");
151    
152                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
153                            DDLRecordSet.class.getName(), actionRequest);
154    
155                    DDLRecordSet recordSet = null;
156    
157                    if (cmd.equals(Constants.ADD)) {
158                            recordSet = DDLRecordSetServiceUtil.addRecordSet(
159                                    groupId, ddmStructureId, null, nameMap, descriptionMap,
160                                    DDLRecordSetConstants.MIN_DISPLAY_ROWS_DEFAULT, scope,
161                                    serviceContext);
162                    }
163                    else {
164                            recordSet = DDLRecordSetServiceUtil.updateRecordSet(
165                                    recordSetId, ddmStructureId, nameMap, descriptionMap,
166                                    DDLRecordSetConstants.MIN_DISPLAY_ROWS_DEFAULT, serviceContext);
167                    }
168    
169                    String workflowDefinition = ParamUtil.getString(
170                            actionRequest, "workflowDefinition");
171    
172                    WorkflowDefinitionLinkLocalServiceUtil.updateWorkflowDefinitionLink(
173                            serviceContext.getUserId(), serviceContext.getCompanyId(), groupId,
174                            DDLRecordSet.class.getName(), recordSet.getRecordSetId(), 0,
175                            workflowDefinition);
176    
177                    String portletResource = ParamUtil.getString(
178                            actionRequest, "portletResource");
179    
180                    if (Validator.isNotNull(portletResource)) {
181                            PortletPreferences preferences =
182                                    PortletPreferencesFactoryUtil.getPortletSetup(
183                                            actionRequest, portletResource);
184    
185                            preferences.reset("detailDDMTemplateId");
186                            preferences.reset("editable");
187                            preferences.reset("listDDMTemplateId");
188                            preferences.reset("spreadsheet");
189    
190                            preferences.setValue(
191                                    "recordSetId", String.valueOf(recordSet.getRecordSetId()));
192    
193                            preferences.store();
194                    }
195    
196                    return recordSet;
197            }
198    
199    }