1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.documentlibrary.service.impl;
21  
22  import com.liferay.portal.PortalException;
23  import com.liferay.portal.SystemException;
24  import com.liferay.portal.kernel.dao.orm.QueryUtil;
25  import com.liferay.portal.util.PropsValues;
26  import com.liferay.portlet.documentlibrary.NoSuchFileRankException;
27  import com.liferay.portlet.documentlibrary.model.DLFileRank;
28  import com.liferay.portlet.documentlibrary.model.DLFolder;
29  import com.liferay.portlet.documentlibrary.service.base.DLFileRankLocalServiceBaseImpl;
30  import com.liferay.portlet.documentlibrary.util.comparator.FileRankCreateDateComparator;
31  
32  import java.util.Date;
33  import java.util.List;
34  
35  /**
36   * <a href="DLFileRankLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   *
40   */
41  public class DLFileRankLocalServiceImpl extends DLFileRankLocalServiceBaseImpl {
42  
43      public void deleteFileRanks(long userId) throws SystemException {
44          dlFileRankPersistence.removeByUserId(userId);
45      }
46  
47      public void deleteFileRanks(long folderId, String name)
48          throws SystemException {
49  
50          dlFileRankPersistence.removeByF_N(folderId, name);
51      }
52  
53      public List<DLFileRank> getFileRanks(long groupId, long userId)
54          throws SystemException {
55  
56          return getFileRanks(
57              groupId, userId, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
58      }
59  
60      public List<DLFileRank> getFileRanks(
61              long groupId, long userId, int start, int end)
62          throws SystemException {
63  
64          return dlFileRankPersistence.findByG_U(
65              groupId, userId, start, end, new FileRankCreateDateComparator());
66      }
67  
68      public DLFileRank updateFileRank(
69              long groupId, long companyId, long userId, long folderId,
70              String name)
71          throws PortalException, SystemException {
72  
73          if (!PropsValues.DL_FILE_RANK_ENABLED) {
74              return null;
75          }
76  
77          try {
78              dlFileRankPersistence.removeByC_U_F_N(
79                  companyId, userId, folderId, name);
80          }
81          catch (NoSuchFileRankException nsfre) {
82          }
83  
84          DLFolder folder = dlFolderPersistence.findByPrimaryKey(folderId);
85  
86          long fileRankId = counterLocalService.increment();
87  
88          DLFileRank fileRank = dlFileRankPersistence.create(fileRankId);
89  
90          fileRank.setGroupId(folder.getGroupId());
91          fileRank.setCompanyId(companyId);
92          fileRank.setUserId(userId);
93          fileRank.setCreateDate(new Date());
94          fileRank.setFolderId(folderId);
95          fileRank.setName(name);
96  
97          dlFileRankPersistence.update(fileRank, false);
98  
99          if (dlFileRankPersistence.countByG_U(groupId, userId) > 5) {
100             List<DLFileRank> fileRanks = getFileRanks(groupId, userId);
101 
102             DLFileRank lastFileRank = fileRanks.get(fileRanks.size() - 1);
103 
104             dlFileRankPersistence.remove(lastFileRank);
105         }
106 
107         return fileRank;
108     }
109 
110 }