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.panel.display.factory;
35  
36  import java.util.ArrayList;
37  import java.util.Collections;
38  import java.util.List;
39  
40  import javax.servlet.http.HttpServletRequest;
41  
42  import org.apache.commons.collections.CollectionUtils;
43  import org.apache.commons.lang3.StringUtils;
44  import org.apache.commons.lang3.math.NumberUtils;
45  
46  import fr.paris.lutece.plugins.directory.modules.multiview.business.record.panel.IRecordPanel;
47  import fr.paris.lutece.plugins.directory.modules.multiview.business.record.panel.configuration.RecordPanelConfiguration;
48  import fr.paris.lutece.plugins.directory.modules.multiview.business.record.panel.initializer.IRecordPanelInitializer;
49  import fr.paris.lutece.plugins.directory.modules.multiview.util.DirectoryMultiviewConstants;
50  import fr.paris.lutece.plugins.directory.modules.multiview.web.record.panel.display.IRecordPanelDisplay;
51  import fr.paris.lutece.plugins.directory.modules.multiview.web.record.panel.display.initializer.IRecordPanelDisplayInitializer;
52  import fr.paris.lutece.plugins.directory.modules.multiview.web.record.panel.display.initializer.factory.IRecordPanelDisplayInitializerFactory;
53  import fr.paris.lutece.plugins.directory.modules.multiview.web.record.panel.display.initializer.factory.RecordDisplayInitializerFactoryFacade;
54  import fr.paris.lutece.plugins.directory.modules.multiview.web.record.util.RecordListPositionComparator;
55  
56  /**
57   * Factory for RecordPanel objects
58   */
59  public class RecordPanelDisplayFactory
60  {
61      // Constants
62      private static final int INDEX_LIST_PANEL_FIRST_POSITION = NumberUtils.INTEGER_ZERO;
63      private static final boolean ACTIVE_LIST_PANEL = Boolean.TRUE;
64  
65      /**
66       * Create the list of all RecordPanelDisplay ordered by their position
67       * 
68       * @param request
69       *            The HttpServletRequest to retrieve the information from
70       * @param listRecordPanel
71       *            The list of all IRecordPanel used for building IRecordFilterDisplay
72       * @return the list of all RecordPanelDisplay ordered by their position
73       */
74      public List<IRecordPanelDisplay> createRecordPanelDisplayList( HttpServletRequest request, List<IRecordPanel> listRecordPanel )
75      {
76          List<IRecordPanelDisplay> listRecordPanelDisplay = new ArrayList<>( );
77          List<IRecordPanelDisplayFactory> listRecordPanelDisplayFactory = new RecordPanelDisplayFactoryFacade( ).buildRecordPanelDisplayFactoryList( );
78  
79          if ( !CollectionUtils.isEmpty( listRecordPanelDisplayFactory ) )
80          {
81              IRecordPanelDisplay recordPanelDisplay = null;
82              for ( IRecordPanel recordPanel : listRecordPanel )
83              {
84                  for ( IRecordPanelDisplayFactory recordPanelDisplayFactory : listRecordPanelDisplayFactory )
85                  {
86                      recordPanelDisplay = recordPanelDisplayFactory.buildRecordPanelDisplay( recordPanel );
87                      if ( recordPanelDisplay != null )
88                      {
89                          configureRecordPanelDisplay( request, recordPanelDisplay );
90  
91                          listRecordPanelDisplay.add( recordPanelDisplay );
92                          break;
93                      }
94                  }
95              }
96  
97              // Sort the list by the position of each elements
98              Collections.sort( listRecordPanelDisplay, new RecordListPositionComparator( ) );
99  
100             // Manage the active RecordDisplayPanel of the list
101             manageActiveRecordPanelDisplay( listRecordPanelDisplay );
102         }
103 
104         return listRecordPanelDisplay;
105     }
106 
107     /**
108      * Configure the RecordPanelDisplay by defining if its active or not and build all the RecordPanelInitializer with their RecordParameters from its
109      * RecordPanel
110      * 
111      * @param request
112      *            The request to retrieve the information from
113      * @param recordPanelDisplay
114      *            The recordPanelInitializer to configure with the information from the request
115      */
116     private void configureRecordPanelDisplay( HttpServletRequest request, IRecordPanelDisplay recordPanelDisplay )
117     {
118         boolean bIsSelectedPanel = isSelectedPanel( request, recordPanelDisplay.getTechnicalCode( ) );
119         recordPanelDisplay.setActive( bIsSelectedPanel );
120 
121         buildRecordPanelDisplayInitializer( request, recordPanelDisplay.getRecordPanel( ) );
122     }
123 
124     /**
125      * Check if the panel is the selected panel or not. Activate the default panel if not found
126      * 
127      * @param request
128      *            The HttpServletRequest to retrieve the information from
129      * @param strPanelTechnicalCode
130      *            The name of the panel to analyze
131      * @return true if the panel of the given name is the panel to analyze false otherwise
132      */
133     private boolean isSelectedPanel( HttpServletRequest request, String strPanelTechnicalCode )
134     {
135         boolean bIsSelectedPanel = Boolean.FALSE;
136 
137         // We will retrieve the name of the current selected panel
138         String strRecordPanelSelected = request.getParameter( DirectoryMultiviewConstants.PARAMETER_SELECTED_PANEL );
139         if ( StringUtils.isNotBlank( strRecordPanelSelected ) )
140         {
141             bIsSelectedPanel = strPanelTechnicalCode.equals( strRecordPanelSelected );
142         }
143         else
144         {
145             // If there is no selected panel we will see if there was a previous selected one
146             String strPreviousRecordPanelSelected = request.getParameter( DirectoryMultiviewConstants.PARAMETER_CURRENT_SELECTED_PANEL );
147             if ( StringUtils.isNotBlank( strPreviousRecordPanelSelected ) )
148             {
149                 bIsSelectedPanel = strPanelTechnicalCode.equals( strPreviousRecordPanelSelected );
150             }
151         }
152 
153         return bIsSelectedPanel;
154     }
155 
156     /**
157      * Build the list of all RecordPanelDisplayInitializer of all RecordPanelInitializer of the RecordPänel
158      * 
159      * @param request
160      *            The HttpServletRequest used to build the list of all RecordPanelDisplayInitializer
161      * @param recordPanel
162      *            The IRecordPanel to the RecordPanelInitializer from
163      */
164     public void buildRecordPanelDisplayInitializer( HttpServletRequest request, IRecordPanel recordPanel )
165     {
166         List<IRecordPanelDisplayInitializerFactory> listRecordPanelDisplayInitializerFactory = new RecordDisplayInitializerFactoryFacade( )
167                 .buildRecordPanelDisplayInitializerList( );
168 
169         if ( !CollectionUtils.isEmpty( listRecordPanelDisplayInitializerFactory ) && recordPanel != null )
170         {
171             RecordPanelConfiguration recordPanelConfiguration = recordPanel.getRecordPanelConfiguration( );
172 
173             if ( recordPanelConfiguration != null )
174             {
175                 for ( IRecordPanelDisplayInitializerFactory recordPanelDisplayInitializerFactory : listRecordPanelDisplayInitializerFactory )
176                 {
177                     List<IRecordPanelInitializer> listRecordPanelInitializer = recordPanelConfiguration.getListRecordPanelInitializer( );
178 
179                     buildPanelDisplayInitializerRecordParameters( request, recordPanelDisplayInitializerFactory, listRecordPanelInitializer );
180                 }
181             }
182         }
183     }
184 
185     /**
186      * Build the RecordPanelDisplayInitializer associated to the given IRecordPanelDisplayInitializerFactory from the specified list of IRecordPanelInitializer
187      * and build its RecordParameters with the request
188      * 
189      * @param request
190      *            The request used to build the RecordParameters of the RecordPanelInitializer
191      * @param recordPanelDisplayInitializerFactory
192      *            The IRecordPanelDisplayInitializerFactory used to build the IRecordPanelDisplayInitializer
193      * @param listRecordPanelInitializer
194      *            The list of all RecordPanelInitializer to retrieve those which is associated to the given IRecordPanelDisplayInitializerFactory
195      */
196     private void buildPanelDisplayInitializerRecordParameters( HttpServletRequest request,
197             IRecordPanelDisplayInitializerFactory recordPanelDisplayInitializerFactory, List<IRecordPanelInitializer> listRecordPanelInitializer )
198     {
199         if ( recordPanelDisplayInitializerFactory != null && !CollectionUtils.isEmpty( listRecordPanelInitializer ) )
200         {
201             for ( IRecordPanelInitializer recordPanelInitializer : listRecordPanelInitializer )
202             {
203                 IRecordPanelDisplayInitializer recordPanelDisplayInitializer = recordPanelDisplayInitializerFactory
204                         .buildRecordPanelDisplay( recordPanelInitializer );
205                 if ( recordPanelDisplayInitializer != null )
206                 {
207                     recordPanelDisplayInitializer.setRecordPanelInitializer( recordPanelInitializer );
208                     recordPanelDisplayInitializer.buildRecordParameters( request );
209                 }
210             }
211         }
212     }
213 
214     /**
215      * Check if there is an active RecordDisplay Panel in the given list and if there is no one set the first element in the list (which is in first position)
216      * as active
217      * 
218      * @param listRecordPanelDisplay
219      *            The list of RecordDisplayPanel to analyze
220      */
221     private void manageActiveRecordPanelDisplay( List<IRecordPanelDisplay> listRecordPanelDisplay )
222     {
223         boolean bActivePanelPresent = Boolean.FALSE;
224 
225         for ( IRecordPanelDisplay recordPanelDisplaySorted : listRecordPanelDisplay )
226         {
227             if ( recordPanelDisplaySorted.isActive( ) )
228             {
229                 bActivePanelPresent = Boolean.TRUE;
230                 break;
231             }
232         }
233 
234         if ( !bActivePanelPresent )
235         {
236             listRecordPanelDisplay.get( INDEX_LIST_PANEL_FIRST_POSITION ).setActive( ACTIVE_LIST_PANEL );
237         }
238     }
239 }