1   /**
2    * Copyright (c) 2000-2008 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.model.Layout;
32  import com.liferay.portal.security.auth.PrincipalException;
33  import com.liferay.portal.struts.PortletAction;
34  import com.liferay.portal.theme.ThemeDisplay;
35  import com.liferay.portal.util.PortalUtil;
36  import com.liferay.portal.util.WebKeys;
37  import com.liferay.portlet.ActionRequestImpl;
38  import com.liferay.portlet.PortletURLImpl;
39  import com.liferay.portlet.journal.DuplicateTemplateIdException;
40  import com.liferay.portlet.journal.NoSuchTemplateException;
41  import com.liferay.portlet.journal.RequiredTemplateException;
42  import com.liferay.portlet.journal.TemplateDescriptionException;
43  import com.liferay.portlet.journal.TemplateIdException;
44  import com.liferay.portlet.journal.TemplateNameException;
45  import com.liferay.portlet.journal.TemplateSmallImageNameException;
46  import com.liferay.portlet.journal.TemplateSmallImageSizeException;
47  import com.liferay.portlet.journal.TemplateXslException;
48  import com.liferay.portlet.journal.model.JournalTemplate;
49  import com.liferay.portlet.journal.model.impl.JournalTemplateImpl;
50  import com.liferay.portlet.journal.service.JournalTemplateServiceUtil;
51  import com.liferay.portlet.journal.util.JournalUtil;
52  import com.liferay.util.JS;
53  
54  import java.io.File;
55  
56  import javax.portlet.ActionRequest;
57  import javax.portlet.ActionResponse;
58  import javax.portlet.PortletConfig;
59  import javax.portlet.PortletRequest;
60  import javax.portlet.RenderRequest;
61  import javax.portlet.RenderResponse;
62  import javax.portlet.WindowState;
63  
64  import org.apache.struts.action.ActionForm;
65  import org.apache.struts.action.ActionForward;
66  import org.apache.struts.action.ActionMapping;
67  
68  /**
69   * <a href="EditTemplateAction.java.html"><b><i>View Source</i></b></a>
70   *
71   * @author Brian Wing Shun Chan
72   *
73   */
74  public class EditTemplateAction extends PortletAction {
75  
76      public void processAction(
77              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
78              ActionRequest actionRequest, ActionResponse actionResponse)
79          throws Exception {
80  
81          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
82  
83          JournalTemplate template = null;
84  
85          try {
86              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
87                  template = updateTemplate(actionRequest);
88              }
89              else if (cmd.equals(Constants.DELETE)) {
90                  deleteTemplates(actionRequest);
91              }
92  
93              String redirect = ParamUtil.getString(actionRequest, "redirect");
94  
95              if (template != null) {
96                  boolean saveAndContinue = ParamUtil.getBoolean(
97                      actionRequest, "saveAndContinue");
98  
99                  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().getName());
112 
113                 setForward(actionRequest, "portlet.journal.error");
114             }
115             else if (e instanceof DuplicateTemplateIdException ||
116                      e instanceof RequiredTemplateException ||
117                      e instanceof TemplateDescriptionException ||
118                      e instanceof TemplateIdException ||
119                      e instanceof TemplateNameException ||
120                      e instanceof TemplateSmallImageNameException ||
121                      e instanceof TemplateSmallImageSizeException ||
122                      e instanceof TemplateXslException) {
123 
124                 SessionErrors.add(actionRequest, e.getClass().getName());
125 
126                 if (e instanceof RequiredTemplateException) {
127                     actionResponse.sendRedirect(
128                         ParamUtil.getString(actionRequest, "redirect"));
129                 }
130             }
131             else {
132                 throw e;
133             }
134         }
135     }
136 
137     public ActionForward render(
138             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
139             RenderRequest renderRequest, RenderResponse renderResponse)
140         throws Exception {
141 
142         try {
143             String cmd = ParamUtil.getString(renderRequest, Constants.CMD);
144 
145             if (!cmd.equals(Constants.ADD)) {
146                 ActionUtil.getTemplate(renderRequest);
147             }
148         }
149         catch (NoSuchTemplateException nsse) {
150 
151             // Let this slide because the user can manually input a template id
152             // for a new template that does not yet exist.
153 
154         }
155         catch (Exception e) {
156             if (//e instanceof NoSuchTemplateException ||
157                 e instanceof PrincipalException) {
158 
159                 SessionErrors.add(renderRequest, e.getClass().getName());
160 
161                 return mapping.findForward("portlet.journal.error");
162             }
163             else {
164                 throw e;
165             }
166         }
167 
168         return mapping.findForward(
169             getForward(renderRequest, "portlet.journal.edit_template"));
170     }
171 
172     protected void deleteTemplates(ActionRequest actionRequest)
173         throws Exception {
174 
175         long groupId = ParamUtil.getLong(actionRequest, "groupId");
176 
177         String[] deleteTemplateIds = StringUtil.split(
178             ParamUtil.getString(actionRequest, "deleteTemplateIds"));
179 
180         for (int i = 0; i < deleteTemplateIds.length; i++) {
181             JournalTemplateServiceUtil.deleteTemplate(
182                 groupId, deleteTemplateIds[i]);
183 
184             JournalUtil.removeRecentTemplate(
185                 actionRequest, deleteTemplateIds[i]);
186         }
187     }
188 
189     protected String getSaveAndContinueRedirect(
190             PortletConfig portletConfig, ActionRequest actionRequest,
191             JournalTemplate template, String redirect)
192         throws Exception {
193 
194         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
195             WebKeys.THEME_DISPLAY);
196 
197         String originalRedirect = ParamUtil.getString(
198             actionRequest, "originalRedirect");
199 
200         PortletURLImpl portletURL = new PortletURLImpl(
201             (ActionRequestImpl)actionRequest, portletConfig.getPortletName(),
202             themeDisplay.getPlid(), PortletRequest.RENDER_PHASE);
203 
204         portletURL.setWindowState(WindowState.MAXIMIZED);
205 
206         portletURL.setParameter("struts_action", "/journal/edit_template");
207         portletURL.setParameter(Constants.CMD, Constants.UPDATE, false);
208         portletURL.setParameter("redirect", redirect, false);
209         portletURL.setParameter("originalRedirect", originalRedirect, false);
210         portletURL.setParameter(
211             "groupId", String.valueOf(template.getGroupId()), false);
212         portletURL.setParameter("templateId", template.getTemplateId(), false);
213 
214         return portletURL.toString();
215     }
216 
217     protected JournalTemplate updateTemplate(ActionRequest actionRequest)
218         throws Exception {
219 
220         UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(
221             actionRequest);
222 
223         String cmd = ParamUtil.getString(uploadRequest, Constants.CMD);
224 
225         Layout layout = (Layout)uploadRequest.getAttribute(WebKeys.LAYOUT);
226 
227         long groupId = ParamUtil.getLong(uploadRequest, "groupId");
228 
229         String templateId = ParamUtil.getString(uploadRequest, "templateId");
230         boolean autoTemplateId = ParamUtil.getBoolean(
231             uploadRequest, "autoTemplateId");
232 
233         String structureId = ParamUtil.getString(uploadRequest, "structureId");
234         String name = ParamUtil.getString(uploadRequest, "name");
235         String description = ParamUtil.getString(uploadRequest, "description");
236 
237         String xsl = ParamUtil.getString(uploadRequest, "xsl");
238         String xslContent = JS.decodeURIComponent(
239             ParamUtil.getString(uploadRequest, "xslContent"));
240         boolean formatXsl = ParamUtil.getBoolean(uploadRequest, "formatXsl");
241 
242         if (Validator.isNull(xsl)) {
243             xsl = xslContent;
244         }
245 
246         String langType = ParamUtil.getString(
247             uploadRequest, "langType", JournalTemplateImpl.LANG_TYPE_XSL);
248 
249         boolean cacheable = ParamUtil.getBoolean(uploadRequest, "cacheable");
250 
251         boolean smallImage = ParamUtil.getBoolean(uploadRequest, "smallImage");
252         String smallImageURL = ParamUtil.getString(
253             uploadRequest, "smallImageURL");
254         File smallFile = uploadRequest.getFile("smallFile");
255 
256         String[] communityPermissions = uploadRequest.getParameterValues(
257             "communityPermissions");
258         String[] guestPermissions = uploadRequest.getParameterValues(
259             "guestPermissions");
260 
261         JournalTemplate template = null;
262 
263         if (cmd.equals(Constants.ADD)) {
264 
265             // Add template
266 
267             template = JournalTemplateServiceUtil.addTemplate(
268                 templateId, autoTemplateId, layout.getPlid(), structureId, name,
269                 description, xsl, formatXsl, langType, cacheable, smallImage,
270                 smallImageURL, smallFile, communityPermissions,
271                 guestPermissions);
272         }
273         else {
274 
275             // Update template
276 
277             template = JournalTemplateServiceUtil.updateTemplate(
278                 groupId, templateId, structureId, name, description, xsl,
279                 formatXsl, langType, cacheable, smallImage, smallImageURL,
280                 smallFile);
281         }
282 
283         // Recent templates
284 
285         JournalUtil.addRecentTemplate(actionRequest, template);
286 
287         return template;
288     }
289 
290 }