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