View Javadoc
1   /*
2    * Copyright (c) 2002-2023, City of 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.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   * The indexer service for Solr.
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       * Creates a new SolrPageIndexer
82       */
83      public SolrCoordinateIndexer( )
84      {
85          LIST_RESSOURCES_NAME.add( RESSOURCE_COORDONNEES );
86      }
87  
88      /**
89       * {@inheritDoc}
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                 // Generates the item to index
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      * Builds a document which will be used by Lucene during the indexing of the pages of the site with the following fields : summary, uid, url, contents,
127      * title and description.
128      *
129      * @return the built Document
130      * @param strUrl
131      *            The base URL for documents
132      * @param page
133      *            the page to index
134      * @throws SiteMessageException
135      *             occurs when a site message need to be displayed
136      */
137     private SolrItem getItem( Coordonnee coord, String strUrl ) throws SiteMessageException
138     {
139 
140         // make a new, empty SolrItem
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         // solrItem.setDate( formResponse.getCreation( ) );
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      * Returns the name of the indexer.
161      *
162      * @return the name of the indexer
163      */
164     @Override
165     public String getName( )
166     {
167         return NAME;
168     }
169 
170     /**
171      * Returns the version.
172      *
173      * @return the version.
174      */
175     @Override
176     public String getVersion( )
177     {
178         return VERSION;
179     }
180 
181     /**
182      * {@inheritDoc}
183      */
184     @Override
185     public String getDescription( )
186     {
187         return DESCRIPTION;
188     }
189 
190     /**
191      * {@inheritDoc}
192      */
193     @Override
194     public boolean isEnable( )
195     {
196         return "true".equalsIgnoreCase( AppPropertiesService.getProperty( PROPERTY_INDEXER_ENABLE ) );
197     }
198 
199     /**
200      * {@inheritDoc}
201      */
202     @Override
203     public List<Field> getAdditionalFields( )
204     {
205         return null;
206     }
207 
208     /**
209      * {@inheritDoc}
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             // Page page = PageHome.getPage( nIdDocument );
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      * {@inheritDoc}
232      */
233     @Override
234     public List<String> getResourcesName( )
235     {
236         return LIST_RESSOURCES_NAME;
237     }
238 
239     /**
240      * {@inheritDoc}
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 }