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