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.business.fichelocale.dao.IXmlDAO;
38  import fr.paris.lutece.plugins.dila.business.fichelocale.dto.XmlDTO;
39  import fr.paris.lutece.plugins.dila.service.IDilaBatchXMLService;
40  import fr.paris.lutece.portal.service.util.AppLogService;
41  
42  import java.io.File;
43  import java.io.IOException;
44  import java.util.ArrayList;
45  import java.util.Arrays;
46  import java.util.List;
47  
48  import javax.inject.Inject;
49  import javax.inject.Named;
50  
51  import org.apache.commons.collections.CollectionUtils;
52  import org.apache.commons.lang.StringUtils;
53  import org.jdom2.Attribute;
54  import org.jdom2.Document;
55  import org.jdom2.Element;
56  import org.jdom2.JDOMException;
57  import org.jdom2.Namespace;
58  import org.jdom2.input.SAXBuilder;
59  
60  
61  /**
62   * Implementation of {@link IDilaBatchXMLService}
63   */
64  public class DilaBatchXMLService implements IDilaBatchXMLService
65  {
66  
67      /**
68       * List of XML file that will not be indexed
69       */
70      public static final List<String> listXMLNotSupported = Arrays.asList( "arborescence.xml", "redirections.xml",
71              "journal.xml", "centresdecontact.xml", "ressourcesenligne.xml", "dossiers.xml", "glossaire.xml" );
72  
73      /**
74       * Type of resource
75       */
76      private static final String RESSOURCE_TYPE = "Ressource";
77  
78      /**
79       * SAX builder
80       */
81      private static final SAXBuilder _saxBuilder = new SAXBuilder( );
82  
83      /**
84       * URI for namespace "dc"
85       */
86      private static final String NAMESPACE_URI_DC = "http://purl.org/dc/elements/1.1/";
87  
88      /**
89       * Namespace "dc"
90       */
91      private static final Namespace NAMESPACE_DC = Namespace.getNamespace( "dc", NAMESPACE_URI_DC );
92  
93      /**
94       * XML attribute : ID
95       */
96      private static final String ATTRIBUTE_ID = "ID";
97  
98      /**
99       * XML child : title
100      */
101     private static final String CHILD_TITLE = "title";
102 
103     /**
104      * XML child : type
105      */
106     private static final String CHILD_TYPE = "type";
107 
108     /**
109      * XML child : FilDAriane
110      */
111     private static final String CHILD_BREADCRUMB = "FilDAriane";
112 
113     @Inject
114     @Named( "dilaXmlDAO" )
115     private IXmlDAO _dilaXmlDAO;
116 
117     @Override
118     public void processXMLFile( File file, AudienceCategoryEnum typeXML ) throws JDOMException, IOException
119     {
120         AppLogService.debug( "Processing file : " + file.getName( ) );
121 
122         if ( DilaBatchXMLService.listXMLNotSupported.contains( file.getName( ).toLowerCase( ) ) )
123         {
124             AppLogService.debug( "File not indexed" );
125         }
126         else
127         {
128             // DILA XML
129             XmlDTO dilaXml = buildDocument( file, typeXML );
130 
131             // Create or update XML
132             List<XmlDTO> listDilaXmls = _dilaXmlDAO.findAll( );
133             int nIndex = listDilaXmls.indexOf( dilaXml );
134 
135             if ( nIndex != -1 )
136             {
137                 XmlDTO dilaXmlExist = listDilaXmls.get( nIndex );
138 
139                 AppLogService.debug( "Update" );
140                 dilaXml.setId( dilaXmlExist.getId( ) );
141                 _dilaXmlDAO.store( dilaXml );
142             }
143             else
144             {
145                 AppLogService.debug( "Create" );
146                 _dilaXmlDAO.create( dilaXml );
147             }
148         }
149     }
150 
151     @Override
152     public XmlDTO buildDocument( File file, AudienceCategoryEnum typeXML ) throws JDOMException, IOException
153     {
154         XmlDTO dilaXml = new XmlDTO( );
155         dilaXml.setIdAudience( typeXML.getId( ) );
156 
157         // Convert file to document object
158         Document document = _saxBuilder.build( file );
159 
160         // Get root
161         Element root = document.getRootElement( );
162 
163         // Get attribute ID
164         Attribute rootAttrId = root.getAttribute( ATTRIBUTE_ID );
165 
166         if ( rootAttrId != null )
167         {
168             String strId = rootAttrId.getValue( );
169             dilaXml.setIdXml( strId );
170         }
171 
172         // Get title
173         Element title = root.getChild( CHILD_TITLE, NAMESPACE_DC );
174 
175         if ( title != null )
176         {
177             String strTitle = title.getTextTrim( );
178             dilaXml.setTitle( strTitle );
179         }
180 
181         // Get type
182         Element type = root.getChild( CHILD_TYPE, NAMESPACE_DC );
183 
184         if ( type != null )
185         {
186             if ( file.getName( ).startsWith( "R" ) )
187             {
188                 dilaXml.setResourceType( RESSOURCE_TYPE );
189             }
190             else
191             {
192                 String strType = type.getTextTrim( );
193                 dilaXml.setResourceType( strType );
194             }
195         }
196 
197         // Get breadcrumb
198         Element rootBreadcrumb = root.getChild( CHILD_BREADCRUMB );
199 
200         if ( rootBreadcrumb != null )
201         {
202             List<Element> listBreadcrumb = rootBreadcrumb.getChildren( );
203 
204             if ( CollectionUtils.isNotEmpty( listBreadcrumb ) )
205             {
206                 List<String> listBreadcrumbId = new ArrayList<String>( listBreadcrumb.size( ) );
207 
208                 for ( Element elementBreadcrumb : listBreadcrumb )
209                 {
210                     listBreadcrumbId.add( elementBreadcrumb.getAttributeValue( ATTRIBUTE_ID ) );
211                 }
212 
213                 String strBreadcrumb = StringUtils.join( listBreadcrumbId, ";" );
214                 dilaXml.setBreadCrumb( strBreadcrumb );
215             }
216         }
217 
218         return dilaXml;
219     }
220 
221     @Override
222     public void delete( )
223     {
224         _dilaXmlDAO.delete( );
225     }
226 }