1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.plugins.folderlisting.service;
35
36 import fr.paris.lutece.portal.service.resource.Resource;
37 import fr.paris.lutece.portal.service.resource.ResourceLoader;
38 import fr.paris.lutece.portal.service.util.AppLogService;
39 import fr.paris.lutece.portal.service.util.AppPathService;
40 import fr.paris.lutece.portal.service.util.AppPropertiesService;
41 import fr.paris.lutece.util.filesystem.DirectoryNotFoundException;
42 import fr.paris.lutece.util.filesystem.FileSystemUtil;
43
44 import java.io.File;
45 import java.io.FileInputStream;
46 import java.io.IOException;
47
48 import java.util.ArrayList;
49 import java.util.Collection;
50 import java.util.Properties;
51
52
53 public class FolderLoaderProperties implements ResourceLoader
54 {
55
56
57 private static final String PROPERTY_FOLDER_FILES_PATH = "folderlisting.files.path";
58 private static final String PROPERTY_FOLDER_NAME = "folder.name";
59 private static final String PROPERTY_FOLDER_PATH = "folder.path";
60 private static final String PROPERTY_FOLDER_WORKGROUP = "folder.workgroup";
61 private static final String EXT_FOLDER_FILES = ".properties";
62 private String _strFolderFilesPath;
63
64
65
66
67 public FolderLoaderProperties( )
68 {
69 super( );
70 _strFolderFilesPath = AppPropertiesService.getProperty( PROPERTY_FOLDER_FILES_PATH );
71 }
72
73
74
75
76
77 public Collection getResources( )
78 {
79 ArrayList listFolders = new ArrayList( );
80
81 try
82 {
83 String strRootDirectory = AppPathService.getWebAppPath( );
84
85 for ( File file : FileSystemUtil.getFiles( strRootDirectory, _strFolderFilesPath ) )
86 {
87 String fileName = file.getName( );
88
89 if ( fileName.endsWith( EXT_FOLDER_FILES ) )
90 {
91 Folder folder = loadFolder( file );
92 listFolders.add( folder );
93 }
94 }
95 }
96 catch ( DirectoryNotFoundException e )
97 {
98 AppLogService.error( e.getMessage( ), e );
99 }
100
101 return listFolders;
102 }
103
104
105
106
107
108
109 public Resource getResource( String strId )
110 {
111 Resource resource = null;
112 String strFilePath = AppPathService.getPath( PROPERTY_FOLDER_FILES_PATH, strId + EXT_FOLDER_FILES );
113 File file = new File( strFilePath );
114
115 if ( file.exists( ) )
116 {
117 resource = loadFolder( file );
118 }
119
120 return resource;
121 }
122
123
124
125
126
127
128 private Folder loadFolder( File file )
129 {
130 Folder folder = new Folder( );
131 Properties properties = new Properties( );
132
133 try
134 {
135 FileInputStream is = new FileInputStream( file );
136 properties.load( is );
137 folder.setName( properties.getProperty( PROPERTY_FOLDER_NAME ) );
138 folder.setPath( properties.getProperty( PROPERTY_FOLDER_PATH ) );
139 folder.setWorkgroup( properties.getProperty( PROPERTY_FOLDER_WORKGROUP ) );
140
141 String strFolderId = file.getName( ).substring( 0, file.getName( ).lastIndexOf( "." ) );
142 folder.setId( strFolderId );
143 }
144 catch ( IOException e )
145 {
146 AppLogService.error( e.getMessage( ), e );
147 }
148
149 return folder;
150 }
151 }