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.service.autopublication;
35  
36  import fr.paris.lutece.plugins.document.business.Document;
37  import fr.paris.lutece.plugins.document.business.DocumentFilter;
38  import fr.paris.lutece.plugins.document.business.DocumentHome;
39  import fr.paris.lutece.plugins.document.business.autopublication.DocumentAutoPublication;
40  import fr.paris.lutece.plugins.document.business.autopublication.DocumentAutoPublicationHome;
41  import fr.paris.lutece.plugins.document.business.portlet.DocumentListPortlet;
42  import fr.paris.lutece.plugins.document.business.portlet.DocumentListPortletHome;
43  import fr.paris.lutece.plugins.document.business.portlet.DocumentPortlet;
44  import fr.paris.lutece.plugins.document.business.portlet.DocumentPortletHome;
45  import fr.paris.lutece.plugins.document.business.workflow.DocumentState;
46  import fr.paris.lutece.plugins.document.service.publishing.PublishingService;
47  import fr.paris.lutece.portal.business.portlet.Portlet;
48  import fr.paris.lutece.portal.business.portlet.PortletHome;
49  
50  import java.util.ArrayList;
51  import java.util.Collection;
52  
53  
54  /**
55   *
56   * AutoPublication Service
57   */
58  public class AutoPublicationService
59  {
60      private static AutoPublicationService _singleton;
61  
62      /**
63       * Get the auto publication service instance
64       * @return The instance of the auto publication service
65       */
66      public static AutoPublicationService getInstance(  )
67      {
68          if ( _singleton == null )
69          {
70              _singleton = new AutoPublicationService(  );
71          }
72  
73          return _singleton;
74      }
75  
76      /**
77       * Initialize the {@link DocumentAutoPublication} service
78       *
79       */
80      public void init(  )
81      {
82          DocumentAutoPublication.init(  );
83      }
84  
85      /**
86       * Process auto publication
87       * @return The log of the process
88       */
89      public String processAutoPublishing(  )
90      {
91          StringBuffer sbLogs = new StringBuffer(  );
92  
93          sbLogs.append( "\r\n[Start] Starting Auto publication daemon...\r\n" );
94  
95          long lDuration = System.currentTimeMillis(  );
96  
97          for ( DocumentAutoPublication documentAutoPublication : DocumentAutoPublicationHome.findAll(  ) )
98          {
99              for ( Document document : findPublishableDocumentsList( documentAutoPublication.getIdPortlet(  ),
100                     documentAutoPublication.getIdSpace(  ) ) )
101             {
102                 sbLogs.append( "\r\nPublishing Document " + document.getId(  ) + " : '" + document.getTitle(  ) +
103                     "'...\r\n" );
104                 PublishingService.getInstance(  ).assign( document.getId(  ), documentAutoPublication.getIdPortlet(  ) );
105                 PublishingService.getInstance(  ).publish( document.getId(  ), documentAutoPublication.getIdPortlet(  ) );
106                 PublishingService.getInstance(  )
107                                  .changeDocumentOrder( document.getId(  ), documentAutoPublication.getIdPortlet(  ), 1 ); //Set new published document at the first place 
108             }
109         }
110 
111         sbLogs.append( "\r\n[End] Duration : " + ( System.currentTimeMillis(  ) - lDuration ) + " milliseconds\r\n" );
112 
113         return sbLogs.toString(  );
114     }
115 
116     /**
117      * Find the list of documents publishable for the specified portlet
118      * The list is filtered :
119      * <ul>
120      * <li>by space</li>
121      * <li>by state</li>
122      * <li>by portlet code document type</li>
123      * <li>by portlet categories</li>
124      * <li>by validity period</li>
125      * </ul>
126      *
127      * @param nPortletId The portlet Id
128      * @param nSpaceId The space Id
129      * @return A Collection of documents
130      */
131     private Collection<Document> findPublishableDocumentsList( int nPortletId, int nSpaceId )
132     {
133         Collection<Document> listPublishableDocuments = new ArrayList<Document>(  );
134         Portlet portlet = PortletHome.findByPrimaryKey( nPortletId );
135         String documentTypeCode = null;
136         int[] arrayCategories = null;
137 
138         if ( DocumentListPortletHome.getInstance(  ).getPortletTypeId(  ).equals( portlet.getPortletTypeId(  ) ) ) //equivalent to : if(portlet instanceof DocumentPortlet)
139         {
140             DocumentListPortletris/lutece/plugins/document/business/portlet/DocumentListPortlet.html#DocumentListPortlet">DocumentListPortlet documentListPortlet = (DocumentListPortlet) portlet;
141             documentTypeCode = documentListPortlet.getDocumentTypeCode(  );
142             arrayCategories = documentListPortlet.getIdCategory(  );
143         }
144 
145         if ( DocumentPortletHome.getInstance(  ).getPortletTypeId(  ).equals( portlet.getPortletTypeId(  ) ) )
146         {
147             DocumentPortlet../fr/paris/lutece/plugins/document/business/portlet/DocumentPortlet.html#DocumentPortlet">DocumentPortlet documentPortlet = (DocumentPortlet) portlet;
148             documentTypeCode = documentPortlet.getDocumentTypeCode(  );
149             arrayCategories = documentPortlet.getIdCategory(  );
150         }
151 
152         if ( ( documentTypeCode == null ) && ( arrayCategories == null ) ) //Error : auto publication mapped with a non document portlet !
153         {
154             return listPublishableDocuments;
155         }
156 
157         DocumentFiltersiness/DocumentFilter.html#DocumentFilter">DocumentFilter documentFilter = new DocumentFilter(  );
158         documentFilter.setIdSpace( nSpaceId ); //Filter by Space
159         documentFilter.setIdState( DocumentState.STATE_VALIDATE ); // Filter by state
160         documentFilter.setCodeDocumentType( documentTypeCode ); //Filter by document type code
161         documentFilter.setCategoriesId( arrayCategories );
162 
163         for ( Document document : DocumentHome.findByFilter( documentFilter, null ) )
164         {
165             if ( document.isValid(  ) &&
166                     !PublishingService.getInstance(  ).isPublished( document.getId(  ), nPortletId ) ) //isValid = Check the publication period
167             {
168                 listPublishableDocuments.add( document );
169             }
170         }
171 
172         return listPublishableDocuments;
173     }
174 
175     /**
176      * Returns count of published documents of a portlet and space
177      *
178      * @param nPortletId the identifier of the portlet
179      * @param nSpaceId the identifier of the space
180      * @return number of documents
181      */
182     public static int findCountByPortletAndSpace( int nPortletId, int nSpaceId )
183     {
184         Collection<Document> listDocuments = PublishingService.getInstance(  )
185                                                               .getPublishedDocumentsByPortletId( nPortletId );
186         int nCount = 0;
187 
188         for ( Document document : listDocuments )
189         {
190             if ( document.getSpaceId(  ) == nSpaceId )
191             {
192                 nCount++;
193             }
194         }
195 
196         return nCount;
197     }
198 }