1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.service.impl;
24  
25  import com.liferay.portal.AddressCityException;
26  import com.liferay.portal.AddressStreetException;
27  import com.liferay.portal.AddressZipException;
28  import com.liferay.portal.PortalException;
29  import com.liferay.portal.SystemException;
30  import com.liferay.portal.kernel.util.Validator;
31  import com.liferay.portal.model.Account;
32  import com.liferay.portal.model.Address;
33  import com.liferay.portal.model.Contact;
34  import com.liferay.portal.model.Organization;
35  import com.liferay.portal.model.User;
36  import com.liferay.portal.model.impl.ListTypeImpl;
37  import com.liferay.portal.service.base.AddressLocalServiceBaseImpl;
38  import com.liferay.portal.util.PortalUtil;
39  
40  import java.rmi.RemoteException;
41  
42  import java.util.Date;
43  import java.util.Iterator;
44  import java.util.List;
45  
46  /**
47   * <a href="AddressLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
48   *
49   * @author Brian Wing Shun Chan
50   * @author Alexander Chow
51   *
52   */
53  public class AddressLocalServiceImpl extends AddressLocalServiceBaseImpl {
54  
55      public Address addAddress(
56              long userId, String className, long classPK, String street1,
57              String street2, String street3, String city, String zip,
58              long regionId, long countryId, int typeId, boolean mailing,
59              boolean primary)
60          throws PortalException, SystemException {
61  
62          User user = userPersistence.findByPrimaryKey(userId);
63          long classNameId = PortalUtil.getClassNameId(className);
64          Date now = new Date();
65  
66          validate(
67              0, user.getCompanyId(), classNameId, classPK, street1, city, zip,
68              regionId, countryId, typeId, mailing, primary);
69  
70          long addressId = counterLocalService.increment();
71  
72          Address address = addressPersistence.create(addressId);
73  
74          address.setCompanyId(user.getCompanyId());
75          address.setUserId(user.getUserId());
76          address.setUserName(user.getFullName());
77          address.setCreateDate(now);
78          address.setModifiedDate(now);
79          address.setClassNameId(classNameId);
80          address.setClassPK(classPK);
81          address.setStreet1(street1);
82          address.setStreet2(street2);
83          address.setStreet3(street3);
84          address.setCity(city);
85          address.setZip(zip);
86          address.setRegionId(regionId);
87          address.setCountryId(countryId);
88          address.setTypeId(typeId);
89          address.setMailing(mailing);
90          address.setPrimary(primary);
91  
92          addressPersistence.update(address, false);
93  
94          return address;
95      }
96  
97      public void deleteAddress(long addressId)
98          throws PortalException, SystemException {
99  
100         addressPersistence.remove(addressId);
101     }
102 
103     public void deleteAddresses(long companyId, String className, long classPK)
104         throws SystemException {
105 
106         long classNameId = PortalUtil.getClassNameId(className);
107 
108         addressPersistence.removeByC_C_C(companyId, classNameId, classPK);
109     }
110 
111     public Address getAddress(long addressId)
112         throws PortalException, SystemException {
113 
114         return addressPersistence.findByPrimaryKey(addressId);
115     }
116 
117     public List<Address> getAddresses() throws SystemException {
118         return addressPersistence.findAll();
119     }
120 
121     public List<Address> getAddresses(
122             long companyId, String className, long classPK)
123         throws SystemException {
124 
125         long classNameId = PortalUtil.getClassNameId(className);
126 
127         return addressPersistence.findByC_C_C(companyId, classNameId, classPK);
128     }
129 
130     public Address updateAddress(
131             long addressId, String street1, String street2, String street3,
132             String city, String zip, long regionId, long countryId, int typeId,
133             boolean mailing, boolean primary)
134         throws PortalException, SystemException {
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.setModifiedDate(new Date());
143         address.setStreet1(street1);
144         address.setStreet2(street2);
145         address.setStreet3(street3);
146         address.setCity(city);
147         address.setZip(zip);
148         address.setRegionId(regionId);
149         address.setCountryId(countryId);
150         address.setTypeId(typeId);
151         address.setMailing(mailing);
152         address.setPrimary(primary);
153 
154         addressPersistence.update(address, false);
155 
156         return address;
157     }
158 
159     protected void validate(
160             long addressId, long companyId, long classNameId, long classPK,
161             String street1, String city, String zip, long regionId,
162             long countryId, int typeId, boolean mailing, boolean primary)
163         throws PortalException, SystemException {
164 
165         if (Validator.isNull(street1)) {
166             throw new AddressStreetException();
167         }
168         else if (Validator.isNull(city)) {
169             throw new AddressCityException();
170         }
171         else if (Validator.isNull(zip)) {
172             throw new AddressZipException();
173         }
174 
175         if (addressId > 0) {
176             Address address = addressPersistence.findByPrimaryKey(addressId);
177 
178             companyId = address.getCompanyId();
179             classNameId = address.getClassNameId();
180             classPK = address.getClassPK();
181         }
182 
183         try {
184             if ((classNameId == PortalUtil.getClassNameId(Account.class)) ||
185                 (classNameId == PortalUtil.getClassNameId(Contact.class)) ||
186                 (classNameId ==
187                     PortalUtil.getClassNameId(Organization.class))) {
188 
189                 listTypeService.validate(
190                     typeId, classNameId, ListTypeImpl.ADDRESS);
191             }
192         }
193         catch (RemoteException re) {
194             throw new SystemException(re);
195         }
196 
197         validate(addressId, companyId, classNameId, classPK, mailing, primary);
198     }
199 
200     protected void validate(
201             long addressId, long companyId, long classNameId, long classPK,
202             boolean mailing, boolean primary)
203         throws SystemException {
204 
205         // Check to make sure there isn't another address with the same company
206         // id, class name, and class pk that also has mailing set to true
207 
208         if (mailing) {
209             Iterator<Address> itr = addressPersistence.findByC_C_C_M(
210                 companyId, classNameId, classPK, mailing).iterator();
211 
212             while (itr.hasNext()) {
213                 Address address = itr.next();
214 
215                 if ((addressId <= 0) ||
216                     (address.getAddressId() != addressId)) {
217 
218                     address.setMailing(false);
219 
220                     addressPersistence.update(address, false);
221                 }
222             }
223         }
224 
225         // Check to make sure there isn't another address with the same company
226         // id, class name, and class pk that also has primary set to true
227 
228         if (primary) {
229             Iterator<Address> itr = addressPersistence.findByC_C_C_P(
230                 companyId, classNameId, classPK, primary).iterator();
231 
232             while (itr.hasNext()) {
233                 Address address = itr.next();
234 
235                 if ((addressId <= 0) ||
236                     (address.getAddressId() != addressId)) {
237 
238                     address.setPrimary(false);
239 
240                     addressPersistence.update(address, false);
241                 }
242             }
243         }
244     }
245 
246 }