1
19
20 package com.liferay.portal.service.impl;
21
22 import com.liferay.portal.AddressCityException;
23 import com.liferay.portal.AddressStreetException;
24 import com.liferay.portal.AddressZipException;
25 import com.liferay.portal.PortalException;
26 import com.liferay.portal.SystemException;
27 import com.liferay.portal.kernel.util.Validator;
28 import com.liferay.portal.model.Account;
29 import com.liferay.portal.model.Address;
30 import com.liferay.portal.model.Contact;
31 import com.liferay.portal.model.Organization;
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 if (addressId > 0) {
173 Address address = addressPersistence.findByPrimaryKey(addressId);
174
175 companyId = address.getCompanyId();
176 classNameId = address.getClassNameId();
177 classPK = address.getClassPK();
178 }
179
180 try {
181 if ((classNameId == PortalUtil.getClassNameId(Account.class)) ||
182 (classNameId == PortalUtil.getClassNameId(Contact.class)) ||
183 (classNameId ==
184 PortalUtil.getClassNameId(Organization.class))) {
185
186 listTypeService.validate(
187 typeId, classNameId, ListTypeImpl.ADDRESS);
188 }
189 }
190 catch (RemoteException re) {
191 throw new SystemException(re);
192 }
193
194 validate(addressId, companyId, classNameId, classPK, mailing, primary);
195 }
196
197 protected void validate(
198 long addressId, long companyId, long classNameId, long classPK,
199 boolean mailing, boolean primary)
200 throws SystemException {
201
202
205 if (mailing) {
206 Iterator<Address> itr = addressPersistence.findByC_C_C_M(
207 companyId, classNameId, classPK, mailing).iterator();
208
209 while (itr.hasNext()) {
210 Address address = itr.next();
211
212 if ((addressId <= 0) ||
213 (address.getAddressId() != addressId)) {
214
215 address.setMailing(false);
216
217 addressPersistence.update(address, false);
218 }
219 }
220 }
221
222
225 if (primary) {
226 Iterator<Address> itr = addressPersistence.findByC_C_C_P(
227 companyId, classNameId, classPK, primary).iterator();
228
229 while (itr.hasNext()) {
230 Address address = itr.next();
231
232 if ((addressId <= 0) ||
233 (address.getAddressId() != addressId)) {
234
235 address.setPrimary(false);
236
237 addressPersistence.update(address, false);
238 }
239 }
240 }
241 }
242
243 }