1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.service.impl;
21  
22  import com.liferay.portal.EmailAddressException;
23  import com.liferay.portal.PortalException;
24  import com.liferay.portal.SystemException;
25  import com.liferay.portal.kernel.util.Validator;
26  import com.liferay.portal.model.EmailAddress;
27  import com.liferay.portal.model.User;
28  import com.liferay.portal.model.impl.ListTypeImpl;
29  import com.liferay.portal.service.base.EmailAddressLocalServiceBaseImpl;
30  import com.liferay.portal.util.PortalUtil;
31  
32  import java.rmi.RemoteException;
33  
34  import java.util.Date;
35  import java.util.Iterator;
36  import java.util.List;
37  
38  /**
39   * <a href="EmailAddressLocalServiceImpl.java.html"><b><i>View Source</i></b>
40   * </a>
41   *
42   * @author Brian Wing Shun Chan
43   * @author Alexander Chow
44   *
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         try {
157             listTypeService.validate(
158                 typeId, classNameId, ListTypeImpl.EMAIL_ADDRESS);
159         }
160         catch (RemoteException re) {
161             throw new SystemException(re);
162         }
163 
164         validate(emailAddressId, companyId, classNameId, classPK, primary);
165     }
166 
167     protected void validate(
168             long emailAddressId, long companyId, long classNameId, long classPK,
169             boolean primary)
170         throws SystemException {
171 
172         // Check to make sure there isn't another emailAddress with the same
173         // company id, class name, and class pk that also has primary set to
174         // true
175 
176         if (primary) {
177             Iterator<EmailAddress> itr = emailAddressPersistence.findByC_C_C_P(
178                 companyId, classNameId, classPK, primary).iterator();
179 
180             while (itr.hasNext()) {
181                 EmailAddress emailAddress = itr.next();
182 
183                 if ((emailAddressId <= 0) ||
184                     (emailAddress.getEmailAddressId() != emailAddressId)) {
185 
186                     emailAddress.setPrimary(false);
187 
188                     emailAddressPersistence.update(emailAddress, false);
189                 }
190             }
191         }
192     }
193 
194 }