1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.documentlibrary.service.impl;
16  
17  import com.liferay.portal.SystemException;
18  import com.liferay.portal.kernel.dao.orm.QueryUtil;
19  import com.liferay.portal.kernel.log.Log;
20  import com.liferay.portal.kernel.log.LogFactoryUtil;
21  import com.liferay.portal.service.ServiceContext;
22  import com.liferay.portal.util.PropsValues;
23  import com.liferay.portlet.documentlibrary.model.DLFileRank;
24  import com.liferay.portlet.documentlibrary.service.base.DLFileRankLocalServiceBaseImpl;
25  import com.liferay.portlet.documentlibrary.util.comparator.FileRankCreateDateComparator;
26  
27  import java.util.Date;
28  import java.util.List;
29  
30  /**
31   * <a href="DLFileRankLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Brian Wing Shun Chan
34   */
35  public class DLFileRankLocalServiceImpl extends DLFileRankLocalServiceBaseImpl {
36  
37      /**
38       * @deprecated
39       */
40      public DLFileRank addFileRank(
41              long groupId, long companyId, long userId, long folderId,
42              String name)
43          throws SystemException {
44  
45          return addFileRank(
46              groupId, companyId, userId, folderId, name, new ServiceContext());
47      }
48  
49      public DLFileRank addFileRank(
50              long groupId, long companyId, long userId, long folderId,
51              String name, ServiceContext serviceContext)
52          throws SystemException {
53  
54          long fileRankId = counterLocalService.increment();
55  
56          DLFileRank fileRank = dlFileRankPersistence.create(fileRankId);
57  
58          fileRank.setGroupId(groupId);
59          fileRank.setCompanyId(companyId);
60          fileRank.setUserId(userId);
61          fileRank.setCreateDate(serviceContext.getCreateDate(null));
62          fileRank.setFolderId(folderId);
63          fileRank.setName(name);
64  
65          try {
66              dlFileRankPersistence.update(fileRank, false);
67          }
68          catch (SystemException se) {
69              if (_log.isWarnEnabled()) {
70                  _log.warn(
71                      "Add failed, fetch {companyId=" + companyId + ", userId=" +
72                          userId + ", folderId=" + folderId + ", name=" + name +
73                              "}");
74              }
75  
76              fileRank = dlFileRankPersistence.fetchByC_U_F_N(
77                  companyId, userId, folderId, name, false);
78  
79              if (fileRank == null) {
80                  throw se;
81              }
82          }
83  
84          return fileRank;
85      }
86  
87      public void deleteFileRanks(long userId) throws SystemException {
88          dlFileRankPersistence.removeByUserId(userId);
89      }
90  
91      public void deleteFileRanks(long folderId, String name)
92          throws SystemException {
93  
94          dlFileRankPersistence.removeByF_N(folderId, name);
95      }
96  
97      public List<DLFileRank> getFileRanks(long groupId, long userId)
98          throws SystemException {
99  
100         return getFileRanks(
101             groupId, userId, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
102     }
103 
104     public List<DLFileRank> getFileRanks(
105             long groupId, long userId, int start, int end)
106         throws SystemException {
107 
108         return dlFileRankPersistence.findByG_U(
109             groupId, userId, start, end, new FileRankCreateDateComparator());
110     }
111 
112     /**
113      * @deprecated
114      */
115     public DLFileRank updateFileRank(
116             long groupId, long companyId, long userId, long folderId,
117             String name)
118         throws SystemException {
119 
120         return updateFileRank(
121             groupId, companyId, userId, folderId, name, new ServiceContext());
122     }
123 
124     public DLFileRank updateFileRank(
125             long groupId, long companyId, long userId, long folderId,
126             String name, ServiceContext serviceContext)
127         throws SystemException {
128 
129         if (!PropsValues.DL_FILE_RANK_ENABLED) {
130             return null;
131         }
132 
133         DLFileRank fileRank = dlFileRankPersistence.fetchByC_U_F_N(
134             companyId, userId, folderId, name);
135 
136         if (fileRank != null) {
137             fileRank.setCreateDate(serviceContext.getCreateDate(new Date()));
138 
139             dlFileRankPersistence.update(fileRank, false);
140         }
141         else {
142             fileRank = addFileRank(
143                 groupId, companyId, userId, folderId, name, serviceContext);
144         }
145 
146         if (dlFileRankPersistence.countByG_U(groupId, userId) > 5) {
147             List<DLFileRank> fileRanks = getFileRanks(groupId, userId);
148 
149             DLFileRank lastFileRank = fileRanks.get(fileRanks.size() - 1);
150 
151             long lastFileRankId = lastFileRank.getFileRankId();
152 
153             try {
154                 dlFileRankPersistence.remove(lastFileRank);
155             }
156             catch (Exception e) {
157                 _log.warn(
158                     "Failed to remove file rank " + lastFileRankId +
159                         " because another thread already removed it");
160             }
161         }
162 
163         return fileRank;
164     }
165 
166     private static Log _log = LogFactoryUtil.getLog(
167         DLFileRankLocalServiceImpl.class);
168 
169 }