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.mylutece.modules.users.service.search;
35
36 import java.io.IOException;
37 import java.util.ArrayList;
38 import java.util.List;
39
40 import org.apache.lucene.document.Document;
41 import org.apache.lucene.document.Field;
42 import org.apache.lucene.document.StringField;
43 import org.apache.lucene.document.TextField;
44
45 import fr.paris.lutece.plugins.mylutece.modules.users.business.MyLuteceSearchUserHome;
46 import fr.paris.lutece.plugins.mylutece.service.search.MyLuteceSearchUser;
47 import fr.paris.lutece.portal.service.content.XPageAppService;
48 import fr.paris.lutece.portal.service.message.SiteMessageException;
49 import fr.paris.lutece.portal.service.plugin.Plugin;
50 import fr.paris.lutece.portal.service.plugin.PluginService;
51 import fr.paris.lutece.portal.service.search.IndexationService;
52 import fr.paris.lutece.portal.service.search.SearchIndexer;
53 import fr.paris.lutece.portal.service.search.SearchItem;
54 import fr.paris.lutece.portal.service.util.AppPathService;
55 import fr.paris.lutece.portal.service.util.AppPropertiesService;
56 import fr.paris.lutece.util.ReferenceItem;
57 import fr.paris.lutece.util.ReferenceList;
58 import fr.paris.lutece.util.url.UrlItem;
59
60
61
62
63 public class MyLuteceSearchUserIndexer implements SearchIndexer
64 {
65 public static final String SHORT_NAME = "mlu";
66 private static final String ENABLE_VALUE_TRUE = "1";
67 public static final String PROPERTY_INDEXER_NAME = "mylutece-users.indexer.name";
68 private static final String PROPERTY_INDEXER_DESCRIPTION = "mylutece-users.indexer.description";
69 private static final String PROPERTY_INDEXER_VERSION = "mylutece-users.indexer.version";
70 private static final String PROPERTY_INDEXER_ENABLE = "mylutece-users.indexer.enable";
71 public static final String PROPERTY_INDEX_TYPE_PAGE = "myLuteceSearchUser";
72 private static final String PARAMETER_SEARCHUSER_ID = "myLuteceSearchUser_id";
73 private static final String JSP_SEARCH_SEARCHUSER = "";
74 public static final String FIELD_ID_TITLE = "id";
75 public static final String FIELD_LOGIN_TITLE = "login";
76 public static final String FIELD_GIVEN_NAME_TITLE = "givenName";
77 public static final String FIELD_LAST_NAME_TITLE = "LastName";
78 public static final String FIELD_EMAIL_TITLE = "email";
79 String _pluginName = "mylutece-users";
80
81
82
83
84
85
86
87
88
89
90
91
92 public void indexDocuments( ) throws IOException, InterruptedException, SiteMessageException
93 {
94 String strPortalUrl = AppPathService.getPortalUrl( );
95 Plugin plugin = PluginService.getPlugin( _pluginName );
96 List<MyLuteceSearchUser> listMyLuteceSearchUsers = MyLuteceSearchUserHome.getMyLuteceSearchUsersList( );
97 for ( MyLuteceSearchUser myLuteceSearchUser : listMyLuteceSearchUsers )
98 {
99 UrlItem url = new UrlItem( strPortalUrl );
100 url.addParameter( XPageAppService.PARAM_XPAGE_APP, _pluginName );
101 url.addParameter( PARAMETER_SEARCHUSER_ID, myLuteceSearchUser.getId( ) );
102 org.apache.lucene.document.Document docMyLuteceSearchUser = getDocument( myLuteceSearchUser, plugin );
103 IndexationService.write( docMyLuteceSearchUser );
104 }
105 }
106
107
108
109
110
111
112
113
114 public List<Document> getDocuments( String strId ) throws IOException, InterruptedException, SiteMessageException
115 {
116 ArrayList<org.apache.lucene.document.Document> listDocuments = new ArrayList<>( );
117 String strPortalUrl = AppPathService.getPortalUrl( );
118 Plugin plugin = PluginService.getPlugin( _pluginName );
119 MyLuteceSearchUser myLuteceSearchUser = MyLuteceSearchUserHome.findByPrimaryKey( Integer.parseInt( strId ) );
120 if ( myLuteceSearchUser != null )
121 {
122 UrlItem url = new UrlItem( strPortalUrl );
123 url.addParameter( XPageAppService.PARAM_XPAGE_APP, _pluginName );
124 url.addParameter( PARAMETER_SEARCHUSER_ID, myLuteceSearchUser.getId( ) );
125 org.apache.lucene.document.Document docMyLuteceSearchUser = null;
126 try
127 {
128 docMyLuteceSearchUser = getDocument( myLuteceSearchUser, plugin );
129 }
130 catch( Exception e )
131 {
132 String strMessage = "MyLuteceSearchUser ID : " + myLuteceSearchUser.getId( );
133 IndexationService.error( this, e, strMessage );
134 }
135 if ( docMyLuteceSearchUser != null )
136 {
137 listDocuments.add( docMyLuteceSearchUser );
138 }
139 }
140 return listDocuments;
141 }
142
143
144
145
146 public String getName( )
147 {
148 return AppPropertiesService.getProperty( PROPERTY_INDEXER_NAME );
149 }
150
151
152
153
154 public String getVersion( )
155 {
156 return AppPropertiesService.getProperty( PROPERTY_INDEXER_VERSION );
157 }
158
159
160
161
162 public boolean isEnable( )
163 {
164 boolean bReturn = false;
165 String strEnable = AppPropertiesService.getProperty( PROPERTY_INDEXER_ENABLE );
166 if ( ( strEnable != null ) && ( strEnable.equalsIgnoreCase( Boolean.TRUE.toString( ) ) || strEnable.equals( ENABLE_VALUE_TRUE ) )
167 && PluginService.isPluginEnable( _pluginName ) )
168 {
169 bReturn = true;
170 }
171 return bReturn;
172 }
173
174
175
176
177
178
179 public String getDescription( )
180 {
181 return AppPropertiesService.getProperty( PROPERTY_INDEXER_DESCRIPTION );
182 }
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202 private Document getDocument( MyLuteceSearchUser myLuteceSearchUser, Plugin plugin ) throws IOException, InterruptedException, SiteMessageException
203 {
204
205 org.apache.lucene.document.Document doc = new org.apache.lucene.document.Document( );
206 doc.add( new Field( SearchItem.FIELD_CONTENTS, getContentToIndex( myLuteceSearchUser ), TextField.TYPE_NOT_STORED ) );
207 doc.add( new StringField( SearchItem.FIELD_UID, String.valueOf( myLuteceSearchUser.getId( ) ), Field.Store.YES ) );
208 doc.add( new StringField( SearchItem.FIELD_TYPE, _pluginName, Field.Store.YES ) );
209 doc.add( new TextField( SearchItem.FIELD_TITLE, getFullName( myLuteceSearchUser ), Field.Store.YES ) );
210 ReferenceList listAttribute = myLuteceSearchUser.getAttributes( );
211
212 if ( listAttribute != null )
213 {
214 for ( ReferenceItem attribute : listAttribute )
215 {
216 doc.add( new Field( "attribute_" + attribute.getName( ), attribute.getCode( ), TextField.TYPE_STORED ) );
217 }
218 }
219
220 return doc;
221 }
222
223
224
225
226
227
228
229
230 private static String getContentToIndex( MyLuteceSearchUser myLuteceSearchUser )
231 {
232 StringBuilder sbContentToIndex = new StringBuilder( );
233 sbContentToIndex.append( myLuteceSearchUser.getLogin( ) );
234 sbContentToIndex.append( " " );
235 sbContentToIndex.append( myLuteceSearchUser.getGivenName( ) );
236 sbContentToIndex.append( " " );
237 sbContentToIndex.append( myLuteceSearchUser.getLastName( ) );
238 sbContentToIndex.append( " " );
239 sbContentToIndex.append( myLuteceSearchUser.getEmail( ) );
240 return sbContentToIndex.toString( );
241 }
242
243
244
245
246
247
248
249
250 private static String getFullName( MyLuteceSearchUser myLuteceSearchUser )
251 {
252 StringBuilder sbContentToIndex = new StringBuilder( );
253 sbContentToIndex.append( myLuteceSearchUser.getLastName( ) );
254 sbContentToIndex.append( " " );
255 sbContentToIndex.append( myLuteceSearchUser.getGivenName( ) );
256 return sbContentToIndex.toString( );
257 }
258
259
260
261
262 public List<String> getListType( )
263 {
264 List<String> listType = new ArrayList<>( );
265 listType.add( _pluginName );
266 return listType;
267 }
268
269
270
271
272 public String getSpecificSearchAppUrl( )
273 {
274 return JSP_SEARCH_SEARCHUSER;
275 }
276 }