1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class MBStatsUserLocalServiceImpl
46      extends MBStatsUserLocalServiceBaseImpl {
47  
48      public MBStatsUser addStatsUser(long groupId, long userId)
49          throws SystemException {
50  
51          long statsUserId = counterLocalService.increment();
52  
53          MBStatsUser statsUser = mbStatsUserPersistence.create(statsUserId);
54  
55          statsUser.setGroupId(groupId);
56          statsUser.setUserId(userId);
57  
58          try {
59              mbStatsUserPersistence.update(statsUser, false);
60          }
61          catch (SystemException se) {
62              if (_log.isWarnEnabled()) {
63                  _log.warn(
64                      "Add failed, fetch {groupId=" + groupId + ", userId=" +
65                          userId + "}");
66              }
67  
68              statsUser = mbStatsUserPersistence.fetchByG_U(
69                  groupId, userId, false);
70  
71              if (statsUser == null) {
72                  throw se;
73              }
74          }
75  
76          return statsUser;
77      }
78  
79      /**
80       * @deprecated
81       */
82      public void deleteStatsUserByGroupId(long groupId)
83          throws SystemException {
84  
85          deleteStatsUsersByGroupId(groupId);
86      }
87  
88      public void deleteStatsUsersByGroupId(long groupId)
89          throws SystemException {
90  
91          mbStatsUserPersistence.removeByGroupId(groupId);
92      }
93  
94      /**
95       * @deprecated
96       */
97      public void deleteStatsUserByUserId(long userId) throws SystemException {
98          deleteStatsUsersByUserId(userId);
99      }
100 
101     public void deleteStatsUsersByUserId(long userId) throws SystemException {
102         mbStatsUserPersistence.removeByUserId(userId);
103     }
104 
105     public int getMessageCountByUserId(long userId) throws SystemException {
106         DynamicQuery query = DynamicQueryFactoryUtil.forClass(
107             MBStatsUser.class, MBStatsUserImpl.TABLE_NAME,
108             PortalClassLoaderUtil.getClassLoader());
109 
110         query = query.setProjection(
111             ProjectionFactoryUtil.sum("messageCount"));
112 
113         query = query.add(PropertyFactoryUtil.forName("userId").eq(userId));
114 
115         List<Object> results = mbStatsUserLocalService.dynamicQuery(query);
116 
117         return (Integer)results.get(0);
118     }
119 
120     public MBStatsUser getStatsUser(long groupId, long userId)
121         throws SystemException {
122 
123         MBStatsUser statsUser = mbStatsUserPersistence.fetchByG_U(
124             groupId, userId);
125 
126         if (statsUser == null) {
127             statsUser = mbStatsUserLocalService.addStatsUser(groupId, userId);
128         }
129 
130         return statsUser;
131     }
132 
133     /**
134      * @deprecated
135      */
136     public List<MBStatsUser> getStatsUsers(long groupId, int start, int end)
137         throws SystemException {
138 
139         return getStatsUsersByGroupId(groupId, start, end);
140     }
141 
142     public List<MBStatsUser> getStatsUsersByGroupId(
143             long groupId, int start, int end)
144         throws SystemException {
145 
146         return mbStatsUserPersistence.findByG_M(groupId, 0, start, end);
147     }
148 
149     public List<MBStatsUser> getStatsUsersByUserId(long userId)
150         throws SystemException {
151 
152         return mbStatsUserPersistence.findByUserId(userId);
153     }
154 
155     public int getStatsUsersByGroupIdCount(long groupId)
156         throws SystemException {
157 
158         return mbStatsUserPersistence.countByG_M(groupId, 0);
159     }
160 
161     /**
162      * @deprecated
163      */
164     public int getStatsUsersCount(long groupId) throws SystemException {
165         return getStatsUsersByGroupIdCount(groupId);
166     }
167 
168     public MBStatsUser updateStatsUser(long groupId, long userId)
169         throws SystemException {
170 
171         return updateStatsUser(groupId, userId, null);
172     }
173 
174     public MBStatsUser updateStatsUser(
175             long groupId, long userId, Date lastPostDate)
176         throws SystemException {
177 
178         int messageCount = mbMessagePersistence.countByG_U(groupId, userId);
179 
180         MBStatsUser statsUser = getStatsUser(groupId, userId);
181 
182         statsUser.setMessageCount(messageCount);
183 
184         if (lastPostDate != null) {
185             statsUser.setLastPostDate(lastPostDate);
186         }
187 
188         mbStatsUserPersistence.update(statsUser, false);
189 
190         return statsUser;
191     }
192 
193     private static Log _log =
194         LogFactoryUtil.getLog(MBStatsUserLocalServiceImpl.class);
195 
196 }