1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.messageboards.service.impl;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.util.GetterUtil;
28  import com.liferay.portal.model.User;
29  import com.liferay.portal.util.PortalUtil;
30  import com.liferay.portal.util.PropsUtil;
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  import org.apache.commons.logging.Log;
41  import org.apache.commons.logging.LogFactory;
42  
43  /**
44   * <a href="MBBanLocalServiceImpl.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   *
48   */
49  public class MBBanLocalServiceImpl extends MBBanLocalServiceBaseImpl {
50  
51      public MBBan addBan(long userId, long plid, long banUserId)
52          throws PortalException, SystemException {
53  
54          User user = userPersistence.findByPrimaryKey(userId);
55          long groupId = PortalUtil.getPortletGroupId(plid);
56          Date now = new Date();
57  
58          long banId = counterLocalService.increment();
59  
60          MBBan ban = mbBanPersistence.fetchByG_B(groupId, banUserId);
61  
62          if (ban == null) {
63              ban = mbBanPersistence.create(banId);
64  
65              ban.setGroupId(groupId);
66              ban.setCompanyId(user.getCompanyId());
67              ban.setUserId(user.getUserId());
68              ban.setUserName(user.getFullName());
69              ban.setCreateDate(now);
70              ban.setBanUserId(banUserId);
71          }
72  
73          ban.setModifiedDate(now);
74  
75          mbBanPersistence.update(ban);
76  
77          return ban;
78      }
79  
80      public void checkBan(long groupId, long banUserId)
81          throws PortalException, SystemException {
82  
83          if (hasBan(groupId, banUserId)) {
84              throw new BannedUserException();
85          }
86      }
87  
88      public void deleteBan(long plid, long banUserId)
89          throws PortalException, SystemException {
90  
91          long groupId = PortalUtil.getPortletGroupId(plid);
92  
93          try {
94              mbBanPersistence.removeByG_B(groupId, banUserId);
95          }
96          catch (NoSuchBanException nsbe) {
97          }
98      }
99  
100     public void deleteBansByBanUserId(long banUserId) throws SystemException {
101         mbBanPersistence.removeByBanUserId(banUserId);
102     }
103 
104     public void deleteBansByGroupId(long groupId) throws SystemException {
105         mbBanPersistence.removeByGroupId(groupId);
106     }
107 
108     public void expireBans() throws SystemException {
109         long now = System.currentTimeMillis();
110 
111         int expireInterval = GetterUtil.getInteger(PropsUtil.get(
112             PropsUtil.MESSAGE_BOARDS_EXPIRE_BAN_INTERVAL));
113 
114         List bans = mbBanPersistence.findAll();
115 
116         for (int i = 0; i < bans.size(); i++) {
117             MBBan ban = (MBBan)bans.get(i);
118 
119             long unbanDate = MBUtil.getUnbanDate(ban, expireInterval).getTime();
120 
121             if (now >= unbanDate) {
122                 if (_log.isDebugEnabled()) {
123                     _log.debug(
124                         "Auto expiring ban " + ban.getBanId() + " on user " +
125                             ban.getBanUserId());
126                 }
127 
128                 mbBanPersistence.remove(ban);
129             }
130         }
131     }
132 
133     public List getBans(long groupId, int start, int end)
134         throws SystemException {
135 
136         return mbBanPersistence.findByGroupId(groupId, start, end);
137     }
138 
139     public int getBansCount(long groupId) throws SystemException {
140         return mbBanPersistence.countByGroupId(groupId);
141     }
142 
143     public boolean hasBan(long groupId, long banUserId)
144         throws SystemException {
145 
146         if (mbBanPersistence.fetchByG_B(groupId, banUserId) == null) {
147             return false;
148         }
149         else {
150             return true;
151         }
152     }
153 
154     private static Log _log = LogFactory.getLog(MBBanLocalServiceImpl.class);
155 
156 }