1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.enterpriseadmin.action;
21  
22  import com.liferay.portal.DuplicateOrganizationException;
23  import com.liferay.portal.NoSuchCountryException;
24  import com.liferay.portal.NoSuchListTypeException;
25  import com.liferay.portal.NoSuchOrganizationException;
26  import com.liferay.portal.OrganizationNameException;
27  import com.liferay.portal.OrganizationParentException;
28  import com.liferay.portal.RequiredOrganizationException;
29  import com.liferay.portal.kernel.servlet.SessionErrors;
30  import com.liferay.portal.kernel.util.Constants;
31  import com.liferay.portal.kernel.util.ParamUtil;
32  import com.liferay.portal.kernel.util.StringUtil;
33  import com.liferay.portal.model.Organization;
34  import com.liferay.portal.model.OrganizationConstants;
35  import com.liferay.portal.security.auth.PrincipalException;
36  import com.liferay.portal.service.OrganizationServiceUtil;
37  import com.liferay.portal.struts.PortletAction;
38  
39  import javax.portlet.ActionRequest;
40  import javax.portlet.ActionResponse;
41  import javax.portlet.PortletConfig;
42  import javax.portlet.RenderRequest;
43  import javax.portlet.RenderResponse;
44  
45  import org.apache.struts.action.ActionForm;
46  import org.apache.struts.action.ActionForward;
47  import org.apache.struts.action.ActionMapping;
48  
49  /**
50   * <a href="EditOrganizationAction.java.html"><b><i>View Source</i></b></a>
51   *
52   * @author Brian Wing Shun Chan
53   *
54   */
55  public class EditOrganizationAction extends PortletAction {
56  
57      public void processAction(
58              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
59              ActionRequest actionRequest, ActionResponse actionResponse)
60          throws Exception {
61  
62          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
63  
64          try {
65              Organization organization = null;
66  
67              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
68                  organization = updateOrganization(actionRequest);
69              }
70              else if (cmd.equals(Constants.DELETE)) {
71                  deleteOrganizations(actionRequest);
72              }
73  
74              String redirect = ParamUtil.getString(actionRequest, "redirect");
75  
76              if (organization != null) {
77                  redirect += organization.getOrganizationId();
78              }
79  
80              sendRedirect(actionRequest, actionResponse, redirect);
81          }
82          catch (Exception e) {
83              if (e instanceof NoSuchOrganizationException ||
84                  e instanceof PrincipalException) {
85  
86                  SessionErrors.add(actionRequest, e.getClass().getName());
87  
88                  setForward(actionRequest, "portlet.enterprise_admin.error");
89              }
90              else if (e instanceof DuplicateOrganizationException ||
91                       e instanceof NoSuchCountryException ||
92                       e instanceof NoSuchListTypeException ||
93                       e instanceof OrganizationNameException ||
94                       e instanceof OrganizationParentException ||
95                       e instanceof RequiredOrganizationException) {
96  
97                  SessionErrors.add(actionRequest, e.getClass().getName());
98  
99                  if (e instanceof RequiredOrganizationException) {
100                     actionResponse.sendRedirect(
101                         ParamUtil.getString(actionRequest, "redirect"));
102                 }
103             }
104             else {
105                 throw e;
106             }
107         }
108     }
109 
110     public ActionForward render(
111             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
112             RenderRequest renderRequest, RenderResponse renderResponse)
113         throws Exception {
114 
115         try {
116             ActionUtil.getOrganization(renderRequest);
117         }
118         catch (Exception e) {
119             if (e instanceof NoSuchOrganizationException ||
120                 e instanceof PrincipalException) {
121 
122                 SessionErrors.add(renderRequest, e.getClass().getName());
123 
124                 return mapping.findForward("portlet.enterprise_admin.error");
125             }
126             else {
127                 throw e;
128             }
129         }
130 
131         return mapping.findForward(getForward(
132             renderRequest, "portlet.enterprise_admin.edit_organization"));
133     }
134 
135     protected void deleteOrganizations(ActionRequest actionRequest)
136         throws Exception {
137 
138         long[] deleteOrganizationIds = StringUtil.split(
139             ParamUtil.getString(actionRequest, "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 actionRequest)
148         throws Exception {
149 
150         long organizationId = ParamUtil.getLong(
151             actionRequest, "organizationId");
152 
153         long parentOrganizationId = ParamUtil.getLong(
154             actionRequest, "parentOrganizationId",
155             OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID);
156         String name = ParamUtil.getString(actionRequest, "name");
157         boolean recursable = ParamUtil.getBoolean(actionRequest, "recursable");
158         int statusId = ParamUtil.getInteger(actionRequest, "statusId");
159         int type = ParamUtil.getInteger(actionRequest, "type");
160         long regionId = ParamUtil.getLong(actionRequest, "regionId");
161         long countryId = ParamUtil.getLong(actionRequest, "countryId");
162         String comments = ParamUtil.getString(actionRequest, "comments");
163 
164         Organization organization = null;
165 
166         if (organizationId <= 0) {
167 
168             // Add organization
169 
170             organization = OrganizationServiceUtil.addOrganization(
171                 parentOrganizationId, name, type, recursable, regionId,
172                 countryId, statusId, comments);
173         }
174         else {
175 
176             // Update organization
177 
178             organization = OrganizationServiceUtil.updateOrganization(
179                 organizationId, parentOrganizationId, name, type,
180                 recursable, regionId, countryId, statusId, comments);
181         }
182 
183         return organization;
184     }
185 
186 }