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