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