1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.messageboards.service.impl;
21  
22  import com.liferay.portal.SystemException;
23  import com.liferay.portal.kernel.log.Log;
24  import com.liferay.portal.kernel.log.LogFactoryUtil;
25  import com.liferay.portlet.messageboards.model.MBStatsUser;
26  import com.liferay.portlet.messageboards.service.base.MBStatsUserLocalServiceBaseImpl;
27  
28  import java.util.Date;
29  import java.util.List;
30  
31  /**
32   * <a href="MBStatsUserLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   *
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 deleteStatsUserByGroupId(long groupId)
72          throws SystemException {
73  
74          mbStatsUserPersistence.removeByGroupId(groupId);
75      }
76  
77      public void deleteStatsUserByUserId(long userId) throws SystemException {
78          mbStatsUserPersistence.removeByUserId(userId);
79      }
80  
81      public MBStatsUser getStatsUser(long groupId, long userId)
82          throws SystemException {
83  
84          MBStatsUser statsUser = mbStatsUserPersistence.fetchByG_U(
85              groupId, userId);
86  
87          if (statsUser == null) {
88              statsUser = mbStatsUserLocalService.addStatsUser(groupId, userId);
89          }
90  
91          return statsUser;
92      }
93  
94      public List<MBStatsUser> getStatsUsers(long groupId, int start, int end)
95          throws SystemException {
96  
97          return mbStatsUserPersistence.findByG_M(groupId, 0, start, end);
98      }
99  
100     public int getStatsUsersCount(long groupId) throws SystemException {
101         return mbStatsUserPersistence.countByG_M(groupId, 0);
102     }
103 
104     public MBStatsUser updateStatsUser(long groupId, long userId)
105         throws SystemException {
106 
107         return updateStatsUser(groupId, userId, null);
108     }
109 
110     public MBStatsUser updateStatsUser(
111             long groupId, long userId, Date lastPostDate)
112         throws SystemException {
113 
114         int messageCount = mbMessagePersistence.countByG_U(groupId, userId);
115 
116         MBStatsUser statsUser = getStatsUser(groupId, userId);
117 
118         statsUser.setMessageCount(messageCount);
119 
120         if (lastPostDate != null) {
121             statsUser.setLastPostDate(lastPostDate);
122         }
123 
124         mbStatsUserPersistence.update(statsUser, false);
125 
126         return statsUser;
127     }
128 
129     private static Log _log =
130         LogFactoryUtil.getLog(MBStatsUserLocalServiceImpl.class);
131 
132 }