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);
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 getAddresses() throws SystemException {
115 return addressPersistence.findAll();
116 }
117
118 public List getAddresses(long companyId, String className, long classPK)
119 throws SystemException {
120
121 long classNameId = PortalUtil.getClassNameId(className);
122
123 return addressPersistence.findByC_C_C(companyId, classNameId, classPK);
124 }
125
126 public Address updateAddress(
127 long addressId, String street1, String street2, String street3,
128 String city, String zip, long regionId, long countryId, int typeId,
129 boolean mailing, boolean primary)
130 throws PortalException, SystemException {
131
132 validate(
133 addressId, 0, 0, 0, street1, city, zip, regionId, countryId, typeId,
134 mailing, primary);
135
136 Address address = addressPersistence.findByPrimaryKey(addressId);
137
138 address.setModifiedDate(new Date());
139 address.setStreet1(street1);
140 address.setStreet2(street2);
141 address.setStreet3(street3);
142 address.setCity(city);
143 address.setZip(zip);
144 address.setRegionId(regionId);
145 address.setCountryId(countryId);
146 address.setTypeId(typeId);
147 address.setMailing(mailing);
148 address.setPrimary(primary);
149
150 addressPersistence.update(address);
151
152 return address;
153 }
154
155 protected void validate(
156 long addressId, long companyId, long classNameId, long classPK,
157 String street1, String city, String zip, long regionId,
158 long countryId, int typeId, boolean mailing, boolean primary)
159 throws PortalException, SystemException {
160
161 if (Validator.isNull(street1)) {
162 throw new AddressStreetException();
163 }
164 else if (Validator.isNull(city)) {
165 throw new AddressCityException();
166 }
167 else if (Validator.isNull(zip)) {
168 throw new AddressZipException();
169 }
170
171
173
181
182 if (addressId > 0) {
183 Address address = addressPersistence.findByPrimaryKey(addressId);
184
185 companyId = address.getCompanyId();
186 classNameId = address.getClassNameId();
187 classPK = address.getClassPK();
188 }
189
190 try {
191 listTypeService.validate(typeId, classNameId, ListTypeImpl.ADDRESS);
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 PortalException, SystemException {
204
205
208 if (mailing) {
209 Iterator itr = addressPersistence.findByC_C_C_M(
210 companyId, classNameId, classPK, mailing).iterator();
211
212 while (itr.hasNext()) {
213 Address address = (Address)itr.next();
214
215 if ((addressId <= 0) ||
216 (address.getAddressId() != addressId)) {
217
218 address.setMailing(false);
219
220 addressPersistence.update(address);
221 }
222 }
223 }
224
225
228 if (primary) {
229 Iterator itr = addressPersistence.findByC_C_C_P(
230 companyId, classNameId, classPK, primary).iterator();
231
232 while (itr.hasNext()) {
233 Address address = (Address)itr.next();
234
235 if ((addressId <= 0) ||
236 (address.getAddressId() != addressId)) {
237
238 address.setPrimary(false);
239
240 addressPersistence.update(address);
241 }
242 }
243 }
244 }
245
246 }