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