001    /**
002     * Copyright (c) 2000-2012 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.NoSuchStatsUserException;
023    import com.liferay.portlet.blogs.model.BlogsEntry;
024    import com.liferay.portlet.blogs.model.BlogsStatsUser;
025    import com.liferay.portlet.blogs.service.base.BlogsStatsUserLocalServiceBaseImpl;
026    import com.liferay.portlet.blogs.util.comparator.EntryDisplayDateComparator;
027    import com.liferay.portlet.blogs.util.comparator.StatsUserLastPostDateComparator;
028    
029    import java.util.Date;
030    import java.util.List;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     * @author Mate Thurzo
035     */
036    public class BlogsStatsUserLocalServiceImpl
037            extends BlogsStatsUserLocalServiceBaseImpl {
038    
039            public void deleteStatsUser(BlogsStatsUser statsUsers)
040                    throws SystemException {
041    
042                    blogsStatsUserPersistence.remove(statsUsers);
043            }
044    
045            public void deleteStatsUser(long statsUserId)
046                    throws PortalException, SystemException {
047    
048                    BlogsStatsUser statsUsers = blogsStatsUserPersistence.findByPrimaryKey(
049                            statsUserId);
050    
051                    deleteStatsUser(statsUsers);
052            }
053    
054            public void deleteStatsUserByGroupId(long groupId)
055                    throws SystemException {
056    
057                    List<BlogsStatsUser> statsUsers =
058                            blogsStatsUserPersistence.findByGroupId(groupId);
059    
060                    for (BlogsStatsUser statsUser : statsUsers) {
061                            deleteStatsUser(statsUser);
062                    }
063            }
064    
065            public void deleteStatsUserByUserId(long userId) throws SystemException {
066                    List<BlogsStatsUser> statsUsers =
067                            blogsStatsUserPersistence.findByUserId(userId);
068    
069                    for (BlogsStatsUser statsUser : statsUsers) {
070                            deleteStatsUser(statsUser);
071                    }
072            }
073    
074            public List<BlogsStatsUser> getCompanyStatsUsers(
075                            long companyId, int start, int end)
076                    throws SystemException {
077    
078                    return blogsStatsUserPersistence.findByC_NotE(
079                            companyId, 0, start, end, new StatsUserLastPostDateComparator());
080            }
081    
082            public List<BlogsStatsUser> getCompanyStatsUsers(
083                            long companyId, int start, int end, OrderByComparator obc)
084                    throws SystemException {
085    
086                    return blogsStatsUserPersistence.findByC_NotE(
087                            companyId, 0, start, end, obc);
088            }
089    
090            public int getCompanyStatsUsersCount(long companyId)
091                    throws SystemException {
092    
093                    return blogsStatsUserPersistence.countByC_NotE(companyId, 0);
094            }
095    
096            public List<BlogsStatsUser> getGroupsStatsUsers(
097                            long companyId, long groupId, int start, int end)
098                    throws SystemException {
099    
100                    return blogsStatsUserFinder.findByGroupIds(
101                            companyId, groupId, start, end);
102            }
103    
104            public List<BlogsStatsUser> getGroupStatsUsers(
105                            long groupId, int start, int end)
106                    throws SystemException {
107    
108                    return blogsStatsUserPersistence.findByG_NotE(
109                            groupId, 0, start, end, new StatsUserLastPostDateComparator());
110            }
111    
112            public List<BlogsStatsUser> getGroupStatsUsers(
113                            long groupId, int start, int end, OrderByComparator obc)
114                    throws SystemException {
115    
116                    return blogsStatsUserPersistence.findByG_NotE(
117                            groupId, 0, start, end, obc);
118            }
119    
120            public int getGroupStatsUsersCount(long groupId) throws SystemException {
121                    return blogsStatsUserPersistence.countByG_NotE(groupId, 0);
122            }
123    
124            public List<BlogsStatsUser> getOrganizationStatsUsers(
125                            long organizationId, int start, int end)
126                    throws SystemException {
127    
128                    return blogsStatsUserFinder.findByOrganizationId(
129                            organizationId, start, end, new StatsUserLastPostDateComparator());
130            }
131    
132            public List<BlogsStatsUser> getOrganizationStatsUsers(
133                            long organizationId, int start, int end, OrderByComparator obc)
134                    throws SystemException {
135    
136                    return blogsStatsUserFinder.findByOrganizationId(
137                            organizationId, start, end, obc);
138            }
139    
140            public int getOrganizationStatsUsersCount(long organizationId)
141                    throws SystemException {
142    
143                    return blogsStatsUserFinder.countByOrganizationId(organizationId);
144            }
145    
146            public BlogsStatsUser getStatsUser(long groupId, long userId)
147                    throws PortalException, SystemException {
148    
149                    BlogsStatsUser statsUser = blogsStatsUserPersistence.fetchByG_U(
150                            groupId, userId);
151    
152                    if (statsUser == null) {
153                            Group group = groupPersistence.findByPrimaryKey(groupId);
154    
155                            long statsUserId = counterLocalService.increment();
156    
157                            statsUser = blogsStatsUserPersistence.create(statsUserId);
158    
159                            statsUser.setCompanyId(group.getCompanyId());
160                            statsUser.setGroupId(groupId);
161                            statsUser.setUserId(userId);
162    
163                            blogsStatsUserPersistence.update(statsUser, false);
164                    }
165    
166                    return statsUser;
167            }
168    
169            public void updateStatsUser(long groupId, long userId)
170                    throws PortalException, SystemException {
171    
172                    updateStatsUser(groupId, userId, null);
173            }
174    
175            public void updateStatsUser(long groupId, long userId, Date displayDate)
176                    throws PortalException, SystemException {
177    
178                    Date now = new Date();
179    
180                    int entryCount = blogsEntryPersistence.countByG_U_LtD_S(
181                            groupId, userId, now, WorkflowConstants.STATUS_APPROVED);
182    
183                    if (entryCount == 0) {
184                            try {
185                                    blogsStatsUserPersistence.removeByG_U(groupId, userId);
186                            }
187                            catch (NoSuchStatsUserException nssue) {
188                            }
189    
190                            return;
191                    }
192    
193                    BlogsStatsUser statsUser = getStatsUser(groupId, userId);
194    
195                    statsUser.setEntryCount(entryCount);
196    
197                    BlogsEntry blogsEntry = blogsEntryPersistence.findByG_U_LtD_S_First(
198                            groupId, userId, now, WorkflowConstants.STATUS_APPROVED,
199                            new EntryDisplayDateComparator());
200    
201                    Date lastDisplayDate = blogsEntry.getDisplayDate();
202    
203                    Date lastPostDate = statsUser.getLastPostDate();
204    
205                    if ((displayDate != null) && displayDate.before(now)) {
206                            if (lastPostDate == null) {
207                                    statsUser.setLastPostDate(displayDate);
208                            }
209                            else if (displayDate.after(lastPostDate)) {
210                                    statsUser.setLastPostDate(displayDate);
211                            }
212                            else if (lastDisplayDate.before(lastPostDate)) {
213                                    statsUser.setLastPostDate(lastDisplayDate);
214                            }
215                    }
216                    else if (displayDate == null) {
217                            if (lastPostDate == null) {
218                                    statsUser.setLastPostDate(lastDisplayDate);
219                            }
220                            else if (lastPostDate.before(lastDisplayDate)) {
221                                    statsUser.setLastPostDate(lastDisplayDate);
222                            }
223                    }
224    
225                    blogsStatsUserPersistence.update(statsUser, false);
226            }
227    
228    }