1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.journal.action;
24  
25  import com.liferay.portal.kernel.servlet.SessionErrors;
26  import com.liferay.portal.kernel.upload.UploadPortletRequest;
27  import com.liferay.portal.kernel.util.Constants;
28  import com.liferay.portal.kernel.util.ParamUtil;
29  import com.liferay.portal.kernel.util.StringUtil;
30  import com.liferay.portal.kernel.util.Validator;
31  import com.liferay.portal.security.auth.PrincipalException;
32  import com.liferay.portal.struts.PortletAction;
33  import com.liferay.portal.theme.ThemeDisplay;
34  import com.liferay.portal.util.PortalUtil;
35  import com.liferay.portal.util.WebKeys;
36  import com.liferay.portlet.ActionRequestImpl;
37  import com.liferay.portlet.PortletURLImpl;
38  import com.liferay.portlet.journal.DuplicateTemplateIdException;
39  import com.liferay.portlet.journal.NoSuchTemplateException;
40  import com.liferay.portlet.journal.RequiredTemplateException;
41  import com.liferay.portlet.journal.TemplateDescriptionException;
42  import com.liferay.portlet.journal.TemplateIdException;
43  import com.liferay.portlet.journal.TemplateNameException;
44  import com.liferay.portlet.journal.TemplateSmallImageNameException;
45  import com.liferay.portlet.journal.TemplateSmallImageSizeException;
46  import com.liferay.portlet.journal.TemplateXslException;
47  import com.liferay.portlet.journal.model.JournalTemplate;
48  import com.liferay.portlet.journal.model.impl.JournalTemplateImpl;
49  import com.liferay.portlet.journal.service.JournalTemplateServiceUtil;
50  import com.liferay.portlet.journal.util.JournalUtil;
51  import com.liferay.util.JS;
52  
53  import java.io.File;
54  
55  import javax.portlet.ActionRequest;
56  import javax.portlet.ActionResponse;
57  import javax.portlet.PortletConfig;
58  import javax.portlet.PortletRequest;
59  import javax.portlet.RenderRequest;
60  import javax.portlet.RenderResponse;
61  import javax.portlet.WindowState;
62  
63  import org.apache.struts.action.ActionForm;
64  import org.apache.struts.action.ActionForward;
65  import org.apache.struts.action.ActionMapping;
66  
67  /**
68   * <a href="EditTemplateAction.java.html"><b><i>View Source</i></b></a>
69   *
70   * @author Brian Wing Shun Chan
71   *
72   */
73  public class EditTemplateAction extends PortletAction {
74  
75      public void processAction(
76              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
77              ActionRequest actionRequest, ActionResponse actionResponse)
78          throws Exception {
79  
80          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
81  
82          JournalTemplate template = null;
83  
84          try {
85              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
86                  template = updateTemplate(actionRequest);
87              }
88              else if (cmd.equals(Constants.DELETE)) {
89                  deleteTemplates(actionRequest);
90              }
91  
92              String redirect = ParamUtil.getString(actionRequest, "redirect");
93  
94              if (template != null) {
95                  boolean saveAndContinue = ParamUtil.getBoolean(
96                      actionRequest, "saveAndContinue");
97  
98                  if (saveAndContinue) {
99                      redirect = getSaveAndContinueRedirect(
100                         portletConfig, actionRequest, template, redirect);
101                 }
102             }
103 
104             sendRedirect(actionRequest, actionResponse, redirect);
105         }
106         catch (Exception e) {
107             if (e instanceof NoSuchTemplateException ||
108                 e instanceof PrincipalException) {
109 
110                 SessionErrors.add(actionRequest, e.getClass().getName());
111 
112                 setForward(actionRequest, "portlet.journal.error");
113             }
114             else if (e instanceof DuplicateTemplateIdException ||
115                      e instanceof RequiredTemplateException ||
116                      e instanceof TemplateDescriptionException ||
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().getName());
124 
125                 if (e instanceof RequiredTemplateException) {
126                     actionResponse.sendRedirect(
127                         ParamUtil.getString(actionRequest, "redirect"));
128                 }
129             }
130             else {
131                 throw e;
132             }
133         }
134     }
135 
136     public ActionForward render(
137             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
138             RenderRequest renderRequest, RenderResponse renderResponse)
139         throws Exception {
140 
141         try {
142             String cmd = ParamUtil.getString(renderRequest, Constants.CMD);
143 
144             if (!cmd.equals(Constants.ADD)) {
145                 ActionUtil.getTemplate(renderRequest);
146             }
147         }
148         catch (NoSuchTemplateException nsse) {
149 
150             // Let this slide because the user can manually input a template id
151             // for a new template that does not yet exist.
152 
153         }
154         catch (Exception e) {
155             if (//e instanceof NoSuchTemplateException ||
156                 e instanceof PrincipalException) {
157 
158                 SessionErrors.add(renderRequest, e.getClass().getName());
159 
160                 return mapping.findForward("portlet.journal.error");
161             }
162             else {
163                 throw e;
164             }
165         }
166 
167         return mapping.findForward(
168             getForward(renderRequest, "portlet.journal.edit_template"));
169     }
170 
171     protected void deleteTemplates(ActionRequest actionRequest)
172         throws Exception {
173 
174         long groupId = ParamUtil.getLong(actionRequest, "groupId");
175 
176         String[] deleteTemplateIds = StringUtil.split(
177             ParamUtil.getString(actionRequest, "deleteTemplateIds"));
178 
179         for (int i = 0; i < deleteTemplateIds.length; i++) {
180             JournalTemplateServiceUtil.deleteTemplate(
181                 groupId, deleteTemplateIds[i]);
182 
183             JournalUtil.removeRecentTemplate(
184                 actionRequest, deleteTemplateIds[i]);
185         }
186     }
187 
188     protected String getSaveAndContinueRedirect(
189             PortletConfig portletConfig, ActionRequest actionRequest,
190             JournalTemplate template, String redirect)
191         throws Exception {
192 
193         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
194             WebKeys.THEME_DISPLAY);
195 
196         String originalRedirect = ParamUtil.getString(
197             actionRequest, "originalRedirect");
198 
199         PortletURLImpl portletURL = new PortletURLImpl(
200             (ActionRequestImpl)actionRequest, portletConfig.getPortletName(),
201             themeDisplay.getPlid(), PortletRequest.RENDER_PHASE);
202 
203         portletURL.setWindowState(WindowState.MAXIMIZED);
204 
205         portletURL.setParameter("struts_action", "/journal/edit_template");
206         portletURL.setParameter(Constants.CMD, Constants.UPDATE, false);
207         portletURL.setParameter("redirect", redirect, false);
208         portletURL.setParameter("originalRedirect", originalRedirect, false);
209         portletURL.setParameter(
210             "groupId", String.valueOf(template.getGroupId()), false);
211         portletURL.setParameter("templateId", template.getTemplateId(), false);
212 
213         return portletURL.toString();
214     }
215 
216     protected JournalTemplate updateTemplate(ActionRequest actionRequest)
217         throws Exception {
218 
219         UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(
220             actionRequest);
221 
222         String cmd = ParamUtil.getString(uploadRequest, Constants.CMD);
223 
224         long groupId = ParamUtil.getLong(uploadRequest, "groupId");
225 
226         String templateId = ParamUtil.getString(uploadRequest, "templateId");
227         boolean autoTemplateId = ParamUtil.getBoolean(
228             uploadRequest, "autoTemplateId");
229 
230         String structureId = ParamUtil.getString(uploadRequest, "structureId");
231         String name = ParamUtil.getString(uploadRequest, "name");
232         String description = ParamUtil.getString(uploadRequest, "description");
233 
234         String xsl = ParamUtil.getString(uploadRequest, "xsl");
235         String xslContent = JS.decodeURIComponent(
236             ParamUtil.getString(uploadRequest, "xslContent"));
237         boolean formatXsl = ParamUtil.getBoolean(uploadRequest, "formatXsl");
238 
239         if (Validator.isNull(xsl)) {
240             xsl = xslContent;
241         }
242 
243         String langType = ParamUtil.getString(
244             uploadRequest, "langType", JournalTemplateImpl.LANG_TYPE_XSL);
245 
246         boolean cacheable = ParamUtil.getBoolean(uploadRequest, "cacheable");
247 
248         boolean smallImage = ParamUtil.getBoolean(uploadRequest, "smallImage");
249         String smallImageURL = ParamUtil.getString(
250             uploadRequest, "smallImageURL");
251         File smallFile = uploadRequest.getFile("smallFile");
252 
253         String[] communityPermissions = uploadRequest.getParameterValues(
254             "communityPermissions");
255         String[] guestPermissions = uploadRequest.getParameterValues(
256             "guestPermissions");
257 
258         JournalTemplate template = null;
259 
260         if (cmd.equals(Constants.ADD)) {
261 
262             // Add template
263 
264             template = JournalTemplateServiceUtil.addTemplate(
265                 groupId, templateId, autoTemplateId, structureId, name,
266                 description, xsl, formatXsl, langType, cacheable, smallImage,
267                 smallImageURL, smallFile, communityPermissions,
268                 guestPermissions);
269         }
270         else {
271 
272             // Update template
273 
274             template = JournalTemplateServiceUtil.updateTemplate(
275                 groupId, templateId, structureId, name, description, xsl,
276                 formatXsl, langType, cacheable, smallImage, smallImageURL,
277                 smallFile);
278         }
279 
280         // Recent templates
281 
282         JournalUtil.addRecentTemplate(actionRequest, template);
283 
284         return template;
285     }
286 
287 }