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.util;
35  
36  import java.util.ArrayList;
37  import java.util.LinkedHashMap;
38  import java.util.List;
39  import java.util.Locale;
40  import java.util.Map;
41  
42  import org.apache.commons.collections.CollectionUtils;
43  import org.apache.commons.lang3.StringUtils;
44  
45  import fr.paris.lutece.plugins.directory.modules.multiview.business.record.DirectoryRecordItem;
46  import fr.paris.lutece.plugins.directory.modules.multiview.business.record.column.RecordColumnCell;
47  import fr.paris.lutece.plugins.directory.modules.multiview.web.record.column.display.IRecordColumnDisplay;
48  import fr.paris.lutece.portal.service.template.AppTemplateService;
49  
50  /**
51   * Builder class for the template of RecordColumnDisplay and RecordFilterDisplay objects
52   */
53  public final class RecordListTemplateBuilder
54  {
55      // Templates
56      private static final String TEMPLATE_MULTI_DIRECTORY_TABLE = "admin/plugins/directory/modules/multiview/includes/include_manage_multi_record_directory_table.html";
57  
58      // Marks
59      private static final String MARK_DIRECTORY_RECORD_ITEM_LIST = "directory_record_item_list";
60      private static final String MARK_RECORD_COLUMN_HEADER_TEMPLATE_LIST = "record_column_header_template_list";
61      private static final String MARK_RECORD_LINE_TEMPLATE_LIST = "record_line_template_list";
62      private static final String MARK_RECORD_DETAILS_REDIRECT_BASE_URL = "redirect_details_base_url";
63  
64      /**
65       * Constructor
66       */
67      private RecordListTemplateBuilder( )
68      {
69  
70      }
71  
72      /**
73       * Build the template of the table of all records
74       * 
75       * @param listRecordColumnDisplay
76       *            The list of all RecordColumnDisplay objects to build the global template of the record columns from
77       * @param listDirectoryRecordItem
78       *            The list of all DirectoryItem used to build the tab with all records
79       * @param locale
80       *            The locale to used for build template
81       * @param strRedirectionDetailsBaseUrl
82       *            The base url to use for the redirection on the details page
83       * @param strSortUrl
84       *            The url to use for sort a column
85       * @return the global template of all RecordColumnDisplay objects
86       */
87      public static String buildTableTemplate( List<IRecordColumnDisplay> listRecordColumnDisplay, List<DirectoryRecordItem> listDirectoryRecordItem,
88              Locale locale, String strRedirectionDetailsBaseUrl, String strSortUrl )
89      {
90          String strTableTemplate = StringUtils.EMPTY;
91  
92          if ( !CollectionUtils.isEmpty( listRecordColumnDisplay ) && !CollectionUtils.isEmpty( listDirectoryRecordItem ) )
93          {
94              // Build the list of all record column header template
95              List<String> listRecordColumnHeaderTemplate = buildRecordColumnHeaderTemplateList( listRecordColumnDisplay, locale, strSortUrl );
96  
97              // Build the list of all RecordColumnLineTemplate
98              List<RecordColumnLineTemplate> listRecordColumnLineTemplate = buildRecordColumnLineTemplateList( listRecordColumnDisplay, listDirectoryRecordItem,
99                      locale );
100 
101             // Build the model
102             Map<String, Object> model = new LinkedHashMap<>( );
103             model.put( MARK_RECORD_COLUMN_HEADER_TEMPLATE_LIST, listRecordColumnHeaderTemplate );
104             model.put( MARK_DIRECTORY_RECORD_ITEM_LIST, listDirectoryRecordItem );
105             model.put( MARK_RECORD_LINE_TEMPLATE_LIST, listRecordColumnLineTemplate );
106             model.put( MARK_RECORD_DETAILS_REDIRECT_BASE_URL, strRedirectionDetailsBaseUrl );
107 
108             strTableTemplate = AppTemplateService.getTemplate( TEMPLATE_MULTI_DIRECTORY_TABLE, locale, model ).getHtml( );
109         }
110 
111         return strTableTemplate;
112     }
113 
114     /**
115      * Build the list of all record column header template
116      * 
117      * @param listRecordColumnDisplay
118      *            The list of all record column display to retrieve the header template from
119      * @param locale
120      *            The locale to use for build the template
121      * @param strSortUrl
122      *            The url to use for sort a column (can be null)
123      * @return the list of all record column header template
124      */
125     private static List<String> buildRecordColumnHeaderTemplateList( List<IRecordColumnDisplay> listRecordColumnDisplay, Locale locale, String strSortUrl )
126     {
127         List<String> listRecordColumnHeaderTemplate = new ArrayList<>( );
128 
129         if ( listRecordColumnDisplay != null && !listRecordColumnDisplay.isEmpty( ) )
130         {
131             for ( IRecordColumnDisplay recordColumnDisplay : listRecordColumnDisplay )
132             {
133                 String strRecordColumnDisplayHeaderTemplate = recordColumnDisplay.buildRecordColumnHeaderTemplate( strSortUrl, locale );
134                 listRecordColumnHeaderTemplate.add( strRecordColumnDisplayHeaderTemplate );
135             }
136         }
137 
138         return listRecordColumnHeaderTemplate;
139     }
140 
141     /**
142      * Build the list of all RecordColumnLineTemplate
143      * 
144      * @param listRecordColumnDisplay
145      *            The list of record column display to use for build the map of line template
146      * @param listDirectoryRecordItem
147      *            The list of record item to use for build the map of line template
148      * @param locale
149      *            The locale to use for build the template
150      * @return the list of all RecordColumnLineTemplate
151      */
152     private static List<RecordColumnLineTemplate> buildRecordColumnLineTemplateList( List<IRecordColumnDisplay> listRecordColumnDisplay,
153             List<DirectoryRecordItem> listDirectoryRecordItem, Locale locale )
154     {
155         List<RecordColumnLineTemplate> listRecordColumnLineTemplate = new ArrayList<>( );
156 
157         if ( listRecordColumnDisplay != null && !listRecordColumnDisplay.isEmpty( ) && listDirectoryRecordItem != null && !listDirectoryRecordItem.isEmpty( ) )
158         {
159             for ( DirectoryRecordItem directoryRecordItem : listDirectoryRecordItem )
160             {
161                 int nIdRecord = directoryRecordItem.getIdRecord( );
162                 RecordColumnLineTemplate recordColumnLineTemplate = new RecordColumnLineTemplate( nIdRecord );
163 
164                 List<RecordColumnCell> listRecordColumnCell = directoryRecordItem.getDirectoryRecordCellValues( );
165                 populateLineTemplateFromCellValues( recordColumnLineTemplate, listRecordColumnCell, listRecordColumnDisplay, locale );
166 
167                 listRecordColumnLineTemplate.add( recordColumnLineTemplate );
168             }
169         }
170 
171         return listRecordColumnLineTemplate;
172     }
173 
174     /**
175      * Populate the given RecordColumnLineTemplate with the value from the list of RecordcolumnCell and the display from the given list of IRecordColumnDisplay
176      * 
177      * @param recordColumnLineTemplate
178      *            The RecordColumnLineTemplate to populate
179      * @param listRecordColumnCell
180      *            The list of RecordColumnCell to retrieve the values from
181      * @param listRecordColumnDisplay
182      *            The list of IRecordColumnDisplayto use for the column
183      * @param locale
184      *            The locale to use for build the templates
185      */
186     private static void populateLineTemplateFromCellValues( RecordColumnLineTemplate recordColumnLineTemplate, List<RecordColumnCell> listRecordColumnCell,
187             List<IRecordColumnDisplay> listRecordColumnDisplay, Locale locale )
188     {
189         if ( listRecordColumnCell != null && !listRecordColumnCell.isEmpty( ) )
190         {
191             for ( int index = 0; index < listRecordColumnCell.size( ); index++ )
192             {
193                 // We will increment the index to retrieve the record column display because the first column is at position 1
194                 int nColumnDisplayPosition = index + 1;
195                 IRecordColumnDisplay recordColumnDisplay = findRecordColumnDisplayByPosition( nColumnDisplayPosition, listRecordColumnDisplay );
196                 if ( recordColumnDisplay != null )
197                 {
198                     RecordColumnCell recordColumnCell = listRecordColumnCell.get( index );
199                     String strRecordColunmCellTemplate = recordColumnDisplay.buildRecordColumnCellTemplate( recordColumnCell, locale );
200                     recordColumnLineTemplate.addRecordColumnCellTemplate( strRecordColunmCellTemplate );
201                 }
202             }
203         }
204     }
205 
206     /**
207      * Find the RecordColumnDisplay in the given list with the specified position or null if not found
208      * 
209      * @param nRecordColumnPosition
210      *            The position of the RecordColumnDisplay to retrieve
211      * @param listRecordColumnDisplay
212      *            The list of RecordColumnDisplay from where to find the column with the specified position
213      * @return the RecordColumnDisplay with the specified position nor null if not found
214      */
215     private static IRecordColumnDisplay findRecordColumnDisplayByPosition( int nRecordColumnPosition, List<IRecordColumnDisplay> listRecordColumnDisplay )
216     {
217         IRecordColumnDisplay recordColumnDisplayResult = null;
218 
219         if ( listRecordColumnDisplay != null && !listRecordColumnDisplay.isEmpty( ) )
220         {
221             for ( IRecordColumnDisplay recordColumnDisplay : listRecordColumnDisplay )
222             {
223                 if ( recordColumnDisplay.getPosition( ) == nRecordColumnPosition )
224                 {
225                     recordColumnDisplayResult = recordColumnDisplay;
226                     break;
227                 }
228             }
229         }
230 
231         return recordColumnDisplayResult;
232     }
233 }