1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.messageboards.service.impl;
21  
22  import com.liferay.portal.PortalException;
23  import com.liferay.portal.SystemException;
24  import com.liferay.portal.kernel.annotation.Propagation;
25  import com.liferay.portal.kernel.annotation.Transactional;
26  import com.liferay.portal.kernel.log.Log;
27  import com.liferay.portal.kernel.log.LogFactoryUtil;
28  import com.liferay.portal.model.User;
29  import com.liferay.portal.service.ServiceContext;
30  import com.liferay.portal.util.PropsValues;
31  import com.liferay.portlet.messageboards.BannedUserException;
32  import com.liferay.portlet.messageboards.NoSuchBanException;
33  import com.liferay.portlet.messageboards.model.MBBan;
34  import com.liferay.portlet.messageboards.service.base.MBBanLocalServiceBaseImpl;
35  import com.liferay.portlet.messageboards.util.MBUtil;
36  
37  import java.util.Date;
38  import java.util.List;
39  
40  /**
41   * <a href="MBBanLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   *
45   */
46  public class MBBanLocalServiceImpl extends MBBanLocalServiceBaseImpl {
47  
48      public MBBan addBan(
49              long userId, long banUserId, ServiceContext serviceContext)
50          throws PortalException, SystemException {
51  
52          User user = userPersistence.findByPrimaryKey(userId);
53          long groupId = serviceContext.getScopeGroupId();
54          Date now = new Date();
55  
56          long banId = counterLocalService.increment();
57  
58          MBBan ban = mbBanPersistence.fetchByG_B(groupId, banUserId);
59  
60          if (ban == null) {
61              ban = mbBanPersistence.create(banId);
62  
63              ban.setGroupId(groupId);
64              ban.setCompanyId(user.getCompanyId());
65              ban.setUserId(user.getUserId());
66              ban.setUserName(user.getFullName());
67              ban.setCreateDate(now);
68              ban.setBanUserId(banUserId);
69          }
70  
71          ban.setModifiedDate(now);
72  
73          mbBanPersistence.update(ban, false);
74  
75          return ban;
76      }
77  
78      @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
79      public void checkBan(long groupId, long banUserId)
80          throws PortalException, SystemException {
81  
82          if (hasBan(groupId, banUserId)) {
83              throw new BannedUserException();
84          }
85      }
86  
87      public void deleteBan(long banUserId, ServiceContext serviceContext)
88          throws SystemException {
89  
90          long groupId = serviceContext.getScopeGroupId();
91  
92          try {
93              mbBanPersistence.removeByG_B(groupId, banUserId);
94          }
95          catch (NoSuchBanException nsbe) {
96          }
97      }
98  
99      public void deleteBansByBanUserId(long banUserId) throws SystemException {
100         mbBanPersistence.removeByBanUserId(banUserId);
101     }
102 
103     public void deleteBansByGroupId(long groupId) throws SystemException {
104         mbBanPersistence.removeByGroupId(groupId);
105     }
106 
107     public void expireBans() throws SystemException {
108         long now = System.currentTimeMillis();
109 
110         List<MBBan> bans = mbBanPersistence.findAll();
111 
112         for (MBBan ban : bans) {
113             long unbanDate = MBUtil.getUnbanDate(
114                 ban, PropsValues.MESSAGE_BOARDS_EXPIRE_BAN_INTERVAL).getTime();
115 
116             if (now >= unbanDate) {
117                 if (_log.isDebugEnabled()) {
118                     _log.debug(
119                         "Auto expiring ban " + ban.getBanId() + " on user " +
120                             ban.getBanUserId());
121                 }
122 
123                 mbBanPersistence.remove(ban);
124             }
125         }
126     }
127 
128     public List<MBBan> getBans(long groupId, int start, int end)
129         throws SystemException {
130 
131         return mbBanPersistence.findByGroupId(groupId, start, end);
132     }
133 
134     public int getBansCount(long groupId) throws SystemException {
135         return mbBanPersistence.countByGroupId(groupId);
136     }
137 
138     public boolean hasBan(long groupId, long banUserId)
139         throws SystemException {
140 
141         if (mbBanPersistence.fetchByG_B(groupId, banUserId) == null) {
142             return false;
143         }
144         else {
145             return true;
146         }
147     }
148 
149     private static Log _log =
150         LogFactoryUtil.getLog(MBBanLocalServiceImpl.class);
151 
152 }