001    /**
002     * Copyright (c) 2000-2013 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.List;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     * @author Alexander Chow
033     */
034    public class EmailAddressLocalServiceImpl
035            extends EmailAddressLocalServiceBaseImpl {
036    
037            public EmailAddress addEmailAddress(
038                            long userId, String className, long classPK, String address,
039                            int typeId, boolean primary)
040                    throws PortalException, SystemException {
041    
042                    User user = userPersistence.findByPrimaryKey(userId);
043                    long classNameId = PortalUtil.getClassNameId(className);
044                    Date now = new Date();
045    
046                    validate(
047                            0, user.getCompanyId(), classNameId, classPK, address, typeId,
048                            primary);
049    
050                    long emailAddressId = counterLocalService.increment();
051    
052                    EmailAddress emailAddress = emailAddressPersistence.create(
053                            emailAddressId);
054    
055                    emailAddress.setCompanyId(user.getCompanyId());
056                    emailAddress.setUserId(user.getUserId());
057                    emailAddress.setUserName(user.getFullName());
058                    emailAddress.setCreateDate(now);
059                    emailAddress.setModifiedDate(now);
060                    emailAddress.setClassNameId(classNameId);
061                    emailAddress.setClassPK(classPK);
062                    emailAddress.setAddress(address);
063                    emailAddress.setTypeId(typeId);
064                    emailAddress.setPrimary(primary);
065    
066                    emailAddressPersistence.update(emailAddress);
067    
068                    return emailAddress;
069            }
070    
071            public void deleteEmailAddresses(
072                            long companyId, String className, long classPK)
073                    throws SystemException {
074    
075                    long classNameId = PortalUtil.getClassNameId(className);
076    
077                    List<EmailAddress> emailAddresses = emailAddressPersistence.findByC_C_C(
078                            companyId, classNameId, classPK);
079    
080                    for (EmailAddress emailAddress : emailAddresses) {
081                            deleteEmailAddress(emailAddress);
082                    }
083            }
084    
085            public List<EmailAddress> getEmailAddresses() throws SystemException {
086                    return emailAddressPersistence.findAll();
087            }
088    
089            public List<EmailAddress> getEmailAddresses(
090                            long companyId, String className, long classPK)
091                    throws SystemException {
092    
093                    long classNameId = PortalUtil.getClassNameId(className);
094    
095                    return emailAddressPersistence.findByC_C_C(
096                            companyId, classNameId, classPK);
097            }
098    
099            public EmailAddress updateEmailAddress(
100                            long emailAddressId, String address, int typeId, boolean primary)
101                    throws PortalException, SystemException {
102    
103                    validate(emailAddressId, 0, 0, 0, address, typeId, primary);
104    
105                    EmailAddress emailAddress = emailAddressPersistence.findByPrimaryKey(
106                            emailAddressId);
107    
108                    emailAddress.setModifiedDate(new Date());
109                    emailAddress.setAddress(address);
110                    emailAddress.setTypeId(typeId);
111                    emailAddress.setPrimary(primary);
112    
113                    emailAddressPersistence.update(emailAddress);
114    
115                    return emailAddress;
116            }
117    
118            protected void validate(
119                            long emailAddressId, long companyId, long classNameId, long classPK,
120                            boolean primary)
121                    throws SystemException {
122    
123                    // Check to make sure there isn't another emailAddress with the same
124                    // company id, class name, and class pk that also has primary set to
125                    // true
126    
127                    if (primary) {
128                            List<EmailAddress> emailAddresses =
129                                    emailAddressPersistence.findByC_C_C_P(
130                                            companyId, classNameId, classPK, primary);
131    
132                            for (EmailAddress emailAddress : emailAddresses) {
133                                    if ((emailAddressId <= 0) ||
134                                            (emailAddress.getEmailAddressId() != emailAddressId)) {
135    
136                                            emailAddress.setPrimary(false);
137    
138                                            emailAddressPersistence.update(emailAddress);
139                                    }
140                            }
141                    }
142            }
143    
144            protected void validate(
145                            long emailAddressId, long companyId, long classNameId, long classPK,
146                            String address, int typeId, boolean primary)
147                    throws PortalException, SystemException {
148    
149                    if (!Validator.isEmailAddress(address)) {
150                            throw new EmailAddressException();
151                    }
152    
153                    if (emailAddressId > 0) {
154                            EmailAddress emailAddress =
155                                    emailAddressPersistence.findByPrimaryKey(emailAddressId);
156    
157                            companyId = emailAddress.getCompanyId();
158                            classNameId = emailAddress.getClassNameId();
159                            classPK = emailAddress.getClassPK();
160                    }
161    
162                    listTypeService.validate(
163                            typeId, classNameId, ListTypeConstants.EMAIL_ADDRESS);
164    
165                    validate(emailAddressId, companyId, classNameId, classPK, primary);
166            }
167    
168    }