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.dynamicdatamapping.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.LocalizationUtil;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.portal.security.auth.PrincipalException;
024    import com.liferay.portal.service.ServiceContext;
025    import com.liferay.portal.service.ServiceContextFactory;
026    import com.liferay.portal.struts.PortletAction;
027    import com.liferay.portal.theme.ThemeDisplay;
028    import com.liferay.portal.util.PortalUtil;
029    import com.liferay.portal.util.WebKeys;
030    import com.liferay.portlet.PortletPreferencesFactoryUtil;
031    import com.liferay.portlet.PortletURLImpl;
032    import com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException;
033    import com.liferay.portlet.dynamicdatamapping.TemplateNameException;
034    import com.liferay.portlet.dynamicdatamapping.TemplateScriptException;
035    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
036    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
037    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants;
038    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureLocalServiceUtil;
039    import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateServiceUtil;
040    import com.liferay.util.JS;
041    
042    import java.util.Locale;
043    import java.util.Map;
044    
045    import javax.portlet.ActionRequest;
046    import javax.portlet.ActionResponse;
047    import javax.portlet.PortletConfig;
048    import javax.portlet.PortletPreferences;
049    import javax.portlet.PortletRequest;
050    import javax.portlet.RenderRequest;
051    import javax.portlet.RenderResponse;
052    
053    import org.apache.struts.action.ActionForm;
054    import org.apache.struts.action.ActionForward;
055    import org.apache.struts.action.ActionMapping;
056    
057    /**
058     * @author Eduardo Lundgren
059     */
060    public class EditTemplateAction extends PortletAction {
061    
062            @Override
063            public void processAction(
064                            ActionMapping actionMapping, ActionForm actionForm,
065                            PortletConfig portletConfig, ActionRequest actionRequest,
066                            ActionResponse actionResponse)
067                    throws Exception {
068    
069                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
070    
071                    DDMTemplate template = null;
072    
073                    try {
074                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
075                                    template = updateTemplate(actionRequest);
076                            }
077                            else if (cmd.equals(Constants.DELETE)) {
078                                    deleteTemplate(actionRequest);
079                            }
080    
081                            if (Validator.isNotNull(cmd)) {
082                                    String redirect = ParamUtil.getString(
083                                            actionRequest, "redirect");
084    
085                                    if (template != null) {
086                                            boolean saveAndContinue = ParamUtil.getBoolean(
087                                                    actionRequest, "saveAndContinue");
088    
089                                            if (saveAndContinue) {
090                                                    redirect = getSaveAndContinueRedirect(
091                                                            portletConfig, actionRequest, template, redirect);
092                                            }
093                                    }
094    
095                                    sendRedirect(actionRequest, actionResponse, redirect);
096                            }
097                    }
098                    catch (Exception e) {
099                            if (e instanceof NoSuchTemplateException ||
100                                    e instanceof PrincipalException) {
101    
102                                    SessionErrors.add(actionRequest, e.getClass());
103    
104                                    setForward(actionRequest, "portlet.dynamic_data_mapping.error");
105                            }
106                            else if (e instanceof TemplateNameException ||
107                                             e instanceof TemplateScriptException) {
108    
109                                    SessionErrors.add(actionRequest, e.getClass(), e);
110                            }
111                            else {
112                                    throw e;
113                            }
114                    }
115            }
116    
117            @Override
118            public ActionForward render(
119                            ActionMapping actionMapping, ActionForm actionForm,
120                            PortletConfig portletConfig, RenderRequest renderRequest,
121                            RenderResponse renderResponse)
122                    throws Exception {
123    
124                    try {
125                            ActionUtil.getStructure(renderRequest);
126                            ActionUtil.getTemplate(renderRequest);
127                    }
128                    catch (Exception e) {
129                            if (e instanceof NoSuchTemplateException ||
130                                    e instanceof PrincipalException) {
131    
132                                    SessionErrors.add(renderRequest, e.getClass());
133    
134                                    return actionMapping.findForward(
135                                            "portlet.dynamic_data_mapping.error");
136                            }
137                            else {
138                                    throw e;
139                            }
140                    }
141    
142                    return actionMapping.findForward(
143                            getForward(
144                                    renderRequest, "portlet.dynamic_data_mapping.edit_template"));
145            }
146    
147            protected void deleteTemplate(ActionRequest actionRequest)
148                    throws Exception {
149    
150                    long templateId = ParamUtil.getLong(actionRequest, "templateId");
151    
152                    if (templateId > 0) {
153                            DDMTemplateServiceUtil.deleteTemplate(templateId);
154                    }
155            }
156    
157            protected String getSaveAndContinueRedirect(
158                            PortletConfig portletConfig, ActionRequest actionRequest,
159                            DDMTemplate template, String redirect)
160                    throws Exception {
161    
162                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
163                            WebKeys.THEME_DISPLAY);
164    
165                    long structureId = ParamUtil.getLong(actionRequest, "structureId");
166                    String availableFields = ParamUtil.getString(
167                            actionRequest, "availableFields");
168                    String saveCallback = ParamUtil.getString(
169                            actionRequest, "saveCallback");
170    
171                    PortletURLImpl portletURL = new PortletURLImpl(
172                            actionRequest, portletConfig.getPortletName(),
173                            themeDisplay.getPlid(), PortletRequest.RENDER_PHASE);
174    
175                    portletURL.setWindowState(actionRequest.getWindowState());
176    
177                    portletURL.setParameter(Constants.CMD, Constants.UPDATE, false);
178                    portletURL.setParameter(
179                            "struts_action", "/dynamic_data_mapping/edit_template");
180                    portletURL.setParameter("redirect", redirect, false);
181                    portletURL.setParameter(
182                            "templateId", String.valueOf(template.getTemplateId()), false);
183                    portletURL.setParameter(
184                            "groupId", String.valueOf(template.getGroupId()), false);
185                    portletURL.setParameter(
186                            "structureId", String.valueOf(structureId), false);
187                    portletURL.setParameter("type", template.getType(), false);
188                    portletURL.setParameter("availableFields", availableFields, false);
189                    portletURL.setParameter("saveCallback", saveCallback, false);
190    
191                    return portletURL.toString();
192            }
193    
194            protected DDMTemplate updateTemplate(ActionRequest actionRequest)
195                    throws Exception {
196    
197                    UploadPortletRequest uploadPortletRequest =
198                            PortalUtil.getUploadPortletRequest(actionRequest);
199    
200                    long templateId = ParamUtil.getLong(uploadPortletRequest, "templateId");
201    
202                    long groupId = ParamUtil.getLong(uploadPortletRequest, "groupId");
203                    long structureId = ParamUtil.getLong(
204                            uploadPortletRequest, "structureId");
205                    Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap(
206                            actionRequest, "name");
207                    Map<Locale, String> descriptionMap =
208                            LocalizationUtil.getLocalizationMap(actionRequest, "description");
209                    String type = ParamUtil.getString(uploadPortletRequest, "type");
210                    String mode = ParamUtil.getString(uploadPortletRequest, "mode");
211                    String language = ParamUtil.getString(
212                            uploadPortletRequest, "language",
213                            DDMTemplateConstants.LANG_TYPE_VM);
214    
215                    String script = ParamUtil.getString(uploadPortletRequest, "script");
216                    String scriptContent = JS.decodeURIComponent(
217                            ParamUtil.getString(uploadPortletRequest, "scriptContent"));
218    
219                    if (Validator.isNull(script)) {
220                            script = scriptContent;
221                    }
222    
223                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
224                            DDMTemplate.class.getName(), actionRequest);
225    
226                    DDMTemplate template = null;
227    
228                    if (templateId <= 0) {
229                            DDMStructure structure = DDMStructureLocalServiceUtil.getStructure(
230                                    structureId);
231    
232                            template = DDMTemplateServiceUtil.addTemplate(
233                                    groupId, structure.getStructureId(), nameMap, descriptionMap,
234                                    type, mode, language, script, serviceContext);
235                    }
236                    else {
237                            template = DDMTemplateServiceUtil.updateTemplate(
238                                    templateId, nameMap, descriptionMap, type, mode, language,
239                                    script, serviceContext);
240                    }
241    
242                    String portletResource = ParamUtil.getString(
243                            actionRequest, "portletResource");
244    
245                    if (Validator.isNotNull(portletResource)) {
246                            PortletPreferences preferences =
247                                    PortletPreferencesFactoryUtil.getPortletSetup(
248                                            actionRequest, portletResource);
249    
250                            if (type.equals(DDMTemplateConstants.TEMPLATE_TYPE_DETAIL)) {
251                                    preferences.setValue(
252                                            "detailDDMTemplateId",
253                                            String.valueOf(template.getTemplateId()));
254                            }
255                            else {
256                                    preferences.setValue(
257                                            "listDDMTemplateId",
258                                            String.valueOf(template.getTemplateId()));
259                            }
260    
261                            preferences.store();
262                    }
263    
264                    return template;
265            }
266    
267    }