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.service.base.DLFileRankLocalServiceBaseImpl;
026    import com.liferay.portlet.documentlibrary.util.comparator.FileRankCreateDateComparator;
027    
028    import java.util.List;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     */
033    public class DLFileRankLocalServiceImpl extends DLFileRankLocalServiceBaseImpl {
034    
035            public DLFileRank addFileRank(
036                            long groupId, long companyId, long userId, long fileEntryId,
037                            ServiceContext serviceContext)
038                    throws SystemException {
039    
040                    long fileRankId = counterLocalService.increment();
041    
042                    DLFileRank dlFileRank = dlFileRankPersistence.create(fileRankId);
043    
044                    dlFileRank.setGroupId(groupId);
045                    dlFileRank.setCompanyId(companyId);
046                    dlFileRank.setUserId(userId);
047                    dlFileRank.setCreateDate(serviceContext.getCreateDate(null));
048                    dlFileRank.setFileEntryId(fileEntryId);
049    
050                    try {
051                            dlFileRankPersistence.update(dlFileRank, false);
052                    }
053                    catch (SystemException se) {
054                            if (_log.isWarnEnabled()) {
055                                    _log.warn(
056                                            "Add failed, fetch {companyId=" + companyId + ", userId=" +
057                                                    userId + ", fileEntryId=" + fileEntryId + "}");
058                            }
059    
060                            dlFileRank = dlFileRankPersistence.fetchByC_U_F(
061                                    companyId, userId, fileEntryId, false);
062    
063                            if (dlFileRank == null) {
064                                    throw se;
065                            }
066                    }
067    
068                    return dlFileRank;
069            }
070    
071            public void checkFileRanks() throws SystemException {
072                    List<Object[]> staleFileRanks = dlFileRankFinder.findByStaleRanks(
073                            PropsValues.DL_FILE_RANK_MAX_SIZE);
074    
075                    for (Object[] staleFileRank : staleFileRanks) {
076                            long groupId = (Long)staleFileRank[0];
077                            long userId = (Long)staleFileRank[1];
078    
079                            List<DLFileRank> dlFileRanks = dlFileRankPersistence.findByG_U(
080                                    groupId, userId, PropsValues.DL_FILE_RANK_MAX_SIZE,
081                                    QueryUtil.ALL_POS, new FileRankCreateDateComparator());
082    
083                            for (DLFileRank dlFileRank : dlFileRanks) {
084                                    long fileRankId = dlFileRank.getFileRankId();
085    
086                                    try {
087                                            dlFileRankPersistence.remove(dlFileRank);
088                                    }
089                                    catch (Exception e) {
090                                            if (_log.isWarnEnabled()) {
091                                                    _log.warn("Unable to remove file rank " + fileRankId);
092                                            }
093                                    }
094                            }
095                    }
096            }
097    
098            public void deleteFileRank(DLFileRank dlFileRank) throws SystemException {
099                    dlFileRankPersistence.remove(dlFileRank);
100            }
101    
102            public void deleteFileRank(long fileRankId)
103                    throws PortalException, SystemException {
104    
105                    DLFileRank dlFileRank = dlFileRankPersistence.findByPrimaryKey(
106                            fileRankId);
107    
108                    deleteFileRank(dlFileRank);
109            }
110    
111            public void deleteFileRanksByFileEntryId(long fileEntryId)
112                    throws SystemException {
113    
114                    List<DLFileRank> dlFileRanks = dlFileRankPersistence.findByFileEntryId(
115                            fileEntryId);
116    
117                    for (DLFileRank dlFileRank : dlFileRanks) {
118                            deleteFileRank(dlFileRank);
119                    }
120            }
121    
122            public void deleteFileRanksByUserId(long userId) throws SystemException {
123                    List<DLFileRank> dlFileRanks = dlFileRankPersistence.findByUserId(
124                            userId);
125    
126                    for (DLFileRank dlFileRank : dlFileRanks) {
127                            deleteFileRank(dlFileRank);
128                    }
129            }
130    
131            public List<DLFileRank> getFileRanks(long groupId, long userId)
132                    throws SystemException {
133    
134                    return dlFileRankPersistence.findByG_U(
135                            groupId, userId, 0, PropsValues.DL_FILE_RANK_MAX_SIZE,
136                            new FileRankCreateDateComparator());
137            }
138    
139            public DLFileRank updateFileRank(
140                            long groupId, long companyId, long userId, long fileEntryId,
141                            ServiceContext serviceContext)
142                    throws SystemException {
143    
144                    if (!PropsValues.DL_FILE_RANK_ENABLED) {
145                            return null;
146                    }
147    
148                    DLFileRank dlFileRank = dlFileRankPersistence.fetchByC_U_F(
149                            companyId, userId, fileEntryId);
150    
151                    if (dlFileRank != null) {
152                            dlFileRank.setCreateDate(serviceContext.getCreateDate(null));
153    
154                            try {
155                                    dlFileRankPersistence.update(dlFileRank, false);
156                            }
157                            catch (Exception e) {
158                                    if (_log.isWarnEnabled()) {
159                                            _log.warn(
160                                                    "Update failed, fetch {companyId=" + companyId +
161                                                            ", userId=" + userId + ", fileEntryId=" +
162                                                                    fileEntryId + "}");
163                                    }
164                            }
165                    }
166                    else {
167                            dlFileRank = addFileRank(
168                                    groupId, companyId, userId, fileEntryId, serviceContext);
169                    }
170    
171                    return dlFileRank;
172            }
173    
174            private static Log _log = LogFactoryUtil.getLog(
175                    DLFileRankLocalServiceImpl.class);
176    
177    }