1   /**
2    * Copyright (c) 2000-2007 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.impl.OrganizationImpl;
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("comments")) {
74                  organization = updateComments(req);
75              }
76              else if (cmd.equals(Constants.DELETE)) {
77                  deleteOrganizations(req);
78              }
79  
80              String redirect = ParamUtil.getString(req, "redirect");
81  
82              if (organization != null) {
83                  redirect += organization.getOrganizationId();
84              }
85  
86              sendRedirect(req, res, redirect);
87          }
88          catch (Exception e) {
89              if (e instanceof NoSuchOrganizationException ||
90                  e instanceof PrincipalException) {
91  
92                  SessionErrors.add(req, e.getClass().getName());
93  
94                  setForward(req, "portlet.enterprise_admin.error");
95              }
96              else if (e instanceof DuplicateOrganizationException ||
97                       e instanceof NoSuchCountryException ||
98                       e instanceof NoSuchListTypeException ||
99                       e instanceof OrganizationNameException ||
100                      e instanceof OrganizationParentException ||
101                      e instanceof RequiredOrganizationException) {
102 
103                 SessionErrors.add(req, e.getClass().getName());
104 
105                 if (e instanceof RequiredOrganizationException) {
106                     res.sendRedirect(ParamUtil.getString(req, "redirect"));
107                 }
108             }
109             else {
110                 throw e;
111             }
112         }
113     }
114 
115     public ActionForward render(
116             ActionMapping mapping, ActionForm form, PortletConfig config,
117             RenderRequest req, RenderResponse res)
118         throws Exception {
119 
120         try {
121             ActionUtil.getOrganization(req);
122         }
123         catch (Exception e) {
124             if (e instanceof NoSuchOrganizationException ||
125                 e instanceof PrincipalException) {
126 
127                 SessionErrors.add(req, e.getClass().getName());
128 
129                 return mapping.findForward("portlet.enterprise_admin.error");
130             }
131             else {
132                 throw e;
133             }
134         }
135 
136         return mapping.findForward(
137             getForward(req, "portlet.enterprise_admin.edit_organization"));
138     }
139 
140     protected boolean isLocation() {
141         return _LOCATION;
142     }
143 
144     protected void deleteOrganizations(ActionRequest req) throws Exception {
145         long[] deleteOrganizationIds = StringUtil.split(
146             ParamUtil.getString(req, "deleteOrganizationIds"), 0L);
147 
148         for (int i = 0; i < deleteOrganizationIds.length; i++) {
149             OrganizationServiceUtil.deleteOrganization(
150                 deleteOrganizationIds[i]);
151         }
152     }
153 
154     protected Organization updateComments(ActionRequest req) throws Exception {
155         long organizationId = ParamUtil.getLong(req, "organizationId");
156 
157         String comments = ParamUtil.getString(req, "comments");
158 
159         return OrganizationServiceUtil.updateOrganization(
160             organizationId, comments);
161     }
162 
163     protected Organization updateOrganization(ActionRequest req)
164         throws Exception {
165 
166         long organizationId = ParamUtil.getLong(req, "organizationId");
167 
168         long parentOrganizationId = ParamUtil.getLong(
169             req, "parentOrganizationId",
170             OrganizationImpl.DEFAULT_PARENT_ORGANIZATION_ID);
171         String name = ParamUtil.getString(req, "name");
172         boolean recursable = ParamUtil.getBoolean(req, "recursable");
173         int statusId = ParamUtil.getInteger(req, "statusId");
174         long regionId = ParamUtil.getLong(req, "regionId");
175         long countryId = ParamUtil.getLong(req, "countryId");
176 
177         Organization organization = null;
178 
179         if (organizationId <= 0) {
180 
181             // Add organization
182 
183             organization = OrganizationServiceUtil.addOrganization(
184                 parentOrganizationId, name, isLocation(), recursable, regionId,
185                 countryId, statusId);
186         }
187         else {
188 
189             // Update organization
190 
191             organization = OrganizationServiceUtil.updateOrganization(
192                 organizationId, parentOrganizationId, name, isLocation(),
193                 recursable, regionId, countryId, statusId);
194         }
195 
196         return organization;
197     }
198 
199     private static final boolean _LOCATION = false;
200 
201 }