001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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(
092                                                            "Unable to remove file rank " + fileRankId);
093                                            }
094                                    }
095                            }
096                    }
097            }
098    
099            public void deleteFileRank(DLFileRank dlFileRank) throws SystemException {
100                    dlFileRankPersistence.remove(dlFileRank);
101            }
102    
103            public void deleteFileRank(long fileRankId)
104                    throws PortalException, SystemException {
105    
106                    DLFileRank dlFileRank = dlFileRankPersistence.findByPrimaryKey(
107                            fileRankId);
108    
109                    deleteFileRank(dlFileRank);
110            }
111    
112            public void deleteFileRanksByFileEntryId(long fileEntryId)
113                    throws SystemException {
114    
115                    List<DLFileRank> dlFileRanks = dlFileRankPersistence.findByFileEntryId(
116                            fileEntryId);
117    
118                    for (DLFileRank dlFileRank : dlFileRanks) {
119                            deleteFileRank(dlFileRank);
120                    }
121            }
122    
123            public void deleteFileRanksByUserId(long userId) throws SystemException {
124                    List<DLFileRank> dlFileRanks = dlFileRankPersistence.findByUserId(
125                            userId);
126    
127                    for (DLFileRank dlFileRank : dlFileRanks) {
128                            deleteFileRank(dlFileRank);
129                    }
130            }
131    
132            public List<DLFileRank> getFileRanks(long groupId, long userId)
133                    throws SystemException {
134    
135                    return dlFileRankPersistence.findByG_U(
136                            groupId, userId, 0, PropsValues.DL_FILE_RANK_MAX_SIZE,
137                            new FileRankCreateDateComparator());
138            }
139    
140            public DLFileRank updateFileRank(
141                            long groupId, long companyId, long userId, long fileEntryId,
142                            ServiceContext serviceContext)
143                    throws SystemException {
144    
145                    if (!PropsValues.DL_FILE_RANK_ENABLED) {
146                            return null;
147                    }
148    
149                    DLFileRank dlFileRank = dlFileRankPersistence.fetchByC_U_F(
150                            companyId, userId, fileEntryId);
151    
152                    if (dlFileRank != null) {
153                            dlFileRank.setCreateDate(serviceContext.getCreateDate(null));
154    
155                            dlFileRankPersistence.update(dlFileRank, false);
156                    }
157                    else {
158                            dlFileRank = addFileRank(
159                                    groupId, companyId, userId, fileEntryId, serviceContext);
160                    }
161    
162                    return dlFileRank;
163            }
164    
165            private static Log _log = LogFactoryUtil.getLog(
166                    DLFileRankLocalServiceImpl.class);
167    
168    }