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 if (addressId > 0) {
174 Address address = addressPersistence.findByPrimaryKey(addressId);
175
176 companyId = address.getCompanyId();
177 classNameId = address.getClassNameId();
178 classPK = address.getClassPK();
179 }
180
181 if ((classNameId == PortalUtil.getClassNameId(Account.class)) ||
182 (classNameId == PortalUtil.getClassNameId(Contact.class)) ||
183 (classNameId == PortalUtil.getClassNameId(Organization.class))) {
184
185 listTypeService.validate(typeId, classNameId, ListTypeImpl.ADDRESS);
186 }
187
188 validate(addressId, companyId, classNameId, classPK, mailing, primary);
189 }
190
191 protected void validate(
192 long addressId, long companyId, long classNameId, long classPK,
193 boolean mailing, boolean primary)
194 throws SystemException {
195
196
199 if (mailing) {
200 Iterator<Address> itr = addressPersistence.findByC_C_C_M(
201 companyId, classNameId, classPK, mailing).iterator();
202
203 while (itr.hasNext()) {
204 Address address = itr.next();
205
206 if ((addressId <= 0) ||
207 (address.getAddressId() != addressId)) {
208
209 address.setMailing(false);
210
211 addressPersistence.update(address, false);
212 }
213 }
214 }
215
216
219 if (primary) {
220 Iterator<Address> itr = addressPersistence.findByC_C_C_P(
221 companyId, classNameId, classPK, primary).iterator();
222
223 while (itr.hasNext()) {
224 Address address = itr.next();
225
226 if ((addressId <= 0) ||
227 (address.getAddressId() != addressId)) {
228
229 address.setPrimary(false);
230
231 addressPersistence.update(address, false);
232 }
233 }
234 }
235 }
236
237 }