001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.service.impl;
016    
017    import com.liferay.portal.EmailAddressException;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.model.EmailAddress;
022    import com.liferay.portal.model.ListTypeConstants;
023    import com.liferay.portal.model.User;
024    import com.liferay.portal.service.base.EmailAddressLocalServiceBaseImpl;
025    import com.liferay.portal.util.PortalUtil;
026    
027    import java.util.Date;
028    import java.util.Iterator;
029    import java.util.List;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     * @author Alexander Chow
034     */
035    public class EmailAddressLocalServiceImpl
036            extends EmailAddressLocalServiceBaseImpl {
037    
038            public EmailAddress addEmailAddress(
039                            long userId, String className, long classPK, String address,
040                            int typeId, boolean primary)
041                    throws PortalException, SystemException {
042    
043                    User user = userPersistence.findByPrimaryKey(userId);
044                    long classNameId = PortalUtil.getClassNameId(className);
045                    Date now = new Date();
046    
047                    validate(
048                            0, user.getCompanyId(), classNameId, classPK, address, typeId,
049                            primary);
050    
051                    long emailAddressId = counterLocalService.increment();
052    
053                    EmailAddress emailAddress = emailAddressPersistence.create(
054                            emailAddressId);
055    
056                    emailAddress.setCompanyId(user.getCompanyId());
057                    emailAddress.setUserId(user.getUserId());
058                    emailAddress.setUserName(user.getFullName());
059                    emailAddress.setCreateDate(now);
060                    emailAddress.setModifiedDate(now);
061                    emailAddress.setClassNameId(classNameId);
062                    emailAddress.setClassPK(classPK);
063                    emailAddress.setAddress(address);
064                    emailAddress.setTypeId(typeId);
065                    emailAddress.setPrimary(primary);
066    
067                    emailAddressPersistence.update(emailAddress, false);
068    
069                    return emailAddress;
070            }
071    
072            @Override
073            public void deleteEmailAddress(EmailAddress emailAddress)
074                    throws SystemException {
075    
076                    emailAddressPersistence.remove(emailAddress);
077            }
078    
079            @Override
080            public void deleteEmailAddress(long emailAddressId)
081                    throws PortalException, SystemException {
082    
083                    EmailAddress emailAddress = emailAddressPersistence.findByPrimaryKey(
084                            emailAddressId);
085    
086                    deleteEmailAddress(emailAddress);
087            }
088    
089            public void deleteEmailAddresses(
090                            long companyId, String className, long classPK)
091                    throws SystemException {
092    
093                    long classNameId = PortalUtil.getClassNameId(className);
094    
095                    List<EmailAddress> emailAddresses = emailAddressPersistence.findByC_C_C(
096                            companyId, classNameId, classPK);
097    
098                    for (EmailAddress emailAddress : emailAddresses) {
099                            deleteEmailAddress(emailAddress);
100                    }
101            }
102    
103            @Override
104            public EmailAddress getEmailAddress(long emailAddressId)
105                    throws PortalException, SystemException {
106    
107                    return emailAddressPersistence.findByPrimaryKey(emailAddressId);
108            }
109    
110            public List<EmailAddress> getEmailAddresses() throws SystemException {
111                    return emailAddressPersistence.findAll();
112            }
113    
114            public List<EmailAddress> getEmailAddresses(
115                            long companyId, String className, long classPK)
116                    throws SystemException {
117    
118                    long classNameId = PortalUtil.getClassNameId(className);
119    
120                    return emailAddressPersistence.findByC_C_C(
121                            companyId, classNameId, classPK);
122            }
123    
124            public EmailAddress updateEmailAddress(
125                            long emailAddressId, String address, int typeId, boolean primary)
126                    throws PortalException, SystemException {
127    
128                    validate(emailAddressId, 0, 0, 0, address, typeId, primary);
129    
130                    EmailAddress emailAddress = emailAddressPersistence.findByPrimaryKey(
131                            emailAddressId);
132    
133                    emailAddress.setModifiedDate(new Date());
134                    emailAddress.setAddress(address);
135                    emailAddress.setTypeId(typeId);
136                    emailAddress.setPrimary(primary);
137    
138                    emailAddressPersistence.update(emailAddress, false);
139    
140                    return emailAddress;
141            }
142    
143            protected void validate(
144                            long emailAddressId, long companyId, long classNameId, long classPK,
145                            boolean primary)
146                    throws SystemException {
147    
148                    // Check to make sure there isn't another emailAddress with the same
149                    // company id, class name, and class pk that also has primary set to
150                    // true
151    
152                    if (primary) {
153                            Iterator<EmailAddress> itr = emailAddressPersistence.findByC_C_C_P(
154                                    companyId, classNameId, classPK, primary).iterator();
155    
156                            while (itr.hasNext()) {
157                                    EmailAddress emailAddress = itr.next();
158    
159                                    if ((emailAddressId <= 0) ||
160                                            (emailAddress.getEmailAddressId() != emailAddressId)) {
161    
162                                            emailAddress.setPrimary(false);
163    
164                                            emailAddressPersistence.update(emailAddress, false);
165                                    }
166                            }
167                    }
168            }
169    
170            protected void validate(
171                            long emailAddressId, long companyId, long classNameId, long classPK,
172                            String address, int typeId, boolean primary)
173                    throws PortalException, SystemException {
174    
175                    if (!Validator.isEmailAddress(address)) {
176                            throw new EmailAddressException();
177                    }
178    
179                    if (emailAddressId > 0) {
180                            EmailAddress emailAddress =
181                                    emailAddressPersistence.findByPrimaryKey(emailAddressId);
182    
183                            companyId = emailAddress.getCompanyId();
184                            classNameId = emailAddress.getClassNameId();
185                            classPK = emailAddress.getClassPK();
186                    }
187    
188                    listTypeService.validate(
189                            typeId, classNameId, ListTypeConstants.EMAIL_ADDRESS);
190    
191                    validate(emailAddressId, companyId, classNameId, classPK, primary);
192            }
193    
194    }