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