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