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.repository.capabilities;
016    
017    import com.liferay.portal.kernel.comment.CommentManagerUtil;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.repository.capabilities.CommentCapability;
020    import com.liferay.portal.kernel.repository.event.RepositoryEventAware;
021    import com.liferay.portal.kernel.repository.event.RepositoryEventListener;
022    import com.liferay.portal.kernel.repository.event.RepositoryEventType;
023    import com.liferay.portal.kernel.repository.model.FileEntry;
024    import com.liferay.portal.kernel.repository.registry.RepositoryEventRegistry;
025    import com.liferay.portlet.documentlibrary.model.DLFileEntryConstants;
026    
027    /**
028     * @author Adolfo P??rez
029     */
030    public class LiferayCommentCapability
031            implements CommentCapability, RepositoryEventAware {
032    
033            @Override
034            public void registerRepositoryEventListeners(
035                    RepositoryEventRegistry repositoryEventRegistry) {
036    
037                    repositoryEventRegistry.registerRepositoryEventListener(
038                            RepositoryEventType.Add.class, FileEntry.class,
039                            _COMMENT_ADD_FILE_ENTRY_EVENT_LISTENER);
040                    repositoryEventRegistry.registerRepositoryEventListener(
041                            RepositoryEventType.Delete.class, FileEntry.class,
042                            _COMMENT_DELETE_FILE_ENTRY_EVENT_LISTENER);
043            }
044    
045            private static final RepositoryEventListener
046                    <RepositoryEventType.Add, FileEntry>
047                            _COMMENT_ADD_FILE_ENTRY_EVENT_LISTENER =
048                                    new RepositoryEventListener
049                                            <RepositoryEventType.Add, FileEntry>() {
050    
051                                            @Override
052                                            public void execute(FileEntry fileEntry)
053                                                    throws PortalException {
054    
055                                                    CommentManagerUtil.addDiscussion(
056                                                            fileEntry.getUserId(), fileEntry.getGroupId(),
057                                                            DLFileEntryConstants.getClassName(),
058                                                            fileEntry.getFileEntryId(),
059                                                            fileEntry.getUserName());
060                                            }
061    
062                                    };
063    
064            private static final RepositoryEventListener
065                    <RepositoryEventType.Delete, FileEntry>
066                            _COMMENT_DELETE_FILE_ENTRY_EVENT_LISTENER =
067                                    new RepositoryEventListener
068                                            <RepositoryEventType.Delete, FileEntry>() {
069    
070                                            @Override
071                                            public void execute(FileEntry fileEntry)
072                                                    throws PortalException {
073    
074                                                    CommentManagerUtil.deleteDiscussion(
075                                                            DLFileEntryConstants.getClassName(),
076                                                            fileEntry.getFileEntryId());
077                                            }
078    
079                                    };
080    
081    }