View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
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   *  This loader fetches agendas stored in a property file
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       * A loader importing agenda in properties files
73       */
74      public AgendaLoaderProperties(  )
75      {
76          super(  );
77          _strFilesPath = AppPropertiesService.getProperty( PROPERTY_CALENDAR_FILES_PATH );
78      }
79  
80      /**
81       * Get the agenda represented by property files
82       * @return Return the agenda resources
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      * Get a resource by its Id
115      * @param strId The resource Id
116      * @return The resource
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      * Return the page
134      * @return The agenda
135      * @param strId The id
136      * @param file The File to load
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 }