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