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.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
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
150
151
152
153
154
155
156
157
158
159 protected Document getDocument( LocalDTO local ) throws IOException, InterruptedException, SiteMessageException
160 {
161 String strPageBaseUrl = AppPropertiesService.getProperty( PROPERTY_PAGE_BASE_URL );
162
163
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
170
171 doc.add( new Field( SearchItem.FIELD_TYPE, local.getType( ).getLabel( ), Field.Store.YES,
172 Field.Index.NOT_ANALYZED ) );
173
174
175
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
188
189
190 String strIdPage = "" + local.getId( );
191 doc.add( new Field( SearchItem.FIELD_UID, strIdPage, Field.Store.NO, Field.Index.NOT_ANALYZED ) );
192
193
194
195 doc.add( new Field( SearchItem.FIELD_TITLE, local.getTitle( ), Field.Store.YES, Field.Index.NOT_ANALYZED ) );
196
197
198 return doc;
199 }
200 }