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