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.blog.service;
35
36 import java.util.ArrayList;
37 import java.util.List;
38
39 import org.apache.commons.collections4.CollectionUtils;
40
41 import fr.paris.lutece.plugins.blog.business.Blog;
42 import fr.paris.lutece.plugins.blog.business.BlogFilter;
43 import fr.paris.lutece.plugins.blog.business.BlogHome;
44 import fr.paris.lutece.plugins.blog.business.DocContent;
45 import fr.paris.lutece.plugins.blog.business.DocContentHome;
46 import fr.paris.lutece.plugins.blog.business.IndexerAction;
47 import fr.paris.lutece.plugins.blog.business.Tag;
48 import fr.paris.lutece.plugins.blog.business.TagHome;
49 import fr.paris.lutece.plugins.blog.business.portlet.BlogPublicationHome;
50 import fr.paris.lutece.plugins.blog.service.docsearch.BlogSearchService;
51 import fr.paris.lutece.plugins.blog.utils.BlogUtils;
52 import fr.paris.lutece.portal.business.event.ResourceEvent;
53 import fr.paris.lutece.portal.service.event.ResourceEventManager;
54 import fr.paris.lutece.portal.service.util.AppException;
55 import fr.paris.lutece.util.sql.TransactionManager;
56
57
58
59
60 public class BlogService
61 {
62
63 private static BlogServicervice/BlogService.html#BlogService">BlogService _singleton = new BlogService( );
64
65
66
67
68
69
70 public static BlogService getInstance( )
71 {
72 return _singleton;
73 }
74
75
76
77
78
79
80
81 public void createBlog( Blog blog )
82
83 {
84 createBlog( blog, blog.getDocContent( ) );
85 }
86
87
88
89
90
91
92
93 public void updateBlog( Blog blog )
94
95 {
96 updateBlog( blog, blog.getDocContent( ) );
97
98 }
99
100
101
102
103
104
105
106 public void deleteBlog( int nId )
107
108 {
109 TransactionManager.beginTransaction( BlogPlugin.getPlugin( ) );
110
111 try
112 {
113 TagHome.removeTagDoc( nId );
114 DocContentHome.remove( nId );
115 BlogHome.remove( nId );
116 BlogHome.removeVersions( nId );
117 TransactionManager.commitTransaction( BlogPlugin.getPlugin( ) );
118 }
119 catch( Exception e )
120 {
121 TransactionManager.rollBack( BlogPlugin.getPlugin( ) );
122 throw new AppException( e.getMessage( ), e );
123 }
124 BlogSearchService.getInstance( ).addIndexerAction( nId, IndexerAction.TASK_DELETE );
125 }
126
127
128
129
130
131
132
133
134
135 public void deleteBlogVersion( int nIdBlog, int nVersion )
136 {
137 BlogHome.removeSpecificVersion( nIdBlog, nVersion );
138 }
139
140
141
142
143
144
145
146 public void revertBlogToPreviousVersion( Blog blog )
147 {
148
149 List<Blog> blogVersionsList = getLastBlogVersionsList( blog.getId( ), 2 );
150
151
152 if ( CollectionUtils.isNotEmpty( blogVersionsList ) && blogVersionsList.size( ) > 1 )
153 {
154 TransactionManager.beginTransaction( BlogPlugin.getPlugin( ) );
155
156 try
157 {
158
159 Blog previousBlogVersion = blogVersionsList.get( 1 );
160
161
162 BlogHome.update( previousBlogVersion );
163
164
165 BlogHome.removeSpecificVersion( blog.getId( ), blog.getVersion( ) );
166
167 TransactionManager.commitTransaction( BlogPlugin.getPlugin( ) );
168 }
169 catch( Exception e )
170 {
171 TransactionManager.rollBack( BlogPlugin.getPlugin( ) );
172 throw new AppException( e.getMessage( ), e );
173 }
174 }
175 }
176
177
178
179
180
181
182
183
184
185
186 public List<Blog> getLastBlogVersionsList( int nIdBlog, int nLimit )
187 {
188 return BlogHome.getLastBlogVersionsList( nIdBlog, nLimit );
189 }
190
191
192
193
194
195
196
197
198
199 public void createBlog( Blog blog, List<DocContent> docContent )
200
201 {
202
203 TransactionManager.beginTransaction( BlogPlugin.getPlugin( ) );
204
205 try
206 {
207 BlogHome.addInitialVersion( blog );
208 for ( Tag tag : blog.getTag( ) )
209 {
210
211 TagHome.create( tag.getIdTag( ), blog.getId( ), tag.getPriority( ) );
212 }
213 if ( docContent != null )
214 {
215 for ( DocContent docCont : docContent )
216 {
217 DocContentHome.insertInBlog( blog.getId( ), docCont.getId( ), docCont.getPriority( ) );
218
219 }
220
221 }
222 TransactionManager.commitTransaction( BlogPlugin.getPlugin( ) );
223 }
224 catch( Exception e )
225 {
226 TransactionManager.rollBack( BlogPlugin.getPlugin( ) );
227 throw new AppException( e.getMessage( ), e );
228 }
229
230 BlogSearchService.getInstance( ).addIndexerAction( blog.getId( ), IndexerAction.TASK_CREATE );
231 }
232
233
234
235
236
237
238
239 private void updateDocContent( DocContent docContent, int nIdBlog )
240 {
241 if ( docContent != null && docContent.getId( ) != 0 )
242 {
243 DocContentHome.removeInBlogById( docContent.getId( ) );
244 DocContentHome.insertInBlog( nIdBlog, docContent.getId( ), docContent.getPriority( ) );
245
246 }
247 else
248 if ( docContent != null )
249 {
250 DocContentHome.create( docContent );
251 DocContentHome.insertInBlog( nIdBlog, docContent.getId( ), docContent.getPriority( ) );
252 }
253
254 }
255
256
257
258
259
260
261
262
263
264 public void updateBlog( Blog blog, List<DocContent> docContent )
265
266 {
267 TransactionManager.beginTransaction( BlogPlugin.getPlugin( ) );
268
269 try
270 {
271 BlogHome.addNewVersion( blog );
272 if ( docContent != null )
273 {
274 updateDocContentList( blog, docContent );
275 }
276
277 TagHome.removeTagDoc( blog.getId( ) );
278 for ( Tag tag : blog.getTag( ) )
279 {
280 TagHome.create( tag.getIdTag( ), blog.getId( ), tag.getPriority( ) );
281 }
282 TransactionManager.commitTransaction( BlogPlugin.getPlugin( ) );
283 }
284 catch( Exception e )
285 {
286 TransactionManager.rollBack( BlogPlugin.getPlugin( ) );
287 throw new AppException( e.getMessage( ), e );
288 }
289
290 fireUpdateBlogEvent( blog.getId( ) );
291 }
292
293 private void updateDocContentList( Blog blog, List<DocContent> docContent )
294 {
295 List<DocContent> listDocContent = DocContentHome.getDocsContentByHtmlDoc( blog.getId( ) );
296 List<DocContent> listToCompare = new ArrayList<>( );
297 listToCompare.addAll( listDocContent );
298
299 for ( DocContent docCont : docContent )
300 {
301 if ( listDocContent.isEmpty( ) || listDocContent.removeIf( t -> t.getId( ) == docCont.getId( ) ) || docCont.getId( ) == 0 )
302 {
303 if ( listToCompare.stream( ).noneMatch( c -> ( c.getId( ) == docCont.getId( ) ) && ( c.getPriority( ) == docCont.getPriority( ) ) ) )
304 {
305
306 updateDocContent( docCont, blog.getId( ) );
307 }
308 }
309 }
310
311 for ( DocContent docCont : listDocContent )
312 {
313 DocContentHome.removeInBlogById( docCont.getId( ) );
314 }
315 }
316
317
318
319
320
321
322
323
324
325 public void updateBlogWithoutVersion( Blog blog, List<DocContent> docContent )
326
327 {
328 TransactionManager.beginTransaction( BlogPlugin.getPlugin( ) );
329 BlogHome.update( blog );
330 try
331 {
332 if ( docContent != null )
333 {
334 updateDocContentList( blog, docContent );
335 }
336
337 TagHome.removeTagDoc( blog.getId( ) );
338 for ( Tag tag : blog.getTag( ) )
339 {
340
341 TagHome.create( tag.getIdTag( ), blog.getId( ), tag.getPriority( ) );
342 }
343 TransactionManager.commitTransaction( BlogPlugin.getPlugin( ) );
344 }
345 catch( Exception e )
346 {
347 TransactionManager.rollBack( BlogPlugin.getPlugin( ) );
348 throw new AppException( e.getMessage( ), e );
349 }
350
351 fireUpdateBlogEvent( blog.getId( ) );
352 }
353
354
355
356
357
358
359
360
361 public Blog loadBlog( int nIdDocument )
362
363 {
364 Blog blog = BlogHome.findByPrimaryKey( nIdDocument );
365 if ( blog != null )
366 {
367 List<DocContent> docContent = DocContentHome.getDocsContentByHtmlDoc( nIdDocument );
368 blog.setDocContent( docContent );
369 blog.setTag( TagHome.loadByDoc( nIdDocument ) );
370 blog.setBlogPublication( BlogPublicationHome.getDocPublicationByIdDoc( nIdDocument ) );
371 }
372 return blog;
373
374 }
375
376
377
378
379
380
381
382
383
384
385 public Blog loadBlogByVersion( int nIdDocument, int nVersion )
386
387 {
388 Blog blog = BlogHome.findVersion( nIdDocument, nVersion );
389 if ( blog != null )
390 {
391 List<DocContent> docContent = DocContentHome.getDocsContentByHtmlDoc( nIdDocument );
392 blog.setDocContent( docContent );
393 blog.setTag( TagHome.loadByDoc( nIdDocument ) );
394 blog.setBlogPublication( BlogPublicationHome.getDocPublicationByIdDoc( nIdDocument ) );
395 }
396 return blog;
397
398 }
399
400
401
402
403
404
405
406
407 public Blog findByPrimaryKeyWithoutBinaries( int nIdDocument )
408
409 {
410 Blog blog = BlogHome.findByPrimaryKey( nIdDocument );
411 if ( blog != null )
412 {
413 blog.setTag( TagHome.loadByDoc( nIdDocument ) );
414 blog.setBlogPublication( BlogPublicationHome.getDocPublicationByIdDoc( nIdDocument ) );
415 }
416 return blog;
417
418 }
419
420
421
422
423
424
425 public List<Blog> getListBlogWhithBinaries( )
426
427 {
428 List<Blog> listBlogs = getListBlogWithoutBinaries( );
429
430 for ( Blog doc : listBlogs )
431 {
432
433 List<DocContent> docContent = DocContentHome.getDocsContentByHtmlDoc( doc.getId( ) );
434 doc.setDocContent( docContent );
435
436 }
437
438 return listBlogs;
439
440 }
441
442
443
444
445
446
447 public List<Blog> getListBlogWithoutBinaries( )
448
449 {
450 List<Blog> blogList = BlogHome.selectWithoutBinaries( );
451 for ( Blog blog : blogList )
452 {
453 blog.setBlogPublication( BlogPublicationHome.getDocPublicationByIdDoc( blog.getId( ) ) );
454 blog.setTag( TagHome.getTagListByDoc( blog.getId( ) ) );
455 }
456 return blogList;
457
458 }
459
460
461
462
463
464
465
466
467 public List<Blog> searchListBlogByTag( int nIdTag )
468
469 {
470 return BlogHome.getBlogByTag( nIdTag );
471
472 }
473
474
475
476
477
478
479
480
481 public List<Blog> findByFilter( BlogFilter filter )
482 {
483 return BlogHome.findByFilter( filter );
484 }
485
486
487
488
489
490
491
492
493 public List<Blog> getLastModifiedBlogsList( int nLimit )
494 {
495 List<Blog> listBlog = BlogHome.getLastModifiedBlogsList( nLimit );
496 for ( Blog blog : listBlog )
497 {
498 blog.setTag( TagHome.getTagListByDoc( blog.getId( ) ) );
499 List<DocContent> docContent = DocContentHome.getDocsContentByHtmlDoc( blog.getId( ) );
500 blog.setDocContent( docContent );
501 }
502
503 return listBlog;
504 }
505
506 public void fireCreateBlogEvent( int blogId )
507 {
508 ResourceEvent formResponseEvent = new ResourceEvent( );
509 formResponseEvent.setIdResource( String.valueOf( blogId ) );
510 formResponseEvent.setTypeResource( BlogUtils.CONSTANT_TYPE_RESOURCE );
511 ResourceEventManager.fireAddedResource( formResponseEvent );
512 BlogSearchService.getInstance( ).addIndexerAction( blogId, IndexerAction.TASK_CREATE );
513 }
514
515 public void fireUpdateBlogEvent( int blogId )
516 {
517 ResourceEvent formResponseEvent = new ResourceEvent( );
518 formResponseEvent.setIdResource( String.valueOf( blogId ) );
519 formResponseEvent.setTypeResource( BlogUtils.CONSTANT_TYPE_RESOURCE );
520 ResourceEventManager.fireUpdatedResource( formResponseEvent );
521 BlogSearchService.getInstance( ).addIndexerAction( blogId, IndexerAction.TASK_MODIFY );
522 }
523
524 public void fireDeleteBlogEvent( int blogId )
525 {
526 ResourceEvent formResponseEvent = new ResourceEvent( );
527 formResponseEvent.setIdResource( String.valueOf( blogId ) );
528 formResponseEvent.setTypeResource( BlogUtils.CONSTANT_TYPE_RESOURCE );
529 ResourceEventManager.fireDeletedResource( formResponseEvent );
530 BlogSearchService.getInstance( ).addIndexerAction( blogId, IndexerAction.TASK_MODIFY );
531 }
532 }