View Javadoc
1   /**
2    *
3    */
4   package fr.paris.lutece.plugins.dila.service.impl;
5   
6   import fr.paris.lutece.plugins.dila.service.IDilaPivotLocalService;
7   import fr.paris.lutece.plugins.dila.utils.constants.DilaConstants;
8   import fr.paris.lutece.portal.service.util.AppLogService;
9   import fr.paris.lutece.portal.service.util.AppPropertiesService;
10  
11  import java.io.File;
12  import java.io.IOException;
13  import java.io.Serializable;
14  import java.util.ArrayList;
15  import java.util.List;
16  
17  import javax.xml.parsers.DocumentBuilder;
18  import javax.xml.parsers.DocumentBuilderFactory;
19  import javax.xml.parsers.ParserConfigurationException;
20  
21  import org.apache.commons.collections.CollectionUtils;
22  import org.apache.commons.lang.StringUtils;
23  import org.w3c.dom.Document;
24  import org.w3c.dom.Element;
25  import org.w3c.dom.Node;
26  import org.w3c.dom.NodeList;
27  import org.xml.sax.SAXException;
28  
29  
30  /**
31   * This class provides a method to find XML filenames according to code pivot
32   * 
33   */
34  public class DilaPivotLocalService implements IDilaPivotLocalService, Serializable
35  {
36      private static final String ERROR_PIVOT_NOT_FOUND = "Unable to find pivot file ";
37      private static final String TAG_PIVOT_LOCAL = "PivotLocal";
38      private static final String ATTR_ID = "id";
39      private static final String TAG_ORGANISME = "Organisme";
40      private static final String ATTR_PIVOT_LOCAL = "pivotLocal";
41      private static final String TAG_TYPE_ORGANISME = "TypeOrganisme";
42  
43      /** Serial ID */
44      private static final long serialVersionUID = -2515296117212667308L;
45  
46      /**
47       * {@inheritDoc}
48       */
49      @Override
50      public List<String> findByCodePivot( String strCodePivot )
51      {
52          List<String> fileNames = new ArrayList<String>( );
53          String insee = AppPropertiesService.getProperty( DilaConstants.PROPERTY_INSEE );
54          String townDirectory = AppPropertiesService.getProperty( DilaConstants.PROPERTY_XML_DIRECTORY_LOCALES_TOWN );
55          File town = new File( townDirectory + insee.substring( 0, 2 ) + File.separator + insee
56                  + DilaConstants.XML_EXTENSION );
57          DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance( );
58  
59          try
60          {
61              DocumentBuilder builder = factory.newDocumentBuilder( );
62              Document document = builder.parse( town );
63              document.getDocumentElement( ).normalize( );
64  
65              NodeList pivots = document.getElementsByTagName( TAG_TYPE_ORGANISME );
66  
67              for ( int i = 0; i < pivots.getLength( ); i++ )
68              {
69                  Element pivotNode = (Element) pivots.item( i );
70  
71                  if ( ( pivotNode != null ) && strCodePivot.equals( pivotNode.getAttribute( ATTR_PIVOT_LOCAL ) ) )
72                  {
73                      NodeList listOrganisms = pivotNode.getElementsByTagName( TAG_ORGANISME );
74  
75                      for ( int j = 0; j < listOrganisms.getLength( ); j++ )
76                      {
77                          Element organism = (Element) listOrganisms.item( j );
78  
79                          if ( organism != null )
80                          {
81                              fileNames.add( organism.getAttribute( ATTR_ID ) );
82                          }
83                      }
84  
85                      break;
86                  }
87              }
88          }
89          catch ( ParserConfigurationException e )
90          {
91              AppLogService.error( e );
92          }
93          catch ( SAXException e )
94          {
95              AppLogService.error( e );
96          }
97          catch ( IOException e )
98          {
99              AppLogService.error( e );
100         }
101 
102         return fileNames;
103     }
104 
105     /**
106      * {@inheritDoc}
107      */
108     @Override
109     public Document insertPivots( DocumentBuilder builder, Document document ) throws SAXException, IOException
110     {
111         NodeList pivots = document.getElementsByTagName( TAG_PIVOT_LOCAL );
112 
113         for ( int i = 0; i < pivots.getLength( ); i++ )
114         {
115             Node pivotNode = pivots.item( i );
116 
117             if ( pivotNode != null )
118             {
119                 String codePivot = pivotNode.getTextContent( );
120                 pivotNode.setTextContent( StringUtils.EMPTY );
121 
122                 List<String> localOrganisms = this.findByCodePivot( codePivot );
123 
124                 if ( CollectionUtils.isEmpty( localOrganisms ) )
125                 {
126                     pivotNode.getParentNode( ).removeChild( pivotNode );
127                     i--;
128                 }
129                 else
130                 {
131                     for ( String organism : localOrganisms )
132                     {
133                         String[] splitOrganisme = organism.split( "-" );
134                         String directoryOrganisme = AppPropertiesService
135                                 .getProperty( DilaConstants.PROPERTY_XML_DIRECTORY_LOCALES_ORGANISMS );
136                         String fileName = directoryOrganisme + splitOrganisme[1].substring( 0, 2 ) + File.separator
137                                 + organism + DilaConstants.XML_EXTENSION;
138                         File pivotFile = new File( fileName );
139                         if ( !pivotFile.exists( ) )
140                         {
141                             AppLogService.error( ERROR_PIVOT_NOT_FOUND + fileName );
142                             continue;
143                         }
144                         Document documentPivot = builder.parse( pivotFile );
145 
146                         Node pivot = documentPivot.getFirstChild( );
147                         Node importedPivot = document.importNode( pivot, true );
148                         pivotNode.appendChild( importedPivot );
149                     }
150                 }
151 
152                 document.getDocumentElement( ).normalize( );
153             }
154         }
155 
156         return document;
157     }
158 }