001    /**
002     * Copyright (c) 2000-present 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.messageboards.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.systemevent.SystemEvent;
021    import com.liferay.portal.kernel.transaction.Propagation;
022    import com.liferay.portal.kernel.transaction.Transactional;
023    import com.liferay.portal.model.SystemEventConstants;
024    import com.liferay.portal.model.User;
025    import com.liferay.portal.service.ServiceContext;
026    import com.liferay.portal.util.PropsValues;
027    import com.liferay.portlet.messageboards.BannedUserException;
028    import com.liferay.portlet.messageboards.model.MBBan;
029    import com.liferay.portlet.messageboards.service.base.MBBanLocalServiceBaseImpl;
030    import com.liferay.portlet.messageboards.util.MBUtil;
031    
032    import java.util.Date;
033    import java.util.List;
034    
035    /**
036     * @author Brian Wing Shun Chan
037     */
038    public class MBBanLocalServiceImpl extends MBBanLocalServiceBaseImpl {
039    
040            @Override
041            public MBBan addBan(
042                            long userId, long banUserId, ServiceContext serviceContext)
043                    throws PortalException {
044    
045                    User user = userPersistence.findByPrimaryKey(userId);
046                    long groupId = serviceContext.getScopeGroupId();
047    
048                    long banId = counterLocalService.increment();
049    
050                    MBBan ban = mbBanPersistence.fetchByG_B(groupId, banUserId);
051    
052                    if (ban == null) {
053                            ban = mbBanPersistence.create(banId);
054    
055                            ban.setUuid(serviceContext.getUuid());
056                            ban.setGroupId(groupId);
057                            ban.setCompanyId(user.getCompanyId());
058                            ban.setUserId(user.getUserId());
059                            ban.setUserName(user.getFullName());
060                            ban.setBanUserId(banUserId);
061                    }
062    
063                    mbBanPersistence.update(ban);
064    
065                    return ban;
066            }
067    
068            @Override
069            @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
070            public void checkBan(long groupId, long banUserId) throws PortalException {
071                    if (hasBan(groupId, banUserId)) {
072                            throw new BannedUserException();
073                    }
074            }
075    
076            @Override
077            public void deleteBan(long banId) throws PortalException {
078                    MBBan ban = mbBanPersistence.findByPrimaryKey(banId);
079    
080                    mbBanLocalService.deleteBan(ban);
081            }
082    
083            @Override
084            public void deleteBan(long banUserId, ServiceContext serviceContext) {
085                    long groupId = serviceContext.getScopeGroupId();
086    
087                    MBBan ban = mbBanPersistence.fetchByG_B(groupId, banUserId);
088    
089                    if (ban != null) {
090                            mbBanLocalService.deleteBan(ban);
091                    }
092            }
093    
094            @Override
095            @SystemEvent(type = SystemEventConstants.TYPE_DELETE)
096            public void deleteBan(MBBan ban) {
097                    mbBanPersistence.remove(ban);
098            }
099    
100            @Override
101            public void deleteBansByBanUserId(long banUserId) {
102                    List<MBBan> bans = mbBanPersistence.findByBanUserId(banUserId);
103    
104                    for (MBBan ban : bans) {
105                            mbBanLocalService.deleteBan(ban);
106                    }
107            }
108    
109            @Override
110            public void deleteBansByGroupId(long groupId) {
111                    List<MBBan> bans = mbBanPersistence.findByGroupId(groupId);
112    
113                    for (MBBan ban : bans) {
114                            mbBanLocalService.deleteBan(ban);
115                    }
116            }
117    
118            @Override
119            public void expireBans() {
120                    if (PropsValues.MESSAGE_BOARDS_EXPIRE_BAN_INTERVAL <= 0) {
121                            return;
122                    }
123    
124                    long now = System.currentTimeMillis();
125    
126                    List<MBBan> bans = mbBanPersistence.findAll();
127    
128                    for (MBBan ban : bans) {
129                            Date unbanDate = MBUtil.getUnbanDate(
130                                    ban, PropsValues.MESSAGE_BOARDS_EXPIRE_BAN_INTERVAL);
131    
132                            long unbanTime = unbanDate.getTime();
133    
134                            if (now >= unbanTime) {
135                                    if (_log.isDebugEnabled()) {
136                                            _log.debug(
137                                                    "Auto expiring ban " + ban.getBanId() + " on user " +
138                                                            ban.getBanUserId());
139                                    }
140    
141                                    mbBanPersistence.remove(ban);
142                            }
143                    }
144            }
145    
146            @Override
147            public List<MBBan> getBans(long groupId, int start, int end) {
148                    return mbBanPersistence.findByGroupId(groupId, start, end);
149            }
150    
151            @Override
152            public int getBansCount(long groupId) {
153                    return mbBanPersistence.countByGroupId(groupId);
154            }
155    
156            @Override
157            public boolean hasBan(long groupId, long banUserId) {
158                    if (mbBanPersistence.fetchByG_B(groupId, banUserId) == null) {
159                            return false;
160                    }
161                    else {
162                            return true;
163                    }
164            }
165    
166            private static final Log _log = LogFactoryUtil.getLog(
167                    MBBanLocalServiceImpl.class);
168    
169    }