1
22
23 package com.liferay.portlet.messageboards.service.impl;
24
25 import com.liferay.portal.SystemException;
26 import com.liferay.portal.kernel.dao.orm.DynamicQuery;
27 import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
28 import com.liferay.portal.kernel.dao.orm.ProjectionFactoryUtil;
29 import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
30 import com.liferay.portal.kernel.log.Log;
31 import com.liferay.portal.kernel.log.LogFactoryUtil;
32 import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
33 import com.liferay.portlet.messageboards.model.MBStatsUser;
34 import com.liferay.portlet.messageboards.model.impl.MBStatsUserImpl;
35 import com.liferay.portlet.messageboards.service.base.MBStatsUserLocalServiceBaseImpl;
36
37 import java.util.Date;
38 import java.util.List;
39
40
46 public class MBStatsUserLocalServiceImpl
47 extends MBStatsUserLocalServiceBaseImpl {
48
49 public MBStatsUser addStatsUser(long groupId, long userId)
50 throws SystemException {
51
52 long statsUserId = counterLocalService.increment();
53
54 MBStatsUser statsUser = mbStatsUserPersistence.create(statsUserId);
55
56 statsUser.setGroupId(groupId);
57 statsUser.setUserId(userId);
58
59 try {
60 mbStatsUserPersistence.update(statsUser, false);
61 }
62 catch (SystemException se) {
63 if (_log.isWarnEnabled()) {
64 _log.warn(
65 "Add failed, fetch {groupId=" + groupId + ", userId=" +
66 userId + "}");
67 }
68
69 statsUser = mbStatsUserPersistence.fetchByG_U(
70 groupId, userId, false);
71
72 if (statsUser == null) {
73 throw se;
74 }
75 }
76
77 return statsUser;
78 }
79
80
83 public void deleteStatsUserByGroupId(long groupId)
84 throws SystemException {
85
86 deleteStatsUsersByGroupId(groupId);
87 }
88
89 public void deleteStatsUsersByGroupId(long groupId)
90 throws SystemException {
91
92 mbStatsUserPersistence.removeByGroupId(groupId);
93 }
94
95
98 public void deleteStatsUserByUserId(long userId) throws SystemException {
99 deleteStatsUsersByUserId(userId);
100 }
101
102 public void deleteStatsUsersByUserId(long userId) throws SystemException {
103 mbStatsUserPersistence.removeByUserId(userId);
104 }
105
106 public int getMessageCountByUserId(long userId) throws SystemException {
107 DynamicQuery query = DynamicQueryFactoryUtil.forClass(
108 MBStatsUser.class, MBStatsUserImpl.TABLE_NAME,
109 PortalClassLoaderUtil.getClassLoader());
110
111 query = query.setProjection(
112 ProjectionFactoryUtil.sum("messageCount"));
113
114 query = query.add(PropertyFactoryUtil.forName("userId").eq(userId));
115
116 List<Object> results = mbStatsUserLocalService.dynamicQuery(query);
117
118 return (Integer)results.get(0);
119 }
120
121 public MBStatsUser getStatsUser(long groupId, long userId)
122 throws SystemException {
123
124 MBStatsUser statsUser = mbStatsUserPersistence.fetchByG_U(
125 groupId, userId);
126
127 if (statsUser == null) {
128 statsUser = mbStatsUserLocalService.addStatsUser(groupId, userId);
129 }
130
131 return statsUser;
132 }
133
134
137 public List<MBStatsUser> getStatsUsers(long groupId, int start, int end)
138 throws SystemException {
139
140 return getStatsUsersByGroupId(groupId, start, end);
141 }
142
143 public List<MBStatsUser> getStatsUsersByGroupId(
144 long groupId, int start, int end)
145 throws SystemException {
146
147 return mbStatsUserPersistence.findByG_M(groupId, 0, start, end);
148 }
149
150 public List<MBStatsUser> getStatsUsersByUserId(long userId)
151 throws SystemException {
152
153 return mbStatsUserPersistence.findByUserId(userId);
154 }
155
156 public int getStatsUsersByGroupIdCount(long groupId)
157 throws SystemException {
158
159 return mbStatsUserPersistence.countByG_M(groupId, 0);
160 }
161
162
165 public int getStatsUsersCount(long groupId) throws SystemException {
166 return getStatsUsersByGroupIdCount(groupId);
167 }
168
169 public MBStatsUser updateStatsUser(long groupId, long userId)
170 throws SystemException {
171
172 return updateStatsUser(groupId, userId, null);
173 }
174
175 public MBStatsUser updateStatsUser(
176 long groupId, long userId, Date lastPostDate)
177 throws SystemException {
178
179 int messageCount = mbMessagePersistence.countByG_U(groupId, userId);
180
181 MBStatsUser statsUser = getStatsUser(groupId, userId);
182
183 statsUser.setMessageCount(messageCount);
184
185 if (lastPostDate != null) {
186 statsUser.setLastPostDate(lastPostDate);
187 }
188
189 mbStatsUserPersistence.update(statsUser, false);
190
191 return statsUser;
192 }
193
194 private static Log _log =
195 LogFactoryUtil.getLog(MBStatsUserLocalServiceImpl.class);
196
197 }