001    /**
002     * Copyright (c) 2000-2011 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.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.Iterator;
035    import java.util.List;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     * @author Alexander Chow
040     */
041    public class AddressLocalServiceImpl extends AddressLocalServiceBaseImpl {
042    
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, int typeId, boolean mailing,
047                            boolean primary)
048                    throws PortalException, SystemException {
049    
050                    User user = userPersistence.findByPrimaryKey(userId);
051                    long classNameId = PortalUtil.getClassNameId(className);
052                    Date now = new Date();
053    
054                    validate(
055                            0, user.getCompanyId(), classNameId, classPK, street1, city, zip,
056                            regionId, countryId, typeId, mailing, primary);
057    
058                    long addressId = counterLocalService.increment();
059    
060                    Address address = addressPersistence.create(addressId);
061    
062                    address.setCompanyId(user.getCompanyId());
063                    address.setUserId(user.getUserId());
064                    address.setUserName(user.getFullName());
065                    address.setCreateDate(now);
066                    address.setModifiedDate(now);
067                    address.setClassNameId(classNameId);
068                    address.setClassPK(classPK);
069                    address.setStreet1(street1);
070                    address.setStreet2(street2);
071                    address.setStreet3(street3);
072                    address.setCity(city);
073                    address.setZip(zip);
074                    address.setRegionId(regionId);
075                    address.setCountryId(countryId);
076                    address.setTypeId(typeId);
077                    address.setMailing(mailing);
078                    address.setPrimary(primary);
079    
080                    addressPersistence.update(address, false);
081    
082                    return address;
083            }
084    
085            @Override
086            public void deleteAddress(Address address) throws SystemException {
087                    addressPersistence.remove(address);
088            }
089    
090            @Override
091            public void deleteAddress(long addressId)
092                    throws PortalException, SystemException {
093    
094                    Address address = addressPersistence.findByPrimaryKey(addressId);
095    
096                    deleteAddress(address);
097            }
098    
099            public void deleteAddresses(long companyId, String className, long classPK)
100                    throws SystemException {
101    
102                    long classNameId = PortalUtil.getClassNameId(className);
103    
104                    List<Address> addresses = addressPersistence.findByC_C_C(
105                            companyId, classNameId, classPK);
106    
107                    for (Address address : addresses) {
108                            deleteAddress(address);
109                    }
110            }
111    
112            @Override
113            public Address getAddress(long addressId)
114                    throws PortalException, SystemException {
115    
116                    return addressPersistence.findByPrimaryKey(addressId);
117            }
118    
119            public List<Address> getAddresses() throws SystemException {
120                    return addressPersistence.findAll();
121            }
122    
123            public List<Address> getAddresses(
124                            long companyId, String className, long classPK)
125                    throws SystemException {
126    
127                    long classNameId = PortalUtil.getClassNameId(className);
128    
129                    return addressPersistence.findByC_C_C(companyId, classNameId, classPK);
130            }
131    
132            public Address updateAddress(
133                            long addressId, String street1, String street2, String street3,
134                            String city, String zip, long regionId, long countryId, int typeId,
135                            boolean mailing, boolean primary)
136                    throws PortalException, SystemException {
137    
138                    validate(
139                            addressId, 0, 0, 0, street1, city, zip, regionId, countryId, typeId,
140                            mailing, primary);
141    
142                    Address address = addressPersistence.findByPrimaryKey(addressId);
143    
144                    address.setModifiedDate(new Date());
145                    address.setStreet1(street1);
146                    address.setStreet2(street2);
147                    address.setStreet3(street3);
148                    address.setCity(city);
149                    address.setZip(zip);
150                    address.setRegionId(regionId);
151                    address.setCountryId(countryId);
152                    address.setTypeId(typeId);
153                    address.setMailing(mailing);
154                    address.setPrimary(primary);
155    
156                    addressPersistence.update(address, false);
157    
158                    return address;
159            }
160    
161            protected void validate(
162                            long addressId, long companyId, long classNameId, long classPK,
163                            boolean mailing, boolean primary)
164                    throws SystemException {
165    
166                    // Check to make sure there isn't another address with the same company
167                    // id, class name, and class pk that also has mailing set to true
168    
169                    if (mailing) {
170                            Iterator<Address> itr = addressPersistence.findByC_C_C_M(
171                                    companyId, classNameId, classPK, mailing).iterator();
172    
173                            while (itr.hasNext()) {
174                                    Address address = itr.next();
175    
176                                    if ((addressId <= 0) ||
177                                            (address.getAddressId() != addressId)) {
178    
179                                            address.setMailing(false);
180    
181                                            addressPersistence.update(address, false);
182                                    }
183                            }
184                    }
185    
186                    // Check to make sure there isn't another address with the same company
187                    // id, class name, and class pk that also has primary set to true
188    
189                    if (primary) {
190                            Iterator<Address> itr = addressPersistence.findByC_C_C_P(
191                                    companyId, classNameId, classPK, primary).iterator();
192    
193                            while (itr.hasNext()) {
194                                    Address address = itr.next();
195    
196                                    if ((addressId <= 0) ||
197                                            (address.getAddressId() != addressId)) {
198    
199                                            address.setPrimary(false);
200    
201                                            addressPersistence.update(address, false);
202                                    }
203                            }
204                    }
205            }
206    
207            protected void validate(
208                            long addressId, long companyId, long classNameId, long classPK,
209                            String street1, String city, String zip, long regionId,
210                            long countryId, int typeId, boolean mailing, boolean primary)
211                    throws PortalException, SystemException {
212    
213                    if (Validator.isNull(street1)) {
214                            throw new AddressStreetException();
215                    }
216                    else if (Validator.isNull(city)) {
217                            throw new AddressCityException();
218                    }
219                    else if (Validator.isNull(zip)) {
220                            Country country = countryService.fetchCountry(countryId);
221    
222                            if ((country != null) && country.isZipRequired()) {
223                                    throw new AddressZipException();
224                            }
225                    }
226    
227                    if (addressId > 0) {
228                            Address address = addressPersistence.findByPrimaryKey(addressId);
229    
230                            companyId = address.getCompanyId();
231                            classNameId = address.getClassNameId();
232                            classPK = address.getClassPK();
233                    }
234    
235                    if ((classNameId == PortalUtil.getClassNameId(Account.class)) ||
236                            (classNameId == PortalUtil.getClassNameId(Contact.class)) ||
237                            (classNameId == PortalUtil.getClassNameId(Organization.class))) {
238    
239                            listTypeService.validate(
240                                    typeId, classNameId, ListTypeConstants.ADDRESS);
241                    }
242    
243                    validate(addressId, companyId, classNameId, classPK, mailing, primary);
244            }
245    
246    }