1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.layoutprototypes.action;
16  
17  import com.liferay.portal.NoSuchLayoutPrototypeException;
18  import com.liferay.portal.kernel.servlet.SessionErrors;
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.security.auth.PrincipalException;
23  import com.liferay.portal.service.LayoutPrototypeServiceUtil;
24  import com.liferay.portal.struts.PortletAction;
25  import com.liferay.util.LocalizationUtil;
26  
27  import java.util.Locale;
28  import java.util.Map;
29  
30  import javax.portlet.ActionRequest;
31  import javax.portlet.ActionResponse;
32  import javax.portlet.PortletConfig;
33  import javax.portlet.RenderRequest;
34  import javax.portlet.RenderResponse;
35  
36  import org.apache.struts.action.ActionForm;
37  import org.apache.struts.action.ActionForward;
38  import org.apache.struts.action.ActionMapping;
39  
40  /**
41   * <a href="EditLayoutPrototypeAction.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Jorge Ferrer
44   */
45  public class EditLayoutPrototypeAction extends PortletAction {
46  
47      public void processAction(
48              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
49              ActionRequest actionRequest, ActionResponse actionResponse)
50          throws Exception {
51  
52          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
53  
54          try {
55              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
56                  updateLayoutPrototype(actionRequest);
57              }
58              else if (cmd.equals(Constants.DELETE)) {
59                  deleteLayoutPrototypes(actionRequest);
60              }
61  
62              sendRedirect(actionRequest, actionResponse);
63          }
64          catch (Exception e) {
65              if (e instanceof PrincipalException) {
66                  SessionErrors.add(actionRequest, e.getClass().getName());
67  
68                  setForward(actionRequest, "portlet.layout_prototypes.error");
69              }
70              else {
71                  throw e;
72              }
73          }
74      }
75  
76      public ActionForward render(
77              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
78              RenderRequest renderRequest, RenderResponse renderResponse)
79          throws Exception {
80  
81          try {
82              ActionUtil.getLayoutPrototype(renderRequest);
83          }
84          catch (Exception e) {
85              if (e instanceof NoSuchLayoutPrototypeException ||
86                  e instanceof PrincipalException) {
87  
88                  SessionErrors.add(renderRequest, e.getClass().getName());
89  
90                  return mapping.findForward("portlet.layout_prototypes.error");
91              }
92              else {
93                  throw e;
94              }
95          }
96  
97          return mapping.findForward(getForward(
98              renderRequest, "portlet.layout_prototypes.edit_layout_prototype"));
99      }
100 
101     protected void deleteLayoutPrototypes(ActionRequest actionRequest)
102         throws Exception {
103 
104         long[] layoutPrototypeIds = StringUtil.split(
105             ParamUtil.getString(actionRequest, "layoutPrototypeIds"), 0L);
106 
107         for (long layoutPrototypeId : layoutPrototypeIds) {
108             LayoutPrototypeServiceUtil.deleteLayoutPrototype(layoutPrototypeId);
109         }
110     }
111 
112     protected void updateLayoutPrototype(ActionRequest actionRequest)
113         throws Exception {
114 
115         long layoutPrototypeId = ParamUtil.getLong(
116             actionRequest, "layoutPrototypeId");
117 
118         Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap(
119             actionRequest, "name");
120         String description = ParamUtil.getString(actionRequest, "description");
121         boolean active = ParamUtil.getBoolean(actionRequest, "active");
122 
123         if (layoutPrototypeId <= 0) {
124 
125             // Add layout prototoype
126 
127             LayoutPrototypeServiceUtil.addLayoutPrototype(
128                 nameMap, description, active);
129         }
130         else {
131 
132             // Update layout prototoype
133 
134             LayoutPrototypeServiceUtil.updateLayoutPrototype(
135                 layoutPrototypeId, nameMap, description, active);
136         }
137     }
138 
139 }