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.service.ServiceContext;
33  import com.liferay.portal.service.ServiceContextFactory;
34  import com.liferay.portal.struts.PortletAction;
35  import com.liferay.portal.theme.ThemeDisplay;
36  import com.liferay.portal.util.PortalUtil;
37  import com.liferay.portal.util.WebKeys;
38  import com.liferay.portlet.ActionRequestImpl;
39  import com.liferay.portlet.PortletURLImpl;
40  import com.liferay.portlet.journal.DuplicateTemplateIdException;
41  import com.liferay.portlet.journal.NoSuchTemplateException;
42  import com.liferay.portlet.journal.RequiredTemplateException;
43  import com.liferay.portlet.journal.TemplateDescriptionException;
44  import com.liferay.portlet.journal.TemplateIdException;
45  import com.liferay.portlet.journal.TemplateNameException;
46  import com.liferay.portlet.journal.TemplateSmallImageNameException;
47  import com.liferay.portlet.journal.TemplateSmallImageSizeException;
48  import com.liferay.portlet.journal.TemplateXslException;
49  import com.liferay.portlet.journal.model.JournalTemplate;
50  import com.liferay.portlet.journal.model.impl.JournalTemplateImpl;
51  import com.liferay.portlet.journal.service.JournalTemplateServiceUtil;
52  import com.liferay.portlet.journal.util.JournalUtil;
53  import com.liferay.util.JS;
54  
55  import java.io.File;
56  
57  import javax.portlet.ActionRequest;
58  import javax.portlet.ActionResponse;
59  import javax.portlet.PortletConfig;
60  import javax.portlet.PortletRequest;
61  import javax.portlet.RenderRequest;
62  import javax.portlet.RenderResponse;
63  import javax.portlet.WindowState;
64  
65  import org.apache.struts.action.ActionForm;
66  import org.apache.struts.action.ActionForward;
67  import org.apache.struts.action.ActionMapping;
68  
69  /**
70   * <a href="EditTemplateAction.java.html"><b><i>View Source</i></b></a>
71   *
72   * @author Brian Wing Shun Chan
73   * @author Raymond Augé
74   *
75   */
76  public class EditTemplateAction extends PortletAction {
77  
78      public void processAction(
79              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
80              ActionRequest actionRequest, ActionResponse actionResponse)
81          throws Exception {
82  
83          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
84  
85          JournalTemplate template = null;
86  
87          try {
88              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
89                  template = updateTemplate(actionRequest);
90              }
91              else if (cmd.equals(Constants.DELETE)) {
92                  deleteTemplates(actionRequest);
93              }
94  
95              String redirect = ParamUtil.getString(actionRequest, "redirect");
96  
97              if (template != null) {
98                  boolean saveAndContinue = ParamUtil.getBoolean(
99                      actionRequest, "saveAndContinue");
100 
101                 if (saveAndContinue) {
102                     redirect = getSaveAndContinueRedirect(
103                         portletConfig, actionRequest, template, redirect);
104                 }
105             }
106 
107             sendRedirect(actionRequest, actionResponse, redirect);
108         }
109         catch (Exception e) {
110             if (e instanceof NoSuchTemplateException ||
111                 e instanceof PrincipalException) {
112 
113                 SessionErrors.add(actionRequest, e.getClass().getName());
114 
115                 setForward(actionRequest, "portlet.journal.error");
116             }
117             else if (e instanceof DuplicateTemplateIdException ||
118                      e instanceof RequiredTemplateException ||
119                      e instanceof TemplateDescriptionException ||
120                      e instanceof TemplateIdException ||
121                      e instanceof TemplateNameException ||
122                      e instanceof TemplateSmallImageNameException ||
123                      e instanceof TemplateSmallImageSizeException ||
124                      e instanceof TemplateXslException) {
125 
126                 SessionErrors.add(actionRequest, e.getClass().getName());
127 
128                 if (e instanceof RequiredTemplateException) {
129                     actionResponse.sendRedirect(
130                         ParamUtil.getString(actionRequest, "redirect"));
131                 }
132             }
133             else {
134                 throw e;
135             }
136         }
137     }
138 
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             (ActionRequestImpl)actionRequest, portletConfig.getPortletName(),
204             themeDisplay.getPlid(), PortletRequest.RENDER_PHASE);
205 
206         portletURL.setWindowState(WindowState.MAXIMIZED);
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 JournalTemplate updateTemplate(ActionRequest actionRequest)
220         throws Exception {
221 
222         UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(
223             actionRequest);
224 
225         String cmd = ParamUtil.getString(uploadRequest, Constants.CMD);
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         ServiceContext serviceContext = ServiceContextFactory.getInstance(
257             JournalTemplate.class.getName(), actionRequest);
258 
259         JournalTemplate template = null;
260 
261         if (cmd.equals(Constants.ADD)) {
262 
263             // Add template
264 
265             template = JournalTemplateServiceUtil.addTemplate(
266                 groupId, templateId, autoTemplateId, structureId, name,
267                 description, xsl, formatXsl, langType, cacheable, smallImage,
268                 smallImageURL, smallFile, serviceContext);
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, serviceContext);
278         }
279 
280         // Recent templates
281 
282         JournalUtil.addRecentTemplate(actionRequest, template);
283 
284         return template;
285     }
286 
287 }