View Javadoc
1   /*
2    * Copyright (c) 2002-2018, Mairie de 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.directory.modules.multiview.web.record.filter.display.impl;
35  
36  import java.util.LinkedHashMap;
37  import java.util.Map;
38  
39  import javax.servlet.http.HttpServletRequest;
40  
41  import org.apache.commons.lang3.StringUtils;
42  
43  import fr.paris.lutece.plugins.directory.modules.multiview.business.record.RecordParameters;
44  import fr.paris.lutece.plugins.directory.modules.multiview.business.record.filter.IRecordFilter;
45  import fr.paris.lutece.plugins.directory.modules.multiview.web.record.filter.display.IRecordFilterDisplay;
46  import fr.paris.lutece.portal.service.template.AppTemplateService;
47  import fr.paris.lutece.util.ReferenceList;
48  import fr.paris.lutece.util.html.HtmlTemplate;
49  
50  /**
51   * Abstract class for the RecordFilterDisplay objects
52   */
53  public abstract class AbstractRecordFilterDisplay implements IRecordFilterDisplay
54  {
55      // Template
56      protected static final String FILTER_TEMPLATE_NAME = "admin/plugins/directory/modules/multiview/record_filter.html";
57  
58      // Marks
59      protected static final String MARK_FILTER_LIST = "filter_list";
60      protected static final String MARK_FILTER_NAME = "filter_name";
61      protected static final String MARK_FILTER_LIST_VALUE = "filter_list_value";
62  
63      // Constants
64      private static final String DEFAULT_RECORD_FILTER_LABEL = "-";
65  
66      // Variables
67      private int _nPosition;
68      private String _strValue = StringUtils.EMPTY;
69      private String _strTemplate = StringUtils.EMPTY;
70      private IRecordFilter _recordFilter;
71  
72      /**
73       * {@inheritDoc}
74       */
75      @Override
76      public String getValue( )
77      {
78          return _strValue;
79      }
80  
81      /**
82       * Set the value for the RecordFilterDisplay
83       * 
84       * @param strValue
85       *            The value to set to the RecordFilterDisplay
86       */
87      protected void setValue( String strValue )
88      {
89          _strValue = strValue;
90      }
91  
92      /**
93       * {@inheritDoc}
94       */
95      @Override
96      public int getPosition( )
97      {
98          return _nPosition;
99      }
100 
101     /**
102      * {@inheritDoc}
103      */
104     @Override
105     public void setPosition( int nPosition )
106     {
107         _nPosition = nPosition;
108     }
109 
110     /**
111      * {@inheritDoc}
112      */
113     @Override
114     public String getTemplate( )
115     {
116         return _strTemplate;
117     }
118 
119     /**
120      * Set the template
121      * 
122      * @param strTemplate
123      *            The template to setto the filter
124      */
125     protected void setTemplate( String strTemplate )
126     {
127         _strTemplate = strTemplate;
128     }
129 
130     /**
131      * {@inheritDoc}
132      */
133     @Override
134     public IRecordFilter getRecordFilter( )
135     {
136         return _recordFilter;
137     }
138 
139     /**
140      * {@inheritDoc}
141      */
142     @Override
143     public void setRecordFilter( IRecordFilter recordFilter )
144     {
145         _recordFilter = recordFilter;
146     }
147 
148     /**
149      * {@inheritDoc}
150      */
151     @Override
152     public abstract void buildTemplate( HttpServletRequest request );
153 
154     /**
155      * Get the map of all parameter names and values used by the filter
156      * 
157      * @param request
158      *            The request used to retrieve the informations of the filter
159      * @return the map which contains all the parameter names and values of the filter
160      */
161     protected abstract Map<String, Object> getFilterDisplayMapValues( HttpServletRequest request );
162 
163     /**
164      * {@inheritDoc}
165      */
166     @Override
167     public RecordParameters createRecordParameters( HttpServletRequest request )
168     {
169         RecordParameters recordParameters = null;
170 
171         if ( _recordFilter != null )
172         {
173             recordParameters = new RecordParameters( );
174 
175             Map<String, Object> mapKeyNameValues = getFilterDisplayMapValues( request );
176             recordParameters.setRecordParametersMap( mapKeyNameValues );
177 
178             _recordFilter.setRecordParameters( recordParameters );
179         }
180 
181         return recordParameters;
182     }
183 
184     /**
185      * Build the filter template with the given list for the specified parameter and set it to the filter
186      * 
187      * @param request
188      *            The HttpServletRequest to use for building the template
189      * @param referenceList
190      *            The ReferenceList to use to populate the filter template
191      * @param strParameterName
192      *            The name of the parameter to attached the value of the ReferenceList
193      */
194     protected void manageFilterTemplate( HttpServletRequest request, ReferenceList referenceList, String strParameterName )
195     {
196         String strTemplateResult = StringUtils.EMPTY;
197 
198         Map<String, Object> model = new LinkedHashMap<>( );
199         model.put( MARK_FILTER_LIST, referenceList );
200         model.put( MARK_FILTER_LIST_VALUE, getValue( ) );
201         model.put( MARK_FILTER_NAME, strParameterName );
202 
203         HtmlTemplate htmlTemplate = AppTemplateService.getTemplate( FILTER_TEMPLATE_NAME, request.getLocale( ), model );
204         if ( htmlTemplate != null )
205         {
206             strTemplateResult = htmlTemplate.getHtml( );
207         }
208 
209         setTemplate( strTemplateResult );
210     }
211 
212     /**
213      * Return the label of the RecordFilter from its configuration
214      * 
215      * @return the label of the RecordFilter from its configuration or the default label if not found
216      */
217     protected String getRecordFilterDisplayLabel( )
218     {
219         String strRecordFilterDisplayLabel = DEFAULT_RECORD_FILTER_LABEL;
220 
221         IRecordFilter recordFilter = getRecordFilter( );
222         if ( recordFilter != null && recordFilter.getRecordFilterConfiguration( ) != null )
223         {
224             strRecordFilterDisplayLabel = recordFilter.getRecordFilterConfiguration( ).getRecordFilterLabel( );
225         }
226 
227         return strRecordFilterDisplayLabel;
228     }
229 }