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.favorite.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.favorite.business.Favorite;
39  import fr.paris.lutece.plugins.extend.modules.favorite.business.FavoriteFilter;
40  import fr.paris.lutece.plugins.extend.modules.favorite.business.FavoriteHistory;
41  import fr.paris.lutece.plugins.extend.modules.favorite.business.IFavoriteDAO;
42  import fr.paris.lutece.plugins.extend.modules.favorite.service.FavoriteListenerService;
43  import fr.paris.lutece.plugins.extend.modules.favorite.service.extender.FavoriteResourceExtender;
44  import fr.paris.lutece.plugins.extend.service.extender.history.IResourceExtenderHistoryService;
45  import fr.paris.lutece.portal.service.security.LuteceUser;
46  
47  import org.apache.commons.collections.CollectionUtils;
48  import org.springframework.transaction.annotation.Transactional;
49  
50  import java.util.List;
51  
52  import javax.inject.Inject;
53  import javax.servlet.http.HttpServletRequest;
54  
55  /**
56   *
57   * favoriteService
58   *
59   */
60  public class FavoriteService implements IFavoriteService
61  {
62      /** The Constant BEAN_SERVICE. */
63      public static final String BEAN_SERVICE = "extendfavorite.favoriteService";
64      @Inject
65      private IFavoriteDAO _favoriteDAO;
66      @Inject
67      private IResourceExtenderHistoryService _resourceExtenderHistoryService;
68      @Inject
69      private IFavoriteHistoryService _favoriteHistoryService;
70  
71      /**
72       * {@inheritDoc}
73       */
74      @Override
75      @Transactional( FavoritePlugin.TRANSACTION_MANAGER )
76      public void create( Favorite favorite )
77      {
78          _favoriteDAO.insert( favorite, FavoritePlugin.getPlugin( ) );
79      }
80  
81      /**
82       * {@inheritDoc}
83       */
84      @Override
85      @Transactional( FavoritePlugin.TRANSACTION_MANAGER )
86      public void update( Favorite favorite )
87      {
88          _favoriteDAO.store( favorite, FavoritePlugin.getPlugin( ) );
89      }
90  
91      /**
92       * {@inheritDoc}
93       */
94      @Override
95      @Transactional( FavoritePlugin.TRANSACTION_MANAGER )
96      public synchronized void doFavorite( String strIdExtendableResource, String strExtendableResourceType, int nVoteValue, HttpServletRequest request )
97      {
98          Favorite favorite = findByResource( strIdExtendableResource, strExtendableResourceType );
99  
100         // Create the favorite if not exists
101         if ( favorite == null )
102         {
103             favorite = new Favorite( );
104             favorite.setIdExtendableResource( strIdExtendableResource );
105             favorite.setExtendableResourceType( strExtendableResourceType );
106             favorite.setFavoriteCount( 1 );
107             create( favorite );
108         }
109         else
110         {
111             favorite.setFavoriteCount( favorite.getFavoriteCount( ) + 1 );
112             update( favorite );
113         }
114 
115         ResourceExtenderHistory history = _resourceExtenderHistoryService.create( FavoriteResourceExtender.RESOURCE_EXTENDER, strIdExtendableResource,
116                 strExtendableResourceType, request );
117 
118         FavoriteHistoryules/favorite/business/FavoriteHistory.html#FavoriteHistory">FavoriteHistory favoriteHistory = new FavoriteHistory( );
119         favoriteHistory.setIdExtenderHistory( history.getIdHistory( ) );
120         favoriteHistory.setFavoriteValue( nVoteValue );
121         _favoriteHistoryService.create( favoriteHistory );
122         // Call Listener
123         FavoriteListenerService.favorite( strExtendableResourceType, strIdExtendableResource, request );
124 
125     }
126 
127     /**
128      * {@inheritDoc}
129      */
130     @Override
131     @Transactional( FavoritePlugin.TRANSACTION_MANAGER )
132     public synchronized void doCancelFavorite( LuteceUser user, String strIdExtendableResource, String strExtendableResourceType, HttpServletRequest request )
133     {
134         ResourceExtenderHistoryFilter resourceExtenderHistoryFilter = new ResourceExtenderHistoryFilter( );
135         resourceExtenderHistoryFilter.setUserGuid( user.getName( ) );
136         resourceExtenderHistoryFilter.setIdExtendableResource( strIdExtendableResource );
137         resourceExtenderHistoryFilter.setExtendableResourceType( strExtendableResourceType );
138         resourceExtenderHistoryFilter.setExtenderType( FavoriteResourceExtender.RESOURCE_EXTENDER );
139 
140         List<ResourceExtenderHistory> histories = _resourceExtenderHistoryService.findByFilter( resourceExtenderHistoryFilter );
141 
142         if ( CollectionUtils.isNotEmpty( histories ) )
143         {
144             for ( ResourceExtenderHistory history : histories )
145             {
146                 FavoriteHistory favoriteHistory = _favoriteHistoryService.findByHistoryExtenderId( history.getIdHistory( ) );
147 
148                 if ( favoriteHistory != null )
149                 {
150                     _favoriteHistoryService.remove( favoriteHistory.getIdFavoriteHistory( ) );
151 
152                     Favorite favorite = findByResource( strIdExtendableResource, strExtendableResourceType );
153                     favorite.setFavoriteCount( favorite.getFavoriteCount( ) - 1 );
154                     update( favorite );
155                     // Call Listener
156                     FavoriteListenerService.cancelFavorite( strExtendableResourceType, strIdExtendableResource, request );
157 
158                 }
159 
160                 _resourceExtenderHistoryService.remove( Integer.valueOf( "" + history.getIdHistory( ) ) );
161             }
162         }
163     }
164 
165     /**
166      * {@inheritDoc}
167      */
168     @Override
169     @Transactional( FavoritePlugin.TRANSACTION_MANAGER )
170     public void remove( int nIdFavorite )
171     {
172         _favoriteDAO.delete( nIdFavorite, FavoritePlugin.getPlugin( ) );
173     }
174 
175     /**
176      * {@inheritDoc}
177      */
178     @Override
179     @Transactional( FavoritePlugin.TRANSACTION_MANAGER )
180     public void removeByResource( String strIdExtendableResource, String strExtendableResourceType )
181     {
182         _favoriteDAO.deleteByResource( strIdExtendableResource, strExtendableResourceType, FavoritePlugin.getPlugin( ) );
183     }
184 
185     // GET
186 
187     /**
188      * {@inheritDoc}
189      */
190     @Override
191     public Favorite findByPrimaryKey( int nIdFavorite )
192     {
193         return _favoriteDAO.load( nIdFavorite, FavoritePlugin.getPlugin( ) );
194     }
195 
196     /**
197      * {@inheritDoc}
198      */
199     @Override
200     public Favorite findByResource( String strIdExtendableResource, String strExtendableResourceType )
201     {
202         return _favoriteDAO.loadByResource( strIdExtendableResource, strExtendableResourceType, FavoritePlugin.getPlugin( ) );
203     }
204 
205     /**
206      * {@inheritDoc}
207      */
208     @Override
209     public List<Favorite> findByFilter( FavoriteFilter filter )
210     {
211         return _favoriteDAO.loadByFilter( filter, FavoritePlugin.getPlugin( ) );
212     }
213 }