1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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  /**
38   * <a href="DLFileRankLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   */
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 }