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