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