1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.blogs.service.impl;
21  
22  import com.liferay.portal.PortalException;
23  import com.liferay.portal.SystemException;
24  import com.liferay.portal.kernel.util.OrderByComparator;
25  import com.liferay.portal.model.Group;
26  import com.liferay.portlet.blogs.model.BlogsEntry;
27  import com.liferay.portlet.blogs.model.BlogsStatsUser;
28  import com.liferay.portlet.blogs.service.base.BlogsStatsUserLocalServiceBaseImpl;
29  import com.liferay.portlet.blogs.util.comparator.EntryDisplayDateComparator;
30  import com.liferay.portlet.blogs.util.comparator.StatsUserLastPostDateComparator;
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 start, int end)
57          throws SystemException {
58  
59          return blogsStatsUserPersistence.findByC_E(
60              companyId, 0, start, end, new StatsUserLastPostDateComparator());
61      }
62  
63      public List<BlogsStatsUser> getCompanyStatsUsers(
64              long companyId, int start, int end, OrderByComparator obc)
65          throws SystemException {
66  
67          return blogsStatsUserPersistence.findByC_E(
68              companyId, 0, start, end, obc);
69      }
70  
71      public int getCompanyStatsUsersCount(long companyId)
72          throws SystemException {
73  
74          return blogsStatsUserPersistence.countByC_E(companyId, 0);
75      }
76  
77      public List<BlogsStatsUser> getGroupStatsUsers(
78              long groupId, int start, int end)
79          throws SystemException {
80  
81          return blogsStatsUserPersistence.findByG_E(
82              groupId, 0, start, end, new StatsUserLastPostDateComparator());
83      }
84  
85      public List<BlogsStatsUser> getGroupStatsUsers(
86              long groupId, int start, int end, OrderByComparator obc)
87          throws SystemException {
88  
89          return blogsStatsUserPersistence.findByG_E(groupId, 0, start, end, obc);
90      }
91  
92      public int getGroupStatsUsersCount(long groupId) throws SystemException {
93          return blogsStatsUserPersistence.countByG_E(groupId, 0);
94      }
95  
96      public List<BlogsStatsUser> getOrganizationStatsUsers(
97              long organizationId, int start, int end)
98          throws SystemException {
99  
100         return blogsStatsUserFinder.findByOrganizationId(
101             organizationId, start, end, new StatsUserLastPostDateComparator());
102     }
103 
104     public List<BlogsStatsUser> getOrganizationStatsUsers(
105             long organizationId, int start, int end, OrderByComparator obc)
106         throws SystemException {
107 
108         return blogsStatsUserFinder.findByOrganizationId(
109             organizationId, start, end, obc);
110     }
111 
112     public int getOrganizationStatsUsersCount(long organizationId)
113         throws SystemException {
114 
115         return blogsStatsUserFinder.countByOrganizationId(organizationId);
116     }
117 
118     public BlogsStatsUser getStatsUser(long groupId, long userId)
119         throws PortalException, SystemException {
120 
121         BlogsStatsUser statsUser = blogsStatsUserPersistence.fetchByG_U(
122             groupId, userId);
123 
124         if (statsUser == null) {
125             Group group = groupPersistence.findByPrimaryKey(groupId);
126 
127             long statsUserId = counterLocalService.increment();
128 
129             statsUser = blogsStatsUserPersistence.create(statsUserId);
130 
131             statsUser.setCompanyId(group.getCompanyId());
132             statsUser.setGroupId(groupId);
133             statsUser.setUserId(userId);
134 
135             blogsStatsUserPersistence.update(statsUser, false);
136         }
137 
138         return statsUser;
139     }
140 
141     public void updateStatsUser(long groupId, long userId)
142         throws PortalException, SystemException {
143 
144         updateStatsUser(groupId, userId, null);
145     }
146 
147     public void updateStatsUser(long groupId, long userId, Date displayDate)
148         throws PortalException, SystemException {
149 
150         int entryCount = blogsEntryPersistence.countByG_U(groupId, userId);
151 
152         BlogsStatsUser statsUser = getStatsUser(groupId, userId);
153 
154         statsUser.setEntryCount(entryCount);
155 
156         if (displayDate != null) {
157             BlogsEntry blogsEntry = blogsEntryPersistence.findByG_U_First(
158                 groupId, userId, new EntryDisplayDateComparator());
159 
160             Date lastDisplayDate = blogsEntry.getDisplayDate();
161 
162             Date lastPostDate = statsUser.getLastPostDate();
163 
164             if (lastPostDate == null) {
165                 statsUser.setLastPostDate(displayDate);
166             }
167             else if (displayDate.after(lastPostDate)) {
168                 statsUser.setLastPostDate(displayDate);
169             }
170             else if (lastDisplayDate.before(lastPostDate)) {
171                 statsUser.setLastPostDate(lastDisplayDate);
172             }
173         }
174 
175         blogsStatsUserPersistence.update(statsUser, false);
176     }
177 
178 }