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