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