1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.servlet.SessionErrors;
33  import com.liferay.portal.kernel.util.Constants;
34  import com.liferay.portal.kernel.util.ParamUtil;
35  import com.liferay.portal.security.auth.PrincipalException;
36  import com.liferay.portal.service.AddressServiceUtil;
37  import com.liferay.portal.struts.PortletAction;
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  public class EditAddressAction extends PortletAction {
55  
56      public void processAction(
57              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
58              ActionRequest actionRequest, ActionResponse actionResponse)
59          throws Exception {
60  
61          String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
62  
63          try {
64              if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
65                  updateAddress(actionRequest);
66              }
67              else if (cmd.equals(Constants.DELETE)) {
68                  deleteAddress(actionRequest);
69              }
70  
71              sendRedirect(actionRequest, actionResponse);
72          }
73          catch (Exception e) {
74              if (e instanceof NoSuchAddressException ||
75                  e instanceof PrincipalException) {
76  
77                  SessionErrors.add(actionRequest, e.getClass().getName());
78  
79                  setForward(actionRequest, "portlet.enterprise_admin.error");
80              }
81              else if (e instanceof AddressCityException  ||
82                       e instanceof AddressStreetException  ||
83                       e instanceof AddressZipException ||
84                       e instanceof NoSuchCountryException  ||
85                       e instanceof NoSuchListTypeException ||
86                       e instanceof NoSuchRegionException) {
87  
88                  SessionErrors.add(actionRequest, e.getClass().getName());
89              }
90              else {
91                  throw e;
92              }
93          }
94      }
95  
96      public ActionForward render(
97              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
98              RenderRequest renderRequest, RenderResponse renderResponse)
99          throws Exception {
100 
101         try {
102             ActionUtil.getAddress(renderRequest);
103         }
104         catch (Exception e) {
105             if (e instanceof NoSuchAddressException ||
106                 e instanceof PrincipalException) {
107 
108                 SessionErrors.add(renderRequest, e.getClass().getName());
109 
110                 return mapping.findForward("portlet.enterprise_admin.error");
111             }
112             else {
113                 throw e;
114             }
115         }
116 
117         return mapping.findForward(
118             getForward(renderRequest, "portlet.enterprise_admin.edit_address"));
119     }
120 
121     protected void deleteAddress(ActionRequest actionRequest) throws Exception {
122         long addressId = ParamUtil.getLong(actionRequest, "addressId");
123 
124         AddressServiceUtil.deleteAddress(addressId);
125     }
126 
127     protected void updateAddress(ActionRequest actionRequest) throws Exception {
128         long addressId = ParamUtil.getLong(actionRequest, "addressId");
129 
130         String className = ParamUtil.getString(actionRequest, "className");
131         long classPK = ParamUtil.getLong(actionRequest, "classPK");
132 
133         String street1 = ParamUtil.getString(actionRequest, "street1");
134         String street2 = ParamUtil.getString(actionRequest, "street2");
135         String street3 = ParamUtil.getString(actionRequest, "street3");
136         String city = ParamUtil.getString(actionRequest, "city");
137         String zip = ParamUtil.getString(actionRequest, "zip");
138         long regionId = ParamUtil.getLong(actionRequest, "regionId");
139         long countryId = ParamUtil.getLong(actionRequest, "countryId");
140         int typeId = ParamUtil.getInteger(actionRequest, "typeId");
141         boolean mailing = ParamUtil.getBoolean(actionRequest, "mailing");
142         boolean primary = ParamUtil.getBoolean(actionRequest, "primary");
143 
144         if (addressId <= 0) {
145 
146             // Add address
147 
148             AddressServiceUtil.addAddress(
149                 className, classPK, street1, street2, street3, city, zip,
150                 regionId, countryId, typeId, mailing, primary);
151         }
152         else {
153 
154             // Update address
155 
156             AddressServiceUtil.updateAddress(
157                 addressId, street1, street2, street3, city, zip, regionId,
158                 countryId, typeId, mailing, primary);
159         }
160     }
161 
162 }