001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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.List;
035    
036    /**
037     * @author Brian Wing Shun Chan
038     * @author Alexander Chow
039     */
040    public class AddressLocalServiceImpl extends AddressLocalServiceBaseImpl {
041    
042            /**
043             * @deprecated As of 6.2.0, replaced by {@link #addAddress(long, String,
044             *             long, String, String, String, String, String, long, long,
045             *             int, boolean, boolean, ServiceContext)}
046             */
047            @Deprecated
048            @Override
049            public Address addAddress(
050                            long userId, String className, long classPK, String street1,
051                            String street2, String street3, String city, String zip,
052                            long regionId, long countryId, long typeId, boolean mailing,
053                            boolean primary)
054                    throws PortalException {
055    
056                    return addAddress(
057                            userId, className, classPK, street1, street2, street3, city, zip,
058                            regionId, countryId, typeId, mailing, primary,
059                            new ServiceContext());
060            }
061    
062            @Override
063            public Address addAddress(
064                            long userId, String className, long classPK, String street1,
065                            String street2, String street3, String city, String zip,
066                            long regionId, long countryId, long typeId, boolean mailing,
067                            boolean primary, ServiceContext serviceContext)
068                    throws PortalException {
069    
070                    User user = userPersistence.findByPrimaryKey(userId);
071                    long classNameId = classNameLocalService.getClassNameId(className);
072    
073                    validate(
074                            0, user.getCompanyId(), classNameId, classPK, street1, city, zip,
075                            regionId, countryId, typeId, mailing, primary);
076    
077                    long addressId = counterLocalService.increment();
078    
079                    Address address = addressPersistence.create(addressId);
080    
081                    address.setUuid(serviceContext.getUuid());
082                    address.setCompanyId(user.getCompanyId());
083                    address.setUserId(user.getUserId());
084                    address.setUserName(user.getFullName());
085                    address.setClassNameId(classNameId);
086                    address.setClassPK(classPK);
087                    address.setStreet1(street1);
088                    address.setStreet2(street2);
089                    address.setStreet3(street3);
090                    address.setCity(city);
091                    address.setZip(zip);
092                    address.setRegionId(regionId);
093                    address.setCountryId(countryId);
094                    address.setTypeId(typeId);
095                    address.setMailing(mailing);
096                    address.setPrimary(primary);
097    
098                    addressPersistence.update(address);
099    
100                    return address;
101            }
102    
103            @Override
104            @SystemEvent(
105                    action = SystemEventConstants.ACTION_SKIP,
106                    type = SystemEventConstants.TYPE_DELETE
107            )
108            public Address deleteAddress(Address address) {
109                    addressPersistence.remove(address);
110    
111                    return address;
112            }
113    
114            @Override
115            public Address deleteAddress(long addressId) throws PortalException {
116                    Address address = addressPersistence.findByPrimaryKey(addressId);
117    
118                    return addressLocalService.deleteAddress(address);
119            }
120    
121            @Override
122            public void deleteAddresses(
123                    long companyId, String className, long classPK) {
124    
125                    long classNameId = classNameLocalService.getClassNameId(className);
126    
127                    List<Address> addresses = addressPersistence.findByC_C_C(
128                            companyId, classNameId, classPK);
129    
130                    for (Address address : addresses) {
131                            addressLocalService.deleteAddress(address);
132                    }
133            }
134    
135            @Override
136            public List<Address> getAddresses() {
137                    return addressPersistence.findAll();
138            }
139    
140            @Override
141            public List<Address> getAddresses(
142                    long companyId, String className, long classPK) {
143    
144                    long classNameId = classNameLocalService.getClassNameId(className);
145    
146                    return addressPersistence.findByC_C_C(companyId, classNameId, classPK);
147            }
148    
149            @Override
150            public Address updateAddress(
151                            long addressId, String street1, String street2, String street3,
152                            String city, String zip, long regionId, long countryId, long typeId,
153                            boolean mailing, boolean primary)
154                    throws PortalException {
155    
156                    validate(
157                            addressId, 0, 0, 0, street1, city, zip, regionId, countryId, typeId,
158                            mailing, primary);
159    
160                    Address address = addressPersistence.findByPrimaryKey(addressId);
161    
162                    address.setStreet1(street1);
163                    address.setStreet2(street2);
164                    address.setStreet3(street3);
165                    address.setCity(city);
166                    address.setZip(zip);
167                    address.setRegionId(regionId);
168                    address.setCountryId(countryId);
169                    address.setTypeId(typeId);
170                    address.setMailing(mailing);
171                    address.setPrimary(primary);
172    
173                    addressPersistence.update(address);
174    
175                    return address;
176            }
177    
178            protected void validate(
179                    long addressId, long companyId, long classNameId, long classPK,
180                    boolean mailing, boolean primary) {
181    
182                    // Check to make sure there isn't another address with the same company
183                    // id, class name, and class pk that also has mailing set to true
184    
185                    if (mailing) {
186                            List<Address> addresses = addressPersistence.findByC_C_C_M(
187                                    companyId, classNameId, classPK, mailing);
188    
189                            for (Address address : addresses) {
190                                    if ((addressId <= 0) || (address.getAddressId() != addressId)) {
191                                            address.setMailing(false);
192    
193                                            addressPersistence.update(address);
194                                    }
195                            }
196                    }
197    
198                    // Check to make sure there isn't another address with the same company
199                    // id, class name, and class pk that also has primary set to true
200    
201                    if (primary) {
202                            List<Address> addresses = addressPersistence.findByC_C_C_P(
203                                    companyId, classNameId, classPK, primary);
204    
205                            for (Address address : addresses) {
206                                    if ((addressId <= 0) || (address.getAddressId() != addressId)) {
207                                            address.setPrimary(false);
208    
209                                            addressPersistence.update(address);
210                                    }
211                            }
212                    }
213            }
214    
215            protected void validate(
216                            long addressId, long companyId, long classNameId, long classPK,
217                            String street1, String city, String zip, long regionId,
218                            long countryId, long typeId, boolean mailing, boolean primary)
219                    throws PortalException {
220    
221                    if (Validator.isNull(street1)) {
222                            throw new AddressStreetException();
223                    }
224                    else if (Validator.isNull(city)) {
225                            throw new AddressCityException();
226                    }
227                    else if (Validator.isNull(zip)) {
228                            Country country = countryPersistence.fetchByPrimaryKey(countryId);
229    
230                            if ((country != null) && country.isZipRequired()) {
231                                    throw new AddressZipException();
232                            }
233                    }
234    
235                    if (addressId > 0) {
236                            Address address = addressPersistence.findByPrimaryKey(addressId);
237    
238                            companyId = address.getCompanyId();
239                            classNameId = address.getClassNameId();
240                            classPK = address.getClassPK();
241                    }
242    
243                    if ((classNameId ==
244                                    classNameLocalService.getClassNameId(Account.class)) ||
245                            (classNameId ==
246                                    classNameLocalService.getClassNameId(Contact.class)) ||
247                            (classNameId ==
248                                    classNameLocalService.getClassNameId(Organization.class))) {
249    
250                            listTypeLocalService.validate(
251                                    typeId, classNameId, ListTypeConstants.ADDRESS);
252                    }
253    
254                    validate(addressId, companyId, classNameId, classPK, mailing, primary);
255            }
256    
257    }