1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.messageboards.service.impl;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.SystemException;
19  import com.liferay.portal.kernel.annotation.Propagation;
20  import com.liferay.portal.kernel.annotation.Transactional;
21  import com.liferay.portal.kernel.log.Log;
22  import com.liferay.portal.kernel.log.LogFactoryUtil;
23  import com.liferay.portal.model.User;
24  import com.liferay.portal.service.ServiceContext;
25  import com.liferay.portal.util.PropsValues;
26  import com.liferay.portlet.messageboards.BannedUserException;
27  import com.liferay.portlet.messageboards.NoSuchBanException;
28  import com.liferay.portlet.messageboards.model.MBBan;
29  import com.liferay.portlet.messageboards.service.base.MBBanLocalServiceBaseImpl;
30  import com.liferay.portlet.messageboards.util.MBUtil;
31  
32  import java.util.Date;
33  import java.util.List;
34  
35  /**
36   * <a href="MBBanLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   */
40  public class MBBanLocalServiceImpl extends MBBanLocalServiceBaseImpl {
41  
42      public MBBan addBan(
43              long userId, long banUserId, ServiceContext serviceContext)
44          throws PortalException, SystemException {
45  
46          User user = userPersistence.findByPrimaryKey(userId);
47          long groupId = serviceContext.getScopeGroupId();
48          Date now = new Date();
49  
50          long banId = counterLocalService.increment();
51  
52          MBBan ban = mbBanPersistence.fetchByG_B(groupId, banUserId);
53  
54          if (ban == null) {
55              ban = mbBanPersistence.create(banId);
56  
57              ban.setGroupId(groupId);
58              ban.setCompanyId(user.getCompanyId());
59              ban.setUserId(user.getUserId());
60              ban.setUserName(user.getFullName());
61              ban.setCreateDate(now);
62              ban.setBanUserId(banUserId);
63          }
64  
65          ban.setModifiedDate(now);
66  
67          mbBanPersistence.update(ban, false);
68  
69          return ban;
70      }
71  
72      @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
73      public void checkBan(long groupId, long banUserId)
74          throws PortalException, SystemException {
75  
76          if (hasBan(groupId, banUserId)) {
77              throw new BannedUserException();
78          }
79      }
80  
81      public void deleteBan(long banUserId, ServiceContext serviceContext)
82          throws SystemException {
83  
84          long groupId = serviceContext.getScopeGroupId();
85  
86          try {
87              mbBanPersistence.removeByG_B(groupId, banUserId);
88          }
89          catch (NoSuchBanException nsbe) {
90          }
91      }
92  
93      public void deleteBansByBanUserId(long banUserId) throws SystemException {
94          mbBanPersistence.removeByBanUserId(banUserId);
95      }
96  
97      public void deleteBansByGroupId(long groupId) throws SystemException {
98          mbBanPersistence.removeByGroupId(groupId);
99      }
100 
101     public void expireBans() throws SystemException {
102         long now = System.currentTimeMillis();
103 
104         List<MBBan> bans = mbBanPersistence.findAll();
105 
106         for (MBBan ban : bans) {
107             long unbanDate = MBUtil.getUnbanDate(
108                 ban, PropsValues.MESSAGE_BOARDS_EXPIRE_BAN_INTERVAL).getTime();
109 
110             if (now >= unbanDate) {
111                 if (_log.isDebugEnabled()) {
112                     _log.debug(
113                         "Auto expiring ban " + ban.getBanId() + " on user " +
114                             ban.getBanUserId());
115                 }
116 
117                 mbBanPersistence.remove(ban);
118             }
119         }
120     }
121 
122     public List<MBBan> getBans(long groupId, int start, int end)
123         throws SystemException {
124 
125         return mbBanPersistence.findByGroupId(groupId, start, end);
126     }
127 
128     public int getBansCount(long groupId) throws SystemException {
129         return mbBanPersistence.countByGroupId(groupId);
130     }
131 
132     public boolean hasBan(long groupId, long banUserId)
133         throws SystemException {
134 
135         if (mbBanPersistence.fetchByG_B(groupId, banUserId) == null) {
136             return false;
137         }
138         else {
139             return true;
140         }
141     }
142 
143     private static Log _log = LogFactoryUtil.getLog(
144         MBBanLocalServiceImpl.class);
145 
146 }