Skip to content

Commit c56ba43

Browse files
committed
LUT-28558 : manage FileStoreServices by EntryType (if set in properties)
1 parent cb82ee3 commit c56ba43

File tree

7 files changed

+141
-41
lines changed

7 files changed

+141
-41
lines changed

src/java/fr/paris/lutece/plugins/genericattributes/service/ResponseImageResourceProvider.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public ImageResource getImageResource( int nIdResource )
6666

6767
if ( response.getFile( ) != null )
6868
{
69-
File file = GenericAttributeFileService.getInstance().load( response.getFile( ).getFileKey( ) );
69+
File file = GenericAttributeFileService.getInstance().load( response.getFile( ).getFileKey( ), response.getFile( ).getOrigin( ) );
7070

7171
if ( ( file.getPhysicalFile( ) != null ) && FileUtil.hasImageExtension( file.getTitle( ) ) )
7272
{

src/java/fr/paris/lutece/plugins/genericattributes/service/entrytype/AbstractEntryTypeComment.java

+8-20
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import fr.paris.lutece.plugins.genericattributes.business.Entry;
4444
import fr.paris.lutece.plugins.genericattributes.business.Field;
4545
import fr.paris.lutece.plugins.genericattributes.business.FieldHome;
46+
import fr.paris.lutece.plugins.genericattributes.service.file.GenericAttributeFileService;
4647
import fr.paris.lutece.plugins.genericattributes.util.GenericAttributesUtils;
4748
import fr.paris.lutece.portal.business.file.File;
4849
import fr.paris.lutece.portal.business.physicalfile.PhysicalFile;
@@ -60,6 +61,8 @@
6061
*/
6162
public abstract class AbstractEntryTypeComment extends EntryTypeService
6263
{
64+
public static final String ENTRY_TYPE_KEYNAME = "entryTypeComment";
65+
6366
/**
6467
* {@inheritDoc}
6568
*/
@@ -114,14 +117,9 @@ public String getRequestData( Entry entry, HttpServletRequest request, Locale lo
114117
file.setPhysicalFile( physicalFile );
115118

116119
String idFile;
117-
try {
118-
idFile = getFileStoreServiceProvider( ).storeFile( file );
119-
GenericAttributesUtils.createOrUpdateField( entry, FIELD_DOWNLOADABLE_FILE, file.getTitle( ), idFile );
120-
}
121-
catch ( FileServiceException e )
122-
{
123-
AppLogService.error(e);
124-
}
120+
idFile = GenericAttributeFileService.getInstance( ).save( fileItem, ENTRY_TYPE_KEYNAME );
121+
GenericAttributesUtils.createOrUpdateField( entry, FIELD_DOWNLOADABLE_FILE, file.getTitle( ), idFile );
122+
125123

126124
}
127125
}
@@ -133,18 +131,8 @@ private void removeOldFiles( Entry entry )
133131
Field oldFile = entry.getFieldByCode( FIELD_DOWNLOADABLE_FILE );
134132
if ( oldFile != null )
135133
{
136-
try {
137-
getFileStoreServiceProvider( ).delete( oldFile.getValue( ) );
138-
FieldHome.remove( oldFile.getIdField( ) );
139-
} catch (FileServiceException e) {
140-
AppLogService.error(e);
141-
}
142-
134+
GenericAttributeFileService.getInstance( ).delete( oldFile.getValue( ), null);
135+
FieldHome.remove( oldFile.getIdField( ) );
143136
}
144137
}
145-
146-
protected IFileStoreServiceProvider getFileStoreServiceProvider( )
147-
{
148-
return FileService.getInstance( ).getFileStoreServiceProvider( "defaultDatabaseFileStoreProvider" );
149-
}
150138
}

