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.util.Constants;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  import com.liferay.portal.kernel.util.StringUtil;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.model.Layout;
30  import com.liferay.portal.security.auth.PrincipalException;
31  import com.liferay.portal.struts.PortletAction;
32  import com.liferay.portal.util.UploadRequestUtil;
33  import com.liferay.portal.util.WebKeys;
34  import com.liferay.portlet.journal.DuplicateTemplateIdException;
35  import com.liferay.portlet.journal.NoSuchTemplateException;
36  import com.liferay.portlet.journal.RequiredTemplateException;
37  import com.liferay.portlet.journal.TemplateDescriptionException;
38  import com.liferay.portlet.journal.TemplateIdException;
39  import com.liferay.portlet.journal.TemplateNameException;
40  import com.liferay.portlet.journal.TemplateSmallImageNameException;
41  import com.liferay.portlet.journal.TemplateSmallImageSizeException;
42  import com.liferay.portlet.journal.TemplateXslException;
43  import com.liferay.portlet.journal.model.JournalTemplate;
44  import com.liferay.portlet.journal.model.impl.JournalTemplateImpl;
45  import com.liferay.portlet.journal.service.JournalTemplateServiceUtil;
46  import com.liferay.portlet.journal.util.JournalUtil;
47  import com.liferay.util.JS;
48  import com.liferay.util.servlet.SessionErrors;
49  import com.liferay.util.servlet.UploadPortletRequest;
50  
51  import java.io.File;
52  
53  import javax.portlet.ActionRequest;
54  import javax.portlet.ActionResponse;
55  import javax.portlet.PortletConfig;
56  import javax.portlet.RenderRequest;
57  import javax.portlet.RenderResponse;
58  
59  import org.apache.struts.action.ActionForm;
60  import org.apache.struts.action.ActionForward;
61  import org.apache.struts.action.ActionMapping;
62  
63  /**
64   * <a href="EditTemplateAction.java.html"><b><i>View Source</i></b></a>
65   *
66   * @author Brian Wing Shun Chan
67   *
68   */
69  public class EditTemplateAction extends PortletAction {
70  
71      public void processAction(
72              ActionMapping mapping, ActionForm form, PortletConfig config,
73              ActionRequest req, ActionResponse res)
74          throws Exception {
75  
76          String cmd = ParamUtil.getString(req, Constants.CMD);
77  
78          try {
79              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
80                  updateTemplate(req);
81              }
82              else if (cmd.equals(Constants.DELETE)) {
83                  deleteTemplates(req);
84              }
85  
86              sendRedirect(req, res);
87          }
88          catch (Exception e) {
89              if (e instanceof NoSuchTemplateException ||
90                  e instanceof PrincipalException) {
91  
92                  SessionErrors.add(req, e.getClass().getName());
93  
94                  setForward(req, "portlet.journal.error");
95              }
96              else if (e instanceof DuplicateTemplateIdException ||
97                       e instanceof RequiredTemplateException ||
98                       e instanceof TemplateDescriptionException ||
99                       e instanceof TemplateIdException ||
100                      e instanceof TemplateNameException ||
101                      e instanceof TemplateSmallImageNameException ||
102                      e instanceof TemplateSmallImageSizeException ||
103                      e instanceof TemplateXslException) {
104 
105                 SessionErrors.add(req, e.getClass().getName());
106 
107                 if (e instanceof RequiredTemplateException) {
108                     res.sendRedirect(ParamUtil.getString(req, "redirect"));
109                 }
110             }
111             else {
112                 throw e;
113             }
114         }
115     }
116 
117     public ActionForward render(
118             ActionMapping mapping, ActionForm form, PortletConfig config,
119             RenderRequest req, RenderResponse res)
120         throws Exception {
121 
122         try {
123             String cmd = ParamUtil.getString(req, Constants.CMD);
124 
125             if (!cmd.equals(Constants.ADD)) {
126                 ActionUtil.getTemplate(req);
127             }
128         }
129         catch (NoSuchTemplateException nsse) {
130 
131             // Let this slide because the user can manually input a template id
132             // for a new template that does not yet exist.
133 
134         }
135         catch (Exception e) {
136             if (//e instanceof NoSuchTemplateException ||
137                 e instanceof PrincipalException) {
138 
139                 SessionErrors.add(req, e.getClass().getName());
140 
141                 return mapping.findForward("portlet.journal.error");
142             }
143             else {
144                 throw e;
145             }
146         }
147 
148         return mapping.findForward(
149             getForward(req, "portlet.journal.edit_template"));
150     }
151 
152     protected void deleteTemplates(ActionRequest req) throws Exception {
153         long groupId = ParamUtil.getLong(req, "groupId");
154 
155         String[] deleteTemplateIds = StringUtil.split(
156             ParamUtil.getString(req, "deleteTemplateIds"));
157 
158         for (int i = 0; i < deleteTemplateIds.length; i++) {
159             JournalTemplateServiceUtil.deleteTemplate(
160                 groupId, deleteTemplateIds[i]);
161 
162             JournalUtil.removeRecentTemplate(req, deleteTemplateIds[i]);
163         }
164     }
165 
166     protected void updateTemplate(ActionRequest req) throws Exception {
167         UploadPortletRequest uploadReq =
168             UploadRequestUtil.getUploadPortletRequest(req);
169 
170         String cmd = ParamUtil.getString(uploadReq, Constants.CMD);
171 
172         Layout layout = (Layout)uploadReq.getAttribute(WebKeys.LAYOUT);
173 
174         long groupId = ParamUtil.getLong(uploadReq, "groupId");
175 
176         String templateId = ParamUtil.getString(uploadReq, "templateId");
177         boolean autoTemplateId = ParamUtil.getBoolean(
178             uploadReq, "autoTemplateId");
179 
180         String structureId = ParamUtil.getString(uploadReq, "structureId");
181         String name = ParamUtil.getString(uploadReq, "name");
182         String description = ParamUtil.getString(uploadReq, "description");
183 
184         String xsl = ParamUtil.getString(uploadReq, "xsl");
185         String xslContent = JS.decodeURIComponent(
186             ParamUtil.getString(uploadReq, "xslContent"));
187         boolean formatXsl = ParamUtil.getBoolean(uploadReq, "formatXsl");
188 
189         if (Validator.isNull(xsl)) {
190             xsl = xslContent;
191         }
192 
193         String langType = ParamUtil.getString(
194             uploadReq, "langType", JournalTemplateImpl.LANG_TYPE_XSL);
195 
196         boolean cacheable = ParamUtil.getBoolean(uploadReq, "cacheable");
197 
198         boolean smallImage = ParamUtil.getBoolean(uploadReq, "smallImage");
199         String smallImageURL = ParamUtil.getString(uploadReq, "smallImageURL");
200         File smallFile = uploadReq.getFile("smallFile");
201 
202         String[] communityPermissions = uploadReq.getParameterValues(
203             "communityPermissions");
204         String[] guestPermissions = uploadReq.getParameterValues(
205             "guestPermissions");
206 
207         JournalTemplate template = null;
208 
209         if (cmd.equals(Constants.ADD)) {
210 
211             // Add template
212 
213             template = JournalTemplateServiceUtil.addTemplate(
214                 templateId, autoTemplateId, layout.getPlid(), structureId, name,
215                 description, xsl, formatXsl, langType, cacheable, smallImage,
216                 smallImageURL, smallFile, communityPermissions,
217                 guestPermissions);
218         }
219         else {
220 
221             // Update template
222 
223             template = JournalTemplateServiceUtil.updateTemplate(
224                 groupId, templateId, structureId, name, description, xsl,
225                 formatXsl, langType, cacheable, smallImage, smallImageURL,
226                 smallFile);
227         }
228 
229         // Recent templates
230 
231         JournalUtil.addRecentTemplate(req, template);
232     }
233 
234 }