1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.documentlibrary.service.impl;
16  
17  import com.liferay.portal.kernel.dao.orm.QueryUtil;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.kernel.log.Log;
20  import com.liferay.portal.kernel.log.LogFactoryUtil;
21  import com.liferay.portal.util.PropsValues;
22  import com.liferay.portlet.documentlibrary.model.DLFileRank;
23  import com.liferay.portlet.documentlibrary.service.base.DLFileRankLocalServiceBaseImpl;
24  import com.liferay.portlet.documentlibrary.util.comparator.FileRankCreateDateComparator;
25  
26  import java.util.Date;
27  import java.util.List;
28  
29  /**
30   * <a href="DLFileRankLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
31   *
32   * @author Brian Wing Shun Chan
33   */
34  public class DLFileRankLocalServiceImpl extends DLFileRankLocalServiceBaseImpl {
35  
36      public DLFileRank addFileRank(
37              long groupId, long companyId, long userId, long folderId,
38              String name)
39          throws SystemException {
40  
41          long fileRankId = counterLocalService.increment();
42  
43          DLFileRank fileRank = dlFileRankPersistence.create(fileRankId);
44  
45          fileRank.setGroupId(groupId);
46          fileRank.setCompanyId(companyId);
47          fileRank.setUserId(userId);
48          fileRank.setCreateDate(new Date());
49          fileRank.setFolderId(folderId);
50          fileRank.setName(name);
51  
52          try {
53              dlFileRankPersistence.update(fileRank, false);
54          }
55          catch (SystemException se) {
56              if (_log.isWarnEnabled()) {
57                  _log.warn(
58                      "Add failed, fetch {companyId=" + companyId + ", userId=" +
59                          userId + ", folderId=" + folderId + ", name=" + name +
60                              "}");
61              }
62  
63              fileRank = dlFileRankPersistence.fetchByC_U_F_N(
64                  companyId, userId, folderId, name, false);
65  
66              if (fileRank == null) {
67                  throw se;
68              }
69          }
70  
71          return fileRank;
72      }
73  
74      public void deleteFileRanks(long userId) throws SystemException {
75          dlFileRankPersistence.removeByUserId(userId);
76      }
77  
78      public void deleteFileRanks(long folderId, String name)
79          throws SystemException {
80  
81          dlFileRankPersistence.removeByF_N(folderId, name);
82      }
83  
84      public List<DLFileRank> getFileRanks(long groupId, long userId)
85          throws SystemException {
86  
87          return getFileRanks(
88              groupId, userId, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
89      }
90  
91      public List<DLFileRank> getFileRanks(
92              long groupId, long userId, int start, int end)
93          throws SystemException {
94  
95          return dlFileRankPersistence.findByG_U(
96              groupId, userId, start, end, new FileRankCreateDateComparator());
97      }
98  
99      public DLFileRank updateFileRank(
100             long groupId, long companyId, long userId, long folderId,
101             String name)
102         throws SystemException {
103 
104         if (!PropsValues.DL_FILE_RANK_ENABLED) {
105             return null;
106         }
107 
108         DLFileRank fileRank = dlFileRankPersistence.fetchByC_U_F_N(
109             companyId, userId, folderId, name);
110 
111         if (fileRank != null) {
112             fileRank.setCreateDate(new Date());
113 
114             dlFileRankPersistence.update(fileRank, false);
115         }
116         else {
117             fileRank = addFileRank(
118                 groupId, companyId, userId, folderId, name);
119         }
120 
121         if (dlFileRankPersistence.countByG_U(groupId, userId) > 5) {
122             List<DLFileRank> fileRanks = getFileRanks(groupId, userId);
123 
124             DLFileRank lastFileRank = fileRanks.get(fileRanks.size() - 1);
125 
126             long lastFileRankId = lastFileRank.getFileRankId();
127 
128             try {
129                 dlFileRankPersistence.remove(lastFileRank);
130             }
131             catch (Exception e) {
132                 _log.warn(
133                     "Failed to remove file rank " + lastFileRankId +
134                         " because another thread already removed it");
135             }
136         }
137 
138         return fileRank;
139     }
140 
141     private static Log _log = LogFactoryUtil.getLog(
142         DLFileRankLocalServiceImpl.class);
143 
144 }