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 long getMessageCountByUserId(long userId) throws SystemException {
82          DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
83              MBStatsUser.class, MBStatsUserImpl.TABLE_NAME,
84              PortalClassLoaderUtil.getClassLoader());
85  
86          dynamicQuery.setProjection(ProjectionFactoryUtil.sum("messageCount"));
87  
88          dynamicQuery.add(PropertyFactoryUtil.forName("userId").eq(userId));
89  
90          return dynamicQueryCount(dynamicQuery);
91      }
92  
93      public MBStatsUser getStatsUser(long groupId, long userId)
94          throws SystemException {
95  
96          MBStatsUser statsUser = mbStatsUserPersistence.fetchByG_U(
97              groupId, userId);
98  
99          if (statsUser == null) {
100             statsUser = mbStatsUserLocalService.addStatsUser(groupId, userId);
101         }
102 
103         return statsUser;
104     }
105 
106     public List<MBStatsUser> getStatsUsersByGroupId(
107             long groupId, int start, int end)
108         throws SystemException {
109 
110         return mbStatsUserPersistence.findByG_M(groupId, 0, start, end);
111     }
112 
113     public List<MBStatsUser> getStatsUsersByUserId(long userId)
114         throws SystemException {
115 
116         return mbStatsUserPersistence.findByUserId(userId);
117     }
118 
119     public int getStatsUsersByGroupIdCount(long groupId)
120         throws SystemException {
121 
122         return mbStatsUserPersistence.countByG_M(groupId, 0);
123     }
124 
125     public MBStatsUser updateStatsUser(long groupId, long userId)
126         throws SystemException {
127 
128         return updateStatsUser(groupId, userId, null);
129     }
130 
131     public MBStatsUser updateStatsUser(
132             long groupId, long userId, Date lastPostDate)
133         throws SystemException {
134 
135         int messageCount = mbMessagePersistence.countByG_U(groupId, userId);
136 
137         MBStatsUser statsUser = getStatsUser(groupId, userId);
138 
139         statsUser.setMessageCount(messageCount);
140 
141         if (lastPostDate != null) {
142             statsUser.setLastPostDate(lastPostDate);
143         }
144 
145         mbStatsUserPersistence.update(statsUser, false);
146 
147         return statsUser;
148     }
149 
150     private static Log _log = LogFactoryUtil.getLog(
151         MBStatsUserLocalServiceImpl.class);
152 
153 }