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