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