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