1   /**
2    * Copyright (c) 2000-2009 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.EmailAddressException;
26  import com.liferay.portal.PortalException;
27  import com.liferay.portal.SystemException;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.model.EmailAddress;
30  import com.liferay.portal.model.User;
31  import com.liferay.portal.model.impl.ListTypeImpl;
32  import com.liferay.portal.service.base.EmailAddressLocalServiceBaseImpl;
33  import com.liferay.portal.util.PortalUtil;
34  
35  import java.util.Date;
36  import java.util.Iterator;
37  import java.util.List;
38  
39  /**
40   * <a href="EmailAddressLocalServiceImpl.java.html"><b><i>View Source</i></b>
41   * </a>
42   *
43   * @author Brian Wing Shun Chan
44   * @author Alexander Chow
45   *
46   */
47  public class EmailAddressLocalServiceImpl
48      extends EmailAddressLocalServiceBaseImpl {
49  
50      public EmailAddress addEmailAddress(
51              long userId, String className, long classPK, String address,
52              int typeId, boolean primary)
53          throws PortalException, SystemException {
54  
55          User user = userPersistence.findByPrimaryKey(userId);
56          long classNameId = PortalUtil.getClassNameId(className);
57          Date now = new Date();
58  
59          validate(
60              0, user.getCompanyId(), classNameId, classPK, address, typeId,
61              primary);
62  
63          long emailAddressId = counterLocalService.increment();
64  
65          EmailAddress emailAddress = emailAddressPersistence.create(
66              emailAddressId);
67  
68          emailAddress.setCompanyId(user.getCompanyId());
69          emailAddress.setUserId(user.getUserId());
70          emailAddress.setUserName(user.getFullName());
71          emailAddress.setCreateDate(now);
72          emailAddress.setModifiedDate(now);
73          emailAddress.setClassNameId(classNameId);
74          emailAddress.setClassPK(classPK);
75          emailAddress.setAddress(address);
76          emailAddress.setTypeId(typeId);
77          emailAddress.setPrimary(primary);
78  
79          emailAddressPersistence.update(emailAddress, false);
80  
81          return emailAddress;
82      }
83  
84      public void deleteEmailAddress(long emailAddressId)
85          throws PortalException, SystemException {
86  
87          emailAddressPersistence.remove(emailAddressId);
88      }
89  
90      public void deleteEmailAddresses(
91              long companyId, String className, long classPK)
92          throws SystemException {
93  
94          long classNameId = PortalUtil.getClassNameId(className);
95  
96          emailAddressPersistence.removeByC_C_C(companyId, classNameId, classPK);
97      }
98  
99      public EmailAddress getEmailAddress(long emailAddressId)
100         throws PortalException, SystemException {
101 
102         return emailAddressPersistence.findByPrimaryKey(emailAddressId);
103     }
104 
105     public List<EmailAddress> getEmailAddresses() throws SystemException {
106         return emailAddressPersistence.findAll();
107     }
108 
109     public List<EmailAddress> getEmailAddresses(
110             long companyId, String className, long classPK)
111         throws SystemException {
112 
113         long classNameId = PortalUtil.getClassNameId(className);
114 
115         return emailAddressPersistence.findByC_C_C(
116             companyId, classNameId, classPK);
117     }
118 
119     public EmailAddress updateEmailAddress(
120             long emailAddressId, String address, int typeId, boolean primary)
121         throws PortalException, SystemException {
122 
123         validate(emailAddressId, 0, 0, 0, address, typeId, primary);
124 
125         EmailAddress emailAddress = emailAddressPersistence.findByPrimaryKey(
126             emailAddressId);
127 
128         emailAddress.setModifiedDate(new Date());
129         emailAddress.setAddress(address);
130         emailAddress.setTypeId(typeId);
131         emailAddress.setPrimary(primary);
132 
133         emailAddressPersistence.update(emailAddress, false);
134 
135         return emailAddress;
136     }
137 
138     protected void validate(
139             long emailAddressId, long companyId, long classNameId,
140             long classPK, String address, int typeId, boolean primary)
141         throws PortalException, SystemException {
142 
143         if (!Validator.isEmailAddress(address)) {
144             throw new EmailAddressException();
145         }
146 
147         if (emailAddressId > 0) {
148             EmailAddress emailAddress =
149                 emailAddressPersistence.findByPrimaryKey(
150                     emailAddressId);
151 
152             companyId = emailAddress.getCompanyId();
153             classNameId = emailAddress.getClassNameId();
154             classPK = emailAddress.getClassPK();
155         }
156 
157         listTypeService.validate(
158             typeId, classNameId, ListTypeImpl.EMAIL_ADDRESS);
159 
160         validate(emailAddressId, companyId, classNameId, classPK, primary);
161     }
162 
163     protected void validate(
164             long emailAddressId, long companyId, long classNameId, long classPK,
165             boolean primary)
166         throws SystemException {
167 
168         // Check to make sure there isn't another emailAddress with the same
169         // company id, class name, and class pk that also has primary set to
170         // true
171 
172         if (primary) {
173             Iterator<EmailAddress> itr = emailAddressPersistence.findByC_C_C_P(
174                 companyId, classNameId, classPK, primary).iterator();
175 
176             while (itr.hasNext()) {
177                 EmailAddress emailAddress = itr.next();
178 
179                 if ((emailAddressId <= 0) ||
180                     (emailAddress.getEmailAddressId() != emailAddressId)) {
181 
182                     emailAddress.setPrimary(false);
183 
184                     emailAddressPersistence.update(emailAddress, false);
185                 }
186             }
187         }
188     }
189 
190 }