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