1   /**
2    * Copyright (c) 2000-2008 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         // Relax region and country requirement. See LEP-978.
176 
177         /*try {
178             RegionServiceUtil.getRegion(regionId);
179 
180             CountryServiceUtil.getCountry(countryId);
181         }
182         catch (RemoteException re) {
183             throw new SystemException(re);
184         }*/
185 
186         if (addressId > 0) {
187             Address address = addressPersistence.findByPrimaryKey(addressId);
188 
189             companyId = address.getCompanyId();
190             classNameId = address.getClassNameId();
191             classPK = address.getClassPK();
192         }
193 
194         try {
195             if ((classNameId == PortalUtil.getClassNameId(Account.class)) ||
196                 (classNameId == PortalUtil.getClassNameId(Contact.class)) ||
197                 (classNameId ==
198                     PortalUtil.getClassNameId(Organization.class))) {
199 
200                 listTypeService.validate(
201                     typeId, classNameId, ListTypeImpl.ADDRESS);
202             }
203         }
204         catch (RemoteException re) {
205             throw new SystemException(re);
206         }
207 
208         validate(addressId, companyId, classNameId, classPK, mailing, primary);
209     }
210 
211     protected void validate(
212             long addressId, long companyId, long classNameId, long classPK,
213             boolean mailing, boolean primary)
214         throws SystemException {
215 
216         // Check to make sure there isn't another address with the same company
217         // id, class name, and class pk that also has mailing set to true
218 
219         if (mailing) {
220             Iterator<Address> itr = addressPersistence.findByC_C_C_M(
221                 companyId, classNameId, classPK, mailing).iterator();
222 
223             while (itr.hasNext()) {
224                 Address address = itr.next();
225 
226                 if ((addressId <= 0) ||
227                     (address.getAddressId() != addressId)) {
228 
229                     address.setMailing(false);
230 
231                     addressPersistence.update(address, false);
232                 }
233             }
234         }
235 
236         // Check to make sure there isn't another address with the same company
237         // id, class name, and class pk that also has primary set to true
238 
239         if (primary) {
240             Iterator<Address> itr = addressPersistence.findByC_C_C_P(
241                 companyId, classNameId, classPK, primary).iterator();
242 
243             while (itr.hasNext()) {
244                 Address address = itr.next();
245 
246                 if ((addressId <= 0) ||
247                     (address.getAddressId() != addressId)) {
248 
249                     address.setPrimary(false);
250 
251                     addressPersistence.update(address, false);
252                 }
253             }
254         }
255     }
256 
257 }