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.exception.AddressCityException;
018    import com.liferay.portal.exception.AddressStreetException;
019    import com.liferay.portal.exception.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            @Override
043            public Address addAddress(
044                            long userId, String className, long classPK, String street1,
045                            String street2, String street3, String city, String zip,
046                            long regionId, long countryId, long typeId, boolean mailing,
047                            boolean primary, ServiceContext serviceContext)
048                    throws PortalException {
049    
050                    User user = userPersistence.findByPrimaryKey(userId);
051                    long classNameId = classNameLocalService.getClassNameId(className);
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.setUuid(serviceContext.getUuid());
062                    address.setCompanyId(user.getCompanyId());
063                    address.setUserId(user.getUserId());
064                    address.setUserName(user.getFullName());
065                    address.setClassNameId(classNameId);
066                    address.setClassPK(classPK);
067                    address.setStreet1(street1);
068                    address.setStreet2(street2);
069                    address.setStreet3(street3);
070                    address.setCity(city);
071                    address.setZip(zip);
072                    address.setRegionId(regionId);
073                    address.setCountryId(countryId);
074                    address.setTypeId(typeId);
075                    address.setMailing(mailing);
076                    address.setPrimary(primary);
077    
078                    addressPersistence.update(address);
079    
080                    return address;
081            }
082    
083            @Override
084            @SystemEvent(
085                    action = SystemEventConstants.ACTION_SKIP,
086                    type = SystemEventConstants.TYPE_DELETE
087            )
088            public Address deleteAddress(Address address) {
089                    addressPersistence.remove(address);
090    
091                    return address;
092            }
093    
094            @Override
095            public Address deleteAddress(long addressId) throws PortalException {
096                    Address address = addressPersistence.findByPrimaryKey(addressId);
097    
098                    return addressLocalService.deleteAddress(address);
099            }
100    
101            @Override
102            public void deleteAddresses(
103                    long companyId, String className, long classPK) {
104    
105                    long classNameId = classNameLocalService.getClassNameId(className);
106    
107                    List<Address> addresses = addressPersistence.findByC_C_C(
108                            companyId, classNameId, classPK);
109    
110                    for (Address address : addresses) {
111                            addressLocalService.deleteAddress(address);
112                    }
113            }
114    
115            @Override
116            public List<Address> getAddresses() {
117                    return addressPersistence.findAll();
118            }
119    
120            @Override
121            public List<Address> getAddresses(
122                    long companyId, String className, long classPK) {
123    
124                    long classNameId = classNameLocalService.getClassNameId(className);
125    
126                    return addressPersistence.findByC_C_C(companyId, classNameId, classPK);
127            }
128    
129            @Override
130            public Address updateAddress(
131                            long addressId, String street1, String street2, String street3,
132                            String city, String zip, long regionId, long countryId, long typeId,
133                            boolean mailing, boolean primary)
134                    throws PortalException {
135    
136                    validate(
137                            addressId, 0, 0, 0, street1, city, zip, regionId, countryId, typeId,
138                            mailing, primary);
139    
140                    Address address = addressPersistence.findByPrimaryKey(addressId);
141    
142                    address.setStreet1(street1);
143                    address.setStreet2(street2);
144                    address.setStreet3(street3);
145                    address.setCity(city);
146                    address.setZip(zip);
147                    address.setRegionId(regionId);
148                    address.setCountryId(countryId);
149                    address.setTypeId(typeId);
150                    address.setMailing(mailing);
151                    address.setPrimary(primary);
152    
153                    addressPersistence.update(address);
154    
155                    return address;
156            }
157    
158            protected void validate(
159                    long addressId, long companyId, long classNameId, long classPK,
160                    boolean mailing, boolean primary) {
161    
162                    // Check to make sure there isn't another address with the same company
163                    // id, class name, and class pk that also has mailing set to true
164    
165                    if (mailing) {
166                            List<Address> addresses = addressPersistence.findByC_C_C_M(
167                                    companyId, classNameId, classPK, mailing);
168    
169                            for (Address address : addresses) {
170                                    if ((addressId <= 0) || (address.getAddressId() != addressId)) {
171                                            address.setMailing(false);
172    
173                                            addressPersistence.update(address);
174                                    }
175                            }
176                    }
177    
178                    // Check to make sure there isn't another address with the same company
179                    // id, class name, and class pk that also has primary set to true
180    
181                    if (primary) {
182                            List<Address> addresses = addressPersistence.findByC_C_C_P(
183                                    companyId, classNameId, classPK, primary);
184    
185                            for (Address address : addresses) {
186                                    if ((addressId <= 0) || (address.getAddressId() != addressId)) {
187                                            address.setPrimary(false);
188    
189                                            addressPersistence.update(address);
190                                    }
191                            }
192                    }
193            }
194    
195            protected void validate(
196                            long addressId, long companyId, long classNameId, long classPK,
197                            String street1, String city, String zip, long regionId,
198                            long countryId, long typeId, boolean mailing, boolean primary)
199                    throws PortalException {
200    
201                    if (Validator.isNull(street1)) {
202                            throw new AddressStreetException();
203                    }
204                    else if (Validator.isNull(city)) {
205                            throw new AddressCityException();
206                    }
207                    else if (Validator.isNull(zip)) {
208                            Country country = countryPersistence.fetchByPrimaryKey(countryId);
209    
210                            if ((country != null) && country.isZipRequired()) {
211                                    throw new AddressZipException();
212                            }
213                    }
214    
215                    if (addressId > 0) {
216                            Address address = addressPersistence.findByPrimaryKey(addressId);
217    
218                            companyId = address.getCompanyId();
219                            classNameId = address.getClassNameId();
220                            classPK = address.getClassPK();
221                    }
222    
223                    if ((classNameId ==
224                                    classNameLocalService.getClassNameId(Account.class)) ||
225                            (classNameId ==
226                                    classNameLocalService.getClassNameId(Contact.class)) ||
227                            (classNameId ==
228                                    classNameLocalService.getClassNameId(Organization.class))) {
229    
230                            listTypeLocalService.validate(
231                                    typeId, classNameId, ListTypeConstants.ADDRESS);
232                    }
233    
234                    validate(addressId, companyId, classNameId, classPK, mailing, primary);
235            }
236    
237    }