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.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            @Override
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    
051                    try {
052                            dlFileRankPersistence.update(dlFileRank, false);
053                    }
054                    catch (SystemException se) {
055                            if (_log.isWarnEnabled()) {
056                                    _log.warn(
057                                            "Add failed, fetch {companyId=" + companyId + ", userId=" +
058                                                    userId + ", fileEntryId=" + fileEntryId + "}");
059                            }
060    
061                            dlFileRank = dlFileRankPersistence.fetchByC_U_F(
062                                    companyId, userId, fileEntryId, false);
063    
064                            if (dlFileRank == null) {
065                                    throw se;
066                            }
067                    }
068    
069                    return dlFileRank;
070            }
071    
072            @Override
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(
082                                    groupId, userId, 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            @Override
101            public void deleteFileRank(DLFileRank dlFileRank) throws SystemException {
102                    dlFileRankPersistence.remove(dlFileRank);
103            }
104    
105            @Override
106            public void deleteFileRank(long fileRankId)
107                    throws PortalException, SystemException {
108    
109                    DLFileRank dlFileRank = dlFileRankPersistence.findByPrimaryKey(
110                            fileRankId);
111    
112                    deleteFileRank(dlFileRank);
113            }
114    
115            @Override
116            public void deleteFileRanksByFileEntryId(long fileEntryId)
117                    throws SystemException {
118    
119                    List<DLFileRank> dlFileRanks = dlFileRankPersistence.findByFileEntryId(
120                            fileEntryId);
121    
122                    for (DLFileRank dlFileRank : dlFileRanks) {
123                            deleteFileRank(dlFileRank);
124                    }
125            }
126    
127            @Override
128            public void deleteFileRanksByUserId(long userId) throws SystemException {
129                    List<DLFileRank> dlFileRanks = dlFileRankPersistence.findByUserId(
130                            userId);
131    
132                    for (DLFileRank dlFileRank : dlFileRanks) {
133                            deleteFileRank(dlFileRank);
134                    }
135            }
136    
137            @Override
138            public List<DLFileRank> getFileRanks(long groupId, long userId)
139                    throws SystemException {
140    
141                    return dlFileRankPersistence.findByG_U(
142                            groupId, userId, 0, PropsValues.DL_FILE_RANK_MAX_SIZE,
143                            new FileRankCreateDateComparator());
144            }
145    
146            @Override
147            public DLFileRank updateFileRank(
148                            long groupId, long companyId, long userId, long fileEntryId,
149                            ServiceContext serviceContext)
150                    throws SystemException {
151    
152                    if (!PropsValues.DL_FILE_RANK_ENABLED) {
153                            return null;
154                    }
155    
156                    DLFileRank dlFileRank = dlFileRankPersistence.fetchByC_U_F(
157                            companyId, userId, fileEntryId);
158    
159                    if (dlFileRank != null) {
160                            dlFileRank.setCreateDate(serviceContext.getCreateDate(null));
161    
162                            try {
163                                    dlFileRankPersistence.update(dlFileRank, false);
164                            }
165                            catch (Exception e) {
166                                    if (_log.isWarnEnabled()) {
167                                            _log.warn(
168                                                    "Update failed, fetch {companyId=" + companyId +
169                                                            ", userId=" + userId + ", fileEntryId=" +
170                                                                    fileEntryId + "}");
171                                    }
172                            }
173                    }
174                    else {
175                            dlFileRank = addFileRank(
176                                    groupId, companyId, userId, fileEntryId, serviceContext);
177                    }
178    
179                    return dlFileRank;
180            }
181    
182            private static Log _log = LogFactoryUtil.getLog(
183                    DLFileRankLocalServiceImpl.class);
184    
185    }