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.enterpriseadmin.action;
24  
25  import com.liferay.portal.DuplicateOrganizationException;
26  import com.liferay.portal.NoSuchCountryException;
27  import com.liferay.portal.NoSuchListTypeException;
28  import com.liferay.portal.NoSuchOrganizationException;
29  import com.liferay.portal.OrganizationNameException;
30  import com.liferay.portal.OrganizationParentException;
31  import com.liferay.portal.RequiredOrganizationException;
32  import com.liferay.portal.kernel.servlet.SessionErrors;
33  import com.liferay.portal.kernel.util.Constants;
34  import com.liferay.portal.kernel.util.ParamUtil;
35  import com.liferay.portal.kernel.util.StringUtil;
36  import com.liferay.portal.model.Organization;
37  import com.liferay.portal.model.OrganizationConstants;
38  import com.liferay.portal.security.auth.PrincipalException;
39  import com.liferay.portal.service.OrganizationServiceUtil;
40  import com.liferay.portal.struts.PortletAction;
41  
42  import javax.portlet.ActionRequest;
43  import javax.portlet.ActionResponse;
44  import javax.portlet.PortletConfig;
45  import javax.portlet.RenderRequest;
46  import javax.portlet.RenderResponse;
47  
48  import org.apache.struts.action.ActionForm;
49  import org.apache.struts.action.ActionForward;
50  import org.apache.struts.action.ActionMapping;
51  
52  /**
53   * <a href="EditOrganizationAction.java.html"><b><i>View Source</i></b></a>
54   *
55   * @author Brian Wing Shun Chan
56   *
57   */
58  public class EditOrganizationAction extends PortletAction {
59  
60      public void processAction(
61              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
62              ActionRequest actionRequest, ActionResponse actionResponse)
63          throws Exception {
64  
65          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
66  
67          try {
68              Organization organization = null;
69  
70              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
71                  organization = updateOrganization(actionRequest);
72              }
73              else if (cmd.equals(Constants.DELETE)) {
74                  deleteOrganizations(actionRequest);
75              }
76  
77              String redirect = ParamUtil.getString(actionRequest, "redirect");
78  
79              if (organization != null) {
80                  redirect += organization.getOrganizationId();
81              }
82  
83              sendRedirect(actionRequest, actionResponse, redirect);
84          }
85          catch (Exception e) {
86              if (e instanceof NoSuchOrganizationException ||
87                  e instanceof PrincipalException) {
88  
89                  SessionErrors.add(actionRequest, e.getClass().getName());
90  
91                  setForward(actionRequest, "portlet.enterprise_admin.error");
92              }
93              else if (e instanceof DuplicateOrganizationException ||
94                       e instanceof NoSuchCountryException ||
95                       e instanceof NoSuchListTypeException ||
96                       e instanceof OrganizationNameException ||
97                       e instanceof OrganizationParentException ||
98                       e instanceof RequiredOrganizationException) {
99  
100                 SessionErrors.add(actionRequest, e.getClass().getName());
101 
102                 if (e instanceof RequiredOrganizationException) {
103                     actionResponse.sendRedirect(
104                         ParamUtil.getString(actionRequest, "redirect"));
105                 }
106             }
107             else {
108                 throw e;
109             }
110         }
111     }
112 
113     public ActionForward render(
114             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
115             RenderRequest renderRequest, RenderResponse renderResponse)
116         throws Exception {
117 
118         try {
119             ActionUtil.getOrganization(renderRequest);
120         }
121         catch (Exception e) {
122             if (e instanceof NoSuchOrganizationException ||
123                 e instanceof PrincipalException) {
124 
125                 SessionErrors.add(renderRequest, e.getClass().getName());
126 
127                 return mapping.findForward("portlet.enterprise_admin.error");
128             }
129             else {
130                 throw e;
131             }
132         }
133 
134         return mapping.findForward(getForward(
135             renderRequest, "portlet.enterprise_admin.edit_organization"));
136     }
137 
138     protected void deleteOrganizations(ActionRequest actionRequest)
139         throws Exception {
140 
141         long[] deleteOrganizationIds = StringUtil.split(
142             ParamUtil.getString(actionRequest, "deleteOrganizationIds"), 0L);
143 
144         for (int i = 0; i < deleteOrganizationIds.length; i++) {
145             OrganizationServiceUtil.deleteOrganization(
146                 deleteOrganizationIds[i]);
147         }
148     }
149 
150     protected Organization updateOrganization(ActionRequest actionRequest)
151         throws Exception {
152 
153         long organizationId = ParamUtil.getLong(
154             actionRequest, "organizationId");
155 
156         long parentOrganizationId = ParamUtil.getLong(
157             actionRequest, "parentOrganizationId",
158             OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID);
159         String name = ParamUtil.getString(actionRequest, "name");
160         boolean recursable = ParamUtil.getBoolean(actionRequest, "recursable");
161         int statusId = ParamUtil.getInteger(actionRequest, "statusId");
162         int type = ParamUtil.getInteger(actionRequest, "type");
163         long regionId = ParamUtil.getLong(actionRequest, "regionId");
164         long countryId = ParamUtil.getLong(actionRequest, "countryId");
165         String comments = ParamUtil.getString(actionRequest, "comments");
166 
167         Organization organization = null;
168 
169         if (organizationId <= 0) {
170 
171             // Add organization
172 
173             organization = OrganizationServiceUtil.addOrganization(
174                 parentOrganizationId, name, type, recursable, regionId,
175                 countryId, statusId, comments);
176         }
177         else {
178 
179             // Update organization
180 
181             organization = OrganizationServiceUtil.updateOrganization(
182                 organizationId, parentOrganizationId, name, type,
183                 recursable, regionId, countryId, statusId, comments);
184         }
185 
186         return organization;
187     }
188 
189 }