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