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