001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.messageboards.service.impl;
022:
023: import com.liferay.portal.PortalException;
024: import com.liferay.portal.SystemException;
025: import com.liferay.portal.kernel.util.GetterUtil;
026: import com.liferay.portal.model.User;
027: import com.liferay.portal.util.PortalUtil;
028: import com.liferay.portal.util.PropsUtil;
029: import com.liferay.portlet.messageboards.BannedUserException;
030: import com.liferay.portlet.messageboards.NoSuchBanException;
031: import com.liferay.portlet.messageboards.model.MBBan;
032: import com.liferay.portlet.messageboards.service.base.MBBanLocalServiceBaseImpl;
033: import com.liferay.portlet.messageboards.util.MBUtil;
034:
035: import java.util.Date;
036: import java.util.List;
037:
038: import org.apache.commons.logging.Log;
039: import org.apache.commons.logging.LogFactory;
040:
041: /**
042: * <a href="MBBanLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
043: *
044: * @author Brian Wing Shun Chan
045: *
046: */
047: public class MBBanLocalServiceImpl extends MBBanLocalServiceBaseImpl {
048:
049: public MBBan addBan(long userId, long plid, long banUserId)
050: throws PortalException, SystemException {
051:
052: User user = userPersistence.findByPrimaryKey(userId);
053: long groupId = PortalUtil.getPortletGroupId(plid);
054: Date now = new Date();
055:
056: long banId = counterLocalService.increment();
057:
058: MBBan ban = mbBanPersistence.fetchByG_B(groupId, banUserId);
059:
060: if (ban == null) {
061: ban = mbBanPersistence.create(banId);
062:
063: ban.setGroupId(groupId);
064: ban.setCompanyId(user.getCompanyId());
065: ban.setUserId(user.getUserId());
066: ban.setUserName(user.getFullName());
067: ban.setCreateDate(now);
068: ban.setBanUserId(banUserId);
069: }
070:
071: ban.setModifiedDate(now);
072:
073: mbBanPersistence.update(ban);
074:
075: return ban;
076: }
077:
078: public void checkBan(long groupId, long banUserId)
079: throws PortalException, SystemException {
080:
081: if (hasBan(groupId, banUserId)) {
082: throw new BannedUserException();
083: }
084: }
085:
086: public void deleteBan(long plid, long banUserId)
087: throws PortalException, SystemException {
088:
089: long groupId = PortalUtil.getPortletGroupId(plid);
090:
091: try {
092: mbBanPersistence.removeByG_B(groupId, banUserId);
093: } catch (NoSuchBanException nsbe) {
094: }
095: }
096:
097: public void deleteBansByBanUserId(long banUserId)
098: throws SystemException {
099: mbBanPersistence.removeByBanUserId(banUserId);
100: }
101:
102: public void deleteBansByGroupId(long groupId)
103: throws SystemException {
104: mbBanPersistence.removeByGroupId(groupId);
105: }
106:
107: public void expireBans() throws SystemException {
108: long now = System.currentTimeMillis();
109:
110: int expireInterval = GetterUtil.getInteger(PropsUtil
111: .get(PropsUtil.MESSAGE_BOARDS_EXPIRE_BAN_INTERVAL));
112:
113: List bans = mbBanPersistence.findAll();
114:
115: for (int i = 0; i < bans.size(); i++) {
116: MBBan ban = (MBBan) bans.get(i);
117:
118: long unbanDate = MBUtil.getUnbanDate(ban, expireInterval)
119: .getTime();
120:
121: if (now >= unbanDate) {
122: if (_log.isDebugEnabled()) {
123: _log.debug("Auto expiring ban " + ban.getBanId()
124: + " on user " + ban.getBanUserId());
125: }
126:
127: mbBanPersistence.remove(ban);
128: }
129: }
130: }
131:
132: public List getBans(long groupId, int start, int end)
133: throws SystemException {
134:
135: return mbBanPersistence.findByGroupId(groupId, start, end);
136: }
137:
138: public int getBansCount(long groupId) throws SystemException {
139: return mbBanPersistence.countByGroupId(groupId);
140: }
141:
142: public boolean hasBan(long groupId, long banUserId)
143: throws SystemException {
144:
145: if (mbBanPersistence.fetchByG_B(groupId, banUserId) == null) {
146: return false;
147: } else {
148: return true;
149: }
150: }
151:
152: private static Log _log = LogFactory
153: .getLog(MBBanLocalServiceImpl.class);
154:
155: }
|