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