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