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.journal.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.PortletURLImpl;
032    import com.liferay.portlet.journal.DuplicateTemplateIdException;
033    import com.liferay.portlet.journal.NoSuchTemplateException;
034    import com.liferay.portlet.journal.RequiredTemplateException;
035    import com.liferay.portlet.journal.TemplateIdException;
036    import com.liferay.portlet.journal.TemplateNameException;
037    import com.liferay.portlet.journal.TemplateSmallImageNameException;
038    import com.liferay.portlet.journal.TemplateSmallImageSizeException;
039    import com.liferay.portlet.journal.TemplateXslException;
040    import com.liferay.portlet.journal.model.JournalTemplate;
041    import com.liferay.portlet.journal.model.JournalTemplateConstants;
042    import com.liferay.portlet.journal.service.JournalTemplateServiceUtil;
043    import com.liferay.portlet.journal.util.JournalUtil;
044    import com.liferay.util.JS;
045    
046    import java.io.File;
047    
048    import java.util.Locale;
049    import java.util.Map;
050    
051    import javax.portlet.ActionRequest;
052    import javax.portlet.ActionResponse;
053    import javax.portlet.PortletConfig;
054    import javax.portlet.PortletRequest;
055    import javax.portlet.RenderRequest;
056    import javax.portlet.RenderResponse;
057    
058    import org.apache.struts.action.ActionForm;
059    import org.apache.struts.action.ActionForward;
060    import org.apache.struts.action.ActionMapping;
061    
062    /**
063     * @author Brian Wing Shun Chan
064     * @author Raymond Augé
065     */
066    public class EditTemplateAction extends PortletAction {
067    
068            @Override
069            public void processAction(
070                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
071                            ActionRequest actionRequest, ActionResponse actionResponse)
072                    throws Exception {
073    
074                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
075    
076                    JournalTemplate template = null;
077    
078                    try {
079                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
080                                    template = updateTemplate(actionRequest);
081                            }
082                            else if (cmd.equals(Constants.DELETE)) {
083                                    deleteTemplates(actionRequest);
084                            }
085    
086                            String redirect = ParamUtil.getString(actionRequest, "redirect");
087    
088                            if (template != null) {
089                                    boolean saveAndContinue = ParamUtil.getBoolean(
090                                            actionRequest, "saveAndContinue");
091    
092                                    if (saveAndContinue) {
093                                            redirect = getSaveAndContinueRedirect(
094                                                    portletConfig, actionRequest, template, redirect);
095                                    }
096                            }
097    
098                            sendRedirect(actionRequest, actionResponse, redirect);
099                    }
100                    catch (Exception e) {
101                            if (e instanceof NoSuchTemplateException ||
102                                    e instanceof PrincipalException) {
103    
104                                    SessionErrors.add(actionRequest, e.getClass().getName());
105    
106                                    setForward(actionRequest, "portlet.journal.error");
107                            }
108                            else if (e instanceof DuplicateTemplateIdException ||
109                                             e instanceof RequiredTemplateException ||
110                                             e instanceof TemplateIdException ||
111                                             e instanceof TemplateNameException ||
112                                             e instanceof TemplateSmallImageNameException ||
113                                             e instanceof TemplateSmallImageSizeException ||
114                                             e instanceof TemplateXslException) {
115    
116                                    SessionErrors.add(actionRequest, e.getClass().getName());
117    
118                                    if (e instanceof RequiredTemplateException) {
119                                            String redirect = PortalUtil.escapeRedirect(
120                                                    ParamUtil.getString(actionRequest, "redirect"));
121    
122                                            if (Validator.isNotNull(redirect)) {
123                                                    actionResponse.sendRedirect(redirect);
124                                            }
125                                    }
126                            }
127                            else {
128                                    throw e;
129                            }
130                    }
131            }
132    
133            @Override
134            public ActionForward render(
135                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
136                            RenderRequest renderRequest, RenderResponse renderResponse)
137                    throws Exception {
138    
139                    try {
140                            String cmd = ParamUtil.getString(renderRequest, Constants.CMD);
141    
142                            if (!cmd.equals(Constants.ADD)) {
143                                    ActionUtil.getTemplate(renderRequest);
144                            }
145                    }
146                    catch (NoSuchTemplateException nsse) {
147    
148                            // Let this slide because the user can manually input a template id
149                            // for a new template that does not yet exist.
150    
151                    }
152                    catch (Exception e) {
153                            if (//e instanceof NoSuchTemplateException ||
154                                    e instanceof PrincipalException) {
155    
156                                    SessionErrors.add(renderRequest, e.getClass().getName());
157    
158                                    return mapping.findForward("portlet.journal.error");
159                            }
160                            else {
161                                    throw e;
162                            }
163                    }
164    
165                    return mapping.findForward(
166                            getForward(renderRequest, "portlet.journal.edit_template"));
167            }
168    
169            protected void deleteTemplates(ActionRequest actionRequest)
170                    throws Exception {
171    
172                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
173    
174                    String[] deleteTemplateIds = StringUtil.split(
175                            ParamUtil.getString(actionRequest, "deleteTemplateIds"));
176    
177                    for (int i = 0; i < deleteTemplateIds.length; i++) {
178                            JournalTemplateServiceUtil.deleteTemplate(
179                                    groupId, deleteTemplateIds[i]);
180    
181                            JournalUtil.removeRecentTemplate(
182                                    actionRequest, deleteTemplateIds[i]);
183                    }
184            }
185    
186            protected String getSaveAndContinueRedirect(
187                            PortletConfig portletConfig, ActionRequest actionRequest,
188                            JournalTemplate template, String redirect)
189                    throws Exception {
190    
191                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
192                            WebKeys.THEME_DISPLAY);
193    
194                    String originalRedirect = ParamUtil.getString(
195                            actionRequest, "originalRedirect");
196    
197                    PortletURLImpl portletURL = new PortletURLImpl(
198                            actionRequest, portletConfig.getPortletName(),
199                            themeDisplay.getPlid(), PortletRequest.RENDER_PHASE);
200    
201                    portletURL.setWindowState(actionRequest.getWindowState());
202    
203                    portletURL.setParameter("struts_action", "/journal/edit_template");
204                    portletURL.setParameter(Constants.CMD, Constants.UPDATE, false);
205                    portletURL.setParameter("redirect", redirect, false);
206                    portletURL.setParameter("originalRedirect", originalRedirect, false);
207                    portletURL.setParameter(
208                            "groupId", String.valueOf(template.getGroupId()), false);
209                    portletURL.setParameter("templateId", template.getTemplateId(), false);
210    
211                    return portletURL.toString();
212            }
213    
214            protected JournalTemplate updateTemplate(ActionRequest actionRequest)
215                    throws Exception {
216    
217                    UploadPortletRequest uploadPortletRequest =
218                            PortalUtil.getUploadPortletRequest(actionRequest);
219    
220                    String cmd = ParamUtil.getString(uploadPortletRequest, Constants.CMD);
221    
222                    long groupId = ParamUtil.getLong(uploadPortletRequest, "groupId");
223    
224                    String templateId = ParamUtil.getString(
225                            uploadPortletRequest, "templateId");
226                    boolean autoTemplateId = ParamUtil.getBoolean(
227                            uploadPortletRequest, "autoTemplateId");
228    
229                    String structureId = ParamUtil.getString(
230                            uploadPortletRequest, "structureId");
231                    Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap(
232                            actionRequest, "name");
233                    Map<Locale, String> descriptionMap =
234                            LocalizationUtil.getLocalizationMap(actionRequest, "description");
235    
236                    String xsl = ParamUtil.getString(uploadPortletRequest, "xsl");
237                    String xslContent = JS.decodeURIComponent(
238                            ParamUtil.getString(uploadPortletRequest, "xslContent"));
239                    boolean formatXsl = ParamUtil.getBoolean(
240                            uploadPortletRequest, "formatXsl");
241    
242                    if (Validator.isNull(xsl)) {
243                            xsl = xslContent;
244                    }
245    
246                    String langType = ParamUtil.getString(
247                            uploadPortletRequest, "langType",
248                            JournalTemplateConstants.LANG_TYPE_XSL);
249    
250                    boolean cacheable = ParamUtil.getBoolean(
251                            uploadPortletRequest, "cacheable");
252    
253                    boolean smallImage = ParamUtil.getBoolean(
254                            uploadPortletRequest, "smallImage");
255                    String smallImageURL = ParamUtil.getString(
256                            uploadPortletRequest, "smallImageURL");
257                    File smallFile = uploadPortletRequest.getFile("smallFile");
258    
259                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
260                            JournalTemplate.class.getName(), actionRequest);
261    
262                    JournalTemplate template = null;
263    
264                    if (cmd.equals(Constants.ADD)) {
265    
266                            // Add template
267    
268                            template = JournalTemplateServiceUtil.addTemplate(
269                                    groupId, templateId, autoTemplateId, structureId, nameMap,
270                                    descriptionMap, xsl, formatXsl, langType, cacheable, smallImage,
271                                    smallImageURL, smallFile, serviceContext);
272                    }
273                    else {
274    
275                            // Update template
276    
277                            template = JournalTemplateServiceUtil.updateTemplate(
278                                    groupId, templateId, structureId, nameMap, descriptionMap, xsl,
279                                    formatXsl, langType, cacheable, smallImage, smallImageURL,
280                                    smallFile, serviceContext);
281                    }
282    
283                    // Recent templates
284    
285                    JournalUtil.addRecentTemplate(actionRequest, template);
286    
287                    return template;
288            }
289    
290    }