001
014
015 package com.liferay.portal.service.impl;
016
017 import com.liferay.portal.AddressCityException;
018 import com.liferay.portal.AddressStreetException;
019 import com.liferay.portal.AddressZipException;
020 import com.liferay.portal.kernel.exception.PortalException;
021 import com.liferay.portal.kernel.exception.SystemException;
022 import com.liferay.portal.kernel.util.Validator;
023 import com.liferay.portal.model.Account;
024 import com.liferay.portal.model.Address;
025 import com.liferay.portal.model.Contact;
026 import com.liferay.portal.model.Country;
027 import com.liferay.portal.model.ListTypeConstants;
028 import com.liferay.portal.model.Organization;
029 import com.liferay.portal.model.User;
030 import com.liferay.portal.service.base.AddressLocalServiceBaseImpl;
031 import com.liferay.portal.util.PortalUtil;
032
033 import java.util.Date;
034 import java.util.Iterator;
035 import java.util.List;
036
037
041 public class AddressLocalServiceImpl extends AddressLocalServiceBaseImpl {
042
043 public Address addAddress(
044 long userId, String className, long classPK, String street1,
045 String street2, String street3, String city, String zip,
046 long regionId, long countryId, int typeId, boolean mailing,
047 boolean primary)
048 throws PortalException, SystemException {
049
050 User user = userPersistence.findByPrimaryKey(userId);
051 long classNameId = PortalUtil.getClassNameId(className);
052 Date now = new Date();
053
054 validate(
055 0, user.getCompanyId(), classNameId, classPK, street1, city, zip,
056 regionId, countryId, typeId, mailing, primary);
057
058 long addressId = counterLocalService.increment();
059
060 Address address = addressPersistence.create(addressId);
061
062 address.setCompanyId(user.getCompanyId());
063 address.setUserId(user.getUserId());
064 address.setUserName(user.getFullName());
065 address.setCreateDate(now);
066 address.setModifiedDate(now);
067 address.setClassNameId(classNameId);
068 address.setClassPK(classPK);
069 address.setStreet1(street1);
070 address.setStreet2(street2);
071 address.setStreet3(street3);
072 address.setCity(city);
073 address.setZip(zip);
074 address.setRegionId(regionId);
075 address.setCountryId(countryId);
076 address.setTypeId(typeId);
077 address.setMailing(mailing);
078 address.setPrimary(primary);
079
080 addressPersistence.update(address, false);
081
082 return address;
083 }
084
085 @Override
086 public void deleteAddress(Address address) throws SystemException {
087 addressPersistence.remove(address);
088 }
089
090 @Override
091 public void deleteAddress(long addressId)
092 throws PortalException, SystemException {
093
094 Address address = addressPersistence.findByPrimaryKey(addressId);
095
096 deleteAddress(address);
097 }
098
099 public void deleteAddresses(long companyId, String className, long classPK)
100 throws SystemException {
101
102 long classNameId = PortalUtil.getClassNameId(className);
103
104 List<Address> addresses = addressPersistence.findByC_C_C(
105 companyId, classNameId, classPK);
106
107 for (Address address : addresses) {
108 deleteAddress(address);
109 }
110 }
111
112 @Override
113 public Address getAddress(long addressId)
114 throws PortalException, SystemException {
115
116 return addressPersistence.findByPrimaryKey(addressId);
117 }
118
119 public List<Address> getAddresses() throws SystemException {
120 return addressPersistence.findAll();
121 }
122
123 public List<Address> getAddresses(
124 long companyId, String className, long classPK)
125 throws SystemException {
126
127 long classNameId = PortalUtil.getClassNameId(className);
128
129 return addressPersistence.findByC_C_C(companyId, classNameId, classPK);
130 }
131
132 public Address updateAddress(
133 long addressId, String street1, String street2, String street3,
134 String city, String zip, long regionId, long countryId, int typeId,
135 boolean mailing, boolean primary)
136 throws PortalException, SystemException {
137
138 validate(
139 addressId, 0, 0, 0, street1, city, zip, regionId, countryId, typeId,
140 mailing, primary);
141
142 Address address = addressPersistence.findByPrimaryKey(addressId);
143
144 address.setModifiedDate(new Date());
145 address.setStreet1(street1);
146 address.setStreet2(street2);
147 address.setStreet3(street3);
148 address.setCity(city);
149 address.setZip(zip);
150 address.setRegionId(regionId);
151 address.setCountryId(countryId);
152 address.setTypeId(typeId);
153 address.setMailing(mailing);
154 address.setPrimary(primary);
155
156 addressPersistence.update(address, false);
157
158 return address;
159 }
160
161 protected void validate(
162 long addressId, long companyId, long classNameId, long classPK,
163 boolean mailing, boolean primary)
164 throws SystemException {
165
166
167
168
169 if (mailing) {
170 Iterator<Address> itr = addressPersistence.findByC_C_C_M(
171 companyId, classNameId, classPK, mailing).iterator();
172
173 while (itr.hasNext()) {
174 Address address = itr.next();
175
176 if ((addressId <= 0) || (address.getAddressId() != addressId)) {
177 address.setMailing(false);
178
179 addressPersistence.update(address, false);
180 }
181 }
182 }
183
184
185
186
187 if (primary) {
188 Iterator<Address> itr = addressPersistence.findByC_C_C_P(
189 companyId, classNameId, classPK, primary).iterator();
190
191 while (itr.hasNext()) {
192 Address address = itr.next();
193
194 if ((addressId <= 0) || (address.getAddressId() != addressId)) {
195 address.setPrimary(false);
196
197 addressPersistence.update(address, false);
198 }
199 }
200 }
201 }
202
203 protected void validate(
204 long addressId, long companyId, long classNameId, long classPK,
205 String street1, String city, String zip, long regionId,
206 long countryId, int typeId, boolean mailing, boolean primary)
207 throws PortalException, SystemException {
208
209 if (Validator.isNull(street1)) {
210 throw new AddressStreetException();
211 }
212 else if (Validator.isNull(city)) {
213 throw new AddressCityException();
214 }
215 else if (Validator.isNull(zip)) {
216 Country country = countryService.fetchCountry(countryId);
217
218 if ((country != null) && country.isZipRequired()) {
219 throw new AddressZipException();
220 }
221 }
222
223 if (addressId > 0) {
224 Address address = addressPersistence.findByPrimaryKey(addressId);
225
226 companyId = address.getCompanyId();
227 classNameId = address.getClassNameId();
228 classPK = address.getClassPK();
229 }
230
231 if ((classNameId == PortalUtil.getClassNameId(Account.class)) ||
232 (classNameId == PortalUtil.getClassNameId(Contact.class)) ||
233 (classNameId == PortalUtil.getClassNameId(Organization.class))) {
234
235 listTypeService.validate(
236 typeId, classNameId, ListTypeConstants.ADDRESS);
237 }
238
239 validate(addressId, companyId, classNameId, classPK, mailing, primary);
240 }
241
242 }