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.blogs.service.impl;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.util.OrderByComparator;
28  import com.liferay.portal.model.Group;
29  import com.liferay.portlet.blogs.model.BlogsEntry;
30  import com.liferay.portlet.blogs.model.BlogsStatsUser;
31  import com.liferay.portlet.blogs.service.base.BlogsStatsUserLocalServiceBaseImpl;
32  import com.liferay.portlet.blogs.util.comparator.EntryDisplayDateComparator;
33  import com.liferay.portlet.blogs.util.comparator.StatsUserLastPostDateComparator;
34  
35  import java.util.Date;
36  import java.util.List;
37  
38  /**
39   * <a href="BlogsStatsUserLocalServiceImpl.java.html"><b><i>View Source</i></b>
40   * </a>
41   *
42   * @author Brian Wing Shun Chan
43   */
44  public class BlogsStatsUserLocalServiceImpl
45      extends BlogsStatsUserLocalServiceBaseImpl {
46  
47      public void deleteStatsUserByGroupId(long groupId)
48          throws SystemException {
49  
50          blogsStatsUserPersistence.removeByGroupId(groupId);
51      }
52  
53      public void deleteStatsUserByUserId(long userId) throws SystemException {
54          blogsStatsUserPersistence.removeByUserId(userId);
55      }
56  
57      public List<BlogsStatsUser> getCompanyStatsUsers(
58              long companyId, int start, int end)
59          throws SystemException {
60  
61          return blogsStatsUserPersistence.findByC_E(
62              companyId, 0, start, end, new StatsUserLastPostDateComparator());
63      }
64  
65      public List<BlogsStatsUser> getCompanyStatsUsers(
66              long companyId, int start, int end, OrderByComparator obc)
67          throws SystemException {
68  
69          return blogsStatsUserPersistence.findByC_E(
70              companyId, 0, start, end, obc);
71      }
72  
73      public int getCompanyStatsUsersCount(long companyId)
74          throws SystemException {
75  
76          return blogsStatsUserPersistence.countByC_E(companyId, 0);
77      }
78  
79      public List<BlogsStatsUser> getGroupStatsUsers(
80              long groupId, int start, int end)
81          throws SystemException {
82  
83          return blogsStatsUserPersistence.findByG_E(
84              groupId, 0, start, end, new StatsUserLastPostDateComparator());
85      }
86  
87      public List<BlogsStatsUser> getGroupStatsUsers(
88              long groupId, int start, int end, OrderByComparator obc)
89          throws SystemException {
90  
91          return blogsStatsUserPersistence.findByG_E(groupId, 0, start, end, obc);
92      }
93  
94      public int getGroupStatsUsersCount(long groupId) throws SystemException {
95          return blogsStatsUserPersistence.countByG_E(groupId, 0);
96      }
97  
98      public List<BlogsStatsUser> getOrganizationStatsUsers(
99              long organizationId, int start, int end)
100         throws SystemException {
101 
102         return blogsStatsUserFinder.findByOrganizationId(
103             organizationId, start, end, new StatsUserLastPostDateComparator());
104     }
105 
106     public List<BlogsStatsUser> getOrganizationStatsUsers(
107             long organizationId, int start, int end, OrderByComparator obc)
108         throws SystemException {
109 
110         return blogsStatsUserFinder.findByOrganizationId(
111             organizationId, start, end, obc);
112     }
113 
114     public int getOrganizationStatsUsersCount(long organizationId)
115         throws SystemException {
116 
117         return blogsStatsUserFinder.countByOrganizationId(organizationId);
118     }
119 
120     public BlogsStatsUser getStatsUser(long groupId, long userId)
121         throws PortalException, SystemException {
122 
123         BlogsStatsUser statsUser = blogsStatsUserPersistence.fetchByG_U(
124             groupId, userId);
125 
126         if (statsUser == null) {
127             Group group = groupPersistence.findByPrimaryKey(groupId);
128 
129             long statsUserId = counterLocalService.increment();
130 
131             statsUser = blogsStatsUserPersistence.create(statsUserId);
132 
133             statsUser.setCompanyId(group.getCompanyId());
134             statsUser.setGroupId(groupId);
135             statsUser.setUserId(userId);
136 
137             blogsStatsUserPersistence.update(statsUser, false);
138         }
139 
140         return statsUser;
141     }
142 
143     public void updateStatsUser(long groupId, long userId)
144         throws PortalException, SystemException {
145 
146         updateStatsUser(groupId, userId, null);
147     }
148 
149     public void updateStatsUser(long groupId, long userId, Date displayDate)
150         throws PortalException, SystemException {
151 
152         int entryCount = blogsEntryPersistence.countByG_U(groupId, userId);
153 
154         BlogsStatsUser statsUser = getStatsUser(groupId, userId);
155 
156         statsUser.setEntryCount(entryCount);
157 
158         if (displayDate != null) {
159             BlogsEntry blogsEntry = blogsEntryPersistence.findByG_U_First(
160                 groupId, userId, new EntryDisplayDateComparator());
161 
162             Date lastDisplayDate = blogsEntry.getDisplayDate();
163 
164             Date lastPostDate = statsUser.getLastPostDate();
165 
166             if (lastPostDate == null) {
167                 statsUser.setLastPostDate(displayDate);
168             }
169             else if (displayDate.after(lastPostDate)) {
170                 statsUser.setLastPostDate(displayDate);
171             }
172             else if (lastDisplayDate.before(lastPostDate)) {
173                 statsUser.setLastPostDate(lastDisplayDate);
174             }
175         }
176 
177         blogsStatsUserPersistence.update(statsUser, false);
178     }
179 
180 }