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.util.PortalUtil;
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(long userId, long plid, long banUserId)
43          throws PortalException, SystemException {
44  
45          User user = userPersistence.findByPrimaryKey(userId);
46          long groupId = PortalUtil.getScopeGroupId(plid);
47          Date now = new Date();
48  
49          long banId = counterLocalService.increment();
50  
51          MBBan ban = mbBanPersistence.fetchByG_B(groupId, banUserId);
52  
53          if (ban == null) {
54              ban = mbBanPersistence.create(banId);
55  
56              ban.setGroupId(groupId);
57              ban.setCompanyId(user.getCompanyId());
58              ban.setUserId(user.getUserId());
59              ban.setUserName(user.getFullName());
60              ban.setCreateDate(now);
61              ban.setBanUserId(banUserId);
62          }
63  
64          ban.setModifiedDate(now);
65  
66          mbBanPersistence.update(ban, false);
67  
68          return ban;
69      }
70  
71      @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
72      public void checkBan(long groupId, long banUserId)
73          throws PortalException, SystemException {
74  
75          if (hasBan(groupId, banUserId)) {
76              throw new BannedUserException();
77          }
78      }
79  
80      public void deleteBan(long plid, long banUserId) throws SystemException {
81          long groupId = PortalUtil.getScopeGroupId(plid);
82  
83          try {
84              mbBanPersistence.removeByG_B(groupId, banUserId);
85          }
86          catch (NoSuchBanException nsbe) {
87          }
88      }
89  
90      public void deleteBansByBanUserId(long banUserId) throws SystemException {
91          mbBanPersistence.removeByBanUserId(banUserId);
92      }
93  
94      public void deleteBansByGroupId(long groupId) throws SystemException {
95          mbBanPersistence.removeByGroupId(groupId);
96      }
97  
98      public void expireBans() throws SystemException {
99          long now = System.currentTimeMillis();
100 
101         List<MBBan> bans = mbBanPersistence.findAll();
102 
103         for (MBBan ban : bans) {
104             long unbanDate = MBUtil.getUnbanDate(
105                 ban, PropsValues.MESSAGE_BOARDS_EXPIRE_BAN_INTERVAL).getTime();
106 
107             if (now >= unbanDate) {
108                 if (_log.isDebugEnabled()) {
109                     _log.debug(
110                         "Auto expiring ban " + ban.getBanId() + " on user " +
111                             ban.getBanUserId());
112                 }
113 
114                 mbBanPersistence.remove(ban);
115             }
116         }
117     }
118 
119     public List<MBBan> getBans(long groupId, int start, int end)
120         throws SystemException {
121 
122         return mbBanPersistence.findByGroupId(groupId, start, end);
123     }
124 
125     public int getBansCount(long groupId) throws SystemException {
126         return mbBanPersistence.countByGroupId(groupId);
127     }
128 
129     public boolean hasBan(long groupId, long banUserId)
130         throws SystemException {
131 
132         if (mbBanPersistence.fetchByG_B(groupId, banUserId) == null) {
133             return false;
134         }
135         else {
136             return true;
137         }
138     }
139 
140     private static Log _log = LogFactoryUtil.getLog(
141         MBBanLocalServiceImpl.class);
142 
143 }