1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class EditTemplateAction extends PortletAction {
76  
77      public void processAction(
78              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
79              ActionRequest actionRequest, ActionResponse actionResponse)
80          throws Exception {
81  
82          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
83  
84          JournalTemplate template = null;
85  
86          try {
87              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
88                  template = updateTemplate(actionRequest);
89              }
90              else if (cmd.equals(Constants.DELETE)) {
91                  deleteTemplates(actionRequest);
92              }
93  
94              String redirect = ParamUtil.getString(actionRequest, "redirect");
95  
96              if (template != null) {
97                  boolean saveAndContinue = ParamUtil.getBoolean(
98                      actionRequest, "saveAndContinue");
99  
100                 if (saveAndContinue) {
101                     redirect = getSaveAndContinueRedirect(
102                         portletConfig, actionRequest, template, redirect);
103                 }
104             }
105 
106             sendRedirect(actionRequest, actionResponse, redirect);
107         }
108         catch (Exception e) {
109             if (e instanceof NoSuchTemplateException ||
110                 e instanceof PrincipalException) {
111 
112                 SessionErrors.add(actionRequest, e.getClass().getName());
113 
114                 setForward(actionRequest, "portlet.journal.error");
115             }
116             else if (e instanceof DuplicateTemplateIdException ||
117                      e instanceof RequiredTemplateException ||
118                      e instanceof TemplateDescriptionException ||
119                      e instanceof TemplateIdException ||
120                      e instanceof TemplateNameException ||
121                      e instanceof TemplateSmallImageNameException ||
122                      e instanceof TemplateSmallImageSizeException ||
123                      e instanceof TemplateXslException) {
124 
125                 SessionErrors.add(actionRequest, e.getClass().getName());
126 
127                 if (e instanceof RequiredTemplateException) {
128                     actionResponse.sendRedirect(
129                         ParamUtil.getString(actionRequest, "redirect"));
130                 }
131             }
132             else {
133                 throw e;
134             }
135         }
136     }
137 
138     public ActionForward render(
139             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
140             RenderRequest renderRequest, RenderResponse renderResponse)
141         throws Exception {
142 
143         try {
144             String cmd = ParamUtil.getString(renderRequest, Constants.CMD);
145 
146             if (!cmd.equals(Constants.ADD)) {
147                 ActionUtil.getTemplate(renderRequest);
148             }
149         }
150         catch (NoSuchTemplateException nsse) {
151 
152             // Let this slide because the user can manually input a template id
153             // for a new template that does not yet exist.
154 
155         }
156         catch (Exception e) {
157             if (//e instanceof NoSuchTemplateException ||
158                 e instanceof PrincipalException) {
159 
160                 SessionErrors.add(renderRequest, e.getClass().getName());
161 
162                 return mapping.findForward("portlet.journal.error");
163             }
164             else {
165                 throw e;
166             }
167         }
168 
169         return mapping.findForward(
170             getForward(renderRequest, "portlet.journal.edit_template"));
171     }
172 
173     protected void deleteTemplates(ActionRequest actionRequest)
174         throws Exception {
175 
176         long groupId = ParamUtil.getLong(actionRequest, "groupId");
177 
178         String[] deleteTemplateIds = StringUtil.split(
179             ParamUtil.getString(actionRequest, "deleteTemplateIds"));
180 
181         for (int i = 0; i < deleteTemplateIds.length; i++) {
182             JournalTemplateServiceUtil.deleteTemplate(
183                 groupId, deleteTemplateIds[i]);
184 
185             JournalUtil.removeRecentTemplate(
186                 actionRequest, deleteTemplateIds[i]);
187         }
188     }
189 
190     protected String getSaveAndContinueRedirect(
191             PortletConfig portletConfig, ActionRequest actionRequest,
192             JournalTemplate template, String redirect)
193         throws Exception {
194 
195         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
196             WebKeys.THEME_DISPLAY);
197 
198         String originalRedirect = ParamUtil.getString(
199             actionRequest, "originalRedirect");
200 
201         PortletURLImpl portletURL = new PortletURLImpl(
202             (ActionRequestImpl)actionRequest, portletConfig.getPortletName(),
203             themeDisplay.getPlid(), PortletRequest.RENDER_PHASE);
204 
205         portletURL.setWindowState(WindowState.MAXIMIZED);
206 
207         portletURL.setParameter("struts_action", "/journal/edit_template");
208         portletURL.setParameter(Constants.CMD, Constants.UPDATE, false);
209         portletURL.setParameter("redirect", redirect, false);
210         portletURL.setParameter("originalRedirect", originalRedirect, false);
211         portletURL.setParameter(
212             "groupId", String.valueOf(template.getGroupId()), false);
213         portletURL.setParameter("templateId", template.getTemplateId(), false);
214 
215         return portletURL.toString();
216     }
217 
218     protected JournalTemplate updateTemplate(ActionRequest actionRequest)
219         throws Exception {
220 
221         UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(
222             actionRequest);
223 
224         String cmd = ParamUtil.getString(uploadRequest, Constants.CMD);
225 
226         long groupId = ParamUtil.getLong(uploadRequest, "groupId");
227 
228         String templateId = ParamUtil.getString(uploadRequest, "templateId");
229         boolean autoTemplateId = ParamUtil.getBoolean(
230             uploadRequest, "autoTemplateId");
231 
232         String structureId = ParamUtil.getString(uploadRequest, "structureId");
233         String name = ParamUtil.getString(uploadRequest, "name");
234         String description = ParamUtil.getString(uploadRequest, "description");
235 
236         String xsl = ParamUtil.getString(uploadRequest, "xsl");
237         String xslContent = JS.decodeURIComponent(
238             ParamUtil.getString(uploadRequest, "xslContent"));
239         boolean formatXsl = ParamUtil.getBoolean(uploadRequest, "formatXsl");
240 
241         if (Validator.isNull(xsl)) {
242             xsl = xslContent;
243         }
244 
245         String langType = ParamUtil.getString(
246             uploadRequest, "langType", JournalTemplateImpl.LANG_TYPE_XSL);
247 
248         boolean cacheable = ParamUtil.getBoolean(uploadRequest, "cacheable");
249 
250         boolean smallImage = ParamUtil.getBoolean(uploadRequest, "smallImage");
251         String smallImageURL = ParamUtil.getString(
252             uploadRequest, "smallImageURL");
253         File smallFile = uploadRequest.getFile("smallFile");
254 
255         ServiceContext serviceContext = ServiceContextFactory.getInstance(
256             JournalTemplate.class.getName(), actionRequest);
257 
258         JournalTemplate template = null;
259 
260         if (cmd.equals(Constants.ADD)) {
261 
262             // Add template
263 
264             template = JournalTemplateServiceUtil.addTemplate(
265                 groupId, templateId, autoTemplateId, structureId, name,
266                 description, xsl, formatXsl, langType, cacheable, smallImage,
267                 smallImageURL, smallFile, serviceContext);
268         }
269         else {
270 
271             // Update template
272 
273             template = JournalTemplateServiceUtil.updateTemplate(
274                 groupId, templateId, structureId, name, description, xsl,
275                 formatXsl, langType, cacheable, smallImage, smallImageURL,
276                 smallFile, serviceContext);
277         }
278 
279         // Recent templates
280 
281         JournalUtil.addRecentTemplate(actionRequest, template);
282 
283         return template;
284     }
285 
286 }