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.Address;
32  import com.liferay.portal.model.User;
33  import com.liferay.portal.model.impl.ListTypeImpl;
34  import com.liferay.portal.service.base.AddressLocalServiceBaseImpl;
35  import com.liferay.portal.util.PortalUtil;
36  
37  import java.rmi.RemoteException;
38  
39  import java.util.Date;
40  import java.util.Iterator;
41  import java.util.List;
42  
43  /**
44   * <a href="AddressLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   * @author Alexander Chow
48   *
49   */
50  public class AddressLocalServiceImpl extends AddressLocalServiceBaseImpl {
51  
52      public Address addAddress(
53              long userId, String className, long classPK, String street1,
54              String street2, String street3, String city, String zip,
55              long regionId, long countryId, int typeId, boolean mailing,
56              boolean primary)
57          throws PortalException, SystemException {
58  
59          User user = userPersistence.findByPrimaryKey(userId);
60          long classNameId = PortalUtil.getClassNameId(className);
61          Date now = new Date();
62  
63          validate(
64              0, user.getCompanyId(), classNameId, classPK, street1, city, zip,
65              regionId, countryId, typeId, mailing, primary);
66  
67          long addressId = counterLocalService.increment();
68  
69          Address address = addressPersistence.create(addressId);
70  
71          address.setCompanyId(user.getCompanyId());
72          address.setUserId(user.getUserId());
73          address.setUserName(user.getFullName());
74          address.setCreateDate(now);
75          address.setModifiedDate(now);
76          address.setClassNameId(classNameId);
77          address.setClassPK(classPK);
78          address.setStreet1(street1);
79          address.setStreet2(street2);
80          address.setStreet3(street3);
81          address.setCity(city);
82          address.setZip(zip);
83          address.setRegionId(regionId);
84          address.setCountryId(countryId);
85          address.setTypeId(typeId);
86          address.setMailing(mailing);
87          address.setPrimary(primary);
88  
89          addressPersistence.update(address, false);
90  
91          return address;
92      }
93  
94      public void deleteAddress(long addressId)
95          throws PortalException, SystemException {
96  
97          addressPersistence.remove(addressId);
98      }
99  
100     public void deleteAddresses(long companyId, String className, long classPK)
101         throws SystemException {
102 
103         long classNameId = PortalUtil.getClassNameId(className);
104 
105         addressPersistence.removeByC_C_C(companyId, classNameId, classPK);
106     }
107 
108     public Address getAddress(long addressId)
109         throws PortalException, SystemException {
110 
111         return addressPersistence.findByPrimaryKey(addressId);
112     }
113 
114     public List<Address> getAddresses() throws SystemException {
115         return addressPersistence.findAll();
116     }
117 
118     public List<Address> getAddresses(
119             long companyId, String className, long classPK)
120         throws SystemException {
121 
122         long classNameId = PortalUtil.getClassNameId(className);
123 
124         return addressPersistence.findByC_C_C(companyId, classNameId, classPK);
125     }
126 
127     public Address updateAddress(
128             long addressId, String street1, String street2, String street3,
129             String city, String zip, long regionId, long countryId, int typeId,
130             boolean mailing, boolean primary)
131         throws PortalException, SystemException {
132 
133         validate(
134             addressId, 0, 0, 0, street1, city, zip, regionId, countryId, typeId,
135             mailing, primary);
136 
137         Address address = addressPersistence.findByPrimaryKey(addressId);
138 
139         address.setModifiedDate(new Date());
140         address.setStreet1(street1);
141         address.setStreet2(street2);
142         address.setStreet3(street3);
143         address.setCity(city);
144         address.setZip(zip);
145         address.setRegionId(regionId);
146         address.setCountryId(countryId);
147         address.setTypeId(typeId);
148         address.setMailing(mailing);
149         address.setPrimary(primary);
150 
151         addressPersistence.update(address, false);
152 
153         return address;
154     }
155 
156     protected void validate(
157             long addressId, long companyId, long classNameId, long classPK,
158             String street1, String city, String zip, long regionId,
159             long countryId, int typeId, boolean mailing, boolean primary)
160         throws PortalException, SystemException {
161 
162         if (Validator.isNull(street1)) {
163             throw new AddressStreetException();
164         }
165         else if (Validator.isNull(city)) {
166             throw new AddressCityException();
167         }
168         else if (Validator.isNull(zip)) {
169             throw new AddressZipException();
170         }
171 
172         // Relax region and country requirement. See LEP-978.
173 
174         /*try {
175             RegionServiceUtil.getRegion(regionId);
176 
177             CountryServiceUtil.getCountry(countryId);
178         }
179         catch (RemoteException re) {
180             throw new SystemException(re);
181         }*/
182 
183         if (addressId > 0) {
184             Address address = addressPersistence.findByPrimaryKey(addressId);
185 
186             companyId = address.getCompanyId();
187             classNameId = address.getClassNameId();
188             classPK = address.getClassPK();
189         }
190 
191         try {
192             listTypeService.validate(typeId, classNameId, ListTypeImpl.ADDRESS);
193         }
194         catch (RemoteException re) {
195             throw new SystemException(re);
196         }
197 
198         validate(addressId, companyId, classNameId, classPK, mailing, primary);
199     }
200 
201     protected void validate(
202             long addressId, long companyId, long classNameId, long classPK,
203             boolean mailing, boolean primary)
204         throws PortalException, SystemException {
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 mailing set to true
208 
209         if (mailing) {
210             Iterator<Address> itr = addressPersistence.findByC_C_C_M(
211                 companyId, classNameId, classPK, mailing).iterator();
212 
213             while (itr.hasNext()) {
214                 Address address = itr.next();
215 
216                 if ((addressId <= 0) ||
217                     (address.getAddressId() != addressId)) {
218 
219                     address.setMailing(false);
220 
221                     addressPersistence.update(address, false);
222                 }
223             }
224         }
225 
226         // Check to make sure there isn't another address with the same company
227         // id, class name, and class pk that also has primary set to true
228 
229         if (primary) {
230             Iterator<Address> itr = addressPersistence.findByC_C_C_P(
231                 companyId, classNameId, classPK, primary).iterator();
232 
233             while (itr.hasNext()) {
234                 Address address = itr.next();
235 
236                 if ((addressId <= 0) ||
237                     (address.getAddressId() != addressId)) {
238 
239                     address.setPrimary(false);
240 
241                     addressPersistence.update(address, false);
242                 }
243             }
244         }
245     }
246 
247 }