001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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     */
035    public class BlogsStatsUserLocalServiceImpl
036            extends BlogsStatsUserLocalServiceBaseImpl {
037    
038            public void deleteStatsUser(BlogsStatsUser statsUsers)
039                    throws SystemException {
040    
041                    blogsStatsUserPersistence.remove(statsUsers);
042            }
043    
044            public void deleteStatsUser(long statsUserId)
045                    throws PortalException, SystemException {
046    
047                    BlogsStatsUser statsUsers = blogsStatsUserPersistence.findByPrimaryKey(
048                            statsUserId);
049    
050                    deleteStatsUser(statsUsers);
051            }
052    
053            public void deleteStatsUserByGroupId(long groupId)
054                    throws SystemException {
055    
056                    List<BlogsStatsUser> statsUsers =
057                            blogsStatsUserPersistence.findByGroupId(groupId);
058    
059                    for (BlogsStatsUser statsUser : statsUsers) {
060                            deleteStatsUser(statsUser);
061                    }
062            }
063    
064            public void deleteStatsUserByUserId(long userId) throws SystemException {
065                    List<BlogsStatsUser> statsUsers =
066                            blogsStatsUserPersistence.findByUserId(userId);
067    
068                    for (BlogsStatsUser statsUser : statsUsers) {
069                            deleteStatsUser(statsUser);
070                    }
071            }
072    
073            public List<BlogsStatsUser> getCompanyStatsUsers(
074                            long companyId, int start, int end)
075                    throws SystemException {
076    
077                    return blogsStatsUserPersistence.findByC_NotE(
078                            companyId, 0, start, end, new StatsUserLastPostDateComparator());
079            }
080    
081            public List<BlogsStatsUser> getCompanyStatsUsers(
082                            long companyId, int start, int end, OrderByComparator obc)
083                    throws SystemException {
084    
085                    return blogsStatsUserPersistence.findByC_NotE(
086                            companyId, 0, start, end, obc);
087            }
088    
089            public int getCompanyStatsUsersCount(long companyId)
090                    throws SystemException {
091    
092                    return blogsStatsUserPersistence.countByC_NotE(companyId, 0);
093            }
094    
095            public List<BlogsStatsUser> getGroupsStatsUsers(
096                            long companyId, long groupId, int start, int end)
097                    throws SystemException {
098    
099                    return blogsStatsUserFinder.findByGroupIds(
100                            companyId, groupId, start, end);
101            }
102    
103            public List<BlogsStatsUser> getGroupStatsUsers(
104                            long groupId, int start, int end)
105                    throws SystemException {
106    
107                    return blogsStatsUserPersistence.findByG_NotE(
108                            groupId, 0, start, end, new StatsUserLastPostDateComparator());
109            }
110    
111            public List<BlogsStatsUser> getGroupStatsUsers(
112                            long groupId, int start, int end, OrderByComparator obc)
113                    throws SystemException {
114    
115                    return blogsStatsUserPersistence.findByG_NotE(
116                            groupId, 0, start, end, obc);
117            }
118    
119            public int getGroupStatsUsersCount(long groupId) throws SystemException {
120                    return blogsStatsUserPersistence.countByG_NotE(groupId, 0);
121            }
122    
123            public List<BlogsStatsUser> getOrganizationStatsUsers(
124                            long organizationId, int start, int end)
125                    throws SystemException {
126    
127                    return blogsStatsUserFinder.findByOrganizationId(
128                            organizationId, start, end, new StatsUserLastPostDateComparator());
129            }
130    
131            public List<BlogsStatsUser> getOrganizationStatsUsers(
132                            long organizationId, int start, int end, OrderByComparator obc)
133                    throws SystemException {
134    
135                    return blogsStatsUserFinder.findByOrganizationId(
136                            organizationId, start, end, obc);
137            }
138    
139            public int getOrganizationStatsUsersCount(long organizationId)
140                    throws SystemException {
141    
142                    return blogsStatsUserFinder.countByOrganizationId(organizationId);
143            }
144    
145            public BlogsStatsUser getStatsUser(long groupId, long userId)
146                    throws PortalException, SystemException {
147    
148                    BlogsStatsUser statsUser = blogsStatsUserPersistence.fetchByG_U(
149                            groupId, userId);
150    
151                    if (statsUser == null) {
152                            Group group = groupPersistence.findByPrimaryKey(groupId);
153    
154                            long statsUserId = counterLocalService.increment();
155    
156                            statsUser = blogsStatsUserPersistence.create(statsUserId);
157    
158                            statsUser.setCompanyId(group.getCompanyId());
159                            statsUser.setGroupId(groupId);
160                            statsUser.setUserId(userId);
161    
162                            blogsStatsUserPersistence.update(statsUser, false);
163                    }
164    
165                    return statsUser;
166            }
167    
168            public void updateStatsUser(long groupId, long userId)
169                    throws PortalException, SystemException {
170    
171                    updateStatsUser(groupId, userId, null);
172            }
173    
174            public void updateStatsUser(long groupId, long userId, Date displayDate)
175                    throws PortalException, SystemException {
176    
177                    Date now = new Date();
178    
179                    int entryCount = blogsEntryPersistence.countByG_U_LtD_S(
180                            groupId, userId, now, WorkflowConstants.STATUS_APPROVED);
181    
182                    if (entryCount == 0) {
183                            try {
184                                    blogsStatsUserPersistence.removeByG_U(groupId, userId);
185                            }
186                            catch (NoSuchStatsUserException nssue) {
187                            }
188    
189                            return;
190                    }
191    
192                    BlogsStatsUser statsUser = getStatsUser(groupId, userId);
193    
194                    statsUser.setEntryCount(entryCount);
195    
196                    BlogsEntry blogsEntry = blogsEntryPersistence.findByG_U_LtD_S_First(
197                            groupId, userId, now, WorkflowConstants.STATUS_APPROVED,
198                            new EntryDisplayDateComparator());
199    
200                    Date lastDisplayDate = blogsEntry.getDisplayDate();
201    
202                    Date lastPostDate = statsUser.getLastPostDate();
203    
204                    if ((displayDate != null) && displayDate.before(now)) {
205                            if (lastPostDate == null) {
206                                    statsUser.setLastPostDate(displayDate);
207                            }
208                            else if (displayDate.after(lastPostDate)) {
209                                    statsUser.setLastPostDate(displayDate);
210                            }
211                            else if (lastDisplayDate.before(lastPostDate)) {
212                                    statsUser.setLastPostDate(lastDisplayDate);
213                            }
214                    }
215                    else if (lastDisplayDate.before(lastPostDate)) {
216                            statsUser.setLastPostDate(lastDisplayDate);
217                    }
218    
219                    blogsStatsUserPersistence.update(statsUser, false);
220            }
221    
222    }