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.search;
35  
36  import fr.paris.lutece.plugins.dila.business.enums.AudienceCategoryEnum;
37  import fr.paris.lutece.plugins.dila.business.fichelocale.dto.LocalDTO;
38  import fr.paris.lutece.plugins.dila.service.IDilaLocalService;
39  import fr.paris.lutece.portal.service.message.SiteMessageException;
40  import fr.paris.lutece.portal.service.search.IndexationService;
41  import fr.paris.lutece.portal.service.search.PageIndexer;
42  import fr.paris.lutece.portal.service.search.SearchIndexer;
43  import fr.paris.lutece.portal.service.search.SearchItem;
44  import fr.paris.lutece.portal.service.spring.SpringContextService;
45  import fr.paris.lutece.portal.service.util.AppPropertiesService;
46  import fr.paris.lutece.util.url.UrlItem;
47  
48  import org.apache.lucene.document.DateTools;
49  import org.apache.lucene.document.Document;
50  import org.apache.lucene.document.Field;
51  
52  import java.io.IOException;
53  
54  import java.util.ArrayList;
55  import java.util.List;
56  
57  
58  /**
59   * DILA local indexer
60   */
61  public class DilaLocalIndexer implements SearchIndexer
62  {
63      public static final String INDEX_TYPE_XML = "DILA Local";
64      public static final String INDEXER_NAME = "DilaLocalIndexer";
65      protected static final String PROPERTY_PAGE_BASE_URL = "dila.pageIndexer.baseUrl";
66      protected static final String PROPERTY_SEARCH_PAGE_URL = "search.pageSearch.baseUrl";
67      protected static final String PROPERTY_INDEXER_ENABLE = "dila.pageIndexer.enable";
68      protected static final String PARAMETER_PAGE_ID = "xmlFile";
69      protected static final String PARAMETER_PAGE_CATEGORIE = "categorie";
70      private static final String INDEXER_DESCRIPTION = "DILA service for local";
71      private static final String INDEXER_VERSION = "1.0.0";
72      private IDilaLocalService _dilaLocalService = SpringContextService.getBean( "dilaLocalService" );
73  
74      @Override
75      public void indexDocuments(  ) throws IOException, InterruptedException, SiteMessageException
76      {
77          List<LocalDTO> listLocal = _dilaLocalService.findAll(  );
78  
79          for ( LocalDTO local : listLocal )
80          {
81              Document doc = null;
82  
83              try
84              {
85                  doc = getDocument( local );
86              }
87              catch ( Exception e )
88              {
89                  String strMessage = "Page ID : " + local.getId(  );
90                  IndexationService.error( this, e, strMessage );
91              }
92  
93              if ( doc != null )
94              {
95                  IndexationService.write( doc );
96              }
97          }
98      }
99  
100     @Override
101     public List<Document> getDocuments( String strIdDocument )
102         throws IOException, InterruptedException, SiteMessageException
103     {
104         return null;
105     }
106 
107     @Override
108     public String getName(  )
109     {
110         return INDEXER_NAME;
111     }
112 
113     @Override
114     public String getVersion(  )
115     {
116         return INDEXER_VERSION;
117     }
118 
119     @Override
120     public String getDescription(  )
121     {
122         return INDEXER_DESCRIPTION;
123     }
124 
125     @Override
126     public boolean isEnable(  )
127     {
128         String strEnable = AppPropertiesService.getProperty( PROPERTY_INDEXER_ENABLE, "true" );
129 
130         return ( strEnable.equalsIgnoreCase( "true" ) );
131     }
132 
133     @Override
134     public List<String> getListType(  )
135     {
136         List<String> listType = new ArrayList<String>(  );
137         listType.add( PageIndexer.INDEX_TYPE_PAGE );
138 
139         return listType;
140     }
141 
142     @Override
143     public String getSpecificSearchAppUrl(  )
144     {
145         return AppPropertiesService.getProperty( PROPERTY_SEARCH_PAGE_URL );
146     }
147 
148     /**
149      * Builds a document which will be used by Lucene during the indexing of the
150      * pages of the site with the following
151      * fields : summary, uid, url, contents, title and description.
152      * @return the built Document
153      * @param local the xml to index
154      * @throws IOException The IO Exception
155      * @throws InterruptedException The InterruptedException
156      * @throws SiteMessageException occurs when a site message need to be
157      *             displayed
158      */
159     protected Document getDocument( LocalDTO local ) throws IOException, InterruptedException, SiteMessageException
160     {
161         String strPageBaseUrl = AppPropertiesService.getProperty( PROPERTY_PAGE_BASE_URL );
162 
163         // make a new, empty document
164         Document doc = new Document(  );
165 
166         String strDate = DateTools.dateToString( local.getCreationDate(  ), DateTools.Resolution.DAY );
167         doc.add( new Field( SearchItem.FIELD_DATE, strDate, Field.Store.YES, Field.Index.NOT_ANALYZED ) );
168 
169         // Add the url as a field named "url".  Use an UnIndexed field, so
170         // that the url is just stored with the document, but is not searchable.
171         doc.add( new Field( SearchItem.FIELD_TYPE, local.getType(  ).getLabel(  ), Field.Store.YES,
172                 Field.Index.NOT_ANALYZED ) );
173 
174         // Add the url as a field named "url".  Use an UnIndexed field, so
175         // that the url is just stored with the document, but is not searchable.
176         UrlItem url = new UrlItem( strPageBaseUrl );
177         url.addParameter( PARAMETER_PAGE_ID, "" + local.getId(  ) );
178         url.addParameter( PARAMETER_PAGE_CATEGORIE, AudienceCategoryEnum.fromId( local.getIdAudience(  ) ).getLabel(  ) );
179 
180         doc.add( new Field( SearchItem.FIELD_URL, url.getUrl(  ), Field.Store.YES, Field.Index.NOT_ANALYZED ) );
181 
182         StringBuilder content = new StringBuilder(  );
183         content.append( local.getXml(  ) );
184 
185         doc.add( new Field( SearchItem.FIELD_CONTENTS, content.toString(  ), Field.Store.NO, Field.Index.ANALYZED ) );
186 
187         // Add the uid as a field, so that index can be incrementally maintained.
188         // This field is not stored with document, it is indexed, but it is not
189         // tokenized prior to indexing.
190         String strIdPage = "" + local.getId(  );
191         doc.add( new Field( SearchItem.FIELD_UID, strIdPage, Field.Store.NO, Field.Index.NOT_ANALYZED ) );
192 
193         // Add the tag-stripped contents as a Reader-valued Text field so it will
194         // get tokenized and indexed.
195         doc.add( new Field( SearchItem.FIELD_TITLE, local.getTitle(  ), Field.Store.YES, Field.Index.NOT_ANALYZED ) );
196 
197         // return the document
198         return doc;
199     }
200 }