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