View Javadoc
1   /*
2    * Copyright (c) 2002-2021, 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.blog.service.search;
35  
36  import fr.paris.lutece.plugins.blog.business.Blog;
37  import fr.paris.lutece.plugins.blog.service.BlogService;
38  import fr.paris.lutece.plugins.blog.service.docsearch.BlogSearchService;
39  import fr.paris.lutece.plugins.blog.service.docsearch.DefaultBlogIndexer;
40  import fr.paris.lutece.portal.service.content.XPageAppService;
41  import fr.paris.lutece.portal.service.message.SiteMessageException;
42  import fr.paris.lutece.portal.service.search.SearchIndexer;
43  import fr.paris.lutece.portal.service.util.AppLogService;
44  import fr.paris.lutece.portal.service.util.AppPathService;
45  import fr.paris.lutece.portal.service.util.AppPropertiesService;
46  import fr.paris.lutece.util.url.UrlItem;
47  
48  import org.apache.lucene.document.Document;
49  
50  import java.io.IOException;
51  import java.util.ArrayList;
52  import java.util.List;
53  
54  /**
55   * Directory global indexer
56   */
57  public class BlogSearchIndexer implements SearchIndexer
58  {
59      public static final String INDEXER_NAME = "BlogsIndexer";
60      public static final String SHORT_NAME = "hdoc";
61      private static final String BLOGS = "blogs";
62      private static final String INDEXER_DESCRIPTION = "Indexer service for blogs";
63      private static final String INDEXER_VERSION = "1.0.0";
64      private static final String PROPERTY_INDEXER_ENABLE = "blog.globalIndexer.enable";
65  
66      /**
67       * {@inheritDoc}
68       */
69      @Override
70      public String getName( )
71      {
72          return INDEXER_NAME;
73      }
74  
75      /**
76       * {@inheritDoc}
77       */
78      @Override
79      public String getDescription( )
80      {
81          return INDEXER_DESCRIPTION;
82      }
83  
84      /**
85       * {@inheritDoc}
86       */
87      @Override
88      public String getVersion( )
89      {
90          return INDEXER_VERSION;
91      }
92  
93      /**
94       * {@inheritDoc}
95       */
96      @Override
97      public boolean isEnable( )
98      {
99          String strEnable = AppPropertiesService.getProperty( PROPERTY_INDEXER_ENABLE );
100 
101         return ( strEnable.equalsIgnoreCase( "true" ) );
102     }
103 
104     /**
105      * {@inheritDoc}
106      */
107     @Override
108     public List<String> getListType( )
109     {
110         List<String> listType = new ArrayList<>( 1 );
111         listType.add( BLOGS );
112 
113         return listType;
114     }
115 
116     /**
117      * {@inheritDoc}
118      */
119     @Override
120     public String getSpecificSearchAppUrl( )
121     {
122         UrlItem url = new UrlItem( AppPathService.getPortalUrl( ) );
123         url.addParameter( XPageAppService.PARAM_XPAGE_APP, BLOGS );
124 
125         return url.getUrl( );
126     }
127 
128     /**
129      * {@inheritDoc}
130      */
131     @Override
132     public List<Document> getDocuments( String strDocumentId ) throws IOException, InterruptedException, SiteMessageException
133     {
134 
135         int documentId;
136 
137         try
138         {
139             documentId = Integer.parseInt( strDocumentId );
140         }
141         catch( NumberFormatException ne )
142         {
143             AppLogService.error( strDocumentId + " not parseable to an int", ne );
144 
145             return new ArrayList<>( 0 );
146         }
147 
148         Blog blog = BlogService.getInstance( ).loadBlog( documentId );
149         Document doc = DefaultBlogIndexer.getDocument( blog );
150 
151         if ( doc != null )
152         {
153             List<Document> listDocument = new ArrayList<>( 1 );
154             listDocument.add( doc );
155 
156             return listDocument;
157         }
158 
159         return new ArrayList<>( 0 );
160     }
161 
162     /**
163      * {@inheritDoc}
164      */
165     @Override
166     public void indexDocuments( ) throws IOException, InterruptedException, SiteMessageException
167     {
168         BlogSearchService.getInstance( ).processIndexing( true );
169     }
170 
171 }