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.kernel.exception.PortalException;
018    import com.liferay.portal.model.EmailAddress;
019    import com.liferay.portal.model.User;
020    import com.liferay.portal.security.permission.ActionKeys;
021    import com.liferay.portal.service.ServiceContext;
022    import com.liferay.portal.service.base.EmailAddressServiceBaseImpl;
023    import com.liferay.portal.service.permission.CommonPermissionUtil;
024    
025    import java.util.List;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     * @author Alexander Chow
030     */
031    public class EmailAddressServiceImpl extends EmailAddressServiceBaseImpl {
032    
033            @Override
034            public EmailAddress addEmailAddress(
035                            String className, long classPK, String address, long typeId,
036                            boolean primary, ServiceContext serviceContext)
037                    throws PortalException {
038    
039                    CommonPermissionUtil.check(
040                            getPermissionChecker(), className, classPK, ActionKeys.UPDATE);
041    
042                    return emailAddressLocalService.addEmailAddress(
043                            getUserId(), className, classPK, address, typeId, primary,
044                            serviceContext);
045            }
046    
047            @Override
048            public void deleteEmailAddress(long emailAddressId) throws PortalException {
049                    EmailAddress emailAddress = emailAddressPersistence.findByPrimaryKey(
050                            emailAddressId);
051    
052                    CommonPermissionUtil.check(
053                            getPermissionChecker(), emailAddress.getClassNameId(),
054                            emailAddress.getClassPK(), ActionKeys.UPDATE);
055    
056                    emailAddressLocalService.deleteEmailAddress(emailAddress);
057            }
058    
059            /**
060             * Returns the email address with the primary key.
061             *
062             * @param  emailAddressId the primary key of the email address
063             * @return the email address with the primary key, or <code>null</code> if
064             *         an email address with the primary key could not be found or if
065             *         the user did not have permission to view the email address
066             */
067            @Override
068            public EmailAddress fetchEmailAddress(long emailAddressId)
069                    throws PortalException {
070    
071                    EmailAddress emailAddress = emailAddressPersistence.fetchByPrimaryKey(
072                            emailAddressId);
073    
074                    if (emailAddress != null) {
075                            CommonPermissionUtil.check(
076                                    getPermissionChecker(), emailAddress.getClassNameId(),
077                                    emailAddress.getClassPK(), ActionKeys.VIEW);
078                    }
079    
080                    return emailAddress;
081            }
082    
083            @Override
084            public EmailAddress getEmailAddress(long emailAddressId)
085                    throws PortalException {
086    
087                    EmailAddress emailAddress = emailAddressPersistence.findByPrimaryKey(
088                            emailAddressId);
089    
090                    CommonPermissionUtil.check(
091                            getPermissionChecker(), emailAddress.getClassNameId(),
092                            emailAddress.getClassPK(), ActionKeys.VIEW);
093    
094                    return emailAddress;
095            }
096    
097            @Override
098            public List<EmailAddress> getEmailAddresses(String className, long classPK)
099                    throws PortalException {
100    
101                    CommonPermissionUtil.check(
102                            getPermissionChecker(), className, classPK, ActionKeys.VIEW);
103    
104                    User user = getUser();
105    
106                    return emailAddressLocalService.getEmailAddresses(
107                            user.getCompanyId(), className, classPK);
108            }
109    
110            @Override
111            public EmailAddress updateEmailAddress(
112                            long emailAddressId, String address, long typeId, boolean primary)
113                    throws PortalException {
114    
115                    EmailAddress emailAddress = emailAddressPersistence.findByPrimaryKey(
116                            emailAddressId);
117    
118                    CommonPermissionUtil.check(
119                            getPermissionChecker(), emailAddress.getClassNameId(),
120                            emailAddress.getClassPK(), ActionKeys.UPDATE);
121    
122                    return emailAddressLocalService.updateEmailAddress(
123                            emailAddressId, address, typeId, primary);
124            }
125    
126    }