001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.blogs.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.util.OrderByComparator;
020    import com.liferay.portal.kernel.workflow.WorkflowConstants;
021    import com.liferay.portal.model.Group;
022    import com.liferay.portlet.blogs.model.BlogsEntry;
023    import com.liferay.portlet.blogs.model.BlogsStatsUser;
024    import com.liferay.portlet.blogs.service.base.BlogsStatsUserLocalServiceBaseImpl;
025    import com.liferay.portlet.blogs.util.comparator.EntryDisplayDateComparator;
026    import com.liferay.portlet.blogs.util.comparator.StatsUserLastPostDateComparator;
027    
028    import java.util.Date;
029    import java.util.List;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     */
034    public class BlogsStatsUserLocalServiceImpl
035            extends BlogsStatsUserLocalServiceBaseImpl {
036    
037            public void deleteStatsUserByGroupId(long groupId)
038                    throws SystemException {
039    
040                    blogsStatsUserPersistence.removeByGroupId(groupId);
041            }
042    
043            public void deleteStatsUserByUserId(long userId) throws SystemException {
044                    blogsStatsUserPersistence.removeByUserId(userId);
045            }
046    
047            public List<BlogsStatsUser> getCompanyStatsUsers(
048                            long companyId, int start, int end)
049                    throws SystemException {
050    
051                    return blogsStatsUserPersistence.findByC_NotE(
052                            companyId, 0, start, end, new StatsUserLastPostDateComparator());
053            }
054    
055            public List<BlogsStatsUser> getCompanyStatsUsers(
056                            long companyId, int start, int end, OrderByComparator obc)
057                    throws SystemException {
058    
059                    return blogsStatsUserPersistence.findByC_NotE(
060                            companyId, 0, start, end, obc);
061            }
062    
063            public int getCompanyStatsUsersCount(long companyId)
064                    throws SystemException {
065    
066                    return blogsStatsUserPersistence.countByC_NotE(companyId, 0);
067            }
068    
069            public List<BlogsStatsUser> getGroupsStatsUsers(
070                            long companyId, long groupId, int start, int end)
071                    throws SystemException {
072    
073                    return blogsStatsUserFinder.findByGroupIds(
074                            companyId, groupId, start, end);
075            }
076    
077            public List<BlogsStatsUser> getGroupStatsUsers(
078                            long groupId, int start, int end)
079                    throws SystemException {
080    
081                    return blogsStatsUserPersistence.findByG_NotE(
082                            groupId, 0, start, end, new StatsUserLastPostDateComparator());
083            }
084    
085            public List<BlogsStatsUser> getGroupStatsUsers(
086                            long groupId, int start, int end, OrderByComparator obc)
087                    throws SystemException {
088    
089                    return blogsStatsUserPersistence.findByG_NotE(
090                            groupId, 0, start, end, obc);
091            }
092    
093            public int getGroupStatsUsersCount(long groupId) throws SystemException {
094                    return blogsStatsUserPersistence.countByG_NotE(groupId, 0);
095            }
096    
097            public List<BlogsStatsUser> getOrganizationStatsUsers(
098                            long organizationId, int start, int end)
099                    throws SystemException {
100    
101                    return blogsStatsUserFinder.findByOrganizationId(
102                            organizationId, start, end, new StatsUserLastPostDateComparator());
103            }
104    
105            public List<BlogsStatsUser> getOrganizationStatsUsers(
106                            long organizationId, int start, int end, OrderByComparator obc)
107                    throws SystemException {
108    
109                    return blogsStatsUserFinder.findByOrganizationId(
110                            organizationId, start, end, obc);
111            }
112    
113            public int getOrganizationStatsUsersCount(long organizationId)
114                    throws SystemException {
115    
116                    return blogsStatsUserFinder.countByOrganizationId(organizationId);
117            }
118    
119            public BlogsStatsUser getStatsUser(long groupId, long userId)
120                    throws PortalException, SystemException {
121    
122                    BlogsStatsUser statsUser = blogsStatsUserPersistence.fetchByG_U(
123                            groupId, userId);
124    
125                    if (statsUser == null) {
126                            Group group = groupPersistence.findByPrimaryKey(groupId);
127    
128                            long statsUserId = counterLocalService.increment();
129    
130                            statsUser = blogsStatsUserPersistence.create(statsUserId);
131    
132                            statsUser.setCompanyId(group.getCompanyId());
133                            statsUser.setGroupId(groupId);
134                            statsUser.setUserId(userId);
135    
136                            blogsStatsUserPersistence.update(statsUser, false);
137                    }
138    
139                    return statsUser;
140            }
141    
142            public void updateStatsUser(long groupId, long userId)
143                    throws PortalException, SystemException {
144    
145                    updateStatsUser(groupId, userId, null);
146            }
147    
148            public void updateStatsUser(long groupId, long userId, Date displayDate)
149                    throws PortalException, SystemException {
150    
151                    int entryCount = blogsEntryPersistence.countByG_U_S(
152                            groupId, userId, WorkflowConstants.STATUS_APPROVED);
153    
154                    BlogsStatsUser statsUser = getStatsUser(groupId, userId);
155    
156                    statsUser.setEntryCount(entryCount);
157    
158                    if (displayDate != null) {
159                            BlogsEntry blogsEntry = blogsEntryPersistence.findByG_U_S_First(
160                                    groupId, userId, WorkflowConstants.STATUS_APPROVED,
161                                    new EntryDisplayDateComparator());
162    
163                            Date lastDisplayDate = blogsEntry.getDisplayDate();
164    
165                            Date lastPostDate = statsUser.getLastPostDate();
166    
167                            if (lastPostDate == null) {
168                                    statsUser.setLastPostDate(displayDate);
169                            }
170                            else if (displayDate.after(lastPostDate)) {
171                                    statsUser.setLastPostDate(displayDate);
172                            }
173                            else if (lastDisplayDate.before(lastPostDate)) {
174                                    statsUser.setLastPostDate(lastDisplayDate);
175                            }
176                    }
177    
178                    blogsStatsUserPersistence.update(statsUser, false);
179            }
180    
181    }