1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.blogs.service.impl;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.util.OrderByComparator;
28  import com.liferay.portal.model.Group;
29  import com.liferay.portlet.blogs.model.BlogsStatsUser;
30  import com.liferay.portlet.blogs.service.base.BlogsStatsUserLocalServiceBaseImpl;
31  
32  import java.util.Date;
33  import java.util.List;
34  
35  /**
36   * <a href="BlogsStatsUserLocalServiceImpl.java.html"><b><i>View Source</i></b>
37   * </a>
38   *
39   * @author Brian Wing Shun Chan
40   *
41   */
42  public class BlogsStatsUserLocalServiceImpl
43      extends BlogsStatsUserLocalServiceBaseImpl {
44  
45      public void deleteStatsUserByGroupId(long groupId)
46          throws SystemException {
47  
48          blogsStatsUserPersistence.removeByGroupId(groupId);
49      }
50  
51      public void deleteStatsUserByUserId(long userId) throws SystemException {
52          blogsStatsUserPersistence.removeByUserId(userId);
53      }
54  
55      public List<BlogsStatsUser> getCompanyStatsUsers(
56              long companyId, int begin, int end, OrderByComparator obc)
57          throws SystemException {
58  
59          return blogsStatsUserPersistence.findByC_E(
60              companyId, 0, begin, end, obc);
61      }
62  
63      public int getCompanyStatsUsersCount(long companyId)
64          throws SystemException {
65  
66          return blogsStatsUserPersistence.countByC_E(companyId, 0);
67      }
68  
69      public List<BlogsStatsUser> getGroupStatsUsers(
70              long groupId, int begin, int end, OrderByComparator obc)
71          throws SystemException {
72  
73          return blogsStatsUserPersistence.findByG_E(groupId, 0, begin, end, obc);
74      }
75  
76      public int getGroupStatsUsersCount(long groupId) throws SystemException {
77          return blogsStatsUserPersistence.countByG_E(groupId, 0);
78      }
79  
80      public List<BlogsStatsUser> getOrganizationStatsUsers(
81              long organizationId, int begin, int end, OrderByComparator obc)
82          throws SystemException {
83  
84          return blogsStatsUserFinder.findByOrganizationId(
85              organizationId, begin, end, obc);
86      }
87  
88      public int getOrganizationStatsUsersCount(long organizationId)
89          throws SystemException {
90  
91          return blogsStatsUserFinder.countByOrganizationId(organizationId);
92      }
93  
94      public BlogsStatsUser getStatsUser(long groupId, long userId)
95          throws PortalException, SystemException {
96  
97          BlogsStatsUser statsUser = blogsStatsUserPersistence.fetchByG_U(
98              groupId, userId);
99  
100         if (statsUser == null) {
101             Group group = groupPersistence.findByPrimaryKey(groupId);
102 
103             long statsUserId = counterLocalService.increment();
104 
105             statsUser = blogsStatsUserPersistence.create(statsUserId);
106 
107             statsUser.setCompanyId(group.getCompanyId());
108             statsUser.setGroupId(groupId);
109             statsUser.setUserId(userId);
110 
111             blogsStatsUserPersistence.update(statsUser, false);
112         }
113 
114         return statsUser;
115     }
116 
117     public void updateStatsUser(long groupId, long userId, Date lastPostDate)
118         throws PortalException, SystemException {
119 
120         int entryCount = blogsEntryPersistence.countByG_U(groupId, userId);
121 
122         BlogsStatsUser statsUser = getStatsUser(groupId, userId);
123 
124         statsUser.setEntryCount(entryCount);
125         statsUser.setLastPostDate(lastPostDate);
126 
127         blogsStatsUserPersistence.update(statsUser, false);
128     }
129 
130 }