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.modules.text;
35  
36  import java.io.BufferedReader;
37  import java.io.File;
38  import java.io.FileNotFoundException;
39  import java.io.FileReader;
40  import java.io.IOException;
41  
42  import fr.paris.lutece.plugins.calendar.business.Agenda;
43  import fr.paris.lutece.plugins.calendar.business.SimpleAgenda;
44  import fr.paris.lutece.plugins.calendar.business.SimpleEvent;
45  import fr.paris.lutece.plugins.calendar.service.AgendaLoader;
46  import fr.paris.lutece.plugins.calendar.service.Utils;
47  import fr.paris.lutece.portal.service.util.AppLogService;
48  import fr.paris.lutece.portal.service.util.AppPathService;
49  
50  
51  /**
52   * A basic implementation of an Agenda Loader.
53   * This loader constructs agenda by reading events in a text file.
54   * Each line of the text file should contains an event defined as follow :
55   * [date formatted as YYYYMMDD] [description]
56   */
57  public class TextAgendaLoader implements AgendaLoader
58  {
59      private static final int MINIMUM_LINE_LENGTH = 10;
60  
61      /** Creates a new instance of TextAgendaLoader */
62      public TextAgendaLoader(  )
63      {
64      }
65  
66      /**
67       * Load an agenda using its name
68       * @return An agenda object
69       * @param strParameter The parameter of the agenada
70       */
71      public Agenda load( String strParameter )
72      {
73          SimpleAgenda agenda = new SimpleAgenda(  );
74          String strFile = AppPathService.getWebAppPath(  ) + strParameter;
75          File file = new File( strFile );
76          parseFileContent( file, agenda );
77  
78          return agenda;
79      }
80  
81      /**
82       * Parse a text file to extracts events.
83       * @param agenda The agenda
84       * @param file The agenda text file.
85       */
86      private void parseFileContent( File file, SimpleAgenda agenda )
87      {
88          BufferedReader input = null;
89  
90          try
91          {
92              //use buffering
93              //this implementation reads one line at a time
94              //FileReader always assumes default encoding is OK!
95              input = new BufferedReader( new FileReader( file ) );
96  
97              String strLine = null;
98  
99              while ( ( strLine = input.readLine(  ) ) != null )
100             {
101                 if ( strLine.length(  ) > MINIMUM_LINE_LENGTH )
102                 {
103                     String strDate = getDate( strLine );
104 
105                     if ( Utils.isValid( strDate ) )
106                     {
107                         String strEvent = getEvent( strLine );
108                         SimpleEvent event = new SimpleEvent(  );
109                         event.setDate( Utils.getDate( strDate ) );
110                         event.setTitle( strEvent );
111                         agenda.addEvent( event );
112                     }
113                 }
114             }
115         }
116         catch ( FileNotFoundException e )
117         {
118             AppLogService.error( e.getMessage(  ), e );
119         }
120         catch ( IOException e )
121         {
122             AppLogService.error( e.getMessage(  ), e );
123         }
124         finally
125         {
126             try
127             {
128                 if ( input != null )
129                 {
130                     //flush and close both "input" and its underlying FileReader
131                     input.close(  );
132                 }
133             }
134             catch ( IOException e )
135             {
136                 AppLogService.error( e.getMessage(  ), e );
137             }
138         }
139     }
140 
141     /**
142      * Extracts the date from the line
143      * @param strLine A line of the text file
144      * @return the date code
145      */
146     private String getDate( String strLine )
147     {
148         return strLine.substring( 0, 8 );
149     }
150 
151     /**
152      * Extracts the event description from the line
153      * @param strLine A line of the text file
154      * @return the event description
155      */
156     private String getEvent( String strLine )
157     {
158         return strLine.substring( 9 );
159     }
160 }