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            /**
034             * @deprecated As of 6.2.0, replaced by {@link #addEmailAddress(String,
035             *             long, String, int, boolean, ServiceContext)}
036             */
037            @Deprecated
038            @Override
039            public EmailAddress addEmailAddress(
040                            String className, long classPK, String address, long typeId,
041                            boolean primary)
042                    throws PortalException {
043    
044                    CommonPermissionUtil.check(
045                            getPermissionChecker(), className, classPK, ActionKeys.UPDATE);
046    
047                    return emailAddressLocalService.addEmailAddress(
048                            getUserId(), className, classPK, address, typeId, primary);
049            }
050    
051            @Override
052            public EmailAddress addEmailAddress(
053                            String className, long classPK, String address, long typeId,
054                            boolean primary, ServiceContext serviceContext)
055                    throws PortalException {
056    
057                    CommonPermissionUtil.check(
058                            getPermissionChecker(), className, classPK, ActionKeys.UPDATE);
059    
060                    return emailAddressLocalService.addEmailAddress(
061                            getUserId(), className, classPK, address, typeId, primary,
062                            serviceContext);
063            }
064    
065            @Override
066            public void deleteEmailAddress(long emailAddressId) throws PortalException {
067                    EmailAddress emailAddress = emailAddressPersistence.findByPrimaryKey(
068                            emailAddressId);
069    
070                    CommonPermissionUtil.check(
071                            getPermissionChecker(), emailAddress.getClassNameId(),
072                            emailAddress.getClassPK(), ActionKeys.UPDATE);
073    
074                    emailAddressLocalService.deleteEmailAddress(emailAddress);
075            }
076    
077            /**
078             * Returns the email address with the primary key.
079             *
080             * @param  emailAddressId the primary key of the email address
081             * @return the email address with the primary key, or <code>null</code> if
082             *         an email address with the primary key could not be found or if
083             *         the user did not have permission to view the email address
084             * @throws PortalException if a portal exception occurred
085             */
086            @Override
087            public EmailAddress fetchEmailAddress(long emailAddressId)
088                    throws PortalException {
089    
090                    EmailAddress emailAddress = emailAddressPersistence.fetchByPrimaryKey(
091                            emailAddressId);
092    
093                    if (emailAddress != null) {
094                            CommonPermissionUtil.check(
095                                    getPermissionChecker(), emailAddress.getClassNameId(),
096                                    emailAddress.getClassPK(), ActionKeys.VIEW);
097                    }
098    
099                    return emailAddress;
100            }
101    
102            @Override
103            public EmailAddress getEmailAddress(long emailAddressId)
104                    throws PortalException {
105    
106                    EmailAddress emailAddress = emailAddressPersistence.findByPrimaryKey(
107                            emailAddressId);
108    
109                    CommonPermissionUtil.check(
110                            getPermissionChecker(), emailAddress.getClassNameId(),
111                            emailAddress.getClassPK(), ActionKeys.VIEW);
112    
113                    return emailAddress;
114            }
115    
116            @Override
117            public List<EmailAddress> getEmailAddresses(String className, long classPK)
118                    throws PortalException {
119    
120                    CommonPermissionUtil.check(
121                            getPermissionChecker(), className, classPK, ActionKeys.VIEW);
122    
123                    User user = getUser();
124    
125                    return emailAddressLocalService.getEmailAddresses(
126                            user.getCompanyId(), className, classPK);
127            }
128    
129            @Override
130            public EmailAddress updateEmailAddress(
131                            long emailAddressId, String address, long typeId, boolean primary)
132                    throws PortalException {
133    
134                    EmailAddress emailAddress = emailAddressPersistence.findByPrimaryKey(
135                            emailAddressId);
136    
137                    CommonPermissionUtil.check(
138                            getPermissionChecker(), emailAddress.getClassNameId(),
139                            emailAddress.getClassPK(), ActionKeys.UPDATE);
140    
141                    return emailAddressLocalService.updateEmailAddress(
142                            emailAddressId, address, typeId, primary);
143            }
144    
145    }