1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
63
64 public class DilaBatchXMLService implements IDilaBatchXMLService
65 {
66
67
68
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
75
76 private static final String RESSOURCE_TYPE = "Ressource";
77
78
79
80
81 private static final SAXBuilder _saxBuilder = new SAXBuilder( );
82
83
84
85
86 private static final String NAMESPACE_URI_DC = "http://purl.org/dc/elements/1.1/";
87
88
89
90
91 private static final Namespace NAMESPACE_DC = Namespace.getNamespace( "dc", NAMESPACE_URI_DC );
92
93
94
95
96 private static final String ATTRIBUTE_ID = "ID";
97
98
99
100
101 private static final String CHILD_TITLE = "title";
102
103
104
105
106 private static final String CHILD_TYPE = "type";
107
108
109
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
129 XmlDTO dilaXml = buildDocument( file, typeXML );
130
131
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
158 Document document = _saxBuilder.build( file );
159
160
161 Element root = document.getRootElement( );
162
163
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
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
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
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 }