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) throws SystemException {
055                    List<BlogsStatsUser> statsUsers =
056                            blogsStatsUserPersistence.findByGroupId(groupId);
057    
058                    for (BlogsStatsUser statsUser : statsUsers) {
059                            deleteStatsUser(statsUser);
060                    }
061            }
062    
063            public void deleteStatsUserByUserId(long userId) throws SystemException {
064                    List<BlogsStatsUser> statsUsers =
065                            blogsStatsUserPersistence.findByUserId(userId);
066    
067                    for (BlogsStatsUser statsUser : statsUsers) {
068                            deleteStatsUser(statsUser);
069                    }
070            }
071    
072            public List<BlogsStatsUser> getCompanyStatsUsers(
073                            long companyId, int start, int end)
074                    throws SystemException {
075    
076                    return blogsStatsUserPersistence.findByC_NotE(
077                            companyId, 0, start, end, new StatsUserLastPostDateComparator());
078            }
079    
080            public List<BlogsStatsUser> getCompanyStatsUsers(
081                            long companyId, int start, int end, OrderByComparator obc)
082                    throws SystemException {
083    
084                    return blogsStatsUserPersistence.findByC_NotE(
085                            companyId, 0, start, end, obc);
086            }
087    
088            public int getCompanyStatsUsersCount(long companyId)
089                    throws SystemException {
090    
091                    return blogsStatsUserPersistence.countByC_NotE(companyId, 0);
092            }
093    
094            public List<BlogsStatsUser> getGroupsStatsUsers(
095                            long companyId, long groupId, int start, int end)
096                    throws SystemException {
097    
098                    return blogsStatsUserFinder.findByGroupIds(
099                            companyId, groupId, start, end);
100            }
101    
102            public List<BlogsStatsUser> getGroupStatsUsers(
103                            long groupId, int start, int end)
104                    throws SystemException {
105    
106                    return blogsStatsUserPersistence.findByG_NotE(
107                            groupId, 0, start, end, new StatsUserLastPostDateComparator());
108            }
109    
110            public List<BlogsStatsUser> getGroupStatsUsers(
111                            long groupId, int start, int end, OrderByComparator obc)
112                    throws SystemException {
113    
114                    return blogsStatsUserPersistence.findByG_NotE(
115                            groupId, 0, start, end, obc);
116            }
117    
118            public int getGroupStatsUsersCount(long groupId) throws SystemException {
119                    return blogsStatsUserPersistence.countByG_NotE(groupId, 0);
120            }
121    
122            public List<BlogsStatsUser> getOrganizationStatsUsers(
123                            long organizationId, int start, int end)
124                    throws SystemException {
125    
126                    return blogsStatsUserFinder.findByOrganizationId(
127                            organizationId, start, end, new StatsUserLastPostDateComparator());
128            }
129    
130            public List<BlogsStatsUser> getOrganizationStatsUsers(
131                            long organizationId, int start, int end, OrderByComparator obc)
132                    throws SystemException {
133    
134                    return blogsStatsUserFinder.findByOrganizationId(
135                            organizationId, start, end, obc);
136            }
137    
138            public int getOrganizationStatsUsersCount(long organizationId)
139                    throws SystemException {
140    
141                    return blogsStatsUserFinder.countByOrganizationId(organizationId);
142            }
143    
144            public BlogsStatsUser getStatsUser(long groupId, long userId)
145                    throws PortalException, SystemException {
146    
147                    BlogsStatsUser statsUser = blogsStatsUserPersistence.fetchByG_U(
148                            groupId, userId);
149    
150                    if (statsUser == null) {
151                            Group group = groupPersistence.findByPrimaryKey(groupId);
152    
153                            long statsUserId = counterLocalService.increment();
154    
155                            statsUser = blogsStatsUserPersistence.create(statsUserId);
156    
157                            statsUser.setCompanyId(group.getCompanyId());
158                            statsUser.setGroupId(groupId);
159                            statsUser.setUserId(userId);
160    
161                            blogsStatsUserPersistence.update(statsUser);
162                    }
163    
164                    return statsUser;
165            }
166    
167            public void updateStatsUser(long groupId, long userId)
168                    throws PortalException, SystemException {
169    
170                    updateStatsUser(groupId, userId, null);
171            }
172    
173            public void updateStatsUser(long groupId, long userId, Date displayDate)
174                    throws PortalException, SystemException {
175    
176                    Date now = new Date();
177    
178                    int entryCount = blogsEntryPersistence.countByG_U_LtD_S(
179                            groupId, userId, now, WorkflowConstants.STATUS_APPROVED);
180    
181                    if (entryCount == 0) {
182                            try {
183                                    blogsStatsUserPersistence.removeByG_U(groupId, userId);
184                            }
185                            catch (NoSuchStatsUserException nssue) {
186                            }
187    
188                            return;
189                    }
190    
191                    BlogsStatsUser statsUser = getStatsUser(groupId, userId);
192    
193                    statsUser.setEntryCount(entryCount);
194    
195                    BlogsEntry blogsEntry = blogsEntryPersistence.findByG_U_LtD_S_First(
196                            groupId, userId, now, WorkflowConstants.STATUS_APPROVED,
197                            new EntryDisplayDateComparator());
198    
199                    Date lastDisplayDate = blogsEntry.getDisplayDate();
200    
201                    Date lastPostDate = statsUser.getLastPostDate();
202    
203                    if ((displayDate != null) && displayDate.before(now)) {
204                            if (lastPostDate == null) {
205                                    statsUser.setLastPostDate(displayDate);
206                            }
207                            else if (displayDate.after(lastPostDate)) {
208                                    statsUser.setLastPostDate(displayDate);
209                            }
210                            else if (lastDisplayDate.before(lastPostDate)) {
211                                    statsUser.setLastPostDate(lastDisplayDate);
212                            }
213                    }
214                    else if ((lastPostDate == null) ||
215                                     lastPostDate.before(lastDisplayDate)) {
216    
217                            statsUser.setLastPostDate(lastDisplayDate);
218                    }
219    
220                    blogsStatsUserPersistence.update(statsUser);
221            }
222    
223    }