src/java/fr/paris/lutece/plugins/genericattributes/service/entrytype/AbstractEntryTypeFile.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@
6060
*/
6161
public abstract class AbstractEntryTypeFile extends AbstractEntryTypeUpload
6262
{
63-
/**
63+
public static final String ENTRY_TYPE_KEYNAME = "entryTypeFile";
64+
65+
/**
6466
* {@inheritDoc}
6567
*/
6668
@Override
@@ -217,7 +219,6 @@ private Response getResponseFromFile( FileItem fileItem, Entry entry, boolean bC
217219

218220
File file = new File( );
219221
file.setTitle( fileItem.getName( ) );
220-
file.setOrigin( GenericAttributeFileService.getInstance( ).getName( ) );
221222
file.setSize( ( fileItem.getSize( ) < Integer.MAX_VALUE ) ? (int) fileItem.getSize( ) : Integer.MAX_VALUE );
222223

223224
if ( bCreatePhysicalFile )

src/java/fr/paris/lutece/plugins/genericattributes/service/entrytype/AbstractEntryTypeGalleryImage.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@
6262
*/
6363
public abstract class AbstractEntryTypeGalleryImage extends EntryTypeService
6464
{
65+
public static final String ENTRY_TYPE_KEYNAME = "entryTypeGalleryImage";
66+
6567
// PARAMETERS
6668
protected static final String PARAMETER_ID_RESPONSE = "id_response";
6769
protected static final String PARAMETER_CODE_GALLERY = "code_gallery";
@@ -113,7 +115,7 @@ public GenericAttributeError getResponseData( Entry entry, HttpServletRequest re
113115
{
114116
FileImagePublicService.init( );
115117

116-
File file = GenericAttributeFileService.getInstance( ).load( strFileGallery, GenericAttributeFileService.getInstance( ).getName( ) );
118+
File file = GenericAttributeFileService.getInstance( ).load( strFileGallery, ENTRY_TYPE_KEYNAME);
117119

118120
Response response = new Response( );
119121
response.setEntry( entry );

src/java/fr/paris/lutece/plugins/genericattributes/service/entrytype/IEntryTypeService.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public interface IEntryTypeService
139139
String FIELD_ILLUSTRATION_IMAGE = "illustration_image";
140140
String FIELD_GEOJSON = "coordinates_geojson";
141141
String FIELD_ID_LAYER = "DataLayer";
142-
String FIELD_DISABLED = "disabled";
142+
String FIELD_DISABLED = "disabled";
143143
String FIELD_IS_UPDATABLE = "is_updatable";
144144

145145
// attribute

src/java/fr/paris/lutece/plugins/genericattributes/service/file/GenericAttributeFileService.java

+123-16
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,91 @@
3333
*/
3434
package fr.paris.lutece.plugins.genericattributes.service.file;
3535

36+
import java.util.HashMap;
37+
import java.util.List;
38+
import java.util.Map;
39+
import java.util.function.Function;
40+
41+
import org.apache.commons.fileupload.FileItem;
42+
import org.apache.commons.lang3.StringUtils;
43+
3644
import fr.paris.lutece.portal.business.file.File;
3745
import fr.paris.lutece.portal.service.file.FileService;
3846
import fr.paris.lutece.portal.service.file.FileServiceException;
3947
import fr.paris.lutece.portal.service.file.IFileStoreServiceProvider;
4048
import fr.paris.lutece.portal.service.util.AppLogService;
49+
import fr.paris.lutece.portal.service.util.AppPropertiesService;
4150

4251
public class GenericAttributeFileService
4352
{
44-
private static final GenericAttributeFileService _instance = new GenericAttributeFileService( );
45-
private IFileStoreServiceProvider _fileStoreServiceProvider;
53+
private static final String PROPERTY_FILESTORESERVICE_PREFIX = "genericattributes.filestoreservice";
54+
private static final String PROPERTY_FILESTORESERVICE_DEFAULT_SUFFIX = "default";
4655

56+
private static final GenericAttributeFileService _instance = new GenericAttributeFileService( );
57+
private static final Map<String,String> _entryTypeFileServices = new HashMap<>();
58+
59+
/**
60+
* Constructor
61+
*/
4762
private GenericAttributeFileService( )
4863
{
64+
List<String> keyList = AppPropertiesService.getKeys( PROPERTY_FILESTORESERVICE_PREFIX);
65+
66+
// init specific entryType fileStoreService names if exists
67+
keyList.stream().forEach( s -> {
68+
if ( !StringUtils.isAllBlank( AppPropertiesService.getProperty(s) ) )
69+
{
70+
_entryTypeFileServices.put(s, AppPropertiesService.getProperty(s));
71+
}
72+
} );
73+
}
74+
75+
/**
76+
* get FileStoreServiceProvider name for entry type :
77+
* - returns the entry type FileService (if set)
78+
* - otherwise, returns the GenAttr default FileService (if set)
79+
* - otherwise, returns the lutece default FileService
80+
*
81+
* @param strEntryType
82+
* @return the name of the FileStoreServiceProvider
83+
*/
84+
public String getFileStoreProviderName( String strEntryType )
85+
{
86+
if (strEntryType != null )
87+
{
88+
if ( _entryTypeFileServices.containsKey( strEntryType ))
89+
{
90+
return _entryTypeFileServices.get( strEntryType );
91+
}
92+
}
93+
94+
if ( _entryTypeFileServices.containsKey( PROPERTY_FILESTORESERVICE_DEFAULT_SUFFIX ))
95+
{
96+
return _entryTypeFileServices.get( PROPERTY_FILESTORESERVICE_DEFAULT_SUFFIX );
97+
}
98+
99+
return FileService.getInstance( ).getFileStoreServiceProvider( ).getName( );
100+
}
101+
102+
/**
103+
* get FileStoreServiceProvider name :
104+
* - returns the GenAttr default FileService (if set)
105+
* - otherwise, returns the lutece default FileService
106+
*
107+
* Use getFileStoreProviderName( String strEntryType ) to get entryType specific File service
108+
*
109+
* @return the name of the FileStoreServiceProvider
110+
*/
111+
public String getFileStoreProviderName( )
112+
{
113+
return getFileStoreProviderName( null );
49114
}
50115

116+
/**
117+
* get instance of service
118+
*
119+
* @return the instance
120+
*/
51121
public static GenericAttributeFileService getInstance( )
52122
{
53123
return _instance;
@@ -56,16 +126,30 @@ public static GenericAttributeFileService getInstance( )
56126
/**
57127
* Save a file
58128
*
59-
* @param file
60-
* The file
129+
* @param file The lutece file in default generic file Service
61130
* @return The key of the file
62131
*/
63-
public String save( File file )
132+
public String save( File file)
133+
{
134+
return save( file, null);
135+
}
136+
/**
137+
* Save a file
138+
*
139+
* @param file The lutece file
140+
* @param strEntryType the entry type
141+
* @return The key of the file
142+
*/
143+
public String save( File file, String strEntryType)
64144
{
65145
try
66146
{
67-
_fileStoreServiceProvider = FileService.getInstance( ).getFileStoreServiceProvider( file.getOrigin( ) );
68-
return _fileStoreServiceProvider.storeFile( file );
147+
if ( file.getOrigin( ) == null )
148+
{
149+
file.setOrigin( getFileStoreProviderName( strEntryType ) );
150+
}
151+
152+
return FileService.getInstance( ).getFileStoreServiceProvider( file.getOrigin( ) ).storeFile( file );
69153
}
70154
catch( FileServiceException e )
71155
{
@@ -74,6 +158,26 @@ public String save( File file )
74158
}
75159
}
76160

161+
/**
162+
* Save a file
163+
*
164+
* @param file The fileItem
165+
* @param strEntryType the entry type
166+
* @return The key of the file
167+
*/
168+
public String save( FileItem file, String strEntryType)
169+
{
170+
try
171+
{
172+
return FileService.getInstance( ).getFileStoreServiceProvider( getFileStoreProviderName( strEntryType ) ).storeFileItem( file );
173+
}
174+
catch( FileServiceException e )
175+
{
176+
AppLogService.error( e );
177+
return null;
178+
}
179+
}
180+
77181
/**
78182
* Load a file
79183
*
@@ -85,8 +189,12 @@ public File load( String strKey, String strOrigin )
85189
{
86190
try
87191
{
88-
_fileStoreServiceProvider = FileService.getInstance( ).getFileStoreServiceProvider( strOrigin );
89-
return _fileStoreServiceProvider.getFile( strKey );
192+
if ( StringUtils.isEmpty( strOrigin ) )
193+
{
194+
strOrigin = getFileStoreProviderName( ) ;
195+
}
196+
197+
return FileService.getInstance( ).getFileStoreServiceProvider( strOrigin ).getFile( strKey );
90198
}
91199
catch( FileServiceException e )
92200
{
@@ -105,18 +213,17 @@ public void delete( String strKey, String strOrigin )
105213
{
106214
try
107215
{
108-
_fileStoreServiceProvider = FileService.getInstance( ).getFileStoreServiceProvider( strOrigin );
109-
_fileStoreServiceProvider.delete( strKey );
216+
if ( StringUtils.isEmpty( strOrigin ) )
217+
{
218+
strOrigin = getFileStoreProviderName( );
219+
}
220+
221+
FileService.getInstance( ).getFileStoreServiceProvider( strOrigin ).delete( strKey );
110222
}
111223
catch( FileServiceException e )
112224
{
113225
AppLogService.error( e );
114226
}
115227
}
116228

117-
public String getName( )
118-
{
119-
// return default genatt file store provider name
120-
return FileService.getInstance( ).getFileStoreServiceProvider( ).getName( );
121-
}
122229
}

webapp/WEB-INF/conf/plugins/genericattributes.properties

+2
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ genericattributes.telephoneNumber.default.default.region=FR
1212
# export field name
1313
genericattributes.entrytype.geolocalisation.export.field.name=false
1414
genericattributes.entrytype.cartography.export.field.name=false
15+
16+
genericattributes.filestoreservice.default=

0 commit comments

Comments
 (0)