001    /**
002     * Copyright (c) 2000-present 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.systemevent.SystemEvent;
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.SystemEventConstants;
024    import com.liferay.portal.model.User;
025    import com.liferay.portal.service.ServiceContext;
026    import com.liferay.portal.service.base.EmailAddressLocalServiceBaseImpl;
027    
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            /**
038             * @deprecated As of 6.2.0, replaced by {@link #addEmailAddress(long,
039             *             String, long, String, int, boolean, ServiceContext)}
040             */
041            @Deprecated
042            @Override
043            public EmailAddress addEmailAddress(
044                            long userId, String className, long classPK, String address,
045                            long typeId, boolean primary)
046                    throws PortalException {
047    
048                    return addEmailAddress(
049                            userId, className, classPK, address, typeId, primary,
050                            new ServiceContext());
051            }
052    
053            @Override
054            public EmailAddress addEmailAddress(
055                            long userId, String className, long classPK, String address,
056                            long typeId, boolean primary, ServiceContext serviceContext)
057                    throws PortalException {
058    
059                    User user = userPersistence.findByPrimaryKey(userId);
060                    long classNameId = classNameLocalService.getClassNameId(className);
061    
062                    validate(
063                            0, user.getCompanyId(), classNameId, classPK, address, typeId,
064                            primary);
065    
066                    long emailAddressId = counterLocalService.increment();
067    
068                    EmailAddress emailAddress = emailAddressPersistence.create(
069                            emailAddressId);
070    
071                    emailAddress.setUuid(serviceContext.getUuid());
072                    emailAddress.setCompanyId(user.getCompanyId());
073                    emailAddress.setUserId(user.getUserId());
074                    emailAddress.setUserName(user.getFullName());
075                    emailAddress.setClassNameId(classNameId);
076                    emailAddress.setClassPK(classPK);
077                    emailAddress.setAddress(address);
078                    emailAddress.setTypeId(typeId);
079                    emailAddress.setPrimary(primary);
080    
081                    emailAddressPersistence.update(emailAddress);
082    
083                    return emailAddress;
084            }
085    
086            @Override
087            @SystemEvent(
088                    action = SystemEventConstants.ACTION_SKIP,
089                    type = SystemEventConstants.TYPE_DELETE
090            )
091            public EmailAddress deleteEmailAddress(EmailAddress emailAddress) {
092                    emailAddressPersistence.remove(emailAddress);
093    
094                    return emailAddress;
095            }
096    
097            @Override
098            public EmailAddress deleteEmailAddress(long emailAddressId)
099                    throws PortalException {
100    
101                    EmailAddress emailAddress = emailAddressPersistence.findByPrimaryKey(
102                            emailAddressId);
103    
104                    return emailAddressLocalService.deleteEmailAddress(emailAddress);
105            }
106    
107            @Override
108            public void deleteEmailAddresses(
109                    long companyId, String className, long classPK) {
110    
111                    long classNameId = classNameLocalService.getClassNameId(className);
112    
113                    List<EmailAddress> emailAddresses = emailAddressPersistence.findByC_C_C(
114                            companyId, classNameId, classPK);
115    
116                    for (EmailAddress emailAddress : emailAddresses) {
117                            emailAddressLocalService.deleteEmailAddress(emailAddress);
118                    }
119            }
120    
121            @Override
122            public List<EmailAddress> getEmailAddresses() {
123                    return emailAddressPersistence.findAll();
124            }
125    
126            @Override
127            public List<EmailAddress> getEmailAddresses(
128                    long companyId, String className, long classPK) {
129    
130                    long classNameId = classNameLocalService.getClassNameId(className);
131    
132                    return emailAddressPersistence.findByC_C_C(
133                            companyId, classNameId, classPK);
134            }
135    
136            @Override
137            public EmailAddress updateEmailAddress(
138                            long emailAddressId, String address, long typeId, boolean primary)
139                    throws PortalException {
140    
141                    validate(emailAddressId, 0, 0, 0, address, typeId, primary);
142    
143                    EmailAddress emailAddress = emailAddressPersistence.findByPrimaryKey(
144                            emailAddressId);
145    
146                    emailAddress.setAddress(address);
147                    emailAddress.setTypeId(typeId);
148                    emailAddress.setPrimary(primary);
149    
150                    emailAddressPersistence.update(emailAddress);
151    
152                    return emailAddress;
153            }
154    
155            protected void validate(
156                    long emailAddressId, long companyId, long classNameId, long classPK,
157                    boolean primary) {
158    
159                    // Check to make sure there isn't another emailAddress with the same
160                    // company id, class name, and class pk that also has primary set to
161                    // true
162    
163                    if (primary) {
164                            List<EmailAddress> emailAddresses =
165                                    emailAddressPersistence.findByC_C_C_P(
166                                            companyId, classNameId, classPK, primary);
167    
168                            for (EmailAddress emailAddress : emailAddresses) {
169                                    if ((emailAddressId <= 0) ||
170                                            (emailAddress.getEmailAddressId() != emailAddressId)) {
171    
172                                            emailAddress.setPrimary(false);
173    
174                                            emailAddressPersistence.update(emailAddress);
175                                    }
176                            }
177                    }
178            }
179    
180            protected void validate(
181                            long emailAddressId, long companyId, long classNameId, long classPK,
182                            String address, long typeId, boolean primary)
183                    throws PortalException {
184    
185                    if (!Validator.isEmailAddress(address)) {
186                            throw new EmailAddressException();
187                    }
188    
189                    if (emailAddressId > 0) {
190                            EmailAddress emailAddress =
191                                    emailAddressPersistence.findByPrimaryKey(emailAddressId);
192    
193                            companyId = emailAddress.getCompanyId();
194                            classNameId = emailAddress.getClassNameId();
195                            classPK = emailAddress.getClassPK();
196                    }
197    
198                    listTypeLocalService.validate(
199                            typeId, classNameId, ListTypeConstants.EMAIL_ADDRESS);
200    
201                    validate(emailAddressId, companyId, classNameId, classPK, primary);
202            }
203    
204    }