001
014
015 package com.liferay.portlet.messageboards.service.impl;
016
017 import com.liferay.portal.kernel.dao.orm.DynamicQuery;
018 import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
019 import com.liferay.portal.kernel.dao.orm.ProjectionFactoryUtil;
020 import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
021 import com.liferay.portal.kernel.exception.PortalException;
022 import com.liferay.portal.kernel.exception.SystemException;
023 import com.liferay.portal.kernel.log.Log;
024 import com.liferay.portal.kernel.log.LogFactoryUtil;
025 import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
026 import com.liferay.portal.model.Group;
027 import com.liferay.portlet.messageboards.model.MBStatsUser;
028 import com.liferay.portlet.messageboards.model.impl.MBStatsUserImpl;
029 import com.liferay.portlet.messageboards.service.base.MBStatsUserLocalServiceBaseImpl;
030
031 import java.util.Date;
032 import java.util.List;
033
034
037 public class MBStatsUserLocalServiceImpl
038 extends MBStatsUserLocalServiceBaseImpl {
039
040 public MBStatsUser addStatsUser(long groupId, long userId)
041 throws SystemException {
042
043 long statsUserId = counterLocalService.increment();
044
045 MBStatsUser statsUser = mbStatsUserPersistence.create(statsUserId);
046
047 statsUser.setGroupId(groupId);
048 statsUser.setUserId(userId);
049
050 try {
051 mbStatsUserPersistence.update(statsUser, false);
052 }
053 catch (SystemException se) {
054 if (_log.isWarnEnabled()) {
055 _log.warn(
056 "Add failed, fetch {groupId=" + groupId + ", userId=" +
057 userId + "}");
058 }
059
060 statsUser = mbStatsUserPersistence.fetchByG_U(
061 groupId, userId, false);
062
063 if (statsUser == null) {
064 throw se;
065 }
066 }
067
068 return statsUser;
069 }
070
071 public void deleteStatsUser(long statsUserId)
072 throws PortalException, SystemException {
073
074 MBStatsUser statsUser = mbStatsUserPersistence.findByPrimaryKey(
075 statsUserId);
076
077 deleteStatsUser(statsUser);
078 }
079
080 public void deleteStatsUser(MBStatsUser statsUser) throws SystemException {
081 mbStatsUserPersistence.remove(statsUser);
082 }
083
084 public void deleteStatsUsersByGroupId(long groupId) throws SystemException {
085 List<MBStatsUser> statsUsers = mbStatsUserPersistence.findByGroupId(
086 groupId);
087
088 for (MBStatsUser statsUser : statsUsers) {
089 deleteStatsUser(statsUser);
090 }
091 }
092
093 public void deleteStatsUsersByUserId(long userId) throws SystemException {
094 List<MBStatsUser> statsUsers = mbStatsUserPersistence.findByUserId(
095 userId);
096
097 for (MBStatsUser statsUser : statsUsers) {
098 deleteStatsUser(statsUser);
099 }
100 }
101
102 public long getMessageCountByUserId(long userId) throws SystemException {
103 DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
104 MBStatsUser.class, MBStatsUserImpl.TABLE_NAME,
105 PortalClassLoaderUtil.getClassLoader());
106
107 dynamicQuery.setProjection(ProjectionFactoryUtil.sum("messageCount"));
108
109 dynamicQuery.add(PropertyFactoryUtil.forName("userId").eq(userId));
110
111 return dynamicQueryCount(dynamicQuery);
112 }
113
114 public MBStatsUser getStatsUser(long groupId, long userId)
115 throws SystemException {
116
117 MBStatsUser statsUser = mbStatsUserPersistence.fetchByG_U(
118 groupId, userId);
119
120 if (statsUser == null) {
121 statsUser = mbStatsUserLocalService.addStatsUser(groupId, userId);
122 }
123
124 return statsUser;
125 }
126
127 public List<MBStatsUser> getStatsUsersByGroupId(
128 long groupId, int start, int end)
129 throws PortalException, SystemException {
130
131 Group group = groupPersistence.findByPrimaryKey(groupId);
132
133 long defaultUserId = userLocalService.getDefaultUserId(
134 group.getCompanyId());
135
136 return mbStatsUserPersistence.findByG_NotU_NotM(
137 groupId, defaultUserId, 0, start, end);
138 }
139
140 public int getStatsUsersByGroupIdCount(long groupId)
141 throws PortalException, SystemException {
142
143 Group group = groupPersistence.findByPrimaryKey(groupId);
144
145 long defaultUserId = userLocalService.getDefaultUserId(
146 group.getCompanyId());
147
148 return mbStatsUserPersistence.countByG_NotU_NotM(
149 groupId, defaultUserId, 0);
150 }
151
152 public List<MBStatsUser> getStatsUsersByUserId(long userId)
153 throws SystemException {
154
155 return mbStatsUserPersistence.findByUserId(userId);
156 }
157
158 public MBStatsUser updateStatsUser(long groupId, long userId)
159 throws SystemException {
160
161 return updateStatsUser(groupId, userId, null);
162 }
163
164 public MBStatsUser updateStatsUser(
165 long groupId, long userId, Date lastPostDate)
166 throws SystemException {
167
168 int messageCount = mbMessagePersistence.countByG_U(groupId, userId);
169
170 MBStatsUser statsUser = getStatsUser(groupId, userId);
171
172 statsUser.setMessageCount(messageCount);
173
174 if (lastPostDate != null) {
175 statsUser.setLastPostDate(lastPostDate);
176 }
177
178 mbStatsUserPersistence.update(statsUser, false);
179
180 return statsUser;
181 }
182
183 private static Log _log = LogFactoryUtil.getLog(
184 MBStatsUserLocalServiceImpl.class);
185
186 }