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.search.solr.web;
35  
36  import fr.paris.lutece.plugins.search.solr.business.facetintersection.FacetIntersectionHome;
37  import fr.paris.lutece.plugins.search.solr.business.field.Field;
38  import fr.paris.lutece.plugins.search.solr.business.field.FieldHome;
39  import fr.paris.lutece.plugins.search.solr.business.field.SolrFieldManager;
40  import fr.paris.lutece.portal.service.message.AdminMessage;
41  import fr.paris.lutece.portal.service.message.AdminMessageService;
42  import fr.paris.lutece.portal.service.template.AppTemplateService;
43  import fr.paris.lutece.portal.service.util.AppPathService;
44  import fr.paris.lutece.portal.web.admin.PluginAdminPageJspBean;
45  import fr.paris.lutece.util.html.HtmlTemplate;
46  
47  import java.util.HashMap;
48  
49  import javax.servlet.http.HttpServletRequest;
50  
51  import org.apache.commons.lang.StringUtils;
52  
53  /**
54   *
55   * SolrIndexerJspBean
56   *
57   */
58  public class SolrConfigurationJspBean extends PluginAdminPageJspBean
59  {
60      private static final long serialVersionUID = 8010702541285203732L;
61      ////////////////////////////////////////////////////////////////////////////
62      // Constantes
63      public static final String RIGHT_CONFIGURATION = "SOLR_CONFIGURATION_MANAGEMENT";
64      private static final String TEMPLATE_MANAGE_FACET_CONFIGURATION = "admin/search/solr_manage_search_configuration.html";
65      private static final String TEMPLATE_MANAGE_SORT_CONFIGURATION = "admin/search/solr_manage_sort_configuration.html";
66      private static final String TEMPLATE_MANAGE_INTERSECTION_CONFIGURATION = "admin/search/solr_manage_intersection_configuration.html";
67      private static final String MARK_FIELD = "field_list";
68      private static final String MARK_INTERSECTION = "intersections";
69      private static final String MESSAGE_VALID = "search.solr.adminFeature.configuration.valid";
70      private static final String PARAMETER_DEFAULT_SORT = "default_sort";
71  
72      /**
73       * Displays the indexing parameters
74       *
75       * @param request
76       *            the http request
77       * @return the html code which displays the parameters page
78       */
79      private String getFacet( )
80      {
81          HashMap<String, Object> model = new HashMap<>( );
82          model.put( MARK_FIELD, SolrFieldManager.getFieldList( ) );
83  
84          HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_FACET_CONFIGURATION, getLocale( ), model );
85  
86          return template.getHtml( );
87      }
88  
89      private String getSort( )
90      {
91          HashMap<String, Object> model = new HashMap<>( );
92          model.put( MARK_FIELD, SolrFieldManager.getFieldList( ) );
93  
94          HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_SORT_CONFIGURATION, getLocale( ), model );
95  
96          return template.getHtml( );
97      }
98  
99      private String getIntersection( )
100     {
101         HashMap<String, Object> model = new HashMap<>( );
102         model.put( MARK_FIELD, SolrFieldManager.getFacetList( ).values( ) );
103 
104         // load facet intersection
105         model.put( MARK_INTERSECTION, FacetIntersectionHome.getFacetIntersectionsList( ) );
106 
107         HtmlTemplate template = AppTemplateService.getTemplate( TEMPLATE_MANAGE_INTERSECTION_CONFIGURATION, getLocale( ), model );
108 
109         return template.getHtml( );
110     }
111 
112     public String getPage( HttpServletRequest request )
113     {
114         StringBuilder htmlBuffer = new StringBuilder( );
115         htmlBuffer.append( getFacet( ) );
116         htmlBuffer.append( getSort( ) );
117         htmlBuffer.append( getIntersection( ) );
118 
119         return getAdminPage( htmlBuffer.toString( ) );
120     }
121 
122     // Traitement
123     public String doFacet( HttpServletRequest request )
124     {
125         for ( Field field : SolrFieldManager.getFieldList( ) )
126         {
127             field.setEnableFacet( request.getParameter( Integer.toString( field.getIdField( ) ) ) != null );
128             FieldHome.update( field );
129         }
130 
131         return this.validMessage( request );
132     }
133 
134     public String doSort( HttpServletRequest request )
135     {
136         String strDefaultSort = request.getParameter( PARAMETER_DEFAULT_SORT );
137         for ( Field field : SolrFieldManager.getFieldList( ) )
138         {
139             field.setEnableSort( request.getParameter( Integer.toString( field.getIdField( ) ) ) != null );
140             if ( StringUtils.isNotBlank( strDefaultSort ) && StringUtils.isNumeric( strDefaultSort ) )
141             {
142                 field.setDefaultSort( Integer.parseInt( strDefaultSort ) == field.getIdField( ) );
143             }
144 
145             FieldHome.update( field );
146         }
147 
148         return this.validMessage( request );
149     }
150 
151     public String doIntersect( HttpServletRequest request )
152     {
153         // CREATE
154         if ( request.getParameter( "add" ) != null )
155         {
156             FacetIntersectionHome.create( Integer.parseInt( request.getParameter( "field1" ) ), Integer.parseInt( request.getParameter( "field2" ) ) );
157         }
158 
159         // DELETE
160         else
161             if ( request.getParameter( "delete" ) != null )
162             {
163                 FacetIntersectionHome.remove( Integer.parseInt( request.getParameter( "field1" ) ), Integer.parseInt( request.getParameter( "field2" ) ) );
164             }
165 
166         SolrFieldManager.reloadIntersection( );
167 
168         return AppPathService.getBaseUrl( request ) + "jsp/admin/search/solr/ManageSearchConfiguration.jsp?plugin_name=solr";
169     }
170 
171     private String validMessage( HttpServletRequest request )
172     {
173         return AdminMessageService.getMessageUrl( request, MESSAGE_VALID, AdminMessage.TYPE_INFO );
174     }
175 }