Skip to content

Commit ebbf961

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

File tree

8 files changed

+146
-42
lines changed

8 files changed

+146
-42
lines changed

src/java/fr/paris/lutece/plugins/genericattributes/business/ResponseDAO.java

+1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ public synchronized void insert( Response response, Plugin plugin )
111111
else
112112
{
113113
daoUtil.setIntNull( nIndex++ );
114+
daoUtil.setString( nIndex++, null );
114115
}
115116

116117
daoUtil.setInt( nIndex++, Response.CONSTANT_STATUS_ACTIVE );

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

+127-17
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,94 @@
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+
40+
import org.apache.commons.fileupload.FileItem;
41+
import org.apache.commons.lang3.StringUtils;
42+
3643
import fr.paris.lutece.portal.business.file.File;
3744
import fr.paris.lutece.portal.service.file.FileService;
3845
import fr.paris.lutece.portal.service.file.FileServiceException;
39-
import fr.paris.lutece.portal.service.file.IFileStoreServiceProvider;
4046
import fr.paris.lutece.portal.service.util.AppLogService;
47+
import fr.paris.lutece.portal.service.util.AppPropertiesService;
4148

4249
public class GenericAttributeFileService
4350
{
44-
private static final GenericAttributeFileService _instance = new GenericAttributeFileService( );
45-
private IFileStoreServiceProvider _fileStoreServiceProvider;
51+
private static final String PROPERTY_FILESTORESERVICE_PREFIX = "genericattributes.filestoreservice";
52+
private static final String PROPERTY_FILESTORESERVICE_DEFAULT_SUFFIX = "default";
4653

54+
private static final GenericAttributeFileService _instance = new GenericAttributeFileService( );
55+
private static Map<String,String> _entryTypeFileServices;
56+
57+
/**
58+
* Constructor
59+
*/
4760
private GenericAttributeFileService( )
4861
{
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 );
49117
}
50118

119+
/**
120+
* get instance of service
121+
*
122+
* @return the instance
123+
*/
51124
public static GenericAttributeFileService getInstance( )
52125
{
53126
return _instance;
@@ -56,16 +129,30 @@ public static GenericAttributeFileService getInstance( )
56129
/**
57130
* Save a file
58131
*
59-
* @param file
60-
* The file
132+
* @param file The lutece file in default generic file Service
61133
* @return The key of the file
62134
*/
63-
public String save( File file )
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)
64147
{
65148
try
66149
{
67-
_fileStoreServiceProvider = FileService.getInstance( ).getFileStoreServiceProvider( file.getOrigin( ) );
68-
return _fileStoreServiceProvider.storeFile( file );
150+
if ( file.getOrigin( ) == null )
151+
{
152+
file.setOrigin( getFileStoreProviderName( strEntryType ) );
153+
}
154+
155+
return FileService.getInstance( ).getFileStoreServiceProvider( file.getOrigin( ) ).storeFile( file );
69156
}
70157
catch( FileServiceException e )
71158
{
@@ -74,6 +161,26 @@ public String save( File file )
74161
}
75162
}
76163

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+
77184
/**
78185
* Load a file
79186
*
@@ -85,8 +192,12 @@ public File load( String strKey, String strOrigin )
85192
{
86193
try
87194
{
88-
_fileStoreServiceProvider = FileService.getInstance( ).getFileStoreServiceProvider( strOrigin );
89-
return _fileStoreServiceProvider.getFile( strKey );
195+
if ( StringUtils.isEmpty( strOrigin ) )
196+
{
197+
strOrigin = getFileStoreProviderName( ) ;
198+
}
199+
200+
return FileService.getInstance( ).getFileStoreServiceProvider( strOrigin ).getFile( strKey );
90201
}
91202
catch( FileServiceException e )
92203
{
@@ -105,18 +216,17 @@ public void delete( String strKey, String strOrigin )
105216
{
106217
try
107218
{
108-
_fileStoreServiceProvider = FileService.getInstance( ).getFileStoreServiceProvider( strOrigin );
109-
_fileStoreServiceProvider.delete( strKey );
219+
if ( StringUtils.isEmpty( strOrigin ) )
220+
{
221+
strOrigin = getFileStoreProviderName( );
222+
}
223+
224+
FileService.getInstance( ).getFileStoreServiceProvider( strOrigin ).delete( strKey );
110225
}
111226
catch( FileServiceException e )
112227
{
113228
AppLogService.error( e );
114229
}
115230
}
116231

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

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)