1
22
23 package com.liferay.portal.service.impl;
24
25 import com.liferay.portal.AddressCityException;
26 import com.liferay.portal.AddressStreetException;
27 import com.liferay.portal.AddressZipException;
28 import com.liferay.portal.PortalException;
29 import com.liferay.portal.SystemException;
30 import com.liferay.portal.kernel.util.Validator;
31 import com.liferay.portal.model.Account;
32 import com.liferay.portal.model.Address;
33 import com.liferay.portal.model.Contact;
34 import com.liferay.portal.model.Organization;
35 import com.liferay.portal.model.User;
36 import com.liferay.portal.model.impl.ListTypeImpl;
37 import com.liferay.portal.service.base.AddressLocalServiceBaseImpl;
38 import com.liferay.portal.util.PortalUtil;
39
40 import java.rmi.RemoteException;
41
42 import java.util.Date;
43 import java.util.Iterator;
44 import java.util.List;
45
46
53 public class AddressLocalServiceImpl extends AddressLocalServiceBaseImpl {
54
55 public Address addAddress(
56 long userId, String className, long classPK, String street1,
57 String street2, String street3, String city, String zip,
58 long regionId, long countryId, int typeId, boolean mailing,
59 boolean primary)
60 throws PortalException, SystemException {
61
62 User user = userPersistence.findByPrimaryKey(userId);
63 long classNameId = PortalUtil.getClassNameId(className);
64 Date now = new Date();
65
66 validate(
67 0, user.getCompanyId(), classNameId, classPK, street1, city, zip,
68 regionId, countryId, typeId, mailing, primary);
69
70 long addressId = counterLocalService.increment();
71
72 Address address = addressPersistence.create(addressId);
73
74 address.setCompanyId(user.getCompanyId());
75 address.setUserId(user.getUserId());
76 address.setUserName(user.getFullName());
77 address.setCreateDate(now);
78 address.setModifiedDate(now);
79 address.setClassNameId(classNameId);
80 address.setClassPK(classPK);
81 address.setStreet1(street1);
82 address.setStreet2(street2);
83 address.setStreet3(street3);
84 address.setCity(city);
85 address.setZip(zip);
86 address.setRegionId(regionId);
87 address.setCountryId(countryId);
88 address.setTypeId(typeId);
89 address.setMailing(mailing);
90 address.setPrimary(primary);
91
92 addressPersistence.update(address, false);
93
94 return address;
95 }
96
97 public void deleteAddress(long addressId)
98 throws PortalException, SystemException {
99
100 addressPersistence.remove(addressId);
101 }
102
103 public void deleteAddresses(long companyId, String className, long classPK)
104 throws SystemException {
105
106 long classNameId = PortalUtil.getClassNameId(className);
107
108 addressPersistence.removeByC_C_C(companyId, classNameId, classPK);
109 }
110
111 public Address getAddress(long addressId)
112 throws PortalException, SystemException {
113
114 return addressPersistence.findByPrimaryKey(addressId);
115 }
116
117 public List<Address> getAddresses() throws SystemException {
118 return addressPersistence.findAll();
119 }
120
121 public List<Address> getAddresses(
122 long companyId, String className, long classPK)
123 throws SystemException {
124
125 long classNameId = PortalUtil.getClassNameId(className);
126
127 return addressPersistence.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 = addressPersistence.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 addressPersistence.update(address, false);
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 = addressPersistence.findByPrimaryKey(addressId);
188
189 companyId = address.getCompanyId();
190 classNameId = address.getClassNameId();
191 classPK = address.getClassPK();
192 }
193
194 try {
195 if ((classNameId == PortalUtil.getClassNameId(Account.class)) ||
196 (classNameId == PortalUtil.getClassNameId(Contact.class)) ||
197 (classNameId ==
198 PortalUtil.getClassNameId(Organization.class))) {
199
200 listTypeService.validate(
201 typeId, classNameId, ListTypeImpl.ADDRESS);
202 }
203 }
204 catch (RemoteException re) {
205 throw new SystemException(re);
206 }
207
208 validate(addressId, companyId, classNameId, classPK, mailing, primary);
209 }
210
211 protected void validate(
212 long addressId, long companyId, long classNameId, long classPK,
213 boolean mailing, boolean primary)
214 throws SystemException {
215
216
219 if (mailing) {
220 Iterator<Address> itr = addressPersistence.findByC_C_C_M(
221 companyId, classNameId, classPK, mailing).iterator();
222
223 while (itr.hasNext()) {
224 Address address = itr.next();
225
226 if ((addressId <= 0) ||
227 (address.getAddressId() != addressId)) {
228
229 address.setMailing(false);
230
231 addressPersistence.update(address, false);
232 }
233 }
234 }
235
236
239 if (primary) {
240 Iterator<Address> itr = addressPersistence.findByC_C_C_P(
241 companyId, classNameId, classPK, primary).iterator();
242
243 while (itr.hasNext()) {
244 Address address = itr.next();
245
246 if ((addressId <= 0) ||
247 (address.getAddressId() != addressId)) {
248
249 address.setPrimary(false);
250
251 addressPersistence.update(address, false);
252 }
253 }
254 }
255 }
256
257 }