1   /**
2    * Copyright (c) 2000-2009 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  import com.liferay.portlet.blogs.util.comparator.StatsUserLastPostDateComparator;
34  
35  import java.util.Date;
36  import java.util.List;
37  
38  /**
39   * <a href="BlogsStatsUserLocalServiceImpl.java.html"><b><i>View Source</i></b>
40   * </a>
41   *
42   * @author Brian Wing Shun Chan
43   *
44   */
45  public class BlogsStatsUserLocalServiceImpl
46      extends BlogsStatsUserLocalServiceBaseImpl {
47  
48      public void deleteStatsUserByGroupId(long groupId)
49          throws SystemException {
50  
51          blogsStatsUserPersistence.removeByGroupId(groupId);
52      }
53  
54      public void deleteStatsUserByUserId(long userId) throws SystemException {
55          blogsStatsUserPersistence.removeByUserId(userId);
56      }
57  
58      public List<BlogsStatsUser> getCompanyStatsUsers(
59              long companyId, int start, int end)
60          throws SystemException {
61  
62          return blogsStatsUserPersistence.findByC_E(
63              companyId, 0, start, end, new StatsUserLastPostDateComparator());
64      }
65  
66      public List<BlogsStatsUser> getCompanyStatsUsers(
67              long companyId, int start, int end, OrderByComparator obc)
68          throws SystemException {
69  
70          return blogsStatsUserPersistence.findByC_E(
71              companyId, 0, start, end, obc);
72      }
73  
74      public int getCompanyStatsUsersCount(long companyId)
75          throws SystemException {
76  
77          return blogsStatsUserPersistence.countByC_E(companyId, 0);
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(groupId, userId);
154 
155         BlogsStatsUser statsUser = getStatsUser(groupId, userId);
156 
157         statsUser.setEntryCount(entryCount);
158 
159         if (displayDate != null) {
160             BlogsEntry blogsEntry = blogsEntryPersistence.findByG_U_First(
161                 groupId, userId, new EntryDisplayDateComparator());
162 
163             Date lastDisplayDate = blogsEntry.getDisplayDate();
164 
165             Date lastPostDate = statsUser.getLastPostDate();
166 
167             if (lastPostDate == null) {
168                 statsUser.setLastPostDate(displayDate);
169             }
170             else if (displayDate.after(lastPostDate)) {
171                 statsUser.setLastPostDate(displayDate);
172             }
173             else if (lastDisplayDate.before(lastPostDate)) {
174                 statsUser.setLastPostDate(lastDisplayDate);
175             }
176         }
177 
178         blogsStatsUserPersistence.update(statsUser, false);
179     }
180 
181 }