1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class AddressLocalServiceImpl extends AddressLocalServiceBaseImpl {
53  
54      public Address addAddress(
55              long userId, String className, long classPK, String street1,
56              String street2, String street3, String city, String zip,
57              long regionId, long countryId, int typeId, boolean mailing,
58              boolean primary)
59          throws PortalException, SystemException {
60  
61          User user = userPersistence.findByPrimaryKey(userId);
62          long classNameId = PortalUtil.getClassNameId(className);
63          Date now = new Date();
64  
65          validate(
66              0, user.getCompanyId(), classNameId, classPK, street1, city, zip,
67              regionId, countryId, typeId, mailing, primary);
68  
69          long addressId = counterLocalService.increment();
70  
71          Address address = addressPersistence.create(addressId);
72  
73          address.setCompanyId(user.getCompanyId());
74          address.setUserId(user.getUserId());
75          address.setUserName(user.getFullName());
76          address.setCreateDate(now);
77          address.setModifiedDate(now);
78          address.setClassNameId(classNameId);
79          address.setClassPK(classPK);
80          address.setStreet1(street1);
81          address.setStreet2(street2);
82          address.setStreet3(street3);
83          address.setCity(city);
84          address.setZip(zip);
85          address.setRegionId(regionId);
86          address.setCountryId(countryId);
87          address.setTypeId(typeId);
88          address.setMailing(mailing);
89          address.setPrimary(primary);
90  
91          addressPersistence.update(address, false);
92  
93          return address;
94      }
95  
96      public void deleteAddress(long addressId)
97          throws PortalException, SystemException {
98  
99          addressPersistence.remove(addressId);
100     }
101 
102     public void deleteAddresses(long companyId, String className, long classPK)
103         throws SystemException {
104 
105         long classNameId = PortalUtil.getClassNameId(className);
106 
107         addressPersistence.removeByC_C_C(companyId, classNameId, classPK);
108     }
109 
110     public Address getAddress(long addressId)
111         throws PortalException, SystemException {
112 
113         return addressPersistence.findByPrimaryKey(addressId);
114     }
115 
116     public List<Address> getAddresses() throws SystemException {
117         return addressPersistence.findAll();
118     }
119 
120     public List<Address> getAddresses(
121             long companyId, String className, long classPK)
122         throws SystemException {
123 
124         long classNameId = PortalUtil.getClassNameId(className);
125 
126         return addressPersistence.findByC_C_C(companyId, classNameId, classPK);
127     }
128 
129     public Address updateAddress(
130             long addressId, String street1, String street2, String street3,
131             String city, String zip, long regionId, long countryId, int typeId,
132             boolean mailing, boolean primary)
133         throws PortalException, SystemException {
134 
135         validate(
136             addressId, 0, 0, 0, street1, city, zip, regionId, countryId, typeId,
137             mailing, primary);
138 
139         Address address = addressPersistence.findByPrimaryKey(addressId);
140 
141         address.setModifiedDate(new Date());
142         address.setStreet1(street1);
143         address.setStreet2(street2);
144         address.setStreet3(street3);
145         address.setCity(city);
146         address.setZip(zip);
147         address.setRegionId(regionId);
148         address.setCountryId(countryId);
149         address.setTypeId(typeId);
150         address.setMailing(mailing);
151         address.setPrimary(primary);
152 
153         addressPersistence.update(address, false);
154 
155         return address;
156     }
157 
158     protected void validate(
159             long addressId, long companyId, long classNameId, long classPK,
160             String street1, String city, String zip, long regionId,
161             long countryId, int typeId, boolean mailing, boolean primary)
162         throws PortalException, SystemException {
163 
164         if (Validator.isNull(street1)) {
165             throw new AddressStreetException();
166         }
167         else if (Validator.isNull(city)) {
168             throw new AddressCityException();
169         }
170         else if (Validator.isNull(zip)) {
171             throw new AddressZipException();
172         }
173 
174         if (addressId > 0) {
175             Address address = addressPersistence.findByPrimaryKey(addressId);
176 
177             companyId = address.getCompanyId();
178             classNameId = address.getClassNameId();
179             classPK = address.getClassPK();
180         }
181 
182         try {
183             if ((classNameId == PortalUtil.getClassNameId(Account.class)) ||
184                 (classNameId == PortalUtil.getClassNameId(Contact.class)) ||
185                 (classNameId ==
186                     PortalUtil.getClassNameId(Organization.class))) {
187 
188                 listTypeService.validate(
189                     typeId, classNameId, ListTypeImpl.ADDRESS);
190             }
191         }
192         catch (RemoteException re) {
193             throw new SystemException(re);
194         }
195 
196         validate(addressId, companyId, classNameId, classPK, mailing, primary);
197     }
198 
199     protected void validate(
200             long addressId, long companyId, long classNameId, long classPK,
201             boolean mailing, boolean primary)
202         throws SystemException {
203 
204         // Check to make sure there isn't another address with the same company
205         // id, class name, and class pk that also has mailing set to true
206 
207         if (mailing) {
208             Iterator<Address> itr = addressPersistence.findByC_C_C_M(
209                 companyId, classNameId, classPK, mailing).iterator();
210 
211             while (itr.hasNext()) {
212                 Address address = itr.next();
213 
214                 if ((addressId <= 0) ||
215                     (address.getAddressId() != addressId)) {
216 
217                     address.setMailing(false);
218 
219                     addressPersistence.update(address, false);
220                 }
221             }
222         }
223 
224         // Check to make sure there isn't another address with the same company
225         // id, class name, and class pk that also has primary set to true
226 
227         if (primary) {
228             Iterator<Address> itr = addressPersistence.findByC_C_C_P(
229                 companyId, classNameId, classPK, primary).iterator();
230 
231             while (itr.hasNext()) {
232                 Address address = itr.next();
233 
234                 if ((addressId <= 0) ||
235                     (address.getAddressId() != addressId)) {
236 
237                     address.setPrimary(false);
238 
239                     addressPersistence.update(address, false);
240                 }
241             }
242         }
243     }
244 
245 }