View Javadoc
1   /*
2    * Copyright (c) 2002-2020, 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.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   * Indexer service for myLuteceSearchUser Xpages
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       * Index all Local Users
84       * 
85       * @throws IOException
86       *             exception
87       * @throws InterruptedException
88       *             exception
89       * @throws SiteMessageException
90       *             exception
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      * Return a list of lucene document for incremental indexing
109      * 
110      * @param strId
111      *            Document id
112      * @return listDocuments the document list
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      * {@inheritDoc}
145      */
146     public String getName( )
147     {
148         return AppPropertiesService.getProperty( PROPERTY_INDEXER_NAME );
149     }
150 
151     /**
152      * {@inheritDoc}
153      */
154     public String getVersion( )
155     {
156         return AppPropertiesService.getProperty( PROPERTY_INDEXER_VERSION );
157     }
158 
159     /**
160      * {@inheritDoc}
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      * Returns the indexer service description
176      * 
177      * @return The indexer service description
178      */
179     public String getDescription( )
180     {
181         return AppPropertiesService.getProperty( PROPERTY_INDEXER_DESCRIPTION );
182     }
183 
184     /**
185      * 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,
186      * title and description.
187      * 
188      * @return the built Document
189      * @param strUrl
190      *            The base URL for documents
191      * @param myLuteceSearchUser
192      *            the page to index
193      * @param plugin
194      *            The {@link Plugin}
195      * @throws IOException
196      *             The IO Exception
197      * @throws InterruptedException
198      *             The InterruptedException
199      * @throws SiteMessageException
200      *             occurs when a site message need to be displayed
201      */
202     private Document getDocument( MyLuteceSearchUser myLuteceSearchUser, Plugin plugin ) throws IOException, InterruptedException, SiteMessageException
203     {
204         // make a new, empty document
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      * Set the Content to index
225      * 
226      * @param myLuteceSearchUser
227      *            The myLuteceSearchUser to index
228      * @return The content to index
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      * Set the Content to index
245      * 
246      * @param myLuteceSearchUser
247      *            The myLuteceSearchUser to index
248      * @return The content to index
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      * {@inheritDoc}
261      */
262     public List<String> getListType( )
263     {
264         List<String> listType = new ArrayList<>( );
265         listType.add( _pluginName );
266         return listType;
267     }
268 
269     /**
270      * {@inheritDoc}
271      */
272     public String getSpecificSearchAppUrl( )
273     {
274         return JSP_SEARCH_SEARCHUSER;
275     }
276 }