1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.service.impl;
21  
22  import com.liferay.portal.AddressCityException;
23  import com.liferay.portal.AddressStreetException;
24  import com.liferay.portal.AddressZipException;
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.model.Account;
29  import com.liferay.portal.model.Address;
30  import com.liferay.portal.model.Contact;
31  import com.liferay.portal.model.Organization;
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             if ((classNameId == PortalUtil.getClassNameId(Account.class)) ||
193                 (classNameId == PortalUtil.getClassNameId(Contact.class)) ||
194                 (classNameId ==
195                     PortalUtil.getClassNameId(Organization.class))) {
196 
197                 listTypeService.validate(
198                     typeId, classNameId, ListTypeImpl.ADDRESS);
199             }
200         }
201         catch (RemoteException re) {
202             throw new SystemException(re);
203         }
204 
205         validate(addressId, companyId, classNameId, classPK, mailing, primary);
206     }
207 
208     protected void validate(
209             long addressId, long companyId, long classNameId, long classPK,
210             boolean mailing, boolean primary)
211         throws SystemException {
212 
213         // Check to make sure there isn't another address with the same company
214         // id, class name, and class pk that also has mailing set to true
215 
216         if (mailing) {
217             Iterator<Address> itr = addressPersistence.findByC_C_C_M(
218                 companyId, classNameId, classPK, mailing).iterator();
219 
220             while (itr.hasNext()) {
221                 Address address = itr.next();
222 
223                 if ((addressId <= 0) ||
224                     (address.getAddressId() != addressId)) {
225 
226                     address.setMailing(false);
227 
228                     addressPersistence.update(address, false);
229                 }
230             }
231         }
232 
233         // Check to make sure there isn't another address with the same company
234         // id, class name, and class pk that also has primary set to true
235 
236         if (primary) {
237             Iterator<Address> itr = addressPersistence.findByC_C_C_P(
238                 companyId, classNameId, classPK, primary).iterator();
239 
240             while (itr.hasNext()) {
241                 Address address = itr.next();
242 
243                 if ((addressId <= 0) ||
244                     (address.getAddressId() != addressId)) {
245 
246                     address.setPrimary(false);
247 
248                     addressPersistence.update(address, false);
249                 }
250             }
251         }
252     }
253 
254 }