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