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.AddressCityException;
024: import com.liferay.portal.AddressStreetException;
025: import com.liferay.portal.AddressZipException;
026: import com.liferay.portal.NoSuchAddressException;
027: import com.liferay.portal.NoSuchCountryException;
028: import com.liferay.portal.NoSuchListTypeException;
029: import com.liferay.portal.NoSuchRegionException;
030: import com.liferay.portal.kernel.util.Constants;
031: import com.liferay.portal.kernel.util.ParamUtil;
032: import com.liferay.portal.security.auth.PrincipalException;
033: import com.liferay.portal.service.AddressServiceUtil;
034: import com.liferay.portal.struts.PortletAction;
035: import com.liferay.util.servlet.SessionErrors;
036:
037: import javax.portlet.ActionRequest;
038: import javax.portlet.ActionResponse;
039: import javax.portlet.PortletConfig;
040: import javax.portlet.RenderRequest;
041: import javax.portlet.RenderResponse;
042:
043: import org.apache.struts.action.ActionForm;
044: import org.apache.struts.action.ActionForward;
045: import org.apache.struts.action.ActionMapping;
046:
047: /**
048: * <a href="EditAddressAction.java.html"><b><i>View Source</i></b></a>
049: *
050: * @author Brian Wing Shun Chan
051: *
052: */
053: public class EditAddressAction extends PortletAction {
054:
055: public void processAction(ActionMapping mapping, ActionForm form,
056: PortletConfig config, ActionRequest req, ActionResponse res)
057: throws Exception {
058:
059: String cmd = ParamUtil.getString(req, Constants.CMD);
060:
061: try {
062: if (cmd.equals(Constants.ADD)
063: || cmd.equals(Constants.UPDATE)) {
064: updateAddress(req);
065: } else if (cmd.equals(Constants.DELETE)) {
066: deleteAddress(req);
067: }
068:
069: sendRedirect(req, res);
070: } catch (Exception e) {
071: if (e instanceof NoSuchAddressException
072: || e instanceof PrincipalException) {
073:
074: SessionErrors.add(req, e.getClass().getName());
075:
076: setForward(req, "portlet.enterprise_admin.error");
077: } else if (e instanceof AddressCityException
078: || e instanceof AddressStreetException
079: || e instanceof AddressZipException
080: || e instanceof NoSuchCountryException
081: || e instanceof NoSuchListTypeException
082: || e instanceof NoSuchRegionException) {
083:
084: SessionErrors.add(req, e.getClass().getName());
085: } else {
086: throw e;
087: }
088: }
089: }
090:
091: public ActionForward render(ActionMapping mapping, ActionForm form,
092: PortletConfig config, RenderRequest req, RenderResponse res)
093: throws Exception {
094:
095: try {
096: ActionUtil.getAddress(req);
097: } catch (Exception e) {
098: if (e instanceof NoSuchAddressException
099: || e instanceof PrincipalException) {
100:
101: SessionErrors.add(req, e.getClass().getName());
102:
103: return mapping
104: .findForward("portlet.enterprise_admin.error");
105: } else {
106: throw e;
107: }
108: }
109:
110: return mapping.findForward(getForward(req,
111: "portlet.enterprise_admin.edit_address"));
112: }
113:
114: protected void deleteAddress(ActionRequest req) throws Exception {
115: long addressId = ParamUtil.getLong(req, "addressId");
116:
117: AddressServiceUtil.deleteAddress(addressId);
118: }
119:
120: protected void updateAddress(ActionRequest req) throws Exception {
121: long addressId = ParamUtil.getLong(req, "addressId");
122:
123: String className = ParamUtil.getString(req, "className");
124: long classPK = ParamUtil.getLong(req, "classPK");
125:
126: String street1 = ParamUtil.getString(req, "street1");
127: String street2 = ParamUtil.getString(req, "street2");
128: String street3 = ParamUtil.getString(req, "street3");
129: String city = ParamUtil.getString(req, "city");
130: String zip = ParamUtil.getString(req, "zip");
131: long regionId = ParamUtil.getLong(req, "regionId");
132: long countryId = ParamUtil.getLong(req, "countryId");
133: int typeId = ParamUtil.getInteger(req, "typeId");
134: boolean mailing = ParamUtil.getBoolean(req, "mailing");
135: boolean primary = ParamUtil.getBoolean(req, "primary");
136:
137: if (addressId <= 0) {
138:
139: // Add address
140:
141: AddressServiceUtil.addAddress(className, classPK, street1,
142: street2, street3, city, zip, regionId, countryId,
143: typeId, mailing, primary);
144: } else {
145:
146: // Update address
147:
148: AddressServiceUtil.updateAddress(addressId, street1,
149: street2, street3, city, zip, regionId, countryId,
150: typeId, mailing, primary);
151: }
152: }
153:
154: }
|