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