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