1
22
23 package com.liferay.portlet.documentlibrary.service.impl;
24
25 import com.liferay.portal.SystemException;
26 import com.liferay.portal.kernel.dao.orm.QueryUtil;
27 import com.liferay.portal.kernel.log.Log;
28 import com.liferay.portal.kernel.log.LogFactoryUtil;
29 import com.liferay.portal.util.PropsValues;
30 import com.liferay.portlet.documentlibrary.model.DLFileRank;
31 import com.liferay.portlet.documentlibrary.service.base.DLFileRankLocalServiceBaseImpl;
32 import com.liferay.portlet.documentlibrary.util.comparator.FileRankCreateDateComparator;
33
34 import java.util.Date;
35 import java.util.List;
36
37
42 public class DLFileRankLocalServiceImpl extends DLFileRankLocalServiceBaseImpl {
43
44 public DLFileRank addFileRank(
45 long groupId, long companyId, long userId, long folderId,
46 String name)
47 throws SystemException {
48
49 long fileRankId = counterLocalService.increment();
50
51 DLFileRank fileRank = dlFileRankPersistence.create(fileRankId);
52
53 fileRank.setGroupId(groupId);
54 fileRank.setCompanyId(companyId);
55 fileRank.setUserId(userId);
56 fileRank.setCreateDate(new Date());
57 fileRank.setFolderId(folderId);
58 fileRank.setName(name);
59
60 try {
61 dlFileRankPersistence.update(fileRank, false);
62 }
63 catch (SystemException se) {
64 if (_log.isWarnEnabled()) {
65 _log.warn(
66 "Add failed, fetch {companyId=" + companyId + ", userId=" +
67 userId + ", folderId=" + folderId + ", name=" + name +
68 "}");
69 }
70
71 fileRank = dlFileRankPersistence.fetchByC_U_F_N(
72 companyId, userId, folderId, name, false);
73
74 if (fileRank == null) {
75 throw se;
76 }
77 }
78
79 return fileRank;
80 }
81
82 public void deleteFileRanks(long userId) throws SystemException {
83 dlFileRankPersistence.removeByUserId(userId);
84 }
85
86 public void deleteFileRanks(long folderId, String name)
87 throws SystemException {
88
89 dlFileRankPersistence.removeByF_N(folderId, name);
90 }
91
92 public List<DLFileRank> getFileRanks(long groupId, long userId)
93 throws SystemException {
94
95 return getFileRanks(
96 groupId, userId, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
97 }
98
99 public List<DLFileRank> getFileRanks(
100 long groupId, long userId, int start, int end)
101 throws SystemException {
102
103 return dlFileRankPersistence.findByG_U(
104 groupId, userId, start, end, new FileRankCreateDateComparator());
105 }
106
107 public DLFileRank updateFileRank(
108 long groupId, long companyId, long userId, long folderId,
109 String name)
110 throws SystemException {
111
112 if (!PropsValues.DL_FILE_RANK_ENABLED) {
113 return null;
114 }
115
116 DLFileRank fileRank = dlFileRankPersistence.fetchByC_U_F_N(
117 companyId, userId, folderId, name);
118
119 if (fileRank != null) {
120 fileRank.setCreateDate(new Date());
121
122 dlFileRankPersistence.update(fileRank, false);
123 }
124 else {
125 fileRank = addFileRank(
126 groupId, companyId, userId, folderId, name);
127 }
128
129 if (dlFileRankPersistence.countByG_U(groupId, userId) > 5) {
130 List<DLFileRank> fileRanks = getFileRanks(groupId, userId);
131
132 DLFileRank lastFileRank = fileRanks.get(fileRanks.size() - 1);
133
134 dlFileRankPersistence.remove(lastFileRank);
135 }
136
137 return fileRank;
138 }
139
140 private static Log _log =
141 LogFactoryUtil.getLog(DLFileRankLocalServiceImpl.class);
142
143 }