1
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
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
146 AddressServiceUtil.addAddress(
147 className, classPK, street1, street2, street3, city, zip,
148 regionId, countryId, typeId, mailing, primary);
149 }
150 else {
151
152
154 AddressServiceUtil.updateAddress(
155 addressId, street1, street2, street3, city, zip, regionId,
156 countryId, typeId, mailing, primary);
157 }
158 }
159
160 }