1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.blogs.service.impl;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.kernel.util.OrderByComparator;
20  import com.liferay.portal.kernel.workflow.StatusConstants;
21  import com.liferay.portal.model.Group;
22  import com.liferay.portlet.blogs.model.BlogsEntry;
23  import com.liferay.portlet.blogs.model.BlogsStatsUser;
24  import com.liferay.portlet.blogs.service.base.BlogsStatsUserLocalServiceBaseImpl;
25  import com.liferay.portlet.blogs.util.comparator.EntryDisplayDateComparator;
26  import com.liferay.portlet.blogs.util.comparator.StatsUserLastPostDateComparator;
27  
28  import java.util.Date;
29  import java.util.List;
30  
31  /**
32   * <a href="BlogsStatsUserLocalServiceImpl.java.html"><b><i>View Source</i></b>
33   * </a>
34   *
35   * @author Brian Wing Shun Chan
36   */
37  public class BlogsStatsUserLocalServiceImpl
38      extends BlogsStatsUserLocalServiceBaseImpl {
39  
40      public void deleteStatsUserByGroupId(long groupId)
41          throws SystemException {
42  
43          blogsStatsUserPersistence.removeByGroupId(groupId);
44      }
45  
46      public void deleteStatsUserByUserId(long userId) throws SystemException {
47          blogsStatsUserPersistence.removeByUserId(userId);
48      }
49  
50      public List<BlogsStatsUser> getCompanyStatsUsers(
51              long companyId, int start, int end)
52          throws SystemException {
53  
54          return blogsStatsUserPersistence.findByC_E(
55              companyId, 0, start, end, new StatsUserLastPostDateComparator());
56      }
57  
58      public List<BlogsStatsUser> getCompanyStatsUsers(
59              long companyId, int start, int end, OrderByComparator obc)
60          throws SystemException {
61  
62          return blogsStatsUserPersistence.findByC_E(
63              companyId, 0, start, end, obc);
64      }
65  
66      public int getCompanyStatsUsersCount(long companyId)
67          throws SystemException {
68  
69          return blogsStatsUserPersistence.countByC_E(companyId, 0);
70      }
71  
72      public List<BlogsStatsUser> getGroupStatsUsers(
73              long groupId, int start, int end)
74          throws SystemException {
75  
76          return blogsStatsUserPersistence.findByG_E(
77              groupId, 0, start, end, new StatsUserLastPostDateComparator());
78      }
79  
80      public List<BlogsStatsUser> getGroupStatsUsers(
81              long groupId, int start, int end, OrderByComparator obc)
82          throws SystemException {
83  
84          return blogsStatsUserPersistence.findByG_E(groupId, 0, start, end, obc);
85      }
86  
87      public int getGroupStatsUsersCount(long groupId) throws SystemException {
88          return blogsStatsUserPersistence.countByG_E(groupId, 0);
89      }
90  
91      public List<BlogsStatsUser> getOrganizationStatsUsers(
92              long organizationId, int start, int end)
93          throws SystemException {
94  
95          return blogsStatsUserFinder.findByOrganizationId(
96              organizationId, start, end, new StatsUserLastPostDateComparator());
97      }
98  
99      public List<BlogsStatsUser> getOrganizationStatsUsers(
100             long organizationId, int start, int end, OrderByComparator obc)
101         throws SystemException {
102 
103         return blogsStatsUserFinder.findByOrganizationId(
104             organizationId, start, end, obc);
105     }
106 
107     public int getOrganizationStatsUsersCount(long organizationId)
108         throws SystemException {
109 
110         return blogsStatsUserFinder.countByOrganizationId(organizationId);
111     }
112 
113     public BlogsStatsUser getStatsUser(long groupId, long userId)
114         throws PortalException, SystemException {
115 
116         BlogsStatsUser statsUser = blogsStatsUserPersistence.fetchByG_U(
117             groupId, userId);
118 
119         if (statsUser == null) {
120             Group group = groupPersistence.findByPrimaryKey(groupId);
121 
122             long statsUserId = counterLocalService.increment();
123 
124             statsUser = blogsStatsUserPersistence.create(statsUserId);
125 
126             statsUser.setCompanyId(group.getCompanyId());
127             statsUser.setGroupId(groupId);
128             statsUser.setUserId(userId);
129 
130             blogsStatsUserPersistence.update(statsUser, false);
131         }
132 
133         return statsUser;
134     }
135 
136     public void updateStatsUser(long groupId, long userId)
137         throws PortalException, SystemException {
138 
139         updateStatsUser(groupId, userId, null);
140     }
141 
142     public void updateStatsUser(long groupId, long userId, Date displayDate)
143         throws PortalException, SystemException {
144 
145         int entryCount = blogsEntryPersistence.countByG_U_S(
146             groupId, userId, StatusConstants.APPROVED);
147 
148         BlogsStatsUser statsUser = getStatsUser(groupId, userId);
149 
150         statsUser.setEntryCount(entryCount);
151 
152         if (displayDate != null) {
153             BlogsEntry blogsEntry = blogsEntryPersistence.findByG_U_S_First(
154                 groupId, userId, StatusConstants.APPROVED,
155                 new EntryDisplayDateComparator());
156 
157             Date lastDisplayDate = blogsEntry.getDisplayDate();
158 
159             Date lastPostDate = statsUser.getLastPostDate();
160 
161             if (lastPostDate == null) {
162                 statsUser.setLastPostDate(displayDate);
163             }
164             else if (displayDate.after(lastPostDate)) {
165                 statsUser.setLastPostDate(displayDate);
166             }
167             else if (lastDisplayDate.before(lastPostDate)) {
168                 statsUser.setLastPostDate(lastDisplayDate);
169             }
170         }
171 
172         blogsStatsUserPersistence.update(statsUser, false);
173     }
174 
175 }