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