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.calendar.service;
35
36 import fr.paris.lutece.plugins.calendar.business.Agenda;
37 import fr.paris.lutece.plugins.calendar.web.Constants;
38 import fr.paris.lutece.portal.service.resource.Resource;
39 import fr.paris.lutece.portal.service.resource.ResourceLoader;
40 import fr.paris.lutece.portal.service.util.AppException;
41 import fr.paris.lutece.portal.service.util.AppLogService;
42 import fr.paris.lutece.portal.service.util.AppPathService;
43 import fr.paris.lutece.portal.service.util.AppPropertiesService;
44 import fr.paris.lutece.util.filesystem.DirectoryNotFoundException;
45 import fr.paris.lutece.util.filesystem.FileSystemUtil;
46
47 import java.io.File;
48 import java.io.FileInputStream;
49 import java.io.IOException;
50
51 import java.util.ArrayList;
52 import java.util.Collection;
53 import java.util.List;
54 import java.util.Properties;
55
56
57
58
59
60 public class AgendaLoaderProperties implements ResourceLoader
61 {
62 private static final String PROPERTY_CALENDAR_FILES_PATH = "calendar.files.path";
63 private static final String EXT_CALENDAR_FILES = ".properties";
64 private static final String PROPERTY_NAME = "name";
65 private static final String PROPERTY_EVENT_IMAGE = "event.image";
66 private static final String PROPERTY_EVENT_PREFIX = "event.prefix";
67 private static final String PROPERTY_LOADER_CLASS = "loader.class";
68 private static final String PROPERTY_LOADER_PARAMETER = "loader.parameter";
69 private String _strFilesPath;
70
71
72
73
74 public AgendaLoaderProperties( )
75 {
76 super( );
77 _strFilesPath = AppPropertiesService.getProperty( PROPERTY_CALENDAR_FILES_PATH );
78 }
79
80
81
82
83
84 public Collection<AgendaResource> getResources( )
85 {
86 String strRootDirectory = AppPathService.getWebAppPath( );
87 List<AgendaResource> listPages = new ArrayList<AgendaResource>( );
88
89 try
90 {
91 List<File> listFiles = FileSystemUtil.getFiles( strRootDirectory, _strFilesPath );
92
93 for ( File file : listFiles )
94 {
95 String fileName = file.getName( );
96
97 if ( fileName.endsWith( EXT_CALENDAR_FILES ) )
98 {
99 String strId = fileName.substring( 0, fileName.lastIndexOf( "." ) );
100 AgendaResource agenda = loadAgenda( file, strId );
101 listPages.add( agenda );
102 }
103 }
104 }
105 catch ( DirectoryNotFoundException e )
106 {
107 throw new AppException( e.getMessage( ), e );
108 }
109
110 return listPages;
111 }
112
113
114
115
116
117
118 public Resource getResource( String strId )
119 {
120 Resource resource = null;
121 String strFilePath = AppPathService.getPath( PROPERTY_CALENDAR_FILES_PATH, strId + EXT_CALENDAR_FILES );
122 File file = new File( strFilePath );
123
124 if ( file.exists( ) )
125 {
126 resource = loadAgenda( file, strId );
127 }
128
129 return resource;
130 }
131
132
133
134
135
136
137
138 private AgendaResource loadAgenda( File file, String strId )
139 {
140 AgendaResource agenda = new AgendaResource( );
141 Properties properties = new Properties( );
142
143 try
144 {
145 FileInputStream is = new FileInputStream( file );
146 properties.load( is );
147
148 agenda.setId( strId );
149 agenda.setName( properties.getProperty( PROPERTY_NAME ) );
150 agenda.setEventImage( properties.getProperty( PROPERTY_EVENT_IMAGE ) );
151 agenda.setEventPrefix( properties.getProperty( PROPERTY_EVENT_PREFIX ) );
152 agenda.setLoaderClassName( properties.getProperty( PROPERTY_LOADER_CLASS ) );
153 agenda.setLoaderParameter( properties.getProperty( PROPERTY_LOADER_PARAMETER ) );
154
155 try
156 {
157 AgendaLoader loader = (AgendaLoader) Class.forName( agenda.getLoaderClassName( ) ).newInstance( );
158 Agenda a = loader.load( agenda.getLoaderParameter( ) );
159
160 if ( a != null )
161 {
162 a.setName( agenda.getName( ) );
163 a.setKeyName( agenda.getId( ) );
164 agenda.setAgenda( a );
165 agenda.setResourceType( AppPropertiesService.getProperty( Constants.PROPERTY_READ_ONLY ) );
166 }
167 }
168 catch ( ClassNotFoundException e )
169 {
170 AppLogService.error( e.getMessage( ), e );
171 }
172 catch ( IllegalAccessException e )
173 {
174 AppLogService.error( e.getMessage( ), e );
175 }
176 catch ( InstantiationException e )
177 {
178 AppLogService.error( e.getMessage( ), e );
179 }
180 }
181 catch ( IOException e )
182 {
183 AppLogService.error( e.getMessage( ), e );
184 }
185
186 return agenda;
187 }
188 }