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.dbpage.business;
35
36 import fr.paris.lutece.plugins.dbpage.business.section.DbPageSection;
37 import fr.paris.lutece.portal.service.resource.Resource;
38 import fr.paris.lutece.portal.service.resource.ResourceLoader;
39 import fr.paris.lutece.portal.service.util.AppException;
40 import fr.paris.lutece.portal.service.util.AppLogService;
41 import fr.paris.lutece.portal.service.util.AppPathService;
42 import fr.paris.lutece.portal.service.util.AppPropertiesService;
43 import fr.paris.lutece.util.filesystem.DirectoryNotFoundException;
44 import fr.paris.lutece.util.filesystem.FileSystemUtil;
45
46 import java.io.File;
47 import java.io.FileInputStream;
48 import java.io.IOException;
49
50 import java.util.ArrayList;
51 import java.util.Collection;
52 import java.util.List;
53 import java.util.Properties;
54
55
56
57
58
59 public class DbPageLoaderProperties implements ResourceLoader
60 {
61 private static final String PROPERTY_PAGE_TITLE = "page.title";
62 private static final String PROPERTY_PAGE_NAME = "page.paramname";
63 private static final String PROPERTY_PAGE_WORKGROUP = "page.workgroup";
64 private static final String PROPERTY_PAGE_NUMBER_SECTIONS = "page.nbsections";
65 private static final String PROPERTY_SECTION_CLASS = "dbpage.section.type.class.";
66 private static final String PROPERTY_SECTION = "section";
67 private static final String PROPERTY_SECTION_TYPE = ".type";
68 private static final String PROPERTY_SECTION_TEMPLATE = ".template";
69 private static final String PROPERTY_SECTION_COLUMN = ".column";
70 private static final String PROPERTY_SECTION_SQL = ".sql";
71 private static final String PROPERTY_SECTION_TITLE = ".title";
72 private static final String PROPERTY_SECTION_POOL = ".pool";
73 private static final String PROPERTY_SECTION_ROLE = ".role";
74 private static final String PROPERTY_DBPAGE_FILES_PATH = "dbpage.files.path";
75 private static final String EXT_DBPAGE_FILES = ".properties";
76 private String _strDbPageFilesPath;
77
78
79
80
81 public DbPageLoaderProperties( )
82 {
83 super( );
84 _strDbPageFilesPath = AppPropertiesService.getProperty( PROPERTY_DBPAGE_FILES_PATH );
85 }
86
87
88
89
90
91 public Collection<DbPage> getResources( )
92 {
93 List<DbPage> listDbPage = new ArrayList<DbPage>( );
94 String strRootDirectory = AppPathService.getWebAppPath( );
95 List<File> listPages = null;
96
97 try
98 {
99 listPages = FileSystemUtil.getFiles( strRootDirectory, _strDbPageFilesPath );
100
101 for ( File file : listPages )
102 {
103 if ( file.getName( ).endsWith( EXT_DBPAGE_FILES ) )
104 {
105 DbPage dbPage = loadPage( file );
106 listDbPage.add( dbPage );
107 }
108 }
109 }
110 catch ( DirectoryNotFoundException e )
111 {
112 throw new AppException( e.getMessage( ), e );
113 }
114
115 return listDbPage;
116 }
117
118
119
120
121
122
123 public Resource getResource( String strId )
124 {
125 Resource resource = null;
126 String strFilePath = AppPathService.getPath( PROPERTY_DBPAGE_FILES_PATH, strId + EXT_DBPAGE_FILES );
127 File file = new File( strFilePath );
128
129 if ( file.exists( ) )
130 {
131 resource = loadPage( file );
132 }
133
134 return resource;
135 }
136
137
138
139
140
141
142 private DbPage loadPage( File file )
143 {
144 DbPage dbPage = new DbPage( );
145 Properties properties = new Properties( );
146
147 try
148 {
149 FileInputStream is = new FileInputStream( file );
150 properties.load( is );
151
152
153 dbPage.setName( properties.getProperty( PROPERTY_PAGE_NAME ) );
154 dbPage.setTitle( properties.getProperty( PROPERTY_PAGE_TITLE ) );
155 dbPage.setWorkgroup( properties.getProperty( PROPERTY_PAGE_WORKGROUP ) );
156
157 String strNbSection = properties.getProperty( PROPERTY_PAGE_NUMBER_SECTIONS );
158 int nSections = 0;
159 nSections = Integer.parseInt( strNbSection );
160 dbPage.setNbSection( nSections );
161
162 List<DbPageSection> myCollection = loadListSection( properties, nSections );
163 dbPage.setListSection( myCollection );
164 }
165 catch ( IOException e )
166 {
167 AppLogService.error( e.getMessage( ), e );
168 }
169
170 return dbPage;
171 }
172
173
174
175
176
177
178
179 public List<DbPageSection> loadListSection( Properties properties, int nSections )
180 {
181 List<DbPageSection> listSections = new ArrayList<DbPageSection>( );
182
183 for ( int i = 1; i <= nSections; i++ )
184 {
185 String strPrefixSection = PROPERTY_SECTION + i;
186 String strType = properties.getProperty( strPrefixSection + PROPERTY_SECTION_TYPE );
187 String strSectionClass = AppPropertiesService.getProperty( PROPERTY_SECTION_CLASS + strType );
188
189 DbPageSection dbPageSection = null;
190
191 try
192 {
193 dbPageSection = (DbPageSection) Class.forName( strSectionClass ).newInstance( );
194 }
195 catch ( ClassNotFoundException e )
196 {
197 AppLogService.error( e.getMessage( ), e );
198
199 return listSections;
200 }
201 catch ( IllegalAccessException e )
202 {
203 AppLogService.error( e.getMessage( ), e );
204
205 return listSections;
206 }
207 catch ( InstantiationException e )
208 {
209 AppLogService.error( e.getMessage( ), e );
210
211 return listSections;
212 }
213
214 dbPageSection.setType( properties.getProperty( strPrefixSection + PROPERTY_SECTION_TYPE ) );
215 dbPageSection.setTitle( properties.getProperty( strPrefixSection + PROPERTY_SECTION_TITLE ) );
216 dbPageSection.setSql( properties.getProperty( strPrefixSection + PROPERTY_SECTION_SQL ) );
217 dbPageSection.setDbPool( properties.getProperty( strPrefixSection + PROPERTY_SECTION_POOL ) );
218 dbPageSection.setRole( properties.getProperty( strPrefixSection + PROPERTY_SECTION_ROLE ) );
219 dbPageSection.setTemplatePath( properties.getProperty( strPrefixSection + PROPERTY_SECTION_TEMPLATE ) );
220
221 String strTemplate = properties.getProperty( strPrefixSection + PROPERTY_SECTION_TEMPLATE );
222 dbPageSection.setTemplatePath( strTemplate );
223
224
225 String strColumnsName = properties.getProperty( strPrefixSection + PROPERTY_SECTION_COLUMN );
226
227 if ( strColumnsName != null )
228 {
229 List<String> aCollumnsName = new ArrayList<String>( );
230
231 while ( !strColumnsName.equals( "" ) )
232 {
233 int nPos = strColumnsName.indexOf( "," );
234
235 if ( nPos != -1 )
236 {
237 String strValue = strColumnsName.substring( 0, nPos );
238 strValue = strValue.trim( );
239 strColumnsName = strColumnsName.substring( nPos + 1 );
240 aCollumnsName.add( strValue );
241 }
242 else
243 {
244 aCollumnsName.add( strColumnsName.trim( ) );
245 strColumnsName = "";
246 }
247
248 dbPageSection.setColumnNames( aCollumnsName );
249 }
250 }
251
252 listSections.add( dbPageSection );
253 }
254
255 return listSections;
256 }
257 }