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.AddressCityException;
26  import com.liferay.portal.AddressStreetException;
27  import com.liferay.portal.AddressZipException;
28  import com.liferay.portal.NoSuchAddressException;
29  import com.liferay.portal.NoSuchCountryException;
30  import com.liferay.portal.NoSuchListTypeException;
31  import com.liferay.portal.NoSuchRegionException;
32  import com.liferay.portal.kernel.util.Constants;
33  import com.liferay.portal.kernel.util.ParamUtil;
34  import com.liferay.portal.security.auth.PrincipalException;
35  import com.liferay.portal.service.AddressServiceUtil;
36  import com.liferay.portal.struts.PortletAction;
37  import com.liferay.util.servlet.SessionErrors;
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="EditAddressAction.java.html"><b><i>View Source</i></b></a>
51   *
52   * @author Brian Wing Shun Chan
53   *
54   */
55  public class EditAddressAction extends PortletAction {
56  
57      public void processAction(
58              ActionMapping mapping, ActionForm form, PortletConfig config,
59              ActionRequest req, ActionResponse res)
60          throws Exception {
61  
62          String cmd = ParamUtil.getString(req, Constants.CMD);
63  
64          try {
65              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
66                  updateAddress(req);
67              }
68              else if (cmd.equals(Constants.DELETE)) {
69                  deleteAddress(req);
70              }
71  
72              sendRedirect(req, res);
73          }
74          catch (Exception e) {
75              if (e instanceof NoSuchAddressException ||
76                  e instanceof PrincipalException) {
77  
78                  SessionErrors.add(req, e.getClass().getName());
79  
80                  setForward(req, "portlet.enterprise_admin.error");
81              }
82              else if (e instanceof AddressCityException  ||
83                       e instanceof AddressStreetException  ||
84                       e instanceof AddressZipException ||
85                       e instanceof NoSuchCountryException  ||
86                       e instanceof NoSuchListTypeException ||
87                       e instanceof NoSuchRegionException) {
88  
89                  SessionErrors.add(req, e.getClass().getName());
90              }
91              else {
92                  throw e;
93              }
94          }
95      }
96  
97      public ActionForward render(
98              ActionMapping mapping, ActionForm form, PortletConfig config,
99              RenderRequest req, RenderResponse res)
100         throws Exception {
101 
102         try {
103             ActionUtil.getAddress(req);
104         }
105         catch (Exception e) {
106             if (e instanceof NoSuchAddressException ||
107                 e instanceof PrincipalException) {
108 
109                 SessionErrors.add(req, e.getClass().getName());
110 
111                 return mapping.findForward("portlet.enterprise_admin.error");
112             }
113             else {
114                 throw e;
115             }
116         }
117 
118         return mapping.findForward(
119             getForward(req, "portlet.enterprise_admin.edit_address"));
120     }
121 
122     protected void deleteAddress(ActionRequest req) throws Exception {
123         long addressId = ParamUtil.getLong(req, "addressId");
124 
125         AddressServiceUtil.deleteAddress(addressId);
126     }
127 
128     protected void updateAddress(ActionRequest req) throws Exception {
129         long addressId = ParamUtil.getLong(req, "addressId");
130 
131         String className = ParamUtil.getString(req, "className");
132         long classPK = ParamUtil.getLong(req, "classPK");
133 
134         String street1 = ParamUtil.getString(req, "street1");
135         String street2 = ParamUtil.getString(req, "street2");
136         String street3 = ParamUtil.getString(req, "street3");
137         String city = ParamUtil.getString(req, "city");
138         String zip = ParamUtil.getString(req, "zip");
139         long regionId = ParamUtil.getLong(req, "regionId");
140         long countryId = ParamUtil.getLong(req, "countryId");
141         int typeId = ParamUtil.getInteger(req, "typeId");
142         boolean mailing = ParamUtil.getBoolean(req, "mailing");
143         boolean primary = ParamUtil.getBoolean(req, "primary");
144 
145         if (addressId <= 0) {
146 
147             // Add address
148 
149             AddressServiceUtil.addAddress(
150                 className, classPK, street1, street2, street3, city, zip,
151                 regionId, countryId, typeId, mailing, primary);
152         }
153         else {
154 
155             // Update address
156 
157             AddressServiceUtil.updateAddress(
158                 addressId, street1, street2, street3, city, zip, regionId,
159                 countryId, typeId, mailing, primary);
160         }
161     }
162 
163 }