001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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            public void deleteAddresses(long companyId, String className, long classPK)
086                    throws SystemException {
087    
088                    long classNameId = PortalUtil.getClassNameId(className);
089    
090                    List<Address> addresses = addressPersistence.findByC_C_C(
091                            companyId, classNameId, classPK);
092    
093                    for (Address address : addresses) {
094                            deleteAddress(address);
095                    }
096            }
097    
098            public List<Address> getAddresses() throws SystemException {
099                    return addressPersistence.findAll();
100            }
101    
102            public List<Address> getAddresses(
103                            long companyId, String className, long classPK)
104                    throws SystemException {
105    
106                    long classNameId = PortalUtil.getClassNameId(className);
107    
108                    return addressPersistence.findByC_C_C(companyId, classNameId, classPK);
109            }
110    
111            public Address updateAddress(
112                            long addressId, String street1, String street2, String street3,
113                            String city, String zip, long regionId, long countryId, int typeId,
114                            boolean mailing, boolean primary)
115                    throws PortalException, SystemException {
116    
117                    validate(
118                            addressId, 0, 0, 0, street1, city, zip, regionId, countryId, typeId,
119                            mailing, primary);
120    
121                    Address address = addressPersistence.findByPrimaryKey(addressId);
122    
123                    address.setModifiedDate(new Date());
124                    address.setStreet1(street1);
125                    address.setStreet2(street2);
126                    address.setStreet3(street3);
127                    address.setCity(city);
128                    address.setZip(zip);
129                    address.setRegionId(regionId);
130                    address.setCountryId(countryId);
131                    address.setTypeId(typeId);
132                    address.setMailing(mailing);
133                    address.setPrimary(primary);
134    
135                    addressPersistence.update(address, false);
136    
137                    return address;
138            }
139    
140            protected void validate(
141                            long addressId, long companyId, long classNameId, long classPK,
142                            boolean mailing, boolean primary)
143                    throws SystemException {
144    
145                    // Check to make sure there isn't another address with the same company
146                    // id, class name, and class pk that also has mailing set to true
147    
148                    if (mailing) {
149                            Iterator<Address> itr = addressPersistence.findByC_C_C_M(
150                                    companyId, classNameId, classPK, mailing).iterator();
151    
152                            while (itr.hasNext()) {
153                                    Address address = itr.next();
154    
155                                    if ((addressId <= 0) || (address.getAddressId() != addressId)) {
156                                            address.setMailing(false);
157    
158                                            addressPersistence.update(address, false);
159                                    }
160                            }
161                    }
162    
163                    // Check to make sure there isn't another address with the same company
164                    // id, class name, and class pk that also has primary set to true
165    
166                    if (primary) {
167                            Iterator<Address> itr = addressPersistence.findByC_C_C_P(
168                                    companyId, classNameId, classPK, primary).iterator();
169    
170                            while (itr.hasNext()) {
171                                    Address address = itr.next();
172    
173                                    if ((addressId <= 0) || (address.getAddressId() != addressId)) {
174                                            address.setPrimary(false);
175    
176                                            addressPersistence.update(address, false);
177                                    }
178                            }
179                    }
180            }
181    
182            protected void validate(
183                            long addressId, long companyId, long classNameId, long classPK,
184                            String street1, String city, String zip, long regionId,
185                            long countryId, int typeId, boolean mailing, boolean primary)
186                    throws PortalException, SystemException {
187    
188                    if (Validator.isNull(street1)) {
189                            throw new AddressStreetException();
190                    }
191                    else if (Validator.isNull(city)) {
192                            throw new AddressCityException();
193                    }
194                    else if (Validator.isNull(zip)) {
195                            Country country = countryService.fetchCountry(countryId);
196    
197                            if ((country != null) && country.isZipRequired()) {
198                                    throw new AddressZipException();
199                            }
200                    }
201    
202                    if (addressId > 0) {
203                            Address address = addressPersistence.findByPrimaryKey(addressId);
204    
205                            companyId = address.getCompanyId();
206                            classNameId = address.getClassNameId();
207                            classPK = address.getClassPK();
208                    }
209    
210                    if ((classNameId == PortalUtil.getClassNameId(Account.class)) ||
211                            (classNameId == PortalUtil.getClassNameId(Contact.class)) ||
212                            (classNameId == PortalUtil.getClassNameId(Organization.class))) {
213    
214                            listTypeService.validate(
215                                    typeId, classNameId, ListTypeConstants.ADDRESS);
216                    }
217    
218                    validate(addressId, companyId, classNameId, classPK, mailing, primary);
219            }
220    
221    }