1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.service.impl;
16  
17  import com.liferay.portal.EmailAddressException;
18  import com.liferay.portal.kernel.exception.PortalException;
19  import com.liferay.portal.kernel.exception.SystemException;
20  import com.liferay.portal.kernel.util.Validator;
21  import com.liferay.portal.model.EmailAddress;
22  import com.liferay.portal.model.ListTypeConstants;
23  import com.liferay.portal.model.User;
24  import com.liferay.portal.service.base.EmailAddressLocalServiceBaseImpl;
25  import com.liferay.portal.util.PortalUtil;
26  
27  import java.util.Date;
28  import java.util.Iterator;
29  import java.util.List;
30  
31  /**
32   * <a href="EmailAddressLocalServiceImpl.java.html"><b><i>View Source</i></b>
33   * </a>
34   *
35   * @author Brian Wing Shun Chan
36   * @author Alexander Chow
37   */
38  public class EmailAddressLocalServiceImpl
39      extends EmailAddressLocalServiceBaseImpl {
40  
41      public EmailAddress addEmailAddress(
42              long userId, String className, long classPK, String address,
43              int typeId, boolean primary)
44          throws PortalException, SystemException {
45  
46          User user = userPersistence.findByPrimaryKey(userId);
47          long classNameId = PortalUtil.getClassNameId(className);
48          Date now = new Date();
49  
50          validate(
51              0, user.getCompanyId(), classNameId, classPK, address, typeId,
52              primary);
53  
54          long emailAddressId = counterLocalService.increment();
55  
56          EmailAddress emailAddress = emailAddressPersistence.create(
57              emailAddressId);
58  
59          emailAddress.setCompanyId(user.getCompanyId());
60          emailAddress.setUserId(user.getUserId());
61          emailAddress.setUserName(user.getFullName());
62          emailAddress.setCreateDate(now);
63          emailAddress.setModifiedDate(now);
64          emailAddress.setClassNameId(classNameId);
65          emailAddress.setClassPK(classPK);
66          emailAddress.setAddress(address);
67          emailAddress.setTypeId(typeId);
68          emailAddress.setPrimary(primary);
69  
70          emailAddressPersistence.update(emailAddress, false);
71  
72          return emailAddress;
73      }
74  
75      public void deleteEmailAddress(long emailAddressId)
76          throws PortalException, SystemException {
77  
78          emailAddressPersistence.remove(emailAddressId);
79      }
80  
81      public void deleteEmailAddresses(
82              long companyId, String className, long classPK)
83          throws SystemException {
84  
85          long classNameId = PortalUtil.getClassNameId(className);
86  
87          emailAddressPersistence.removeByC_C_C(companyId, classNameId, classPK);
88      }
89  
90      public EmailAddress getEmailAddress(long emailAddressId)
91          throws PortalException, SystemException {
92  
93          return emailAddressPersistence.findByPrimaryKey(emailAddressId);
94      }
95  
96      public List<EmailAddress> getEmailAddresses() throws SystemException {
97          return emailAddressPersistence.findAll();
98      }
99  
100     public List<EmailAddress> getEmailAddresses(
101             long companyId, String className, long classPK)
102         throws SystemException {
103 
104         long classNameId = PortalUtil.getClassNameId(className);
105 
106         return emailAddressPersistence.findByC_C_C(
107             companyId, classNameId, classPK);
108     }
109 
110     public EmailAddress updateEmailAddress(
111             long emailAddressId, String address, int typeId, boolean primary)
112         throws PortalException, SystemException {
113 
114         validate(emailAddressId, 0, 0, 0, address, typeId, primary);
115 
116         EmailAddress emailAddress = emailAddressPersistence.findByPrimaryKey(
117             emailAddressId);
118 
119         emailAddress.setModifiedDate(new Date());
120         emailAddress.setAddress(address);
121         emailAddress.setTypeId(typeId);
122         emailAddress.setPrimary(primary);
123 
124         emailAddressPersistence.update(emailAddress, false);
125 
126         return emailAddress;
127     }
128 
129     protected void validate(
130             long emailAddressId, long companyId, long classNameId,
131             long classPK, String address, int typeId, boolean primary)
132         throws PortalException, SystemException {
133 
134         if (!Validator.isEmailAddress(address)) {
135             throw new EmailAddressException();
136         }
137 
138         if (emailAddressId > 0) {
139             EmailAddress emailAddress =
140                 emailAddressPersistence.findByPrimaryKey(
141                     emailAddressId);
142 
143             companyId = emailAddress.getCompanyId();
144             classNameId = emailAddress.getClassNameId();
145             classPK = emailAddress.getClassPK();
146         }
147 
148         listTypeService.validate(
149             typeId, classNameId, ListTypeConstants.EMAIL_ADDRESS);
150 
151         validate(emailAddressId, companyId, classNameId, classPK, primary);
152     }
153 
154     protected void validate(
155             long emailAddressId, long companyId, long classNameId, long classPK,
156             boolean primary)
157         throws SystemException {
158 
159         // Check to make sure there isn't another emailAddress with the same
160         // company id, class name, and class pk that also has primary set to
161         // true
162 
163         if (primary) {
164             Iterator<EmailAddress> itr = emailAddressPersistence.findByC_C_C_P(
165                 companyId, classNameId, classPK, primary).iterator();
166 
167             while (itr.hasNext()) {
168                 EmailAddress emailAddress = itr.next();
169 
170                 if ((emailAddressId <= 0) ||
171                     (emailAddress.getEmailAddressId() != emailAddressId)) {
172 
173                     emailAddress.setPrimary(false);
174 
175                     emailAddressPersistence.update(emailAddress, false);
176                 }
177             }
178         }
179     }
180 
181 }