1   /**
2    * Copyright (c) 2000-2007 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.counter.service.CounterLocalServiceUtil;
26  import com.liferay.portal.PortalException;
27  import com.liferay.portal.SystemException;
28  import com.liferay.portal.kernel.util.OrderByComparator;
29  import com.liferay.portal.model.Group;
30  import com.liferay.portal.service.persistence.GroupUtil;
31  import com.liferay.portlet.blogs.model.BlogsStatsUser;
32  import com.liferay.portlet.blogs.service.base.BlogsStatsUserLocalServiceBaseImpl;
33  import com.liferay.portlet.blogs.service.persistence.BlogsEntryUtil;
34  import com.liferay.portlet.blogs.service.persistence.BlogsStatsUserFinder;
35  import com.liferay.portlet.blogs.service.persistence.BlogsStatsUserUtil;
36  
37  import java.util.Date;
38  import java.util.List;
39  
40  /**
41   * <a href="BlogsStatsUserLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   *
45   */
46  public class BlogsStatsUserLocalServiceImpl
47      extends BlogsStatsUserLocalServiceBaseImpl {
48  
49      public void deleteStatsUserByGroupId(long groupId)
50          throws SystemException {
51  
52          BlogsStatsUserUtil.removeByGroupId(groupId);
53      }
54  
55      public void deleteStatsUserByUserId(long userId) throws SystemException {
56          BlogsStatsUserUtil.removeByUserId(userId);
57      }
58  
59      public List getCompanyStatsUsers(
60              long companyId, int begin, int end, OrderByComparator obc)
61          throws SystemException {
62  
63          return BlogsStatsUserUtil.findByC_E(companyId, 0, begin, end, obc);
64      }
65  
66      public int getCompanyStatsUsersCount(long companyId)
67          throws SystemException {
68  
69          return BlogsStatsUserUtil.countByC_E(companyId, 0);
70      }
71  
72      public List getGroupStatsUsers(
73              long groupId, int begin, int end, OrderByComparator obc)
74          throws SystemException {
75  
76          return BlogsStatsUserUtil.findByG_E(groupId, 0, begin, end, obc);
77      }
78  
79      public int getGroupStatsUsersCount(long groupId) throws SystemException {
80          return BlogsStatsUserUtil.countByG_E(groupId, 0);
81      }
82  
83      public List getOrganizationStatsUsers(
84              long organizationId, int begin, int end, OrderByComparator obc)
85          throws SystemException {
86  
87          return BlogsStatsUserFinder.findByOrganizationId(
88              organizationId, begin, end, obc);
89      }
90  
91      public int getOrganizationStatsUsersCount(long organizationId)
92          throws SystemException {
93  
94          return BlogsStatsUserFinder.countByOrganizationId(organizationId);
95      }
96  
97      public BlogsStatsUser getStatsUser(long groupId, long userId)
98          throws PortalException, SystemException {
99  
100         BlogsStatsUser statsUser = BlogsStatsUserUtil.fetchByG_U(
101             groupId, userId);
102 
103         if (statsUser == null) {
104             Group group = GroupUtil.findByPrimaryKey(groupId);
105 
106             long statsUserId = CounterLocalServiceUtil.increment();
107 
108             statsUser = BlogsStatsUserUtil.create(statsUserId);
109 
110             statsUser.setCompanyId(group.getCompanyId());
111             statsUser.setGroupId(groupId);
112             statsUser.setUserId(userId);
113 
114             BlogsStatsUserUtil.update(statsUser);
115         }
116 
117         return statsUser;
118     }
119 
120     public void updateStatsUser(long groupId, long userId, Date lastPostDate)
121         throws PortalException, SystemException {
122 
123         int entryCount = BlogsEntryUtil.countByG_U(groupId, userId);
124 
125         BlogsStatsUser statsUser = getStatsUser(groupId, userId);
126 
127         statsUser.setEntryCount(entryCount);
128         statsUser.setLastPostDate(lastPostDate);
129 
130         BlogsStatsUserUtil.update(statsUser);
131     }
132 
133 }