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