1
22
23 package com.liferay.portal.service.impl;
24
25 import com.liferay.counter.service.CounterLocalServiceUtil;
26 import com.liferay.portal.AddressCityException;
27 import com.liferay.portal.AddressStreetException;
28 import com.liferay.portal.AddressZipException;
29 import com.liferay.portal.PortalException;
30 import com.liferay.portal.SystemException;
31 import com.liferay.portal.kernel.util.Validator;
32 import com.liferay.portal.model.Address;
33 import com.liferay.portal.model.User;
34 import com.liferay.portal.model.impl.ListTypeImpl;
35 import com.liferay.portal.service.ListTypeServiceUtil;
36 import com.liferay.portal.service.base.AddressLocalServiceBaseImpl;
37 import com.liferay.portal.service.persistence.AddressUtil;
38 import com.liferay.portal.service.persistence.UserUtil;
39 import com.liferay.portal.util.PortalUtil;
40
41 import java.rmi.RemoteException;
42
43 import java.util.Date;
44 import java.util.Iterator;
45 import java.util.List;
46
47
54 public class AddressLocalServiceImpl extends AddressLocalServiceBaseImpl {
55
56 public Address addAddress(
57 long userId, String className, long classPK, String street1,
58 String street2, String street3, String city, String zip,
59 long regionId, long countryId, int typeId, boolean mailing,
60 boolean primary)
61 throws PortalException, SystemException {
62
63 User user = UserUtil.findByPrimaryKey(userId);
64 long classNameId = PortalUtil.getClassNameId(className);
65 Date now = new Date();
66
67 validate(
68 0, user.getCompanyId(), classNameId, classPK, street1, city, zip,
69 regionId, countryId, typeId, mailing, primary);
70
71 long addressId = CounterLocalServiceUtil.increment();
72
73 Address address = AddressUtil.create(addressId);
74
75 address.setCompanyId(user.getCompanyId());
76 address.setUserId(user.getUserId());
77 address.setUserName(user.getFullName());
78 address.setCreateDate(now);
79 address.setModifiedDate(now);
80 address.setClassNameId(classNameId);
81 address.setClassPK(classPK);
82 address.setStreet1(street1);
83 address.setStreet2(street2);
84 address.setStreet3(street3);
85 address.setCity(city);
86 address.setZip(zip);
87 address.setRegionId(regionId);
88 address.setCountryId(countryId);
89 address.setTypeId(typeId);
90 address.setMailing(mailing);
91 address.setPrimary(primary);
92
93 AddressUtil.update(address);
94
95 return address;
96 }
97
98 public void deleteAddress(long addressId)
99 throws PortalException, SystemException {
100
101 AddressUtil.remove(addressId);
102 }
103
104 public void deleteAddresses(long companyId, String className, long classPK)
105 throws SystemException {
106
107 long classNameId = PortalUtil.getClassNameId(className);
108
109 AddressUtil.removeByC_C_C(companyId, classNameId, classPK);
110 }
111
112 public Address getAddress(long addressId)
113 throws PortalException, SystemException {
114
115 return AddressUtil.findByPrimaryKey(addressId);
116 }
117
118 public List getAddresses() throws SystemException {
119 return AddressUtil.findAll();
120 }
121
122 public List getAddresses(long companyId, String className, long classPK)
123 throws SystemException {
124
125 long classNameId = PortalUtil.getClassNameId(className);
126
127 return AddressUtil.findByC_C_C(companyId, classNameId, classPK);
128 }
129
130 public Address updateAddress(
131 long addressId, String street1, String street2, String street3,
132 String city, String zip, long regionId, long countryId, int typeId,
133 boolean mailing, boolean primary)
134 throws PortalException, SystemException {
135
136 validate(
137 addressId, 0, 0, 0, street1, city, zip, regionId, countryId, typeId,
138 mailing, primary);
139
140 Address address = AddressUtil.findByPrimaryKey(addressId);
141
142 address.setModifiedDate(new Date());
143 address.setStreet1(street1);
144 address.setStreet2(street2);
145 address.setStreet3(street3);
146 address.setCity(city);
147 address.setZip(zip);
148 address.setRegionId(regionId);
149 address.setCountryId(countryId);
150 address.setTypeId(typeId);
151 address.setMailing(mailing);
152 address.setPrimary(primary);
153
154 AddressUtil.update(address);
155
156 return address;
157 }
158
159 protected void validate(
160 long addressId, long companyId, long classNameId, long classPK,
161 String street1, String city, String zip, long regionId,
162 long countryId, int typeId, boolean mailing, boolean primary)
163 throws PortalException, SystemException {
164
165 if (Validator.isNull(street1)) {
166 throw new AddressStreetException();
167 }
168 else if (Validator.isNull(city)) {
169 throw new AddressCityException();
170 }
171 else if (Validator.isNull(zip)) {
172 throw new AddressZipException();
173 }
174
175
177
185
186 if (addressId > 0) {
187 Address address = AddressUtil.findByPrimaryKey(addressId);
188
189 companyId = address.getCompanyId();
190 classNameId = address.getClassNameId();
191 classPK = address.getClassPK();
192 }
193
194 try {
195 ListTypeServiceUtil.validate(
196 typeId, classNameId, ListTypeImpl.ADDRESS);
197 }
198 catch (RemoteException re) {
199 throw new SystemException(re);
200 }
201
202 validate(addressId, companyId, classNameId, classPK, mailing, primary);
203 }
204
205 protected void validate(
206 long addressId, long companyId, long classNameId, long classPK,
207 boolean mailing, boolean primary)
208 throws PortalException, SystemException {
209
210
213 if (mailing) {
214 Iterator itr = AddressUtil.findByC_C_C_M(
215 companyId, classNameId, classPK, mailing).iterator();
216
217 while (itr.hasNext()) {
218 Address address = (Address)itr.next();
219
220 if ((addressId <= 0) ||
221 (address.getAddressId() != addressId)) {
222
223 address.setMailing(false);
224
225 AddressUtil.update(address);
226 }
227 }
228 }
229
230
233 if (primary) {
234 Iterator itr = AddressUtil.findByC_C_C_P(
235 companyId, classNameId, classPK, primary).iterator();
236
237 while (itr.hasNext()) {
238 Address address = (Address)itr.next();
239
240 if ((addressId <= 0) ||
241 (address.getAddressId() != addressId)) {
242
243 address.setPrimary(false);
244
245 AddressUtil.update(address);
246 }
247 }
248 }
249 }
250
251 }