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.layoutsetprototypes.action;
16  
17  import com.liferay.portal.NoSuchLayoutSetPrototypeException;
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.LayoutSetPrototypeServiceUtil;
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="EditLayoutSetPrototypeAction.java.html"><b><i>View Source</i></b>
42   * </a>
43   *
44   * @author Brian Wing Shun Chan
45   */
46  public class EditLayoutSetPrototypeAction extends PortletAction {
47  
48      public void processAction(
49              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
50              ActionRequest actionRequest, ActionResponse actionResponse)
51          throws Exception {
52  
53          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
54  
55          try {
56              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
57                  updateLayoutSetPrototype(actionRequest);
58              }
59              else if (cmd.equals(Constants.DELETE)) {
60                  deleteLayoutSetPrototypes(actionRequest);
61              }
62  
63              sendRedirect(actionRequest, actionResponse);
64          }
65          catch (Exception e) {
66              if (e instanceof PrincipalException) {
67                  SessionErrors.add(actionRequest, e.getClass().getName());
68  
69                  setForward(
70                      actionRequest, "portlet.layout_set_prototypes.error");
71              }
72              else {
73                  throw e;
74              }
75          }
76      }
77  
78      public ActionForward render(
79              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
80              RenderRequest renderRequest, RenderResponse renderResponse)
81          throws Exception {
82  
83          try {
84              ActionUtil.getLayoutSetPrototype(renderRequest);
85          }
86          catch (Exception e) {
87              if (e instanceof NoSuchLayoutSetPrototypeException ||
88                  e instanceof PrincipalException) {
89  
90                  SessionErrors.add(renderRequest, e.getClass().getName());
91  
92                  return mapping.findForward(
93                      "portlet.layout_set_prototypes.error");
94              }
95              else {
96                  throw e;
97              }
98          }
99  
100         return mapping.findForward(getForward(
101             renderRequest,
102             "portlet.layout_set_prototypes.edit_layout_set_prototype"));
103     }
104 
105     protected void deleteLayoutSetPrototypes(ActionRequest actionRequest)
106         throws Exception {
107 
108         long[] layoutSetPrototypeIds = StringUtil.split(
109             ParamUtil.getString(actionRequest, "layoutSetPrototypeIds"), 0L);
110 
111         for (long layoutSetPrototypeId : layoutSetPrototypeIds) {
112             LayoutSetPrototypeServiceUtil.deleteLayoutSetPrototype(
113                 layoutSetPrototypeId);
114         }
115     }
116 
117     protected void updateLayoutSetPrototype(ActionRequest actionRequest)
118         throws Exception {
119 
120         long layoutSetPrototypeId = ParamUtil.getLong(
121             actionRequest, "layoutSetPrototypeId");
122 
123         Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap(
124             actionRequest, "name");
125         String description = ParamUtil.getString(actionRequest, "description");
126         boolean active = ParamUtil.getBoolean(actionRequest, "active");
127 
128         if (layoutSetPrototypeId <= 0) {
129 
130             // Add layout prototoype
131 
132             LayoutSetPrototypeServiceUtil.addLayoutSetPrototype(
133                 nameMap, description, active);
134         }
135         else {
136 
137             // Update layout prototoype
138 
139             LayoutSetPrototypeServiceUtil.updateLayoutSetPrototype(
140                 layoutSetPrototypeId, nameMap, description, active);
141         }
142     }
143 
144 }