001
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.SystemException;
019 import com.liferay.portal.kernel.log.Log;
020 import com.liferay.portal.kernel.log.LogFactoryUtil;
021 import com.liferay.portal.service.ServiceContext;
022 import com.liferay.portal.util.PropsValues;
023 import com.liferay.portlet.documentlibrary.model.DLFileRank;
024 import com.liferay.portlet.documentlibrary.service.base.DLFileRankLocalServiceBaseImpl;
025 import com.liferay.portlet.documentlibrary.util.comparator.FileRankCreateDateComparator;
026
027 import java.util.List;
028
029
032 public class DLFileRankLocalServiceImpl extends DLFileRankLocalServiceBaseImpl {
033
034 public DLFileRank addFileRank(
035 long groupId, long companyId, long userId, long folderId,
036 String name, ServiceContext serviceContext)
037 throws SystemException {
038
039 long fileRankId = counterLocalService.increment();
040
041 DLFileRank fileRank = dlFileRankPersistence.create(fileRankId);
042
043 fileRank.setGroupId(groupId);
044 fileRank.setCompanyId(companyId);
045 fileRank.setUserId(userId);
046 fileRank.setCreateDate(serviceContext.getCreateDate(null));
047 fileRank.setFolderId(folderId);
048 fileRank.setName(name);
049
050 try {
051 dlFileRankPersistence.update(fileRank, false);
052 }
053 catch (SystemException se) {
054 if (_log.isWarnEnabled()) {
055 _log.warn(
056 "Add failed, fetch {companyId=" + companyId + ", userId=" +
057 userId + ", folderId=" + folderId + ", name=" + name +
058 "}");
059 }
060
061 fileRank = dlFileRankPersistence.fetchByC_U_F_N(
062 companyId, userId, folderId, name, false);
063
064 if (fileRank == null) {
065 throw se;
066 }
067 }
068
069 return fileRank;
070 }
071
072 public void deleteFileRanks(long userId) throws SystemException {
073 dlFileRankPersistence.removeByUserId(userId);
074 }
075
076 public void deleteFileRanks(long folderId, String name)
077 throws SystemException {
078
079 dlFileRankPersistence.removeByF_N(folderId, name);
080 }
081
082 public List<DLFileRank> getFileRanks(long groupId, long userId)
083 throws SystemException {
084
085 return getFileRanks(
086 groupId, userId, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
087 }
088
089 public List<DLFileRank> getFileRanks(
090 long groupId, long userId, int start, int end)
091 throws SystemException {
092
093 return dlFileRankPersistence.findByG_U(
094 groupId, userId, start, end, new FileRankCreateDateComparator());
095 }
096
097 public DLFileRank updateFileRank(
098 long groupId, long companyId, long userId, long folderId,
099 String name, ServiceContext serviceContext)
100 throws SystemException {
101
102 if (!PropsValues.DL_FILE_RANK_ENABLED) {
103 return null;
104 }
105
106 DLFileRank fileRank = dlFileRankPersistence.fetchByC_U_F_N(
107 companyId, userId, folderId, name);
108
109 if (fileRank != null) {
110 fileRank.setCreateDate(serviceContext.getCreateDate(null));
111
112 dlFileRankPersistence.update(fileRank, false);
113 }
114 else {
115 fileRank = addFileRank(
116 groupId, companyId, userId, folderId, name, serviceContext);
117 }
118
119 if (dlFileRankPersistence.countByG_U(groupId, userId) > 5) {
120 List<DLFileRank> fileRanks = getFileRanks(groupId, userId);
121
122 DLFileRank lastFileRank = fileRanks.get(fileRanks.size() - 1);
123
124 long lastFileRankId = lastFileRank.getFileRankId();
125
126 try {
127 dlFileRankPersistence.remove(lastFileRank);
128 }
129 catch (Exception e) {
130 _log.warn(
131 "Failed to remove file rank " + lastFileRankId +
132 " because another thread already removed it");
133 }
134 }
135
136 return fileRank;
137 }
138
139 private static Log _log = LogFactoryUtil.getLog(
140 DLFileRankLocalServiceImpl.class);
141
142 }