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.Address;
32 import com.liferay.portal.model.User;
33 import com.liferay.portal.model.impl.ListTypeImpl;
34 import com.liferay.portal.service.base.AddressLocalServiceBaseImpl;
35 import com.liferay.portal.util.PortalUtil;
36
37 import java.rmi.RemoteException;
38
39 import java.util.Date;
40 import java.util.Iterator;
41 import java.util.List;
42
43
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
174
182
183 if (addressId > 0) {
184 Address address = addressPersistence.findByPrimaryKey(addressId);
185
186 companyId = address.getCompanyId();
187 classNameId = address.getClassNameId();
188 classPK = address.getClassPK();
189 }
190
191 try {
192 listTypeService.validate(typeId, classNameId, ListTypeImpl.ADDRESS);
193 }
194 catch (RemoteException re) {
195 throw new SystemException(re);
196 }
197
198 validate(addressId, companyId, classNameId, classPK, mailing, primary);
199 }
200
201 protected void validate(
202 long addressId, long companyId, long classNameId, long classPK,
203 boolean mailing, boolean primary)
204 throws PortalException, SystemException {
205
206
209 if (mailing) {
210 Iterator<Address> itr = addressPersistence.findByC_C_C_M(
211 companyId, classNameId, classPK, mailing).iterator();
212
213 while (itr.hasNext()) {
214 Address address = itr.next();
215
216 if ((addressId <= 0) ||
217 (address.getAddressId() != addressId)) {
218
219 address.setMailing(false);
220
221 addressPersistence.update(address, false);
222 }
223 }
224 }
225
226
229 if (primary) {
230 Iterator<Address> itr = addressPersistence.findByC_C_C_P(
231 companyId, classNameId, classPK, primary).iterator();
232
233 while (itr.hasNext()) {
234 Address address = itr.next();
235
236 if ((addressId <= 0) ||
237 (address.getAddressId() != addressId)) {
238
239 address.setPrimary(false);
240
241 addressPersistence.update(address, false);
242 }
243 }
244 }
245 }
246
247 }