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.BlogsEntry;
30  import com.liferay.portlet.blogs.model.BlogsStatsUser;
31  import com.liferay.portlet.blogs.service.base.BlogsStatsUserLocalServiceBaseImpl;
32  import com.liferay.portlet.blogs.util.comparator.EntryDisplayDateComparator;
33  
34  import java.util.Date;
35  import java.util.List;
36  
37  /**
38   * <a href="BlogsStatsUserLocalServiceImpl.java.html"><b><i>View Source</i></b>
39   * </a>
40   *
41   * @author Brian Wing Shun Chan
42   *
43   */
44  public class BlogsStatsUserLocalServiceImpl
45      extends BlogsStatsUserLocalServiceBaseImpl {
46  
47      public void deleteStatsUserByGroupId(long groupId)
48          throws SystemException {
49  
50          blogsStatsUserPersistence.removeByGroupId(groupId);
51      }
52  
53      public void deleteStatsUserByUserId(long userId) throws SystemException {
54          blogsStatsUserPersistence.removeByUserId(userId);
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, OrderByComparator obc)
73          throws SystemException {
74  
75          return blogsStatsUserPersistence.findByG_E(groupId, 0, start, end, obc);
76      }
77  
78      public int getGroupStatsUsersCount(long groupId) throws SystemException {
79          return blogsStatsUserPersistence.countByG_E(groupId, 0);
80      }
81  
82      public List<BlogsStatsUser> getOrganizationStatsUsers(
83              long organizationId, int start, int end, OrderByComparator obc)
84          throws SystemException {
85  
86          return blogsStatsUserFinder.findByOrganizationId(
87              organizationId, start, end, obc);
88      }
89  
90      public int getOrganizationStatsUsersCount(long organizationId)
91          throws SystemException {
92  
93          return blogsStatsUserFinder.countByOrganizationId(organizationId);
94      }
95  
96      public BlogsStatsUser getStatsUser(long groupId, long userId)
97          throws PortalException, SystemException {
98  
99          BlogsStatsUser statsUser = blogsStatsUserPersistence.fetchByG_U(
100             groupId, userId);
101 
102         if (statsUser == null) {
103             Group group = groupPersistence.findByPrimaryKey(groupId);
104 
105             long statsUserId = counterLocalService.increment();
106 
107             statsUser = blogsStatsUserPersistence.create(statsUserId);
108 
109             statsUser.setCompanyId(group.getCompanyId());
110             statsUser.setGroupId(groupId);
111             statsUser.setUserId(userId);
112 
113             blogsStatsUserPersistence.update(statsUser, false);
114         }
115 
116         return statsUser;
117     }
118 
119     public void updateStatsUser(long groupId, long userId)
120         throws PortalException, SystemException {
121 
122         updateStatsUser(groupId, userId, null);
123     }
124 
125     public void updateStatsUser(long groupId, long userId, Date displayDate)
126         throws PortalException, SystemException {
127 
128         int entryCount = blogsEntryPersistence.countByG_U(groupId, userId);
129 
130         BlogsStatsUser statsUser = getStatsUser(groupId, userId);
131 
132         statsUser.setEntryCount(entryCount);
133 
134         if (displayDate != null) {
135             BlogsEntry blogsEntry = blogsEntryPersistence.findByG_U_First(
136                 groupId, userId, new EntryDisplayDateComparator());
137 
138             Date lastDisplayDate = blogsEntry.getDisplayDate();
139 
140             Date lastPostDate = statsUser.getLastPostDate();
141 
142             if (lastPostDate == null) {
143                 statsUser.setLastPostDate(displayDate);
144             }
145             else if (displayDate.after(lastPostDate)) {
146                 statsUser.setLastPostDate(displayDate);
147             }
148             else if (lastDisplayDate.before(lastPostDate)) {
149                 statsUser.setLastPostDate(lastDisplayDate);
150             }
151         }
152 
153         blogsStatsUserPersistence.update(statsUser, false);
154     }
155 
156 }