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.util.Constants;
33  import com.liferay.portal.kernel.util.ParamUtil;
34  import com.liferay.portal.kernel.util.StringUtil;
35  import com.liferay.portal.model.Organization;
36  import com.liferay.portal.model.OrganizationConstants;
37  import com.liferay.portal.security.auth.PrincipalException;
38  import com.liferay.portal.service.OrganizationServiceUtil;
39  import com.liferay.portal.struts.PortletAction;
40  import com.liferay.util.servlet.SessionErrors;
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 config,
62              ActionRequest req, ActionResponse res)
63          throws Exception {
64  
65          String cmd = ParamUtil.getString(req, Constants.CMD);
66  
67          try {
68              Organization organization = null;
69  
70              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
71                  organization = updateOrganization(req);
72              }
73              else if (cmd.equals(Constants.DELETE)) {
74                  deleteOrganizations(req);
75              }
76  
77              String redirect = ParamUtil.getString(req, "redirect");
78  
79              if (organization != null) {
80                  redirect += organization.getOrganizationId();
81              }
82  
83              sendRedirect(req, res, redirect);
84          }
85          catch (Exception e) {
86              if (e instanceof NoSuchOrganizationException ||
87                  e instanceof PrincipalException) {
88  
89                  SessionErrors.add(req, e.getClass().getName());
90  
91                  setForward(req, "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(req, e.getClass().getName());
101 
102                 if (e instanceof RequiredOrganizationException) {
103                     res.sendRedirect(ParamUtil.getString(req, "redirect"));
104                 }
105             }
106             else {
107                 throw e;
108             }
109         }
110     }
111 
112     public ActionForward render(
113             ActionMapping mapping, ActionForm form, PortletConfig config,
114             RenderRequest req, RenderResponse res)
115         throws Exception {
116 
117         try {
118             ActionUtil.getOrganization(req);
119         }
120         catch (Exception e) {
121             if (e instanceof NoSuchOrganizationException ||
122                 e instanceof PrincipalException) {
123 
124                 SessionErrors.add(req, e.getClass().getName());
125 
126                 return mapping.findForward("portlet.enterprise_admin.error");
127             }
128             else {
129                 throw e;
130             }
131         }
132 
133         return mapping.findForward(
134             getForward(req, "portlet.enterprise_admin.edit_organization"));
135     }
136 
137     protected void deleteOrganizations(ActionRequest req) throws Exception {
138         long[] deleteOrganizationIds = StringUtil.split(
139             ParamUtil.getString(req, "deleteOrganizationIds"), 0L);
140 
141         for (int i = 0; i < deleteOrganizationIds.length; i++) {
142             OrganizationServiceUtil.deleteOrganization(
143                 deleteOrganizationIds[i]);
144         }
145     }
146 
147     protected Organization updateOrganization(ActionRequest req)
148         throws Exception {
149 
150         long organizationId = ParamUtil.getLong(req, "organizationId");
151 
152         long parentOrganizationId = ParamUtil.getLong(
153             req, "parentOrganizationId",
154             OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID);
155         String name = ParamUtil.getString(req, "name");
156         boolean recursable = ParamUtil.getBoolean(req, "recursable");
157         int statusId = ParamUtil.getInteger(req, "statusId");
158         int type = ParamUtil.getInteger(req, "type");
159         long regionId = ParamUtil.getLong(req, "regionId");
160         long countryId = ParamUtil.getLong(req, "countryId");
161         String comments = ParamUtil.getString(req, "comments");
162 
163         Organization organization = null;
164 
165         if (organizationId <= 0) {
166 
167             // Add organization
168 
169             organization = OrganizationServiceUtil.addOrganization(
170                 parentOrganizationId, name, type, recursable, regionId,
171                 countryId, statusId, comments);
172         }
173         else {
174 
175             // Update organization
176 
177             organization = OrganizationServiceUtil.updateOrganization(
178                 organizationId, parentOrganizationId, name, type,
179                 recursable, regionId, countryId, statusId, comments);
180         }
181 
182         return organization;
183     }
184 
185 }