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> getGroupStatsUsers(
72              long groupId, int start, int end)
73          throws SystemException {
74  
75          return blogsStatsUserPersistence.findByG_E(
76              groupId, 0, start, end, new StatsUserLastPostDateComparator());
77      }
78  
79      public List<BlogsStatsUser> getGroupStatsUsers(
80              long groupId, int start, int end, OrderByComparator obc)
81          throws SystemException {
82  
83          return blogsStatsUserPersistence.findByG_E(groupId, 0, start, end, obc);
84      }
85  
86      public int getGroupStatsUsersCount(long groupId) throws SystemException {
87          return blogsStatsUserPersistence.countByG_E(groupId, 0);
88      }
89  
90      public List<BlogsStatsUser> getOrganizationStatsUsers(
91              long organizationId, int start, int end)
92          throws SystemException {
93  
94          return blogsStatsUserFinder.findByOrganizationId(
95              organizationId, start, end, new StatsUserLastPostDateComparator());
96      }
97  
98      public List<BlogsStatsUser> getOrganizationStatsUsers(
99              long organizationId, int start, int end, OrderByComparator obc)
100         throws SystemException {
101 
102         return blogsStatsUserFinder.findByOrganizationId(
103             organizationId, start, end, obc);
104     }
105 
106     public int getOrganizationStatsUsersCount(long organizationId)
107         throws SystemException {
108 
109         return blogsStatsUserFinder.countByOrganizationId(organizationId);
110     }
111 
112     public BlogsStatsUser getStatsUser(long groupId, long userId)
113         throws PortalException, SystemException {
114 
115         BlogsStatsUser statsUser = blogsStatsUserPersistence.fetchByG_U(
116             groupId, userId);
117 
118         if (statsUser == null) {
119             Group group = groupPersistence.findByPrimaryKey(groupId);
120 
121             long statsUserId = counterLocalService.increment();
122 
123             statsUser = blogsStatsUserPersistence.create(statsUserId);
124 
125             statsUser.setCompanyId(group.getCompanyId());
126             statsUser.setGroupId(groupId);
127             statsUser.setUserId(userId);
128 
129             blogsStatsUserPersistence.update(statsUser, false);
130         }
131 
132         return statsUser;
133     }
134 
135     public void updateStatsUser(long groupId, long userId)
136         throws PortalException, SystemException {
137 
138         updateStatsUser(groupId, userId, null);
139     }
140 
141     public void updateStatsUser(long groupId, long userId, Date displayDate)
142         throws PortalException, SystemException {
143 
144         int entryCount = blogsEntryPersistence.countByG_U(groupId, userId);
145 
146         BlogsStatsUser statsUser = getStatsUser(groupId, userId);
147 
148         statsUser.setEntryCount(entryCount);
149 
150         if (displayDate != null) {
151             BlogsEntry blogsEntry = blogsEntryPersistence.findByG_U_First(
152                 groupId, userId, new EntryDisplayDateComparator());
153 
154             Date lastDisplayDate = blogsEntry.getDisplayDate();
155 
156             Date lastPostDate = statsUser.getLastPostDate();
157 
158             if (lastPostDate == null) {
159                 statsUser.setLastPostDate(displayDate);
160             }
161             else if (displayDate.after(lastPostDate)) {
162                 statsUser.setLastPostDate(displayDate);
163             }
164             else if (lastDisplayDate.before(lastPostDate)) {
165                 statsUser.setLastPostDate(lastDisplayDate);
166             }
167         }
168 
169         blogsStatsUserPersistence.update(statsUser, false);
170     }
171 
172 }