001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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            public void deleteEmailAddresses(
073                            long companyId, String className, long classPK)
074                    throws SystemException {
075    
076                    long classNameId = PortalUtil.getClassNameId(className);
077    
078                    List<EmailAddress> emailAddresses = emailAddressPersistence.findByC_C_C(
079                            companyId, classNameId, classPK);
080    
081                    for (EmailAddress emailAddress : emailAddresses) {
082                            deleteEmailAddress(emailAddress);
083                    }
084            }
085    
086            public List<EmailAddress> getEmailAddresses() throws SystemException {
087                    return emailAddressPersistence.findAll();
088            }
089    
090            public List<EmailAddress> getEmailAddresses(
091                            long companyId, String className, long classPK)
092                    throws SystemException {
093    
094                    long classNameId = PortalUtil.getClassNameId(className);
095    
096                    return emailAddressPersistence.findByC_C_C(
097                            companyId, classNameId, classPK);
098            }
099    
100            public EmailAddress updateEmailAddress(
101                            long emailAddressId, String address, int typeId, boolean primary)
102                    throws PortalException, SystemException {
103    
104                    validate(emailAddressId, 0, 0, 0, address, typeId, primary);
105    
106                    EmailAddress emailAddress = emailAddressPersistence.findByPrimaryKey(
107                            emailAddressId);
108    
109                    emailAddress.setModifiedDate(new Date());
110                    emailAddress.setAddress(address);
111                    emailAddress.setTypeId(typeId);
112                    emailAddress.setPrimary(primary);
113    
114                    emailAddressPersistence.update(emailAddress, false);
115    
116                    return emailAddress;
117            }
118    
119            protected void validate(
120                            long emailAddressId, long companyId, long classNameId, long classPK,
121                            boolean primary)
122                    throws SystemException {
123    
124                    // Check to make sure there isn't another emailAddress with the same
125                    // company id, class name, and class pk that also has primary set to
126                    // true
127    
128                    if (primary) {
129                            Iterator<EmailAddress> itr = emailAddressPersistence.findByC_C_C_P(
130                                    companyId, classNameId, classPK, primary).iterator();
131    
132                            while (itr.hasNext()) {
133                                    EmailAddress emailAddress = itr.next();
134    
135                                    if ((emailAddressId <= 0) ||
136                                            (emailAddress.getEmailAddressId() != emailAddressId)) {
137    
138                                            emailAddress.setPrimary(false);
139    
140                                            emailAddressPersistence.update(emailAddress, false);
141                                    }
142                            }
143                    }
144            }
145    
146            protected void validate(
147                            long emailAddressId, long companyId, long classNameId, long classPK,
148                            String address, int typeId, boolean primary)
149                    throws PortalException, SystemException {
150    
151                    if (!Validator.isEmailAddress(address)) {
152                            throw new EmailAddressException();
153                    }
154    
155                    if (emailAddressId > 0) {
156                            EmailAddress emailAddress =
157                                    emailAddressPersistence.findByPrimaryKey(emailAddressId);
158    
159                            companyId = emailAddress.getCompanyId();
160                            classNameId = emailAddress.getClassNameId();
161                            classPK = emailAddress.getClassPK();
162                    }
163    
164                    listTypeService.validate(
165                            typeId, classNameId, ListTypeConstants.EMAIL_ADDRESS);
166    
167                    validate(emailAddressId, companyId, classNameId, classPK, primary);
168            }
169    
170    }