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