1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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  /**
41   * <a href="MBStatsUserLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   *
45   */
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      /**
81       * @deprecated
82       */
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      /**
96       * @deprecated
97       */
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     /**
135      * @deprecated
136      */
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     /**
163      * @deprecated
164      */
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 }