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