001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.enterpriseadmin.action;
022:
023: import com.liferay.portal.DuplicateOrganizationException;
024: import com.liferay.portal.NoSuchCountryException;
025: import com.liferay.portal.NoSuchListTypeException;
026: import com.liferay.portal.NoSuchOrganizationException;
027: import com.liferay.portal.OrganizationNameException;
028: import com.liferay.portal.OrganizationParentException;
029: import com.liferay.portal.RequiredOrganizationException;
030: import com.liferay.portal.kernel.util.Constants;
031: import com.liferay.portal.kernel.util.ParamUtil;
032: import com.liferay.portal.kernel.util.StringUtil;
033: import com.liferay.portal.model.Organization;
034: import com.liferay.portal.model.impl.OrganizationImpl;
035: import com.liferay.portal.security.auth.PrincipalException;
036: import com.liferay.portal.service.OrganizationServiceUtil;
037: import com.liferay.portal.struts.PortletAction;
038: import com.liferay.util.servlet.SessionErrors;
039:
040: import javax.portlet.ActionRequest;
041: import javax.portlet.ActionResponse;
042: import javax.portlet.PortletConfig;
043: import javax.portlet.RenderRequest;
044: import javax.portlet.RenderResponse;
045:
046: import org.apache.struts.action.ActionForm;
047: import org.apache.struts.action.ActionForward;
048: import org.apache.struts.action.ActionMapping;
049:
050: /**
051: * <a href="EditOrganizationAction.java.html"><b><i>View Source</i></b></a>
052: *
053: * @author Brian Wing Shun Chan
054: *
055: */
056: public class EditOrganizationAction extends PortletAction {
057:
058: public void processAction(ActionMapping mapping, ActionForm form,
059: PortletConfig config, ActionRequest req, ActionResponse res)
060: throws Exception {
061:
062: String cmd = ParamUtil.getString(req, Constants.CMD);
063:
064: try {
065: Organization organization = null;
066:
067: if (cmd.equals(Constants.ADD)
068: || cmd.equals(Constants.UPDATE)) {
069: organization = updateOrganization(req);
070: } else if (cmd.equals(Constants.DELETE)) {
071: deleteOrganizations(req);
072: }
073:
074: String redirect = ParamUtil.getString(req, "redirect");
075:
076: if (organization != null) {
077: redirect += organization.getOrganizationId();
078: }
079:
080: sendRedirect(req, res, redirect);
081: } catch (Exception e) {
082: if (e instanceof NoSuchOrganizationException
083: || e instanceof PrincipalException) {
084:
085: SessionErrors.add(req, e.getClass().getName());
086:
087: setForward(req, "portlet.enterprise_admin.error");
088: } else if (e instanceof DuplicateOrganizationException
089: || e instanceof NoSuchCountryException
090: || e instanceof NoSuchListTypeException
091: || e instanceof OrganizationNameException
092: || e instanceof OrganizationParentException
093: || e instanceof RequiredOrganizationException) {
094:
095: SessionErrors.add(req, e.getClass().getName());
096:
097: if (e instanceof RequiredOrganizationException) {
098: res.sendRedirect(ParamUtil.getString(req,
099: "redirect"));
100: }
101: } else {
102: throw e;
103: }
104: }
105: }
106:
107: public ActionForward render(ActionMapping mapping, ActionForm form,
108: PortletConfig config, RenderRequest req, RenderResponse res)
109: throws Exception {
110:
111: try {
112: ActionUtil.getOrganization(req);
113: } catch (Exception e) {
114: if (e instanceof NoSuchOrganizationException
115: || e instanceof PrincipalException) {
116:
117: SessionErrors.add(req, e.getClass().getName());
118:
119: return mapping
120: .findForward("portlet.enterprise_admin.error");
121: } else {
122: throw e;
123: }
124: }
125:
126: return mapping.findForward(getForward(req,
127: "portlet.enterprise_admin.edit_organization"));
128: }
129:
130: protected void deleteOrganizations(ActionRequest req)
131: throws Exception {
132: long[] deleteOrganizationIds = StringUtil.split(ParamUtil
133: .getString(req, "deleteOrganizationIds"), 0L);
134:
135: for (int i = 0; i < deleteOrganizationIds.length; i++) {
136: OrganizationServiceUtil
137: .deleteOrganization(deleteOrganizationIds[i]);
138: }
139: }
140:
141: protected Organization updateOrganization(ActionRequest req)
142: throws Exception {
143:
144: long organizationId = ParamUtil.getLong(req, "organizationId");
145:
146: long parentOrganizationId = ParamUtil.getLong(req,
147: "parentOrganizationId",
148: OrganizationImpl.DEFAULT_PARENT_ORGANIZATION_ID);
149: String name = ParamUtil.getString(req, "name");
150: boolean recursable = ParamUtil.getBoolean(req, "recursable");
151: int statusId = ParamUtil.getInteger(req, "statusId");
152: int type = ParamUtil.getInteger(req, "type");
153: long regionId = ParamUtil.getLong(req, "regionId");
154: long countryId = ParamUtil.getLong(req, "countryId");
155: String comments = ParamUtil.getString(req, "comments");
156:
157: Organization organization = null;
158:
159: if (organizationId <= 0) {
160:
161: // Add organization
162:
163: organization = OrganizationServiceUtil.addOrganization(
164: parentOrganizationId, name, type, recursable,
165: regionId, countryId, statusId, comments);
166: } else {
167:
168: // Update organization
169:
170: organization = OrganizationServiceUtil
171: .updateOrganization(organizationId,
172: parentOrganizationId, name, type,
173: recursable, regionId, countryId, statusId,
174: comments);
175: }
176:
177: return organization;
178: }
179:
180: }
|