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