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.cartography.modules.solr.indexer;
35
36 import java.util.ArrayList;
37 import java.util.List;
38
39 import org.xml.sax.ContentHandler;
40
41 import fr.paris.lutece.plugins.carto.business.Coordonnee;
42 import fr.paris.lutece.plugins.carto.business.CoordonneeHome;
43 import fr.paris.lutece.plugins.search.solr.business.field.Field;
44 import fr.paris.lutece.plugins.search.solr.indexer.SolrIndexer;
45 import fr.paris.lutece.plugins.search.solr.indexer.SolrIndexerService;
46 import fr.paris.lutece.plugins.search.solr.indexer.SolrItem;
47 import fr.paris.lutece.plugins.search.solr.util.LuteceSolrException;
48 import fr.paris.lutece.plugins.search.solr.util.LuteceSolrRuntimeException;
49 import fr.paris.lutece.plugins.search.solr.util.SolrConstants;
50 import fr.paris.lutece.plugins.search.solr.util.TikaIndexerUtil;
51 import fr.paris.lutece.portal.business.page.Page;
52 import fr.paris.lutece.portal.business.page.PageHome;
53 import fr.paris.lutece.portal.service.message.SiteMessageException;
54 import fr.paris.lutece.portal.service.page.IPageService;
55 import fr.paris.lutece.portal.service.spring.SpringContextService;
56 import fr.paris.lutece.portal.service.util.AppException;
57 import fr.paris.lutece.portal.service.util.AppLogService;
58 import fr.paris.lutece.portal.service.util.AppPropertiesService;
59 import fr.paris.lutece.util.url.UrlItem;
60
61
62
63
64
65 public class SolrCoordinateIndexer implements SolrIndexer
66 {
67 public static final String RESSOURCE_COORDONNEES = "COORDONNEES_COORDONNEE";
68 public static final String NAME = "SolrCoordonneeIndexer";
69 private static final String PARAMETER_PAGE_ID = "page_id";
70 private static final String DESCRIPTION = "Solr coordonnees Indexer";
71 private static final String VERSION = "1.0.0";
72 private static final String TYPE = "PAGE";
73 private static final String CATEGORIE = "GeoJSon";
74 private static final String PROPERTY_INDEXER_ENABLE = "solr.indexer.page.enable";
75 private static final String BEAN_PAGE_SERVICE = "pageService";
76 private static final String SHORT_NAME = "page";
77 private static final List<String> LIST_RESSOURCES_NAME = new ArrayList<>( );
78 private static final String PAGE_INDEXATION_ERROR = "[SolrCoordonneeIndexer] An error occured during the indexation of the page number ";
79
80
81
82
83 public SolrCoordinateIndexer( )
84 {
85 LIST_RESSOURCES_NAME.add( RESSOURCE_COORDONNEES );
86 }
87
88
89
90
91 public List<String> indexDocuments( )
92 {
93 List<Coordonnee> lstCoord = CoordonneeHome.getCoordonneesList( );
94 List<String> lstErrors = new ArrayList<>( );
95 List<SolrItem> lstSolrItems = new ArrayList<>( );
96
97 for ( Coordonnee coord : lstCoord )
98 {
99 try
100 {
101
102 SolrItem item = getItem( coord, SolrIndexerService.getBaseUrl( ) );
103 lstSolrItems.add( item );
104 }
105 catch( Exception e )
106 {
107 lstErrors.add( PAGE_INDEXATION_ERROR + coord.getId( ) + " : " + SolrIndexerService.buildErrorMessage( e ) );
108 AppLogService.error( PAGE_INDEXATION_ERROR + coord.getId( ), e );
109 }
110 }
111
112 try
113 {
114 SolrIndexerService.write( lstSolrItems );
115 }
116 catch( Exception e )
117 {
118 lstErrors.add( SolrIndexerService.buildErrorMessage( e ) );
119 AppLogService.error( PAGE_INDEXATION_ERROR, e );
120 }
121
122 return lstErrors;
123 }
124
125
126
127
128
129
130
131
132
133
134
135
136
137 private SolrItem getItem( Coordonnee coord, String strUrl ) throws SiteMessageException
138 {
139
140
141 SolrItem solrItem = new SolrItem( );
142 String nIdCoordinate = String.valueOf( coord.getId( ) );
143 solrItem.setIdResource( nIdCoordinate );
144 solrItem.setSite( SolrIndexerService.getWebAppName( ) );
145 solrItem.setRole( "Cartography" );
146 solrItem.setType( "Cartography" + "_" + coord.getId( ) );
147 solrItem.setUid( coord.getId( ) + "_Coordonnees" );
148 solrItem.setTitle( "Coordonnees" + " #" + nIdCoordinate );
149 solrItem.setContent( "" );
150
151 solrItem.setUrl( "jsp/site/Portal.jsp?page=formsResponse&id_response=" + nIdCoordinate );
152
153 solrItem.addDynamicFieldGeoloc( "coordonnee_geojson", coord.getGeoJson( ), "Coordonnee" );
154 solrItem.addDynamicField( "DataLayer", String.valueOf( coord.getDataLayer( ).getSolrTag( ) ) );
155
156 return solrItem;
157 }
158
159
160
161
162
163
164 @Override
165 public String getName( )
166 {
167 return NAME;
168 }
169
170
171
172
173
174
175 @Override
176 public String getVersion( )
177 {
178 return VERSION;
179 }
180
181
182
183
184 @Override
185 public String getDescription( )
186 {
187 return DESCRIPTION;
188 }
189
190
191
192
193 @Override
194 public boolean isEnable( )
195 {
196 return "true".equalsIgnoreCase( AppPropertiesService.getProperty( PROPERTY_INDEXER_ENABLE ) );
197 }
198
199
200
201
202 @Override
203 public List<Field> getAdditionalFields( )
204 {
205 return null;
206 }
207
208
209
210
211 @Override
212 public List<SolrItem> getDocuments( String strIdDocument )
213 {
214 List<SolrItem> lstItems = new ArrayList<>( );
215 try
216 {
217 int nIdDocument = Integer.parseInt( strIdDocument );
218
219 Coordonnee coord = CoordonneeHome.findByPrimaryKey( nIdDocument ).get( );
220 lstItems.add( getItem( coord, SolrIndexerService.getBaseUrl( ) ) );
221 }
222 catch( Exception e )
223 {
224 throw new LuteceSolrRuntimeException( e.getMessage( ), e );
225 }
226
227 return lstItems;
228 }
229
230
231
232
233 @Override
234 public List<String> getResourcesName( )
235 {
236 return LIST_RESSOURCES_NAME;
237 }
238
239
240
241
242 @Override
243 public String getResourceUid( String strResourceId, String strResourceType )
244 {
245 StringBuilder sb = new StringBuilder( strResourceId );
246 sb.append( SolrConstants.CONSTANT_UNDERSCORE ).append( SHORT_NAME );
247
248 return sb.toString( );
249 }
250 }