View Javadoc
1   /*
2    * Copyright (c) 2002-2023, 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.service.docsearch.DocSearchService;
37  import fr.paris.lutece.portal.service.i18n.I18nService;
38  import fr.paris.lutece.portal.service.resource.ExtendableResourceRemovalListenerService;
39  import fr.paris.lutece.portal.service.spring.SpringContextService;
40  
41  import java.util.Collection;
42  import java.util.List;
43  import java.util.Locale;
44  
45  /**
46   * This class provides instances management methods (create, find, ...) for Document objects
47   */
48  public final class DocumentHome
49  {
50      // Static variable pointed at the DAO instance
51      private static IDocumentDAO _dao = SpringContextService.getBean( "document.documentDAO" );
52  
53      /**
54       * Private constructor - this class need not be instantiated
55       */
56      private DocumentHome( )
57      {
58      }
59  
60      /**
61       * Creation of an instance of document
62       *
63       * @param document
64       *            The instance of the document which contains the informations to store
65       * @return The instance of document which has been created with its primary key.
66       */
67      public static Document../../../../../../fr/paris/lutece/plugins/document/business/Document.html#Document">Document create( Document document )
68      {
69          _dao.insert( document );
70          DocSearchService.getInstance( ).addIndexerAction( document.getId( ), IndexerAction.TASK_CREATE );
71  
72          /*
73           * IndexationService.addIndexerAction( document.getId(), DocumentIndexer.INDEXER_NAME, IndexerAction.TASK_CREATE );
74           */
75          return document;
76      }
77  
78      /**
79       * Update of the document which is specified in parameter
80       *
81       * @return The instance of the document which has been updated
82       * @param bUpdateContent
83       *            True to update content, false otherwise
84       * @param document
85       *            The instance of the document which contains the data to store
86       */
87      public static Document../../../../../../fr/paris/lutece/plugins/document/business/Document.html#Document">Document update( Document document, boolean bUpdateContent )
88      {
89          _dao.store( document, bUpdateContent );
90          DocSearchService.getInstance( ).addIndexerAction( document.getId( ), IndexerAction.TASK_MODIFY );
91  
92          /*
93           * if(PublishingService.getInstance().isPublished(document.getId())) { IndexationService.getInstance().addIndexerAction( document.getId() ,
94           * DocumentIndexer.INDEXER_NAME , IndexerAction.TASK_MODIFY , IndexationService.ALL_DOCUMENT ); }
95           */
96          return document;
97      }
98  
99      /**
100      * Validate of the document attributes
101      * 
102      * @param nIdDocument
103      *            The id of the document
104      */
105     public static void validateAttributes( int nIdDocument )
106     {
107         _dao.validateAttributes( nIdDocument );
108     }
109 
110     /**
111      * Remove the Document whose identifier is specified in parameter
112      *
113      * @param nDocumentId
114      *            The id of the document to remove
115      */
116     public static void remove( int nDocumentId )
117     {
118         _dao.delete( nDocumentId );
119         DocSearchService.getInstance( ).addIndexerAction( nDocumentId, IndexerAction.TASK_DELETE );
120         // We remove extensions of the removed document if any
121         ExtendableResourceRemovalListenerService.doRemoveResourceExtentions( Document.PROPERTY_RESOURCE_TYPE, Integer.toString( nDocumentId ) );
122     }
123 
124     ///////////////////////////////////////////////////////////////////////////
125     // Finders
126 
127     /**
128      * Returns an instance of a document whose identifier is specified in parameter
129      *
130      * @param nKey
131      *            The Primary key of the document
132      * @return An instance of document
133      */
134     public static Document findByPrimaryKey( int nKey )
135     {
136         return _dao.load( nKey );
137     }
138 
139     /**
140      * Returns an instance of a document whose identifier is specified in parameter
141      *
142      * @param nKey
143      *            The Primary key of the document
144      * @return An instance of document
145      */
146     public static Document findByPrimaryKeyWithoutBinaries( int nKey )
147     {
148         return _dao.loadWithoutBinaries( nKey );
149     }
150 
151     /**
152      * Returns documents by space id
153      * 
154      * @param nSpaceId
155      *            The space Id
156      * @return A list of documents
157      */
158     public static List<Document> findBySpaceKey( int nSpaceId )
159     {
160         return _dao.loadFromSpaceId( nSpaceId );
161     }
162 
163     /**
164      * Returns a collection of documents ids
165      * 
166      * @return A collection of documents ids
167      * @param filter
168      *            The filter
169      * @param locale
170      *            The locale
171      */
172     public static Collection<Integer> findPrimaryKeysByFilter( DocumentFilter filter, Locale locale )
173     {
174         return _dao.selectPrimaryKeysByFilter( filter );
175     }
176 
177     /**
178      * Returns a collection of documents objects
179      * 
180      * @return A collection of documents
181      * @param filter
182      *            The filter
183      * @param locale
184      *            The locale
185      */
186     public static List<Document> findByFilter( DocumentFilter filter, Locale locale )
187     {
188         List<Document> listDocuments = _dao.selectByFilter( filter );
189 
190         return (List) I18nService.localizeCollection( listDocuments, locale );
191     }
192 
193     /**
194      * Returns a collection of documents objects If more than one category is specified on filter, the result will corresponding to the document wich matched
195      * with one category at least.
196      * 
197      * @param document
198      *            The {@link Document}
199      * @param locale
200      *            The {@link Locale}
201      * @return A collection of documents
202      */
203     public static List<Document> findByRelatedCategories( Document document, Locale locale )
204     {
205         List<Document> listDocuments = _dao.selectByRelatedCategories( document );
206 
207         return (List) I18nService.localizeCollection( listDocuments, locale );
208     }
209 
210     /**
211      * Get the validated resource of an attribute of a document
212      * 
213      * @param nDocumentId
214      *            The id of the document
215      * @param nAttributeId
216      *            The id of the attribute to get the resource of
217      * @return the document resource, of null if none was found
218      */
219     public static DocumentResource getValidatedResource( int nDocumentId, int nAttributeId )
220     {
221         return _dao.loadSpecificResource( nDocumentId, nAttributeId, true );
222     }
223 
224     /**
225      * Get the working resource of an attribute of a document
226      * 
227      * @param nDocumentId
228      *            The id of the document
229      * @param nAttributeId
230      *            the id of the attribute
231      * @return the document resource, of null if none was found
232      */
233     public static DocumentResource getWorkingResource( int nDocumentId, int nAttributeId )
234     {
235         return _dao.loadSpecificResource( nDocumentId, nAttributeId, false );
236     }
237 
238     /**
239      * Get a document resource
240      * 
241      * @param nDocumentId
242      *            The id of the document
243      * @return The document resource
244      */
245     public static DocumentResource getResource( int nDocumentId )
246     {
247         return _dao.loadResource( nDocumentId );
248     }
249 
250     /**
251      * Get a new primary key
252      * 
253      * @return The new primary key
254      */
255     public static int newPrimaryKey( )
256     {
257         return _dao.newPrimaryKey( );
258     }
259 
260     /**
261      * Gets all documents id
262      * 
263      * @return A collection of Integer
264      */
265     public static Collection<Integer> findAllPrimaryKeys( )
266     {
267         return _dao.selectAllPrimaryKeys( );
268     }
269 
270     /**
271      * Get the list of every documents
272      * 
273      * @return The list of every documents
274      */
275     public static List<Document> findAll( )
276     {
277         return _dao.selectAll( );
278     }
279 
280     /**
281      * Load document attributes
282      * 
283      * @param document
284      *            the document reference
285      */
286     public static void loadAttributes( Document document )
287     {
288         _dao.loadAttributes( document );
289     }
290 
291     /**
292      * Load document attributes
293      * 
294      * @param document
295      *            the document reference
296      * @param bValidated
297      *            true if the content of the document must be validated, false otherwise
298      */
299     public static void loadAttributesWithoutBinaries( Document document, boolean bValidated )
300     {
301         _dao.loadAttributesWithoutBinaries( document, bValidated );
302     }
303 
304     /**
305      * Load document pageTemplatePath
306      * 
307      * @param idPageTemplateDocument
308      *            the Id page template identifier
309      * @return the page template document path
310      */
311     public static String getPageTemplateDocumentPath( int idPageTemplateDocument )
312     {
313         return _dao.getPageTemplateDocumentPath( idPageTemplateDocument );
314     }
315 
316     /**
317      * Load document type and date last modification for HTTP GET conditional request ("If-Modified-Since")
318      * 
319      * @param nDocumentId
320      *            The document id
321      * @return the document
322      */
323     public static Document loadLastModifiedAttributes( int nDocumentId )
324     {
325         return _dao.loadLastModifiedAttributes( nDocumentId );
326     }
327 
328     /**
329      * Load the data of last Document the user worked in from the table
330      *
331      * @param strUserName
332      *            the user name
333      * @return the instance of the Document
334      */
335     public static Document loadLastModifiedDocumentFromUser( String strUserName )
336     {
337         return _dao.loadLastModifiedDocumentFromUser( strUserName );
338     }
339 
340     /**
341      * Load the data of last Document the user worked in from the table
342      *
343      * @return the instance of the Document
344      */
345     public static Document loadLastPublishedDocument( )
346     {
347         return _dao.loadLastPublishedDocument( );
348     }
349 }