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