1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.service.impl;
16  
17  import com.liferay.portal.AddressCityException;
18  import com.liferay.portal.AddressStreetException;
19  import com.liferay.portal.AddressZipException;
20  import com.liferay.portal.kernel.exception.PortalException;
21  import com.liferay.portal.kernel.exception.SystemException;
22  import com.liferay.portal.kernel.util.Validator;
23  import com.liferay.portal.model.Account;
24  import com.liferay.portal.model.Address;
25  import com.liferay.portal.model.Contact;
26  import com.liferay.portal.model.ListTypeConstants;
27  import com.liferay.portal.model.Organization;
28  import com.liferay.portal.model.User;
29  import com.liferay.portal.service.base.AddressLocalServiceBaseImpl;
30  import com.liferay.portal.util.PortalUtil;
31  
32  import java.util.Date;
33  import java.util.Iterator;
34  import java.util.List;
35  
36  /**
37   * <a href="AddressLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   * @author Alexander Chow
41   */
42  public class AddressLocalServiceImpl extends AddressLocalServiceBaseImpl {
43  
44      public Address addAddress(
45              long userId, String className, long classPK, String street1,
46              String street2, String street3, String city, String zip,
47              long regionId, long countryId, int typeId, boolean mailing,
48              boolean primary)
49          throws PortalException, SystemException {
50  
51          User user = userPersistence.findByPrimaryKey(userId);
52          long classNameId = PortalUtil.getClassNameId(className);
53          Date now = new Date();
54  
55          validate(
56              0, user.getCompanyId(), classNameId, classPK, street1, city, zip,
57              regionId, countryId, typeId, mailing, primary);
58  
59          long addressId = counterLocalService.increment();
60  
61          Address address = addressPersistence.create(addressId);
62  
63          address.setCompanyId(user.getCompanyId());
64          address.setUserId(user.getUserId());
65          address.setUserName(user.getFullName());
66          address.setCreateDate(now);
67          address.setModifiedDate(now);
68          address.setClassNameId(classNameId);
69          address.setClassPK(classPK);
70          address.setStreet1(street1);
71          address.setStreet2(street2);
72          address.setStreet3(street3);
73          address.setCity(city);
74          address.setZip(zip);
75          address.setRegionId(regionId);
76          address.setCountryId(countryId);
77          address.setTypeId(typeId);
78          address.setMailing(mailing);
79          address.setPrimary(primary);
80  
81          addressPersistence.update(address, false);
82  
83          return address;
84      }
85  
86      public void deleteAddress(long addressId)
87          throws PortalException, SystemException {
88  
89          addressPersistence.remove(addressId);
90      }
91  
92      public void deleteAddresses(long companyId, String className, long classPK)
93          throws SystemException {
94  
95          long classNameId = PortalUtil.getClassNameId(className);
96  
97          addressPersistence.removeByC_C_C(companyId, classNameId, classPK);
98      }
99  
100     public Address getAddress(long addressId)
101         throws PortalException, SystemException {
102 
103         return addressPersistence.findByPrimaryKey(addressId);
104     }
105 
106     public List<Address> getAddresses() throws SystemException {
107         return addressPersistence.findAll();
108     }
109 
110     public List<Address> getAddresses(
111             long companyId, String className, long classPK)
112         throws SystemException {
113 
114         long classNameId = PortalUtil.getClassNameId(className);
115 
116         return addressPersistence.findByC_C_C(companyId, classNameId, classPK);
117     }
118 
119     public Address updateAddress(
120             long addressId, String street1, String street2, String street3,
121             String city, String zip, long regionId, long countryId, int typeId,
122             boolean mailing, boolean primary)
123         throws PortalException, SystemException {
124 
125         validate(
126             addressId, 0, 0, 0, street1, city, zip, regionId, countryId, typeId,
127             mailing, primary);
128 
129         Address address = addressPersistence.findByPrimaryKey(addressId);
130 
131         address.setModifiedDate(new Date());
132         address.setStreet1(street1);
133         address.setStreet2(street2);
134         address.setStreet3(street3);
135         address.setCity(city);
136         address.setZip(zip);
137         address.setRegionId(regionId);
138         address.setCountryId(countryId);
139         address.setTypeId(typeId);
140         address.setMailing(mailing);
141         address.setPrimary(primary);
142 
143         addressPersistence.update(address, false);
144 
145         return address;
146     }
147 
148     protected void validate(
149             long addressId, long companyId, long classNameId, long classPK,
150             String street1, String city, String zip, long regionId,
151             long countryId, int typeId, boolean mailing, boolean primary)
152         throws PortalException, SystemException {
153 
154         if (Validator.isNull(street1)) {
155             throw new AddressStreetException();
156         }
157         else if (Validator.isNull(city)) {
158             throw new AddressCityException();
159         }
160         else if (Validator.isNull(zip)) {
161             throw new AddressZipException();
162         }
163 
164         if (addressId > 0) {
165             Address address = addressPersistence.findByPrimaryKey(addressId);
166 
167             companyId = address.getCompanyId();
168             classNameId = address.getClassNameId();
169             classPK = address.getClassPK();
170         }
171 
172         if ((classNameId == PortalUtil.getClassNameId(Account.class)) ||
173             (classNameId == PortalUtil.getClassNameId(Contact.class)) ||
174             (classNameId == PortalUtil.getClassNameId(Organization.class))) {
175 
176             listTypeService.validate(
177                 typeId, classNameId, ListTypeConstants.ADDRESS);
178         }
179 
180         validate(addressId, companyId, classNameId, classPK, mailing, primary);
181     }
182 
183     protected void validate(
184             long addressId, long companyId, long classNameId, long classPK,
185             boolean mailing, boolean primary)
186         throws SystemException {
187 
188         // Check to make sure there isn't another address with the same company
189         // id, class name, and class pk that also has mailing set to true
190 
191         if (mailing) {
192             Iterator<Address> itr = addressPersistence.findByC_C_C_M(
193                 companyId, classNameId, classPK, mailing).iterator();
194 
195             while (itr.hasNext()) {
196                 Address address = itr.next();
197 
198                 if ((addressId <= 0) ||
199                     (address.getAddressId() != addressId)) {
200 
201                     address.setMailing(false);
202 
203                     addressPersistence.update(address, false);
204                 }
205             }
206         }
207 
208         // Check to make sure there isn't another address with the same company
209         // id, class name, and class pk that also has primary set to true
210 
211         if (primary) {
212             Iterator<Address> itr = addressPersistence.findByC_C_C_P(
213                 companyId, classNameId, classPK, primary).iterator();
214 
215             while (itr.hasNext()) {
216                 Address address = itr.next();
217 
218                 if ((addressId <= 0) ||
219                     (address.getAddressId() != addressId)) {
220 
221                     address.setPrimary(false);
222 
223                     addressPersistence.update(address, false);
224                 }
225             }
226         }
227     }
228 
229 }