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