1
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
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)
59 throws SystemException {
60
61 return blogsStatsUserPersistence.findByC_E(
62 companyId, 0, start, end, new StatsUserLastPostDateComparator());
63 }
64
65 public List<BlogsStatsUser> getCompanyStatsUsers(
66 long companyId, int start, int end, OrderByComparator obc)
67 throws SystemException {
68
69 return blogsStatsUserPersistence.findByC_E(
70 companyId, 0, start, end, obc);
71 }
72
73 public int getCompanyStatsUsersCount(long companyId)
74 throws SystemException {
75
76 return blogsStatsUserPersistence.countByC_E(companyId, 0);
77 }
78
79 public List<BlogsStatsUser> getGroupStatsUsers(
80 long groupId, int start, int end)
81 throws SystemException {
82
83 return blogsStatsUserPersistence.findByG_E(
84 groupId, 0, start, end, new StatsUserLastPostDateComparator());
85 }
86
87 public List<BlogsStatsUser> getGroupStatsUsers(
88 long groupId, int start, int end, OrderByComparator obc)
89 throws SystemException {
90
91 return blogsStatsUserPersistence.findByG_E(groupId, 0, start, end, obc);
92 }
93
94 public int getGroupStatsUsersCount(long groupId) throws SystemException {
95 return blogsStatsUserPersistence.countByG_E(groupId, 0);
96 }
97
98 public List<BlogsStatsUser> getOrganizationStatsUsers(
99 long organizationId, int start, int end)
100 throws SystemException {
101
102 return blogsStatsUserFinder.findByOrganizationId(
103 organizationId, start, end, new StatsUserLastPostDateComparator());
104 }
105
106 public List<BlogsStatsUser> getOrganizationStatsUsers(
107 long organizationId, int start, int end, OrderByComparator obc)
108 throws SystemException {
109
110 return blogsStatsUserFinder.findByOrganizationId(
111 organizationId, start, end, obc);
112 }
113
114 public int getOrganizationStatsUsersCount(long organizationId)
115 throws SystemException {
116
117 return blogsStatsUserFinder.countByOrganizationId(organizationId);
118 }
119
120 public BlogsStatsUser getStatsUser(long groupId, long userId)
121 throws PortalException, SystemException {
122
123 BlogsStatsUser statsUser = blogsStatsUserPersistence.fetchByG_U(
124 groupId, userId);
125
126 if (statsUser == null) {
127 Group group = groupPersistence.findByPrimaryKey(groupId);
128
129 long statsUserId = counterLocalService.increment();
130
131 statsUser = blogsStatsUserPersistence.create(statsUserId);
132
133 statsUser.setCompanyId(group.getCompanyId());
134 statsUser.setGroupId(groupId);
135 statsUser.setUserId(userId);
136
137 blogsStatsUserPersistence.update(statsUser, false);
138 }
139
140 return statsUser;
141 }
142
143 public void updateStatsUser(long groupId, long userId)
144 throws PortalException, SystemException {
145
146 updateStatsUser(groupId, userId, null);
147 }
148
149 public void updateStatsUser(long groupId, long userId, Date displayDate)
150 throws PortalException, SystemException {
151
152 int entryCount = blogsEntryPersistence.countByG_U(groupId, userId);
153
154 BlogsStatsUser statsUser = getStatsUser(groupId, userId);
155
156 statsUser.setEntryCount(entryCount);
157
158 if (displayDate != null) {
159 BlogsEntry blogsEntry = blogsEntryPersistence.findByG_U_First(
160 groupId, userId, new EntryDisplayDateComparator());
161
162 Date lastDisplayDate = blogsEntry.getDisplayDate();
163
164 Date lastPostDate = statsUser.getLastPostDate();
165
166 if (lastPostDate == null) {
167 statsUser.setLastPostDate(displayDate);
168 }
169 else if (displayDate.after(lastPostDate)) {
170 statsUser.setLastPostDate(displayDate);
171 }
172 else if (lastDisplayDate.before(lastPostDate)) {
173 statsUser.setLastPostDate(lastDisplayDate);
174 }
175 }
176
177 blogsStatsUserPersistence.update(statsUser, false);
178 }
179
180 }