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.blog.business;
35  
36  import java.io.Serializable;
37  import java.sql.Timestamp;
38  import java.util.ArrayList;
39  import java.util.List;
40  
41  import javax.validation.constraints.NotEmpty;
42  import javax.validation.constraints.Size;
43  
44  import fr.paris.lutece.plugins.blog.business.portlet.BlogPublication;
45  import fr.paris.lutece.portal.business.user.AdminUser;
46  import fr.paris.lutece.portal.business.user.AdminUserHome;
47  import fr.paris.lutece.portal.service.rbac.RBACResource;
48  import fr.paris.lutece.portal.service.resource.IExtendableResource;
49  import fr.paris.lutece.util.ReferenceItem;
50  
51  /**
52   * This is the business class for the object Blog
53   */
54  public class Blog extends ReferenceItem implements Serializable, IExtendableResource, RBACResource
55  {
56      public static final String PROPERTY_RESOURCE_TYPE = "BLOG";
57  
58      private static final long serialVersionUID = 1L;
59  
60      // Perimissions
61      public static final String PERMISSION_VIEW = "VIEW";
62      public static final String PERMISSION_CREATE = "CREATE";
63      public static final String PERMISSION_MODIFY = "MODIFY";
64      public static final String PERMISSION_DELETE = "DELETE";
65      public static final String PERMISSION_PUBLISH = "PUBLISH";
66  
67      // Variables declarations
68      private int _nId;
69  
70      private int _nAttachedPortletId;
71  
72      private int _nVersion;
73  
74      @NotEmpty( message = "#i18n{blog.validation.blog.ContentLabel.notEmpty}" )
75      @Size( max = 255, message = "#i18n{blog.validation.blog.ContentLabel.size}" )
76      private String _strContentLabel;
77  
78      private Timestamp _dateCreationDate;
79  
80      private Timestamp _dateUpdateDate;
81  
82      @NotEmpty( message = "#i18n{blog.validation.blog.HtmlContent.notEmpty}" )
83      @Size( message = "#i18n{blog.validation.blog.HtmlContent.size}" )
84      private String _strHtmlContent;
85  
86      @NotEmpty( message = "#i18n{blog.validation.blog.User.notEmpty}" )
87      @Size( max = 255, message = "#i18n{blog.validation.blog.User.size}" )
88      private String _strUser;
89  
90      @NotEmpty( message = "#i18n{blog.validation.blog.UserCreator.notEmpty}" )
91      @Size( max = 255, message = "#i18n{blog.validation.blog.UserCreator.size}" )
92      private String _strUserCreator;
93  
94      @Size( max = 255, message = "#i18n{blog.validation.blog.EditComment.size}" )
95      private String _strEditComment;
96  
97      @Size( message = "#i18n{blog.validation.description.size}" )
98      private String _strDescription;
99  
100     private List<DocContent> _docContent = new ArrayList<>( );
101 
102     private String _strUrl;
103 
104     private boolean _bShareable;
105 
106     private boolean _bLocked;
107 
108     private List<Tag> _tag = new ArrayList<>( );
109 
110     private List<BlogPublication> _blogPublication = new ArrayList<>( );
111 
112     /**
113      * Returns the Id
114      * 
115      * @return The Id
116      */
117     public int getId( )
118     {
119         return _nId;
120     }
121 
122     /**
123      * {@inheritDoc }
124      */
125     @Override
126     public String getName( )
127     {
128         return getContentLabel( );
129     }
130 
131     /**
132      * Sets the Id
133      * 
134      * @param nId
135      *            The Id
136      */
137     public void setId( int nId )
138     {
139         _nId = nId;
140     }
141 
142     /**
143      * Returns the Version
144      * 
145      * @return The Version
146      */
147     public int getVersion( )
148     {
149         return _nVersion;
150     }
151 
152     /**
153      * Sets the Version
154      * 
155      * @param nVersion
156      *            The Version
157      */
158     public void setVersion( int nVersion )
159     {
160         _nVersion = nVersion;
161     }
162 
163     /**
164      * Returns the ContentLabel
165      * 
166      * @return The ContentLabel
167      */
168     public String getContentLabel( )
169     {
170         return _strContentLabel;
171     }
172 
173     /**
174      * Sets the ContentLabel
175      * 
176      * @param strContentLabel
177      *            The ContentLabel
178      */
179     public void setContentLabel( String strContentLabel )
180     {
181         _strContentLabel = strContentLabel;
182     }
183 
184     /**
185      * Returns the CreationDate
186      * 
187      * @return The CreationDate
188      */
189     public Timestamp getCreationDate( )
190     {
191         return _dateCreationDate;
192     }
193 
194     /**
195      * Sets the CreationDate
196      * 
197      * @param dateCreationDate
198      *            The CreationDate
199      */
200     public void setCreationDate( Timestamp dateCreationDate )
201     {
202         _dateCreationDate = dateCreationDate;
203     }
204 
205     /**
206      * Returns the UpdateDate
207      * 
208      * @return The UpdateDate
209      */
210     public Timestamp getUpdateDate( )
211     {
212         return _dateUpdateDate;
213     }
214 
215     /**
216      * Sets the UpdateDate
217      * 
218      * @param dateUpdateDate
219      *            The UpdateDate
220      */
221     public void setUpdateDate( Timestamp dateUpdateDate )
222     {
223         _dateUpdateDate = dateUpdateDate;
224     }
225 
226     /**
227      * Returns the HtmlContent
228      * 
229      * @return The HtmlContent
230      */
231     public String getHtmlContent( )
232     {
233         return _strHtmlContent;
234     }
235 
236     /**
237      * Sets the HtmlContent
238      * 
239      * @param strHtmlContent
240      *            The HtmlContent
241      */
242     public void setHtmlContent( String strHtmlContent )
243     {
244         _strHtmlContent = strHtmlContent;
245     }
246 
247     /**
248      * Returns the User
249      * 
250      * @return The User
251      */
252     public String getUser( )
253     {
254         return _strUser;
255     }
256 
257     /**
258      * Sets the User
259      * 
260      * @param strUser
261      *            The User
262      */
263     public void setUser( String strUser )
264     {
265         _strUser = strUser;
266     }
267 
268     /**
269      * Returns the UserCreator
270      * 
271      * @return The UserCreator
272      */
273     public String getUserCreator( )
274     {
275         return _strUserCreator;
276     }
277 
278     /**
279      * Sets the UserCreator
280      * 
281      * @param strUserCreator
282      *            The UserCreator
283      */
284     public void setUserCreator( String strUserCreator )
285     {
286         _strUserCreator = strUserCreator;
287     }
288 
289     public void setAttachedPortletId( int nAttachedPortletId )
290     {
291         _nAttachedPortletId = nAttachedPortletId;
292     }
293 
294     public int getAttachedPortletId( )
295     {
296         return _nAttachedPortletId;
297     }
298 
299     /**
300      * Returns the EditComment
301      *
302      * @return The EditComment
303      */
304     public String getEditComment( )
305     {
306         return _strEditComment;
307     }
308 
309     /**
310      * Sets the EditComment
311      *
312      * @param strEditComment
313      *            The EditComment
314      */
315     public void setEditComment( String strEditComment )
316     {
317         _strEditComment = strEditComment;
318     }
319 
320     /**
321      * Returns the Description
322      *
323      * @return The Description
324      */
325     public String getDescription( )
326     {
327         return _strDescription;
328     }
329 
330     /**
331      * Sets the Description
332      *
333      * @param strDescription
334      *            The Description
335      */
336     public void setDescription( String strDescription )
337     {
338         _strDescription = strDescription;
339     }
340 
341     /**
342      * Returns the list of DocContent
343      *
344      * @return The DocContent
345      */
346     public List<DocContent> getDocContent( )
347     {
348         return _docContent;
349     }
350 
351     /**
352      * Sets the list DocContent
353      *
354      * @param docContent
355      *            the list DocContent
356      */
357     public void setDocContent( List<DocContent> docContent )
358     {
359         _docContent = docContent;
360     }
361 
362     /**
363      * Sets the docConetnt list
364      *
365      * @param docContent
366      *            list The docContent list
367      */
368     public void addContent( DocContent docContent )
369     {
370 
371         boolean isContain = _docContent.stream( ).anyMatch( dc -> dc.getTextValue( ).equals( docContent.getTextValue( ) ) );
372 
373         if ( !isContain )
374         {
375             _docContent.add( docContent );
376         }
377     }
378 
379     /**
380      * delet the docContent
381      *
382      * @param nIdDoc
383      *            The docContent Id
384      */
385     public void deleteDocContent( int nIdDoc )
386     {
387         _docContent.removeIf( dc -> dc.getId( ) == nIdDoc );
388 
389     }
390 
391     /**
392      * Returns the Url
393      *
394      * @return The Url
395      */
396     public String getUrl( )
397     {
398         return _strUrl;
399     }
400 
401     /**
402      * Sets the Url
403      *
404      * @param strUrl
405      *            The Url
406      */
407     public void setUrl( String strUrl )
408     {
409         _strUrl = strUrl;
410     }
411 
412     /**
413      * Returns the Shareable
414      * 
415      * @return The Shareable
416      */
417     public boolean getShareable( )
418     {
419         return _bShareable;
420     }
421 
422     /**
423      * shareable
424      * 
425      * @param bShareable
426      */
427     public void setShareable( boolean bShareable )
428     {
429         _bShareable = bShareable;
430     }
431 
432     /**
433      * Returns the Locked
434      * 
435      * @return The Locked
436      */
437     public boolean getLocked( )
438     {
439         return _bLocked;
440     }
441 
442     /**
443      * Locked
444      * 
445      * @param bLocked
446      */
447     public void setLocked( boolean bLocked )
448     {
449         _bLocked = bLocked;
450     }
451 
452     /**
453      * Returns the tag list
454      *
455      * @return The tag list
456      */
457     public List<Tag> getTag( )
458     {
459         return _tag;
460     }
461 
462     /**
463      * Sets the tag list
464      *
465      * @param tag
466      *            list The tag list
467      */
468     public void setTag( List<Tag> tag )
469     {
470         _tag = tag;
471     }
472 
473     /**
474      * Sets the tag list
475      *
476      * @param tag
477      *            list The tag list
478      */
479     public void addTag( Tag tag )
480     {
481         boolean bContain = _tag.stream( ).anyMatch( tg -> tg.getIdTag( ) == tag.getIdTag( ) );
482         if ( !bContain )
483         {
484             _tag.add( tag );
485         }
486     }
487 
488     /**
489      * delet the tag
490      *
491      * @param tag
492      *            The tag
493      */
494     public void deleteTag( Tag tag )
495     {
496         _tag.removeIf( tg -> tg.getIdTag( ) == tag.getIdTag( ) );
497 
498     }
499 
500     /**
501      * Returns the blogc list
502      *
503      * @return The BlogPublication list
504      */
505     public List<BlogPublication> getBlogPublication( )
506     {
507         return _blogPublication;
508     }
509 
510     /**
511      * Sets the BlogPublication list
512      *
513      * @param blogPublication
514      *            list The blogPublication list
515      */
516     public void setBlogPublication( List<BlogPublication> blogPublication )
517     {
518         _blogPublication = blogPublication;
519     }
520 
521     /**
522      * Sets the BlogPublication list
523      *
524      * @param blogPublication
525      *            list The BlogPublication list
526      */
527     public void addBlogPublication( BlogPublication blogPublication )
528     {
529 
530         boolean bContain = _blogPublication.stream( )
531                 .anyMatch( blogPub -> ( blogPub.getIdBlog( ) == blogPublication.getIdBlog( ) && blogPub.getIdPortlet( ) == blogPublication.getIdPortlet( ) ) );
532 
533         if ( !bContain )
534         {
535             _blogPublication.add( blogPublication );
536         }
537     }
538 
539     /**
540      * delet the BlogPublication
541      *
542      * @param blogPublication
543      *            The BlogPublication
544      */
545     public void deleteBlogPublication( BlogPublication blogPublication )
546     {
547 
548         _blogPublication
549                 .removeIf( blogPub -> ( blogPub.getIdBlog( ) == blogPublication.getIdBlog( ) && blogPub.getIdPortlet( ) == blogPublication.getIdPortlet( ) ) );
550     }
551 
552     /**
553      * {@inheritDoc}
554      */
555     @Override
556     public String getExtendableResourceDescription( )
557     {
558 
559         return getDescription( );
560     }
561 
562     /**
563      * {@inheritDoc}
564      */
565     @Override
566     public String getExtendableResourceImageUrl( )
567     {
568 
569         return null;
570     }
571 
572     /**
573      * {@inheritDoc}
574      */
575     @Override
576     public String getExtendableResourceName( )
577     {
578 
579         return getContentLabel( );
580     }
581 
582     /**
583      * {@inheritDoc}
584      */
585     @Override
586     public String getExtendableResourceType( )
587     {
588 
589         return PROPERTY_RESOURCE_TYPE;
590     }
591 
592     /**
593      * {@inheritDoc}
594      */
595     @Override
596     public String getIdExtendableResource( )
597     {
598         return Integer.toString( _nId );
599     }
600 
601     /**
602      * {@inheritDoc}
603      */
604     @Override
605     public String getResourceId( )
606     {
607 
608         return String.valueOf( _nId );
609     }
610 
611     /**
612      * {@inheritDoc}
613      */
614     @Override
615     public String getResourceTypeCode( )
616     {
617 
618         return PROPERTY_RESOURCE_TYPE;
619     }
620 
621     /**
622      * Return the user informations
623      * 
624      * @return AdminUser
625      */
626     public AdminUser getUserInfos( )
627     {
628         return AdminUserHome.findUserByLogin( _strUser );
629     }
630 
631     /**
632      * Return the user creator informations
633      * 
634      * @return AdminUser
635      */
636     public AdminUser getUserCreatorInfos( )
637     {
638         return AdminUserHome.findUserByLogin( _strUserCreator );
639     }
640 
641 }