1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.messageboards.service.impl;
16  
17  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
18  import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
19  import com.liferay.portal.kernel.dao.orm.ProjectionFactoryUtil;
20  import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
21  import com.liferay.portal.kernel.exception.SystemException;
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
25  import com.liferay.portlet.messageboards.model.MBStatsUser;
26  import com.liferay.portlet.messageboards.model.impl.MBStatsUserImpl;
27  import com.liferay.portlet.messageboards.service.base.MBStatsUserLocalServiceBaseImpl;
28  
29  import java.util.Date;
30  import java.util.List;
31  
32  /**
33   * <a href="MBStatsUserLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Brian Wing Shun Chan
36   */
37  public class MBStatsUserLocalServiceImpl
38      extends MBStatsUserLocalServiceBaseImpl {
39  
40      public MBStatsUser addStatsUser(long groupId, long userId)
41          throws SystemException {
42  
43          long statsUserId = counterLocalService.increment();
44  
45          MBStatsUser statsUser = mbStatsUserPersistence.create(statsUserId);
46  
47          statsUser.setGroupId(groupId);
48          statsUser.setUserId(userId);
49  
50          try {
51              mbStatsUserPersistence.update(statsUser, false);
52          }
53          catch (SystemException se) {
54              if (_log.isWarnEnabled()) {
55                  _log.warn(
56                      "Add failed, fetch {groupId=" + groupId + ", userId=" +
57                          userId + "}");
58              }
59  
60              statsUser = mbStatsUserPersistence.fetchByG_U(
61                  groupId, userId, false);
62  
63              if (statsUser == null) {
64                  throw se;
65              }
66          }
67  
68          return statsUser;
69      }
70  
71      public void deleteStatsUsersByGroupId(long groupId)
72          throws SystemException {
73  
74          mbStatsUserPersistence.removeByGroupId(groupId);
75      }
76  
77      public void deleteStatsUsersByUserId(long userId) throws SystemException {
78          mbStatsUserPersistence.removeByUserId(userId);
79      }
80  
81      public int getMessageCountByUserId(long userId) throws SystemException {
82          DynamicQuery query = DynamicQueryFactoryUtil.forClass(
83              MBStatsUser.class, MBStatsUserImpl.TABLE_NAME,
84              PortalClassLoaderUtil.getClassLoader());
85  
86          query = query.setProjection(
87              ProjectionFactoryUtil.sum("messageCount"));
88  
89          query = query.add(PropertyFactoryUtil.forName("userId").eq(userId));
90  
91          List<Object> results = dynamicQuery(query);
92  
93          return (Integer)results.get(0);
94      }
95  
96      public MBStatsUser getStatsUser(long groupId, long userId)
97          throws SystemException {
98  
99          MBStatsUser statsUser = mbStatsUserPersistence.fetchByG_U(
100             groupId, userId);
101 
102         if (statsUser == null) {
103             statsUser = mbStatsUserLocalService.addStatsUser(groupId, userId);
104         }
105 
106         return statsUser;
107     }
108 
109     public List<MBStatsUser> getStatsUsersByGroupId(
110             long groupId, int start, int end)
111         throws SystemException {
112 
113         return mbStatsUserPersistence.findByG_M(groupId, 0, start, end);
114     }
115 
116     public List<MBStatsUser> getStatsUsersByUserId(long userId)
117         throws SystemException {
118 
119         return mbStatsUserPersistence.findByUserId(userId);
120     }
121 
122     public int getStatsUsersByGroupIdCount(long groupId)
123         throws SystemException {
124 
125         return mbStatsUserPersistence.countByG_M(groupId, 0);
126     }
127 
128     public MBStatsUser updateStatsUser(long groupId, long userId)
129         throws SystemException {
130 
131         return updateStatsUser(groupId, userId, null);
132     }
133 
134     public MBStatsUser updateStatsUser(
135             long groupId, long userId, Date lastPostDate)
136         throws SystemException {
137 
138         int messageCount = mbMessagePersistence.countByG_U(groupId, userId);
139 
140         MBStatsUser statsUser = getStatsUser(groupId, userId);
141 
142         statsUser.setMessageCount(messageCount);
143 
144         if (lastPostDate != null) {
145             statsUser.setLastPostDate(lastPostDate);
146         }
147 
148         mbStatsUserPersistence.update(statsUser, false);
149 
150         return statsUser;
151     }
152 
153     private static Log _log = LogFactoryUtil.getLog(
154         MBStatsUserLocalServiceImpl.class);
155 
156 }