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.portlet.documentlibrary.service.impl;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryUtil;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.systemevent.SystemEvent;
023    import com.liferay.portal.model.SystemEventConstants;
024    import com.liferay.portal.service.ServiceContext;
025    import com.liferay.portal.util.PropsValues;
026    import com.liferay.portlet.documentlibrary.model.DLFileRank;
027    import com.liferay.portlet.documentlibrary.model.DLFolder;
028    import com.liferay.portlet.documentlibrary.service.base.DLFileRankLocalServiceBaseImpl;
029    import com.liferay.portlet.documentlibrary.util.comparator.FileRankCreateDateComparator;
030    
031    import java.util.List;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     */
036    public class DLFileRankLocalServiceImpl extends DLFileRankLocalServiceBaseImpl {
037    
038            @Override
039            public DLFileRank addFileRank(
040                    long groupId, long companyId, long userId, long fileEntryId,
041                    ServiceContext serviceContext) {
042    
043                    long fileRankId = counterLocalService.increment();
044    
045                    DLFileRank dlFileRank = dlFileRankPersistence.create(fileRankId);
046    
047                    dlFileRank.setGroupId(groupId);
048                    dlFileRank.setCompanyId(companyId);
049                    dlFileRank.setUserId(userId);
050                    dlFileRank.setCreateDate(serviceContext.getCreateDate(null));
051                    dlFileRank.setFileEntryId(fileEntryId);
052                    dlFileRank.setActive(true);
053    
054                    try {
055                            dlFileRankPersistence.update(dlFileRank);
056                    }
057                    catch (SystemException se) {
058                            if (_log.isWarnEnabled()) {
059                                    _log.warn(
060                                            "Add failed, fetch {companyId=" + companyId + ", userId=" +
061                                                    userId + ", fileEntryId=" + fileEntryId + "}");
062                            }
063    
064                            dlFileRank = dlFileRankPersistence.fetchByC_U_F(
065                                    companyId, userId, fileEntryId, false);
066    
067                            if (dlFileRank == null) {
068                                    throw se;
069                            }
070                    }
071    
072                    return dlFileRank;
073            }
074    
075            @Override
076            public void checkFileRanks() {
077                    List<Object[]> staleFileRanks = dlFileRankFinder.findByStaleRanks(
078                            PropsValues.DL_FILE_RANK_MAX_SIZE);
079    
080                    for (Object[] staleFileRank : staleFileRanks) {
081                            long groupId = (Long)staleFileRank[0];
082                            long userId = (Long)staleFileRank[1];
083    
084                            List<DLFileRank> dlFileRanks = dlFileRankPersistence.findByG_U_A(
085                                    groupId, userId, true, PropsValues.DL_FILE_RANK_MAX_SIZE,
086                                    QueryUtil.ALL_POS, new FileRankCreateDateComparator());
087    
088                            for (DLFileRank dlFileRank : dlFileRanks) {
089                                    long fileRankId = dlFileRank.getFileRankId();
090    
091                                    try {
092                                            dlFileRankPersistence.remove(dlFileRank);
093                                    }
094                                    catch (Exception e) {
095                                            if (_log.isWarnEnabled()) {
096                                                    _log.warn("Unable to remove file rank " + fileRankId);
097                                            }
098                                    }
099                            }
100                    }
101            }
102    
103            @Override
104            @SystemEvent(type = SystemEventConstants.TYPE_DELETE)
105            public void deleteFileRank(DLFileRank dlFileRank) {
106                    dlFileRankPersistence.remove(dlFileRank);
107            }
108    
109            @Override
110            public void deleteFileRank(long fileRankId) throws PortalException {
111                    DLFileRank dlFileRank = dlFileRankPersistence.findByPrimaryKey(
112                            fileRankId);
113    
114                    dlFileRankLocalService.deleteFileRank(dlFileRank);
115            }
116    
117            @Override
118            public void deleteFileRanksByFileEntryId(long fileEntryId) {
119                    List<DLFileRank> dlFileRanks = dlFileRankPersistence.findByFileEntryId(
120                            fileEntryId);
121    
122                    for (DLFileRank dlFileRank : dlFileRanks) {
123                            dlFileRankLocalService.deleteFileRank(dlFileRank);
124                    }
125            }
126    
127            @Override
128            public void deleteFileRanksByUserId(long userId) {
129                    List<DLFileRank> dlFileRanks = dlFileRankPersistence.findByUserId(
130                            userId);
131    
132                    for (DLFileRank dlFileRank : dlFileRanks) {
133                            dlFileRankLocalService.deleteFileRank(dlFileRank);
134                    }
135            }
136    
137            @Override
138            public void disableFileRanks(long fileEntryId) {
139                    List<DLFileRank> dlFileRanks = dlFileRankPersistence.findByFileEntryId(
140                            fileEntryId);
141    
142                    for (DLFileRank dlFileRank : dlFileRanks) {
143                            dlFileRank.setActive(false);
144    
145                            dlFileRankPersistence.update(dlFileRank);
146                    }
147            }
148    
149            @Override
150            public void disableFileRanksByFolderId(long folderId)
151                    throws PortalException {
152    
153                    DLFolder dlFolder = dlFolderLocalService.getDLFolder(folderId);
154    
155                    updateFileRanks(dlFolder, false);
156            }
157    
158            @Override
159            public void enableFileRanks(long fileEntryId) {
160                    List<DLFileRank> dlFileRanks = dlFileRankPersistence.findByFileEntryId(
161                            fileEntryId);
162    
163                    for (DLFileRank dlFileRank : dlFileRanks) {
164                            dlFileRank.setActive(true);
165    
166                            dlFileRankPersistence.update(dlFileRank);
167                    }
168            }
169    
170            @Override
171            public void enableFileRanksByFolderId(long folderId)
172                    throws PortalException {
173    
174                    DLFolder dlFolder = dlFolderLocalService.getDLFolder(folderId);
175    
176                    updateFileRanks(dlFolder, true);
177            }
178    
179            @Override
180            public List<DLFileRank> getFileRanks(long groupId, long userId) {
181                    return dlFileRankPersistence.findByG_U_A(
182                            groupId, userId, true, 0, PropsValues.DL_FILE_RANK_MAX_SIZE,
183                            new FileRankCreateDateComparator());
184            }
185    
186            @Override
187            public DLFileRank updateFileRank(
188                    long groupId, long companyId, long userId, long fileEntryId,
189                    ServiceContext serviceContext) {
190    
191                    if (!PropsValues.DL_FILE_RANK_ENABLED) {
192                            return null;
193                    }
194    
195                    DLFileRank dlFileRank = dlFileRankPersistence.fetchByC_U_F(
196                            companyId, userId, fileEntryId);
197    
198                    if (dlFileRank != null) {
199                            dlFileRank.setCreateDate(serviceContext.getCreateDate(null));
200    
201                            try {
202                                    dlFileRankPersistence.update(dlFileRank);
203                            }
204                            catch (Exception e) {
205                                    if (_log.isWarnEnabled()) {
206                                            _log.warn(
207                                                    "Update failed, fetch {companyId=" + companyId +
208                                                            ", userId=" + userId + ", fileEntryId=" +
209                                                                    fileEntryId + "}");
210                                    }
211                            }
212                    }
213                    else {
214                            dlFileRank = addFileRank(
215                                    groupId, companyId, userId, fileEntryId, serviceContext);
216                    }
217    
218                    return dlFileRank;
219            }
220    
221            protected void updateFileRanks(DLFolder dlFolder, boolean active) {
222                    List<DLFolder> dlFolders = dlFolderPersistence.findByG_P(
223                            dlFolder.getGroupId(), dlFolder.getFolderId());
224    
225                    for (DLFolder curDLFolder : dlFolders) {
226                            updateFileRanks(curDLFolder, active);
227                    }
228    
229                    List<DLFileRank> dlFileRanks = dlFileRankFinder.findByFolderId(
230                            dlFolder.getFolderId());
231    
232                    for (DLFileRank dlFileRank : dlFileRanks) {
233                            dlFileRank.setActive(active);
234    
235                            dlFileRankPersistence.update(dlFileRank);
236                    }
237            }
238    
239            private static final Log _log = LogFactoryUtil.getLog(
240                    DLFileRankLocalServiceImpl.class);
241    
242    }