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.document.business;
35  
36  import fr.paris.lutece.plugins.document.business.attributes.DocumentAttribute;
37  import fr.paris.lutece.plugins.document.business.category.Category;
38  import fr.paris.lutece.plugins.document.business.publication.DocumentPublication;
39  import fr.paris.lutece.plugins.document.business.publication.DocumentPublicationHome;
40  import fr.paris.lutece.portal.business.resourceenhancer.ResourceEnhancer;
41  import fr.paris.lutece.portal.service.i18n.I18nService;
42  import fr.paris.lutece.portal.service.i18n.Localizable;
43  import fr.paris.lutece.portal.service.resource.IExtendableResource;
44  import fr.paris.lutece.portal.service.util.AppPropertiesService;
45  import fr.paris.lutece.util.date.DateUtil;
46  import fr.paris.lutece.util.url.UrlItem;
47  import fr.paris.lutece.util.xml.XmlUtil;
48  
49  import org.apache.commons.lang3.StringUtils;
50  
51  import java.util.Date;
52  import java.util.GregorianCalendar;
53  import java.util.List;
54  import java.util.Locale;
55  
56  import javax.servlet.http.HttpServletRequest;
57  
58  
59  /**
60   * A document of the CMS.
61   */
62  public class Document implements Localizable, IExtendableResource
63  {
64      public static final String CODE_DOCUMENT_TYPE_DOWNLOAD = "download";
65      public static final String CODE_DOCUMENT_TYPE_IMAGE = "image";
66      public static final String PROPERTY_RESOURCE_TYPE = "document";
67      private static final String SERVLET_DOCUMENT_PATH = "document";
68      private static final String PROPERTY_DEFAULT_THUMBNAIL = "document.thumbnail.default";
69      private static final String PROPERTY_RESOURCE_PROVIDER_URL = "document.resource.provider.url";
70  
71      /////////////////////////////////////////////////////////////////////////////////
72      // Xml Tags
73      private static final String TAG_DOCUMENT = "document";
74      private static final String TAG_DOCUMENT_ID = "document-id";
75      private static final String TAG_DATE_PUBLICATION = "document-date-publication";
76      private static final String TAG_DOCUMENT_XML_CONTENT = "document-xml-content";
77      private static final String MARK_ID = "id";
78      private static final String MARK_ID_ATTRIBUTE = "id_attribute";
79      private static final String EMPTY_STRING = "";
80      private static final String CONSTANT_QUESTION_MARK = "?";
81      private static final String CONSTANT_EQUALS = "=";
82  
83      // Variables declarations
84      private int _nIdDocument;
85      private String _strCodeDocumentType;
86      private String _strType;
87      private String _strTitle;
88      private String _strSummary;
89      private int _nIdCreator;
90      private int _nIdMailingList;
91      private java.sql.Timestamp _dateCreation;
92      private java.sql.Timestamp _dateModification;
93      private java.sql.Timestamp _dateValidityBegin;
94      private java.sql.Timestamp _dateValidityEnd;
95      private String _strComment;
96      private String _strXmlWorkingContent;
97      private String _strXmlValidatedContent;
98      private String _strXmlMetadata;
99      private int _nIdSpace;
100     private int _nIdPageTemplateDocument;
101     private int _nPublishedStatus;
102     private String _strSpace;
103     private int _nIdState;
104     private String _strState;
105     private List<DocumentAttribute> _listAttributes;
106     private Locale _locale;
107     private List _listActions;
108     private List<Category> _listCategories;
109     private boolean _bSkipPortlet;    // Skips HTML code generation for documents list portlet   
110     private boolean _bSkipCategories; // Skips HTML code generation for category list portlet
111 
112     /**
113      * Returns the IdDocument
114      *
115      * @return The IdDocument
116      */
117     public int getId(  )
118     {
119         return _nIdDocument;
120     }
121 
122     /**
123      * Sets the IdDocument
124      *
125      * @param nIdDocument The IdDocument
126      */
127     public void setId( int nIdDocument )
128     {
129         _nIdDocument = nIdDocument;
130     }
131 
132     /**
133      * Returns the Locale
134      *
135      * @return The Locale
136      */
137     public Locale getLocale(  )
138     {
139         return I18nService.getDefaultLocale(  ); //FIXME The document should store its locale
140     }
141 
142     /**
143      * Sets the Locale
144      *
145      * @param locale The Locale
146      */
147     @Override
148     public void setLocale( Locale locale )
149     {
150         _locale = locale;
151     }
152 
153     /**
154      * Returns the CodeDocumentType
155      *
156      * @return The CodeDocumentType
157      */
158     public String getCodeDocumentType(  )
159     {
160         return _strCodeDocumentType;
161     }
162 
163     /**
164      * Sets the CodeDocumentType
165      *
166      * @param strCodeDocumentType The CodeDocumentType
167      */
168     public void setCodeDocumentType( String strCodeDocumentType )
169     {
170         _strCodeDocumentType = strCodeDocumentType;
171     }
172 
173     /**
174      * Returns the Title
175      *
176      * @return The Title
177      */
178     public String getTitle(  )
179     {
180         return _strTitle;
181     }
182 
183     /**
184      * Sets the Title
185      *
186      * @param strTitle The Title
187      */
188     public void setTitle( String strTitle )
189     {
190         _strTitle = strTitle;
191     }
192 
193     /**
194      * Returns the Summary
195      *
196      * @return The Summary
197      */
198     public String getSummary(  )
199     {
200         return ( _strSummary != null ) ? _strSummary : EMPTY_STRING;
201     }
202 
203     /**
204      * Sets the Summary
205      *
206      * @param strSummary The Summary
207      */
208     public void setSummary( String strSummary )
209     {
210         _strSummary = strSummary;
211     }
212 
213     /**
214      * Returns the DateCreation
215      *
216      * @return The DateCreation
217      */
218     public java.sql.Timestamp getDateCreation(  )
219     {
220         return _dateCreation;
221     }
222 
223     /**
224      * Sets the DateCreation
225      *
226      * @param dateCreation The DateCreation
227      */
228     public void setDateCreation( java.sql.Timestamp dateCreation )
229     {
230         _dateCreation = dateCreation;
231     }
232 
233     /**
234      * Returns the IdCreator
235      *
236      * @return The IdCreator
237      */
238     public int getCreatorId(  )
239     {
240         return _nIdCreator;
241     }
242 
243     /**
244      * Sets the IdCreator
245      *
246      * @param nIdCreator The IdCreator
247      */
248     public void setCreatorId( int nIdCreator )
249     {
250         _nIdCreator = nIdCreator;
251     }
252 
253     /**
254      * Returns the Date of the last Modification
255      *
256      * @return The Date of the last Modification
257      */
258     public java.sql.Timestamp getDateModification(  )
259     {
260         return _dateModification;
261     }
262 
263     /**
264      * Sets the Date of the last Modification
265      *
266      * @param dateModification The Date of the last Modification
267      */
268     public void setDateModification( java.sql.Timestamp dateModification )
269     {
270         _dateModification = dateModification;
271     }
272 
273     /**
274      * Returns the begining Date of the validity period of the document
275      *
276      * @return The begining Date of the validity period of the document
277      */
278     public java.sql.Timestamp getDateValidityBegin(  )
279     {
280         return _dateValidityBegin;
281     }
282 
283     /**
284      * Sets the begining Date of the validity period of the document
285      *
286      * @param dateValidityBegin The begining Date of the validity period of the
287      *            document
288      */
289     public void setDateValidityBegin( java.sql.Timestamp dateValidityBegin )
290     {
291         _dateValidityBegin = dateValidityBegin;
292     }
293 
294     /**
295      * Returns the end Date of the validity period of the document
296      *
297      * @return The end Date of the validity period of the document
298      */
299     public java.sql.Timestamp getDateValidityEnd(  )
300     {
301         return _dateValidityEnd;
302     }
303 
304     /**
305      * Sets the end Date of the validity period of the document
306      *
307      * @param dateValidityEnd The end Date of the validity period of the
308      *            document
309      */
310     public void setDateValidityEnd( java.sql.Timestamp dateValidityEnd )
311     {
312         _dateValidityEnd = dateValidityEnd;
313     }
314 
315     /**
316      * Returns the Comment
317      *
318      * @return The Comment
319      */
320     public String getComment(  )
321     {
322         return ( _strComment != null ) ? _strComment : EMPTY_STRING;
323     }
324 
325     /**
326      * Sets the Comment
327      *
328      * @param strComment The Comment
329      */
330     public void setComment( String strComment )
331     {
332         _strComment = strComment;
333     }
334 
335     /**
336      * Returns the XmlWorkingContent
337      *
338      * @return The XmlWorkingContent
339      */
340     public String getXmlWorkingContent(  )
341     {
342         return _strXmlWorkingContent;
343     }
344 
345     /**
346      * Sets the XmlWorkingContent
347      *
348      * @param strXmlWorkingContent The XmlWorkingContent
349      */
350     public void setXmlWorkingContent( String strXmlWorkingContent )
351     {
352         _strXmlWorkingContent = strXmlWorkingContent;
353     }
354 
355     /**
356      * Returns the XmlValidatedContent
357      *
358      * @return The XmlValidatedContent
359      */
360     public String getXmlValidatedContent(  )
361     {
362         return _strXmlValidatedContent;
363     }
364 
365     /**
366      * Sets the XmlValidatedContent
367      *
368      * @param strXmlValidatedContent The XmlValidatedContent
369      */
370     public void setXmlValidatedContent( String strXmlValidatedContent )
371     {
372         _strXmlValidatedContent = strXmlValidatedContent;
373     }
374 
375     /**
376      * Returns the XmlMetadata
377      *
378      * @return The XmlMetadata
379      */
380     public String getXmlMetadata(  )
381     {
382         return _strXmlMetadata;
383     }
384 
385     /**
386      * Sets the XmlMetadata
387      *
388      * @param strXmlMetadata The XmlMetadata
389      */
390     public void setXmlMetadata( String strXmlMetadata )
391     {
392         _strXmlMetadata = strXmlMetadata;
393     }
394 
395     /**
396      * Returns the IdSpace
397      *
398      * @return The IdSpace
399      */
400     public int getSpaceId(  )
401     {
402         return _nIdSpace;
403     }
404 
405     /**
406      * Sets the IdSpace
407      *
408      * @param nIdSpace The IdSpace
409      */
410     public void setSpaceId( int nIdSpace )
411     {
412         _nIdSpace = nIdSpace;
413     }
414 
415     /**
416      * Returns the Space
417      *
418      * @return The Space
419      */
420     public String getSpace(  )
421     {
422         return _strSpace;
423     }
424 
425     /**
426      * Sets the Space
427      *
428      * @param strSpace The Space
429      */
430     public void setSpace( String strSpace )
431     {
432         _strSpace = strSpace;
433     }
434 
435     /**
436      * Returns the IdState
437      *
438      * @return The IdState
439      */
440     public int getStateId(  )
441     {
442         return _nIdState;
443     }
444 
445     /**
446      * Sets the IdState
447      *
448      * @param nIdState The IdState
449      */
450     public void setStateId( int nIdState )
451     {
452         _nIdState = nIdState;
453     }
454 
455     /**
456      * Returns the State
457      *
458      * @return The State
459      */
460     public String getStateKey(  )
461     {
462         return _strState;
463     }
464 
465     /**
466      * Returns the State
467      *
468      * @return The State
469      */
470     public String getState(  )
471     {
472         return I18nService.getLocalizedString( _strState, _locale );
473     }
474 
475     /**
476      * Sets the State
477      *
478      * @param strState The State
479      */
480     public void setStateKey( String strState )
481     {
482         _strState = strState;
483     }
484 
485     /**
486      * Returns the IdMailingList
487      *
488      * @return The IdMailingList
489      */
490     public int getMailingListId(  )
491     {
492         return _nIdMailingList;
493     }
494 
495     /**
496      * Sets the IdMailingList
497      *
498      * @param nIdMailingList The IdMailingList
499      */
500     public void setMailingListId( int nIdMailingList )
501     {
502         _nIdMailingList = nIdMailingList;
503     }
504 
505     /**
506      * Returns the IdPageTemplateDocument
507      *
508      * @return The IdPageTemplateDocument
509      */
510     public int getPageTemplateDocumentId(  )
511     {
512         return _nIdPageTemplateDocument;
513     }
514 
515     /**
516      * Sets the IdPageTemplateDocument
517      *
518      * @param nIdPageTemplateDocument The IdPageTemplateDocument
519      */
520     public void setPageTemplateDocumentId( int nIdPageTemplateDocument )
521     {
522         _nIdPageTemplateDocument = nIdPageTemplateDocument;
523     }
524 
525     /**
526      * Returns attributes List
527      * @return The document attributes list
528      */
529     public List<DocumentAttribute> getAttributes(  )
530     {
531         return _listAttributes;
532     }
533 
534     /**
535      * Set the document attributes list
536      * @param listAttributes The document attributes list
537      */
538     public void setAttributes( List<DocumentAttribute> listAttributes )
539     {
540         _listAttributes = listAttributes;
541     }
542 
543     /**
544      * Returns Actions List
545      * @return The document Actions list
546      */
547     public List getActions(  )
548     {
549         return _listActions;
550     }
551 
552     /**
553      * Set the document Actions list
554      * @param listActions The document Actions list
555      */
556     public void setActions( List listActions )
557     {
558         _listActions = listActions;
559     }
560 
561     /**
562      * Returns the Type
563      *
564      * @return The Type
565      */
566     public String getType(  )
567     {
568         return _strType;
569     }
570 
571     /**
572      * Sets the Type
573      *
574      * @param strType The Type
575      */
576     public void setType( String strType )
577     {
578         _strType = strType;
579     }
580 
581     /**
582      * @return the _listCategory
583      */
584     public List<Category> getCategories(  )
585     {
586         return _listCategories;
587     }
588 
589     /**
590      * @param listCategory the _listCategory to set
591      */
592     public void setCategories( List<Category> listCategory )
593     {
594         _listCategories = listCategory;
595     }
596 
597     /**
598      * @param nPublishedStatus the nPublishedStatus to set
599      */
600     public void setPublishedStatus( int nPublishedStatus )
601     {
602         _nPublishedStatus = nPublishedStatus;
603     }
604 
605     /**
606      * Returns the PublishedSTatus
607      *
608      * @return The PublishedSTatus
609      */
610     public int getPublishedStatus(  )
611     {
612         return _nPublishedStatus;
613     }
614 
615     /**
616      * Returns the SkipPortlet
617      *
618      * @return The SkipPortlet
619      */
620     public boolean isSkipPortlet(  )
621     {
622         return _bSkipPortlet;
623     }
624 
625     /**
626      * Sets the SkipPortlet
627      *
628      * @param bSkipPortlet The SkipPortlet
629      */
630     public void setSkipPortlet( boolean bSkipPortlet )
631     {
632         _bSkipPortlet = bSkipPortlet;
633     }
634 
635     /**
636      * Returns the SkipCategories
637      *
638      * @return The SkipCategories
639      */
640     public boolean isSkipCategories(  )
641     {
642         return _bSkipCategories;
643     }
644 
645     /**
646      * Sets the SkipCategories
647      *
648      * @param bSkipCategories The SkipCategories
649      */
650     public void setSkipCategories( boolean bSkipCategories )
651     {
652         _bSkipCategories = bSkipCategories;
653     }
654 
655     /**
656      * Returns a Thumbnail url for the document based on a document attribute or
657      * on the document type.
658      * @return A Thumbnail url
659      */
660     public String getThumbnail(  )
661     {
662         String strThumbnailUrl = AppPropertiesService.getProperty( PROPERTY_DEFAULT_THUMBNAIL );
663         String strResourceUrl = AppPropertiesService.getProperty( PROPERTY_RESOURCE_PROVIDER_URL );
664 
665         DocumentType documentType = DocumentTypeHome.findByPrimaryKey( getCodeDocumentType(  ) );
666 
667         if ( documentType.getThumbnailAttributeId(  ) != 0 )
668         {
669             strThumbnailUrl = strResourceUrl + getId(  );
670         }
671         else if ( !documentType.getDefaultThumbnailUrl(  ).equals( "" ) )
672         {
673             strThumbnailUrl = documentType.getDefaultThumbnailUrl(  );
674         }
675 
676         return strThumbnailUrl;
677     }
678 
679     ////////////////////////////////////////////////////////////////////////////
680     // XML Generation
681 
682     /**
683      * Returns the xml of this document
684      * @param request The HTTP Servlet request
685      * @param nIdPortlet the id of the portlet where the document is published
686      * @return the link xml
687      */
688     public String getXml( HttpServletRequest request, int nIdPortlet )
689     {
690         StringBuffer strXml = new StringBuffer(  );
691 
692         XmlUtil.beginElement( strXml, TAG_DOCUMENT );
693 
694         XmlUtil.addElement( strXml, TAG_DOCUMENT_ID, Integer.toString( getId(  ) ) );
695 
696         if ( nIdPortlet != -1 )
697         {
698             DocumentPublication publication = DocumentPublicationHome.findByPrimaryKey( nIdPortlet, getId(  ) );
699             String strDate = DateUtil.getDateString( publication.getDatePublishing(  ), getLocale(  ) );
700             XmlUtil.addElement( strXml, TAG_DATE_PUBLICATION, strDate );
701         }
702 
703         XmlUtil.addElement( strXml, TAG_DOCUMENT_XML_CONTENT, getXmlValidatedContent(  ) );
704 
705         // additionnal info
706         ResourceEnhancer.getXmlAddOn( strXml, PROPERTY_RESOURCE_TYPE, getId(  ) );
707 
708         XmlUtil.endElement( strXml, TAG_DOCUMENT );
709 
710         return strXml.toString(  );
711     }
712 
713     /**
714      * Returns the xml document of this link
715      *
716      * @param nIdPortlet the id of the portlet where the document is published
717      * @param request The HTTP servlet request
718      * @return the link xml document
719      */
720     public String getXmlDocument( HttpServletRequest request, int nIdPortlet )
721     {
722         return XmlUtil.getXmlHeader(  ) + getXml( request, nIdPortlet );
723     }
724 
725     /**
726      * Returns a document attribute by its code
727      * @param strAttributeCode The Attribute Code
728      * @return the attribute object corresponding to the code
729      */
730     public DocumentAttribute getAttribute( String strAttributeCode )
731     {
732         if ( _listAttributes != null )
733         {
734             for ( DocumentAttribute attribute : _listAttributes )
735             {
736                 if ( attribute.getCode(  ).equals( strAttributeCode ) )
737                 {
738                     return attribute;
739                 }
740             }
741         }
742 
743         return null;
744     }
745 
746     /**
747      * Control that an document is valid, i.e. that its period of validity
748      * defined
749      * by its dateValidityBegin and its dateValidityEnd is valid :
750      * an document is valid if the current date > = dateValidityBegin and if
751      * current date < = dateValidityEnd
752      * If the two dates are null, the test of validity will return true
753      * if one of the dates is null, the result of the test will be that carried
754      * out
755      * on the non null date
756      * @return true if the document is valid, false otherwise
757      */
758     public boolean isValid(  )
759     {
760         java.sql.Timestamp dateValidityBegin = getDateValidityBegin(  );
761         java.sql.Timestamp dateValidityEnd = getDateValidityEnd(  );
762 
763         GregorianCalendar gc = new GregorianCalendar(  );
764 
765         java.sql.Date dateToday = new java.sql.Date( gc.getTime(  ).getTime(  ) );
766 
767         if ( ( dateValidityBegin == null ) && ( dateValidityEnd == null ) )
768         {
769             return true;
770         }
771         else if ( dateValidityBegin == null )
772         {
773             // Return true if dateValidityEnd >= DateToday, false otherwise :
774             return ( dateValidityEnd.compareTo( dateToday ) >= 0 );
775         }
776         else if ( dateValidityEnd == null )
777         {
778             // Return true if dateValidityBegin <= DateToday, false otherwise :
779             return ( dateValidityBegin.compareTo( dateToday ) <= 0 );
780         }
781         else
782         {
783             // Return true if dateValidityBegin <= dateToday <= dateValidityEnd, false
784             // otherwise :
785             return ( ( dateValidityBegin.compareTo( dateToday ) <= 0 ) &&
786             ( dateValidityEnd.compareTo( dateToday ) >= 0 ) );
787         }
788     }
789 
790     /**
791      * Control that an document is out of date, i.e. dateValidityEnd has expired
792      * :
793      * @return true if the document is out of date, false otherwise
794      */
795     public boolean isOutOfDate(  )
796     {
797         if ( getDateValidityEnd(  ) == null )
798         {
799             return false;
800         }
801 
802         // Return false if dateValidityEnd >= DateToday, true otherwise :
803         return !( getDateValidityEnd(  ).compareTo( new Date(  ) ) >= 0 );
804     }
805 
806     /**
807      * {@inheritDoc}
808      */
809     @Override
810     public String getIdExtendableResource(  )
811     {
812         return Integer.toString( _nIdDocument );
813     }
814 
815     /**
816      * {@inheritDoc}
817      */
818     @Override
819     public String getExtendableResourceType(  )
820     {
821         return PROPERTY_RESOURCE_TYPE;
822     }
823 
824     /**
825      * {@inheritDoc}
826      */
827     @Override
828     public String getExtendableResourceName(  )
829     {
830         return _strTitle;
831     }
832 
833     /**
834      * {@inheritDoc}
835      */
836     @Override
837     public String getExtendableResourceDescription(  )
838     {
839         return _strSummary;
840     }
841 
842     /**
843      * {@inheritDoc}
844      */
845     @Override
846     public String getExtendableResourceImageUrl(  )
847     {
848         if ( StringUtils.equalsIgnoreCase( _strCodeDocumentType, CODE_DOCUMENT_TYPE_IMAGE ) )
849         {
850             UrlItem urlItem = new UrlItem( SERVLET_DOCUMENT_PATH );
851             urlItem.addParameter( MARK_ID, _nIdDocument );
852 
853             return urlItem.getUrl(  );
854         }
855 
856         if ( _listAttributes != null )
857         {
858             for ( DocumentAttribute attribute : _listAttributes )
859             {
860                 if ( StringUtils.equals( attribute.getCodeAttributeType(  ), "image" ) )
861                 {
862                     UrlItem urlItem = new UrlItem( SERVLET_DOCUMENT_PATH );
863                     urlItem.addParameter( MARK_ID, _nIdDocument );
864                     urlItem.addParameter( MARK_ID_ATTRIBUTE, attribute.getId(  ) );
865 
866                     return urlItem.getUrl(  );
867                 }
868             }
869         }
870 
871         return null;
872     }
873 }