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.dila.service.impl;
35  
36  import fr.paris.lutece.plugins.dila.business.enums.AudienceCategoryEnum;
37  import fr.paris.lutece.plugins.dila.exception.DilaException;
38  import fr.paris.lutece.plugins.dila.service.IDilaBatchXMLService;
39  import fr.paris.lutece.plugins.dila.service.IDilaIndexationService;
40  import fr.paris.lutece.plugins.dila.utils.constants.DilaConstants;
41  import fr.paris.lutece.plugins.dila.utils.filter.FilenameFilterXml;
42  import fr.paris.lutece.portal.service.util.AppLogService;
43  import fr.paris.lutece.portal.service.util.AppPropertiesService;
44  
45  import org.jdom2.JDOMException;
46  
47  import java.io.File;
48  import java.io.IOException;
49  
50  import javax.inject.Inject;
51  import javax.inject.Named;
52  
53  
54  /**
55   * Implementation of {@link IDilaIndexationService}
56   */
57  public class DilaIndexationService implements IDilaIndexationService
58  {
59      @Inject
60      @Named( "dilaBatchXmlService" )
61      private IDilaBatchXMLService _dilaBatchXMLService;
62  
63      @Override
64      public void indexAll(  ) throws DilaException
65      {
66          AppLogService.info( "Begin index" );
67  
68          // Individual
69          process( AppPropertiesService.getProperty( DilaConstants.PROPERTY_XML_FINAL_INDIVIDUALS ),
70              DilaConstants.PROCESS_INDIVIDUAL, AudienceCategoryEnum.INDIVIDUALS );
71  
72          // Association
73          process( AppPropertiesService.getProperty( DilaConstants.PROPERTY_XML_FINAL_ASSO ),
74              DilaConstants.PROCESS_ASSOCIATION, AudienceCategoryEnum.ASSOCIATIONS );
75  
76          // Professional
77          process( AppPropertiesService.getProperty( DilaConstants.PROPERTY_XML_FINAL_PME ),
78              DilaConstants.PROCESS_PROFESSIONAL, AudienceCategoryEnum.PROFESSIONNALS );
79  
80          _dilaBatchXMLService.delete(  );
81      }
82  
83      /**
84       * Process XML files
85       * @param strDirPath directory path to XML files
86       * @param strProcessName process name
87       * @param typeXml the type XML
88       * @throws IOException
89       * @throws JDOMException
90       * @throws DilaException occurs during treatment
91       */
92      private void process( String strDirPath, String strProcessName, AudienceCategoryEnum typeXml )
93          throws DilaException
94      {
95          File dirXML = new File( strDirPath );
96  
97          if ( dirXML.exists(  ) )
98          {
99              File[] listXML = dirXML.listFiles( new FilenameFilterXml(  ) );
100 
101             if ( listXML != null )
102             {
103                 AppLogService.info( "Processing " + strProcessName + " XML files available in directory " + strDirPath );
104 
105                 for ( File file : listXML )
106                 {
107                     try
108                     {
109                         _dilaBatchXMLService.processXMLFile( file, typeXml );
110                     }
111                     catch ( JDOMException jdom )
112                     {
113                         throw new DilaException( "Error during file " + file.getName(  ) + " parsing.", jdom );
114                     }
115                     catch ( IOException ioe )
116                     {
117                         throw new DilaException( "Error during file " + file.getName(  ) + " management.", ioe );
118                     }
119                     catch ( Exception e )
120                     {
121                         throw new DilaException( "Error during file " + file.getName(  ) + " indexation : "+e.getMessage( ), e );
122                     }
123                 }
124             }
125             else
126             {
127                 AppLogService.info( "No " + strProcessName + " XML files in directory " + strDirPath );
128             }
129         }
130     }
131 }