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