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