1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portlet.blogs.service.impl;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.SystemException;
19  import com.liferay.portal.kernel.util.OrderByComparator;
20  import com.liferay.portal.model.Group;
21  import com.liferay.portlet.blogs.model.BlogsEntry;
22  import com.liferay.portlet.blogs.model.BlogsStatsUser;
23  import com.liferay.portlet.blogs.service.base.BlogsStatsUserLocalServiceBaseImpl;
24  import com.liferay.portlet.blogs.util.comparator.EntryDisplayDateComparator;
25  import com.liferay.portlet.blogs.util.comparator.StatsUserLastPostDateComparator;
26  
27  import java.util.Date;
28  import java.util.List;
29  
30  /**
31   * <a href="BlogsStatsUserLocalServiceImpl.java.html"><b><i>View Source</i></b>
32   * </a>
33   *
34   * @author Brian Wing Shun Chan
35   */
36  public class BlogsStatsUserLocalServiceImpl
37      extends BlogsStatsUserLocalServiceBaseImpl {
38  
39      public void deleteStatsUserByGroupId(long groupId)
40          throws SystemException {
41  
42          blogsStatsUserPersistence.removeByGroupId(groupId);
43      }
44  
45      public void deleteStatsUserByUserId(long userId) throws SystemException {
46          blogsStatsUserPersistence.removeByUserId(userId);
47      }
48  
49      public List<BlogsStatsUser> getCompanyStatsUsers(
50              long companyId, int start, int end)
51          throws SystemException {
52  
53          return blogsStatsUserPersistence.findByC_E(
54              companyId, 0, start, end, new StatsUserLastPostDateComparator());
55      }
56  
57      public List<BlogsStatsUser> getCompanyStatsUsers(
58              long companyId, int start, int end, OrderByComparator obc)
59          throws SystemException {
60  
61          return blogsStatsUserPersistence.findByC_E(
62              companyId, 0, start, end, obc);
63      }
64  
65      public int getCompanyStatsUsersCount(long companyId)
66          throws SystemException {
67  
68          return blogsStatsUserPersistence.countByC_E(companyId, 0);
69      }
70  
71      public List<BlogsStatsUser> getGroupsStatsUsers(
72              long companyId, long groupId, int start, int end)
73          throws SystemException {
74  
75          return blogsStatsUserFinder.findByGroupIds(
76              companyId, groupId, start, end);
77      }
78  
79      public List<BlogsStatsUser> getGroupStatsUsers(
80              long groupId, int start, int end)
81          throws SystemException {
82  
83          return blogsStatsUserPersistence.findByG_E(
84              groupId, 0, start, end, new StatsUserLastPostDateComparator());
85      }
86  
87      public List<BlogsStatsUser> getGroupStatsUsers(
88              long groupId, int start, int end, OrderByComparator obc)
89          throws SystemException {
90  
91          return blogsStatsUserPersistence.findByG_E(groupId, 0, start, end, obc);
92      }
93  
94      public int getGroupStatsUsersCount(long groupId) throws SystemException {
95          return blogsStatsUserPersistence.countByG_E(groupId, 0);
96      }
97  
98      public List<BlogsStatsUser> getOrganizationStatsUsers(
99              long organizationId, int start, int end)
100         throws SystemException {
101 
102         return blogsStatsUserFinder.findByOrganizationId(
103             organizationId, start, end, new StatsUserLastPostDateComparator());
104     }
105 
106     public List<BlogsStatsUser> getOrganizationStatsUsers(
107             long organizationId, int start, int end, OrderByComparator obc)
108         throws SystemException {
109 
110         return blogsStatsUserFinder.findByOrganizationId(
111             organizationId, start, end, obc);
112     }
113 
114     public int getOrganizationStatsUsersCount(long organizationId)
115         throws SystemException {
116 
117         return blogsStatsUserFinder.countByOrganizationId(organizationId);
118     }
119 
120     public BlogsStatsUser getStatsUser(long groupId, long userId)
121         throws PortalException, SystemException {
122 
123         BlogsStatsUser statsUser = blogsStatsUserPersistence.fetchByG_U(
124             groupId, userId);
125 
126         if (statsUser == null) {
127             Group group = groupPersistence.findByPrimaryKey(groupId);
128 
129             long statsUserId = counterLocalService.increment();
130 
131             statsUser = blogsStatsUserPersistence.create(statsUserId);
132 
133             statsUser.setCompanyId(group.getCompanyId());
134             statsUser.setGroupId(groupId);
135             statsUser.setUserId(userId);
136 
137             blogsStatsUserPersistence.update(statsUser, false);
138         }
139 
140         return statsUser;
141     }
142 
143     public void updateStatsUser(long groupId, long userId)
144         throws PortalException, SystemException {
145 
146         updateStatsUser(groupId, userId, null);
147     }
148 
149     public void updateStatsUser(long groupId, long userId, Date displayDate)
150         throws PortalException, SystemException {
151 
152         int entryCount = blogsEntryPersistence.countByG_U(groupId, userId);
153 
154         BlogsStatsUser statsUser = getStatsUser(groupId, userId);
155 
156         statsUser.setEntryCount(entryCount);
157 
158         if (displayDate != null) {
159             BlogsEntry blogsEntry = blogsEntryPersistence.findByG_U_First(
160                 groupId, userId, new EntryDisplayDateComparator());
161 
162             Date lastDisplayDate = blogsEntry.getDisplayDate();
163 
164             Date lastPostDate = statsUser.getLastPostDate();
165 
166             if (lastPostDate == null) {
167                 statsUser.setLastPostDate(displayDate);
168             }
169             else if (displayDate.after(lastPostDate)) {
170                 statsUser.setLastPostDate(displayDate);
171             }
172             else if (lastDisplayDate.before(lastPostDate)) {
173                 statsUser.setLastPostDate(lastDisplayDate);
174             }
175         }
176 
177         blogsStatsUserPersistence.update(statsUser, false);
178     }
179 
180 }