1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
61
62
63 public class FollowService implements IFollowService
64 {
65
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
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
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
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
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
126 FollowListenerService.follow( strExtendableResourceType, strIdExtendableResource, request );
127
128 }
129
130
131
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
159 FollowListenerService.cancelFollow( strExtendableResourceType, strIdExtendableResource, request );
160
161 }
162
163 _resourceExtenderHistoryService.remove( Integer.valueOf( "" + history.getIdHistory( ) ) );
164 }
165 }
166 }
167
168
169
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
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
189
190
191
192
193 @Override
194 public Follow findByPrimaryKey( int nIdFollow )
195 {
196 return _followDAO.load( nIdFollow, FollowPlugin.getPlugin( ) );
197 }
198
199
200
201
202 @Override
203 public Follow findByResource( String strIdExtendableResource, String strExtendableResourceType )
204 {
205 return _followDAO.loadByResource( strIdExtendableResource, strExtendableResourceType, FollowPlugin.getPlugin( ) );
206 }
207
208
209
210
211 @Override
212 public List<Follow> findByFilter( FollowFilter filter )
213 {
214 return _followDAO.loadByFilter( filter, FollowPlugin.getPlugin( ) );
215 }
216
217
218
219
220 @Override
221 public boolean isAuthorized( HttpServletRequest request, FollowExtenderConfig config )
222 {
223 return config.isAuthenticatedMode( ) || SecurityService.getInstance( ).getRegisteredUser( request ) != null;
224 }
225 }