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