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