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