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