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.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portlet.messageboards.model.MBStatsUser;
29  import com.liferay.portlet.messageboards.service.base.MBStatsUserLocalServiceBaseImpl;
30  
31  import java.util.Date;
32  import java.util.List;
33  
34  /**
35   * <a href="MBStatsUserLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   *
39   */
40  public class MBStatsUserLocalServiceImpl
41      extends MBStatsUserLocalServiceBaseImpl {
42  
43      public MBStatsUser addStatsUser(long groupId, long userId)
44          throws SystemException {
45  
46          long statsUserId = counterLocalService.increment();
47  
48          MBStatsUser statsUser = mbStatsUserPersistence.create(statsUserId);
49  
50          statsUser.setGroupId(groupId);
51          statsUser.setUserId(userId);
52  
53          try {
54              mbStatsUserPersistence.update(statsUser, false);
55          }
56          catch (SystemException se) {
57              if (_log.isWarnEnabled()) {
58                  _log.warn(
59                      "Add failed, fetch {groupId=" + groupId + ", userId=" +
60                          userId + "}");
61              }
62  
63              statsUser = mbStatsUserPersistence.fetchByG_U(
64                  groupId, userId, false);
65  
66              if (statsUser == null) {
67                  throw se;
68              }
69          }
70  
71          return statsUser;
72      }
73  
74      public void deleteStatsUserByGroupId(long groupId)
75          throws SystemException {
76  
77          mbStatsUserPersistence.removeByGroupId(groupId);
78      }
79  
80      public void deleteStatsUserByUserId(long userId) throws SystemException {
81          mbStatsUserPersistence.removeByUserId(userId);
82      }
83  
84      public MBStatsUser getStatsUser(long groupId, long userId)
85          throws SystemException {
86  
87          MBStatsUser statsUser = mbStatsUserPersistence.fetchByG_U(
88              groupId, userId);
89  
90          if (statsUser == null) {
91              statsUser = mbStatsUserLocalService.addStatsUser(groupId, userId);
92          }
93  
94          return statsUser;
95      }
96  
97      public List<MBStatsUser> getStatsUsers(long groupId, int start, int end)
98          throws SystemException {
99  
100         return mbStatsUserPersistence.findByG_M(groupId, 0, start, end);
101     }
102 
103     public int getStatsUsersCount(long groupId) throws SystemException {
104         return mbStatsUserPersistence.countByG_M(groupId, 0);
105     }
106 
107     public MBStatsUser updateStatsUser(long groupId, long userId)
108         throws SystemException {
109 
110         return updateStatsUser(groupId, userId, null);
111     }
112 
113     public MBStatsUser updateStatsUser(
114             long groupId, long userId, Date lastPostDate)
115         throws SystemException {
116 
117         int messageCount = mbMessagePersistence.countByG_U(groupId, userId);
118 
119         MBStatsUser statsUser = getStatsUser(groupId, userId);
120 
121         statsUser.setMessageCount(messageCount);
122 
123         if (lastPostDate != null) {
124             statsUser.setLastPostDate(lastPostDate);
125         }
126 
127         mbStatsUserPersistence.update(statsUser, false);
128 
129         return statsUser;
130     }
131 
132     private static Log _log =
133         LogFactoryUtil.getLog(MBStatsUserLocalServiceImpl.class);
134 
135 }