1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
13   */
14  
15  package com.liferay.portlet.enterpriseadmin.action;
16  
17  import com.liferay.portal.DuplicateOrganizationException;
18  import com.liferay.portal.NoSuchCountryException;
19  import com.liferay.portal.NoSuchListTypeException;
20  import com.liferay.portal.NoSuchOrganizationException;
21  import com.liferay.portal.OrganizationNameException;
22  import com.liferay.portal.OrganizationParentException;
23  import com.liferay.portal.RequiredOrganizationException;
24  import com.liferay.portal.kernel.servlet.SessionErrors;
25  import com.liferay.portal.kernel.util.Constants;
26  import com.liferay.portal.kernel.util.ParamUtil;
27  import com.liferay.portal.kernel.util.StringUtil;
28  import com.liferay.portal.model.Organization;
29  import com.liferay.portal.model.OrganizationConstants;
30  import com.liferay.portal.security.auth.PrincipalException;
31  import com.liferay.portal.service.OrganizationServiceUtil;
32  import com.liferay.portal.struts.PortletAction;
33  
34  import javax.portlet.ActionRequest;
35  import javax.portlet.ActionResponse;
36  import javax.portlet.PortletConfig;
37  import javax.portlet.RenderRequest;
38  import javax.portlet.RenderResponse;
39  
40  import org.apache.struts.action.ActionForm;
41  import org.apache.struts.action.ActionForward;
42  import org.apache.struts.action.ActionMapping;
43  
44  /**
45   * <a href="EditOrganizationAction.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Brian Wing Shun Chan
48   */
49  public class EditOrganizationAction extends PortletAction {
50  
51      public void processAction(
52              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
53              ActionRequest actionRequest, ActionResponse actionResponse)
54          throws Exception {
55  
56          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
57  
58          try {
59              Organization organization = null;
60  
61              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
62                  organization = updateOrganization(actionRequest);
63              }
64              else if (cmd.equals(Constants.DELETE)) {
65                  deleteOrganizations(actionRequest);
66              }
67  
68              String redirect = ParamUtil.getString(actionRequest, "redirect");
69  
70              if (organization != null) {
71                  redirect += organization.getOrganizationId();
72              }
73  
74              sendRedirect(actionRequest, actionResponse, redirect);
75          }
76          catch (Exception e) {
77              if (e instanceof NoSuchOrganizationException ||
78                  e instanceof PrincipalException) {
79  
80                  SessionErrors.add(actionRequest, e.getClass().getName());
81  
82                  setForward(actionRequest, "portlet.enterprise_admin.error");
83              }
84              else if (e instanceof DuplicateOrganizationException ||
85                       e instanceof NoSuchCountryException ||
86                       e instanceof NoSuchListTypeException ||
87                       e instanceof OrganizationNameException ||
88                       e instanceof OrganizationParentException ||
89                       e instanceof RequiredOrganizationException) {
90  
91                  SessionErrors.add(actionRequest, e.getClass().getName());
92  
93                  if (e instanceof RequiredOrganizationException) {
94                      actionResponse.sendRedirect(
95                          ParamUtil.getString(actionRequest, "redirect"));
96                  }
97              }
98              else {
99                  throw e;
100             }
101         }
102     }
103 
104     public ActionForward render(
105             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
106             RenderRequest renderRequest, RenderResponse renderResponse)
107         throws Exception {
108 
109         try {
110             ActionUtil.getOrganization(renderRequest);
111         }
112         catch (Exception e) {
113             if (e instanceof NoSuchOrganizationException ||
114                 e instanceof PrincipalException) {
115 
116                 SessionErrors.add(renderRequest, e.getClass().getName());
117 
118                 return mapping.findForward("portlet.enterprise_admin.error");
119             }
120             else {
121                 throw e;
122             }
123         }
124 
125         return mapping.findForward(getForward(
126             renderRequest, "portlet.enterprise_admin.edit_organization"));
127     }
128 
129     protected void deleteOrganizations(ActionRequest actionRequest)
130         throws Exception {
131 
132         long[] deleteOrganizationIds = StringUtil.split(
133             ParamUtil.getString(actionRequest, "deleteOrganizationIds"), 0L);
134 
135         for (int i = 0; i < deleteOrganizationIds.length; i++) {
136             OrganizationServiceUtil.deleteOrganization(
137                 deleteOrganizationIds[i]);
138         }
139     }
140 
141     protected Organization updateOrganization(ActionRequest actionRequest)
142         throws Exception {
143 
144         long organizationId = ParamUtil.getLong(
145             actionRequest, "organizationId");
146 
147         long parentOrganizationId = ParamUtil.getLong(
148             actionRequest, "parentOrganizationId",
149             OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID);
150         String name = ParamUtil.getString(actionRequest, "name");
151         boolean recursable = ParamUtil.getBoolean(actionRequest, "recursable");
152         int statusId = ParamUtil.getInteger(actionRequest, "statusId");
153         int type = ParamUtil.getInteger(actionRequest, "type");
154         long regionId = ParamUtil.getLong(actionRequest, "regionId");
155         long countryId = ParamUtil.getLong(actionRequest, "countryId");
156         String comments = ParamUtil.getString(actionRequest, "comments");
157 
158         Organization organization = null;
159 
160         if (organizationId <= 0) {
161 
162             // Add organization
163 
164             organization = OrganizationServiceUtil.addOrganization(
165                 parentOrganizationId, name, type, recursable, regionId,
166                 countryId, statusId, comments);
167         }
168         else {
169 
170             // Update organization
171 
172             organization = OrganizationServiceUtil.updateOrganization(
173                 organizationId, parentOrganizationId, name, type,
174                 recursable, regionId, countryId, statusId, comments);
175         }
176 
177         return organization;
178     }
179 
180 }