1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.messageboards.service.impl;
16  
17  import com.liferay.portal.SystemException;
18  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
19  import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
20  import com.liferay.portal.kernel.dao.orm.ProjectionFactoryUtil;
21  import com.liferay.portal.kernel.dao.orm.PropertyFactoryUtil;
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      /**
72       * @deprecated
73       */
74      public void deleteStatsUserByGroupId(long groupId)
75          throws SystemException {
76  
77          deleteStatsUsersByGroupId(groupId);
78      }
79  
80      /**
81       * @deprecated
82       */
83      public void deleteStatsUserByUserId(long userId) throws SystemException {
84          deleteStatsUsersByUserId(userId);
85      }
86  
87      public void deleteStatsUsersByGroupId(long groupId)
88          throws SystemException {
89  
90          mbStatsUserPersistence.removeByGroupId(groupId);
91      }
92  
93      public void deleteStatsUsersByUserId(long userId) throws SystemException {
94          mbStatsUserPersistence.removeByUserId(userId);
95      }
96  
97      public int getMessageCountByUserId(long userId) throws SystemException {
98          DynamicQuery query = DynamicQueryFactoryUtil.forClass(
99              MBStatsUser.class, MBStatsUserImpl.TABLE_NAME,
100             PortalClassLoaderUtil.getClassLoader());
101 
102         query = query.setProjection(
103             ProjectionFactoryUtil.sum("messageCount"));
104 
105         query = query.add(PropertyFactoryUtil.forName("userId").eq(userId));
106 
107         List<Object> results = mbStatsUserLocalService.dynamicQuery(query);
108 
109         return (Integer)results.get(0);
110     }
111 
112     public MBStatsUser getStatsUser(long groupId, long userId)
113         throws SystemException {
114 
115         MBStatsUser statsUser = mbStatsUserPersistence.fetchByG_U(
116             groupId, userId);
117 
118         if (statsUser == null) {
119             statsUser = mbStatsUserLocalService.addStatsUser(groupId, userId);
120         }
121 
122         return statsUser;
123     }
124 
125     /**
126      * @deprecated
127      */
128     public List<MBStatsUser> getStatsUsers(long groupId, int start, int end)
129         throws SystemException {
130 
131         return getStatsUsersByGroupId(groupId, start, end);
132     }
133 
134     public List<MBStatsUser> getStatsUsersByGroupId(
135             long groupId, int start, int end)
136         throws SystemException {
137 
138         return mbStatsUserPersistence.findByG_M(groupId, 0, start, end);
139     }
140 
141     public int getStatsUsersByGroupIdCount(long groupId)
142         throws SystemException {
143 
144         return mbStatsUserPersistence.countByG_M(groupId, 0);
145     }
146 
147     public List<MBStatsUser> getStatsUsersByUserId(long userId)
148         throws SystemException {
149 
150         return mbStatsUserPersistence.findByUserId(userId);
151     }
152 
153     /**
154      * @deprecated
155      */
156     public int getStatsUsersCount(long groupId) throws SystemException {
157         return getStatsUsersByGroupIdCount(groupId);
158     }
159 
160     public MBStatsUser updateStatsUser(long groupId, long userId)
161         throws SystemException {
162 
163         return updateStatsUser(groupId, userId, null);
164     }
165 
166     public MBStatsUser updateStatsUser(
167             long groupId, long userId, Date lastPostDate)
168         throws SystemException {
169 
170         int messageCount = mbMessagePersistence.countByG_U(groupId, userId);
171 
172         MBStatsUser statsUser = getStatsUser(groupId, userId);
173 
174         statsUser.setMessageCount(messageCount);
175 
176         if (lastPostDate != null) {
177             statsUser.setLastPostDate(lastPostDate);
178         }
179 
180         mbStatsUserPersistence.update(statsUser, false);
181 
182         return statsUser;
183     }
184 
185     private static Log _log = LogFactoryUtil.getLog(
186         MBStatsUserLocalServiceImpl.class);
187 
188 }