1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class EditOrganizationAction extends PortletAction {
58  
59      public void processAction(
60              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
61              ActionRequest actionRequest, ActionResponse actionResponse)
62          throws Exception {
63  
64          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
65  
66          try {
67              Organization organization = null;
68  
69              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
70                  organization = updateOrganization(actionRequest);
71              }
72              else if (cmd.equals(Constants.DELETE)) {
73                  deleteOrganizations(actionRequest);
74              }
75  
76              String redirect = ParamUtil.getString(actionRequest, "redirect");
77  
78              if (organization != null) {
79                  redirect += organization.getOrganizationId();
80              }
81  
82              sendRedirect(actionRequest, actionResponse, redirect);
83          }
84          catch (Exception e) {
85              if (e instanceof NoSuchOrganizationException ||
86                  e instanceof PrincipalException) {
87  
88                  SessionErrors.add(actionRequest, e.getClass().getName());
89  
90                  setForward(actionRequest, "portlet.enterprise_admin.error");
91              }
92              else if (e instanceof DuplicateOrganizationException ||
93                       e instanceof NoSuchCountryException ||
94                       e instanceof NoSuchListTypeException ||
95                       e instanceof OrganizationNameException ||
96                       e instanceof OrganizationParentException ||
97                       e instanceof RequiredOrganizationException) {
98  
99                  SessionErrors.add(actionRequest, e.getClass().getName());
100 
101                 if (e instanceof RequiredOrganizationException) {
102                     actionResponse.sendRedirect(
103                         ParamUtil.getString(actionRequest, "redirect"));
104                 }
105             }
106             else {
107                 throw e;
108             }
109         }
110     }
111 
112     public ActionForward render(
113             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
114             RenderRequest renderRequest, RenderResponse renderResponse)
115         throws Exception {
116 
117         try {
118             ActionUtil.getOrganization(renderRequest);
119         }
120         catch (Exception e) {
121             if (e instanceof NoSuchOrganizationException ||
122                 e instanceof PrincipalException) {
123 
124                 SessionErrors.add(renderRequest, 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(getForward(
134             renderRequest, "portlet.enterprise_admin.edit_organization"));
135     }
136 
137     protected void deleteOrganizations(ActionRequest actionRequest)
138         throws Exception {
139 
140         long[] deleteOrganizationIds = StringUtil.split(
141             ParamUtil.getString(actionRequest, "deleteOrganizationIds"), 0L);
142 
143         for (int i = 0; i < deleteOrganizationIds.length; i++) {
144             OrganizationServiceUtil.deleteOrganization(
145                 deleteOrganizationIds[i]);
146         }
147     }
148 
149     protected Organization updateOrganization(ActionRequest actionRequest)
150         throws Exception {
151 
152         long organizationId = ParamUtil.getLong(
153             actionRequest, "organizationId");
154 
155         long parentOrganizationId = ParamUtil.getLong(
156             actionRequest, "parentOrganizationId",
157             OrganizationConstants.DEFAULT_PARENT_ORGANIZATION_ID);
158         String name = ParamUtil.getString(actionRequest, "name");
159         boolean recursable = ParamUtil.getBoolean(actionRequest, "recursable");
160         int statusId = ParamUtil.getInteger(actionRequest, "statusId");
161         int type = ParamUtil.getInteger(actionRequest, "type");
162         long regionId = ParamUtil.getLong(actionRequest, "regionId");
163         long countryId = ParamUtil.getLong(actionRequest, "countryId");
164         String comments = ParamUtil.getString(actionRequest, "comments");
165 
166         Organization organization = null;
167 
168         if (organizationId <= 0) {
169 
170             // Add organization
171 
172             organization = OrganizationServiceUtil.addOrganization(
173                 parentOrganizationId, name, type, recursable, regionId,
174                 countryId, statusId, comments);
175         }
176         else {
177 
178             // Update organization
179 
180             organization = OrganizationServiceUtil.updateOrganization(
181                 organizationId, parentOrganizationId, name, type,
182                 recursable, regionId, countryId, statusId, comments);
183         }
184 
185         return organization;
186     }
187 
188 }