View Javadoc
1   /*
2    * Copyright (c) 2002-2021, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.extend.modules.follow.service;
35  
36  import fr.paris.lutece.plugins.extend.business.extender.history.ResourceExtenderHistory;
37  import fr.paris.lutece.plugins.extend.business.extender.history.ResourceExtenderHistoryFilter;
38  import fr.paris.lutece.plugins.extend.modules.follow.business.Follow;
39  import fr.paris.lutece.plugins.extend.modules.follow.business.FollowExtenderConfig;
40  import fr.paris.lutece.plugins.extend.modules.follow.business.FollowFilter;
41  import fr.paris.lutece.plugins.extend.modules.follow.business.FollowHistory;
42  import fr.paris.lutece.plugins.extend.modules.follow.business.IFollowDAO;
43  import fr.paris.lutece.plugins.extend.modules.follow.service.extender.FollowResourceExtender;
44  import fr.paris.lutece.plugins.extend.service.extender.history.IResourceExtenderHistoryService;
45  import fr.paris.lutece.portal.service.security.LuteceUser;
46  import fr.paris.lutece.portal.service.security.SecurityService;
47  
48  import org.apache.commons.collections.CollectionUtils;
49  
50  import org.springframework.transaction.annotation.Transactional;
51  
52  import java.util.List;
53  
54  import javax.inject.Inject;
55  
56  import javax.servlet.http.HttpServletRequest;
57  
58  /**
59   *
60   * followService
61   *
62   */
63  public class FollowService implements IFollowService
64  {
65      /** The Constant BEAN_SERVICE. */
66      public static final String BEAN_SERVICE = "extendfollow.followService";
67      @Inject
68      private IFollowDAO _followDAO;
69      @Inject
70      private IResourceExtenderHistoryService _resourceExtenderHistoryService;
71      @Inject
72      private IFollowHistoryService _followHistoryService;
73  
74      /**
75       * {@inheritDoc}
76       */
77      @Override
78      @Transactional( FollowPlugin.TRANSACTION_MANAGER )
79      public void create( Follow follow )
80      {
81          _followDAO.insert( follow, FollowPlugin.getPlugin( ) );
82      }
83  
84      /**
85       * {@inheritDoc}
86       */
87      @Override
88      @Transactional( FollowPlugin.TRANSACTION_MANAGER )
89      public void update( Follow follow )
90      {
91          _followDAO.store( follow, FollowPlugin.getPlugin( ) );
92      }
93  
94      /**
95       * {@inheritDoc}
96       */
97      @Override
98      @Transactional( FollowPlugin.TRANSACTION_MANAGER )
99      public synchronized void doFollow( String strIdExtendableResource, String strExtendableResourceType, int nVoteValue, HttpServletRequest request )
100     {
101         Follow follow = findByResource( strIdExtendableResource, strExtendableResourceType );
102 
103         // Create the follow if not exists
104         if ( follow == null )
105         {
106             follow = new Follow( );
107             follow.setIdExtendableResource( strIdExtendableResource );
108             follow.setExtendableResourceType( strExtendableResourceType );
109             follow.setFollowCount( 1 );
110             create( follow );
111         }
112         else
113         {
114             follow.setFollowCount( follow.getFollowCount( ) + 1 );
115             update( follow );
116         }
117 
118         ResourceExtenderHistory history = _resourceExtenderHistoryService.create( FollowResourceExtender.RESOURCE_EXTENDER, strIdExtendableResource,
119                 strExtendableResourceType, request );
120 
121         FollowHistory/modules/follow/business/FollowHistory.html#FollowHistory">FollowHistory followHistory = new FollowHistory( );
122         followHistory.setIdExtenderHistory( history.getIdHistory( ) );
123         followHistory.setFollowValue( nVoteValue );
124         _followHistoryService.create( followHistory );
125         // Call Listener
126         FollowListenerService.follow( strExtendableResourceType, strIdExtendableResource, request );
127 
128     }
129 
130     /**
131      * {@inheritDoc}
132      */
133     @Override
134     @Transactional( FollowPlugin.TRANSACTION_MANAGER )
135     public synchronized void doCancelFollow( LuteceUser user, String strIdExtendableResource, String strExtendableResourceType, HttpServletRequest request )
136     {
137         ResourceExtenderHistoryFilter resourceExtenderHistoryFilter = new ResourceExtenderHistoryFilter( );
138         resourceExtenderHistoryFilter.setUserGuid( user.getName( ) );
139         resourceExtenderHistoryFilter.setIdExtendableResource( strIdExtendableResource );
140         resourceExtenderHistoryFilter.setExtendableResourceType( strExtendableResourceType );
141         resourceExtenderHistoryFilter.setExtenderType( FollowResourceExtender.RESOURCE_EXTENDER );
142 
143         List<ResourceExtenderHistory> histories = _resourceExtenderHistoryService.findByFilter( resourceExtenderHistoryFilter );
144 
145         if ( CollectionUtils.isNotEmpty( histories ) )
146         {
147             for ( ResourceExtenderHistory history : histories )
148             {
149                 FollowHistory followHistory = _followHistoryService.findByHistoryExtenderId( history.getIdHistory( ) );
150 
151                 if ( followHistory != null )
152                 {
153                     _followHistoryService.remove( followHistory.getIdFollowHistory( ) );
154 
155                     Follow follow = findByResource( strIdExtendableResource, strExtendableResourceType );
156                     follow.setFollowCount( follow.getFollowCount( ) - 1 );
157                     update( follow );
158                     // Call Listener
159                     FollowListenerService.cancelFollow( strExtendableResourceType, strIdExtendableResource, request );
160 
161                 }
162 
163                 _resourceExtenderHistoryService.remove( Integer.valueOf( "" + history.getIdHistory( ) ) );
164             }
165         }
166     }
167 
168     /**
169      * {@inheritDoc}
170      */
171     @Override
172     @Transactional( FollowPlugin.TRANSACTION_MANAGER )
173     public void remove( int nIdFollow )
174     {
175         _followDAO.delete( nIdFollow, FollowPlugin.getPlugin( ) );
176     }
177 
178     /**
179      * {@inheritDoc}
180      */
181     @Override
182     @Transactional( FollowPlugin.TRANSACTION_MANAGER )
183     public void removeByResource( String strIdExtendableResource, String strExtendableResourceType )
184     {
185         _followDAO.deleteByResource( strIdExtendableResource, strExtendableResourceType, FollowPlugin.getPlugin( ) );
186     }
187 
188     // GET
189 
190     /**
191      * {@inheritDoc}
192      */
193     @Override
194     public Follow findByPrimaryKey( int nIdFollow )
195     {
196         return _followDAO.load( nIdFollow, FollowPlugin.getPlugin( ) );
197     }
198 
199     /**
200      * {@inheritDoc}
201      */
202     @Override
203     public Follow findByResource( String strIdExtendableResource, String strExtendableResourceType )
204     {
205         return _followDAO.loadByResource( strIdExtendableResource, strExtendableResourceType, FollowPlugin.getPlugin( ) );
206     }
207 
208     /**
209      * {@inheritDoc}
210      */
211     @Override
212     public List<Follow> findByFilter( FollowFilter filter )
213     {
214         return _followDAO.loadByFilter( filter, FollowPlugin.getPlugin( ) );
215     }
216     
217     /**
218      * {@inheritDoc}
219      */
220     @Override
221     public boolean isAuthorized( HttpServletRequest request, FollowExtenderConfig config )
222     {
223         return config.isAuthenticatedMode( ) || SecurityService.getInstance( ).getRegisteredUser( request ) != null;
224     }
225 }