View Javadoc
1   /*
2    * Copyright (c) 2002-2024, 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.genericattributes.service.file;
35  
36  import java.util.HashMap;
37  import java.util.List;
38  import java.util.Map;
39  
40  import org.apache.commons.fileupload.FileItem;
41  import org.apache.commons.lang3.StringUtils;
42  
43  import fr.paris.lutece.portal.business.file.File;
44  import fr.paris.lutece.portal.service.file.FileService;
45  import fr.paris.lutece.portal.service.file.FileServiceException;
46  import fr.paris.lutece.portal.service.util.AppLogService;
47  import fr.paris.lutece.portal.service.util.AppPropertiesService;
48  
49  public class GenericAttributeFileService
50  {
51  	private static final String PROPERTY_FILESTORESERVICE_PREFIX = "genericattributes.filestoreservice";
52  	private static final String PROPERTY_FILESTORESERVICE_DEFAULT_SUFFIX = "default";
53  
54      private static final GenericAttributeFileServiceervice/file/GenericAttributeFileService.html#GenericAttributeFileService">GenericAttributeFileService _instance = new GenericAttributeFileService( );
55      private static Map<String,String> _entryTypeFileServices;
56  	
57      /**
58       * Constructor
59       */
60      private GenericAttributeFileService( )
61      {
62      	List<String> keyList = AppPropertiesService.getKeys( PROPERTY_FILESTORESERVICE_PREFIX);
63      	
64      	 _entryTypeFileServices = new HashMap<>();
65      	 
66      	// init specific entryType fileStoreService names if exists
67      	 if (keyList != null ) 
68      	 {
69  	    	keyList.stream().forEach( s -> { 
70  	    		if ( !StringUtils.isAllBlank(  AppPropertiesService.getProperty(s) ) )
71  	    		{
72  	    			_entryTypeFileServices.put(s, AppPropertiesService.getProperty(s)); 
73  	    		}
74  	    	} );
75      	 }
76      }
77      
78      /**
79       * get FileStoreServiceProvider name for entry type :
80       * - returns the entry type FileService (if set)
81       * - otherwise, returns the GenAttr default FileService (if set)
82       * - otherwise, returns the lutece default FileService 
83       * 
84       * @param strEntryType
85       * @return the name of the FileStoreServiceProvider
86       */
87      public String getFileStoreProviderName( String strEntryType )
88      {
89      	if (strEntryType != null ) 
90      	{
91      		if ( _entryTypeFileServices.containsKey( strEntryType ))
92      		{
93      			return _entryTypeFileServices.get( strEntryType );
94      		}
95      	}
96      	
97      	if ( _entryTypeFileServices.containsKey( PROPERTY_FILESTORESERVICE_DEFAULT_SUFFIX ))
98  		{
99  			return _entryTypeFileServices.get( PROPERTY_FILESTORESERVICE_DEFAULT_SUFFIX );
100 		}
101     	
102     	return FileService.getInstance( ).getFileStoreServiceProvider( ).getName( );
103     }
104     
105     /**
106      * get FileStoreServiceProvider name  :
107      * - returns the GenAttr default FileService (if set)
108      * - otherwise, returns the lutece default FileService 
109      * 
110      * Use getFileStoreProviderName( String strEntryType ) to get entryType specific File service
111      * 
112      * @return the name of the FileStoreServiceProvider
113      */
114     public String getFileStoreProviderName(  )
115     {
116     	return getFileStoreProviderName( null );
117     }
118 
119     /**
120      * get instance of service
121      * 
122      * @return the instance
123      */
124     public static GenericAttributeFileService getInstance( )
125     {
126         return _instance;
127     }
128 
129     /**
130      * Save a file
131      * 
132      * @param file The lutece file in default generic file Service
133      * @return The key of the file
134      */
135     public String save( File file)
136     {
137     	return save( file, null);
138     }
139     /**
140      * Save a file
141      * 
142      * @param file The lutece file
143      * @param strEntryType the entry type
144      * @return The key of the file
145      */
146     public String save( File file, String strEntryType)
147     {
148         try
149         {
150         	if ( file.getOrigin( ) == null )
151         	{
152         		file.setOrigin( getFileStoreProviderName( strEntryType ) );
153         	}
154         	
155         	return FileService.getInstance( ).getFileStoreServiceProvider( file.getOrigin( ) ).storeFile( file );
156         }
157         catch( FileServiceException e )
158         {
159             AppLogService.error( e );
160             return null;
161         }
162     }
163 
164     /**
165      * Save a file
166      * 
167      * @param file The fileItem
168      * @param strEntryType the entry type
169      * @return The key of the file
170      */
171     public String save( FileItem file, String strEntryType)
172     {
173         try
174         {        	
175         	return FileService.getInstance( ).getFileStoreServiceProvider( getFileStoreProviderName( strEntryType )  ).storeFileItem( file );
176         }
177         catch( FileServiceException e )
178         {
179             AppLogService.error( e );
180             return null;
181         }
182     }
183     
184     /**
185      * Load a file
186      * 
187      * @param strKey
188      *            The key of the file
189      * @return The file
190      */
191     public File load( String strKey, String strOrigin )
192     {
193         try
194         {
195         	if ( StringUtils.isEmpty( strOrigin ) )
196         	{
197         		strOrigin =  getFileStoreProviderName( ) ;
198         	}
199         	
200             return FileService.getInstance( ).getFileStoreServiceProvider( strOrigin ).getFile( strKey );
201         }
202         catch( FileServiceException e )
203         {
204             AppLogService.error( e );
205             return null;
206         }
207     }
208 
209     /**
210      * Delete a file
211      * 
212      * @param strKey
213      *            The key of the file
214      */
215     public void delete( String strKey, String strOrigin )
216     {
217         try
218         {
219         	if ( StringUtils.isEmpty( strOrigin ) )
220         	{
221         		strOrigin =  getFileStoreProviderName( );
222         	}
223         	
224             FileService.getInstance( ).getFileStoreServiceProvider( strOrigin ).delete( strKey );
225         }
226         catch( FileServiceException e )
227         {
228             AppLogService.error( e );
229         }
230     }
231 
232     /**
233      * Update a file
234      *
235      * @param file The lutece file
236      * @param strEntryType the entry type
237      * @return The key of the file
238      */
239     public String update( File file, String strEntryType )
240     {
241         try
242         {
243             if ( file.getOrigin( ) == null )
244             {
245                 file.setOrigin( getFileStoreProviderName( strEntryType ) );
246             }
247 
248             String strOldFileKey = file.getFileKey( );
249             String strNewFileKey = FileService.getInstance( ).getFileStoreServiceProvider( file.getOrigin( ) ).storeFile( file );
250 
251             FileService.getInstance( ).getFileStoreServiceProvider( file.getOrigin( ) ).delete( strOldFileKey );
252 
253             return strNewFileKey;
254         }
255         catch( FileServiceException e )
256         {
257             AppLogService.error( e );
258             return null;
259         }
260     }
261 
262 }