001    /**
002     * Copyright (c) 2000-2010 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.ListTypeConstants;
027    import com.liferay.portal.model.Organization;
028    import com.liferay.portal.model.User;
029    import com.liferay.portal.service.base.AddressLocalServiceBaseImpl;
030    import com.liferay.portal.util.PortalUtil;
031    
032    import java.util.Date;
033    import java.util.Iterator;
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            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, false);
080    
081                    return address;
082            }
083    
084            public void deleteAddress(long addressId)
085                    throws PortalException, SystemException {
086    
087                    addressPersistence.remove(addressId);
088            }
089    
090            public void deleteAddresses(long companyId, String className, long classPK)
091                    throws SystemException {
092    
093                    long classNameId = PortalUtil.getClassNameId(className);
094    
095                    addressPersistence.removeByC_C_C(companyId, classNameId, classPK);
096            }
097    
098            public Address getAddress(long addressId)
099                    throws PortalException, SystemException {
100    
101                    return addressPersistence.findByPrimaryKey(addressId);
102            }
103    
104            public List<Address> getAddresses() throws SystemException {
105                    return addressPersistence.findAll();
106            }
107    
108            public List<Address> getAddresses(
109                            long companyId, String className, long classPK)
110                    throws SystemException {
111    
112                    long classNameId = PortalUtil.getClassNameId(className);
113    
114                    return addressPersistence.findByC_C_C(companyId, classNameId, classPK);
115            }
116    
117            public Address updateAddress(
118                            long addressId, String street1, String street2, String street3,
119                            String city, String zip, long regionId, long countryId, int typeId,
120                            boolean mailing, boolean primary)
121                    throws PortalException, SystemException {
122    
123                    validate(
124                            addressId, 0, 0, 0, street1, city, zip, regionId, countryId, typeId,
125                            mailing, primary);
126    
127                    Address address = addressPersistence.findByPrimaryKey(addressId);
128    
129                    address.setModifiedDate(new Date());
130                    address.setStreet1(street1);
131                    address.setStreet2(street2);
132                    address.setStreet3(street3);
133                    address.setCity(city);
134                    address.setZip(zip);
135                    address.setRegionId(regionId);
136                    address.setCountryId(countryId);
137                    address.setTypeId(typeId);
138                    address.setMailing(mailing);
139                    address.setPrimary(primary);
140    
141                    addressPersistence.update(address, false);
142    
143                    return address;
144            }
145    
146            protected void validate(
147                            long addressId, long companyId, long classNameId, long classPK,
148                            String street1, String city, String zip, long regionId,
149                            long countryId, int typeId, boolean mailing, boolean primary)
150                    throws PortalException, SystemException {
151    
152                    if (Validator.isNull(street1)) {
153                            throw new AddressStreetException();
154                    }
155                    else if (Validator.isNull(city)) {
156                            throw new AddressCityException();
157                    }
158                    else if (Validator.isNull(zip)) {
159                            throw new AddressZipException();
160                    }
161    
162                    if (addressId > 0) {
163                            Address address = addressPersistence.findByPrimaryKey(addressId);
164    
165                            companyId = address.getCompanyId();
166                            classNameId = address.getClassNameId();
167                            classPK = address.getClassPK();
168                    }
169    
170                    if ((classNameId == PortalUtil.getClassNameId(Account.class)) ||
171                            (classNameId == PortalUtil.getClassNameId(Contact.class)) ||
172                            (classNameId == PortalUtil.getClassNameId(Organization.class))) {
173    
174                            listTypeService.validate(
175                                    typeId, classNameId, ListTypeConstants.ADDRESS);
176                    }
177    
178                    validate(addressId, companyId, classNameId, classPK, mailing, primary);
179            }
180    
181            protected void validate(
182                            long addressId, long companyId, long classNameId, long classPK,
183                            boolean mailing, boolean primary)
184                    throws SystemException {
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 mailing set to true
188    
189                    if (mailing) {
190                            Iterator<Address> itr = addressPersistence.findByC_C_C_M(
191                                    companyId, classNameId, classPK, mailing).iterator();
192    
193                            while (itr.hasNext()) {
194                                    Address address = itr.next();
195    
196                                    if ((addressId <= 0) ||
197                                            (address.getAddressId() != addressId)) {
198    
199                                            address.setMailing(false);
200    
201                                            addressPersistence.update(address, false);
202                                    }
203                            }
204                    }
205    
206                    // Check to make sure there isn't another address with the same company
207                    // id, class name, and class pk that also has primary set to true
208    
209                    if (primary) {
210                            Iterator<Address> itr = addressPersistence.findByC_C_C_P(
211                                    companyId, classNameId, classPK, primary).iterator();
212    
213                            while (itr.hasNext()) {
214                                    Address address = itr.next();
215    
216                                    if ((addressId <= 0) ||
217                                            (address.getAddressId() != addressId)) {
218    
219                                            address.setPrimary(false);
220    
221                                            addressPersistence.update(address, false);
222                                    }
223                            }
224                    }
225            }
226    
227    }