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.business.record;
35  
36  import java.io.Serializable;
37  import java.util.Comparator;
38  import java.util.List;
39  
40  import org.apache.commons.lang3.math.NumberUtils;
41  
42  import fr.paris.lutece.plugins.directory.modules.multiview.business.record.column.RecordColumnCell;
43  import fr.paris.lutece.plugins.directory.modules.multiview.business.record.column.RecordColumnCellComparator;
44  import fr.paris.lutece.portal.service.spring.SpringContextService;
45  
46  /**
47   * Comparator for DirectoryRecordItam object. The comparison is based on the value of objects with the given key
48   */
49  public class DirectoryRecordItemComparator implements Comparator<DirectoryRecordItem>, Serializable
50  {
51      /**
52       * Generated UID
53       */
54      private static final long serialVersionUID = -8569813504412874604L;
55  
56      // Constants
57      private static final int SORT_ASCENDANT_DIRECTION = NumberUtils.INTEGER_ONE;
58      private static final int SORT_DESCENDANT_DIRECTION = NumberUtils.INTEGER_MINUS_ONE;
59      private static final String DEFAULT_CONFIGURATION_BEAN_NAME = "directory-multiview.directoryRecordItem.comparator.configuration.default";
60  
61      // Variables
62      private final String _strSortAttributeName;
63      private final int _nCellPosition;
64      private final int _nSortDirection;
65  
66      /**
67       * Constructor
68       * 
69       * @param directoryRecordItemComparatorConfig
70       *            The DirectoryRecordItemComparatorConfig to use for sort the DirectoryRecordItem
71       */
72      public DirectoryRecordItemComparator( DirectoryRecordItemComparatorConfig directoryRecordItemComparatorConfig )
73      {
74          if ( directoryRecordItemComparatorConfig != null && directoryRecordItemComparatorConfig.getColumnToSortPosition( ) != NumberUtils.INTEGER_MINUS_ONE )
75          {
76              _strSortAttributeName = directoryRecordItemComparatorConfig.getSortAttributeName( );
77              _nCellPosition = directoryRecordItemComparatorConfig.getColumnToSortPosition( );
78              _nSortDirection = computeSortDirection( directoryRecordItemComparatorConfig.isAscSort( ) );
79          }
80          else
81          {
82              DirectoryRecordItemComparatorConfig directoryRecordItemComparatorConfigDefault = SpringContextService.getBean( DEFAULT_CONFIGURATION_BEAN_NAME );
83              _strSortAttributeName = directoryRecordItemComparatorConfigDefault.getSortAttributeName( );
84              _nCellPosition = directoryRecordItemComparatorConfigDefault.getColumnToSortPosition( );
85              _nSortDirection = computeSortDirection( directoryRecordItemComparatorConfigDefault.isAscSort( ) );
86          }
87      }
88  
89      /**
90       * Constructor
91       * 
92       * @param directoryRecordItemComparatorConfig
93       *            The DirectoryRecordItemComparatorConfig to use for sort the DirectoryRecordItem
94       * @param directoryRecordItemComparatorConfigDefault
95       *            The DirectoryRecordItemComparatorConfig to use as default configuration if the given DirectoryRecordItemComparatorConfig doesn't have all the
96       *            necessaries information
97       */
98      public DirectoryRecordItemComparator( DirectoryRecordItemComparatorConfig directoryRecordItemComparatorConfig,
99              DirectoryRecordItemComparatorConfig directoryRecordItemComparatorConfigDefault )
100     {
101         if ( directoryRecordItemComparatorConfig != null && directoryRecordItemComparatorConfig.getColumnToSortPosition( ) != NumberUtils.INTEGER_MINUS_ONE )
102         {
103             _strSortAttributeName = directoryRecordItemComparatorConfig.getSortAttributeName( );
104             _nCellPosition = directoryRecordItemComparatorConfig.getColumnToSortPosition( );
105             _nSortDirection = computeSortDirection( directoryRecordItemComparatorConfig.isAscSort( ) );
106         }
107         else
108         {
109             _strSortAttributeName = directoryRecordItemComparatorConfigDefault.getSortAttributeName( );
110             _nCellPosition = directoryRecordItemComparatorConfigDefault.getColumnToSortPosition( );
111             _nSortDirection = computeSortDirection( directoryRecordItemComparatorConfigDefault.isAscSort( ) );
112         }
113     }
114 
115     /**
116      * Compute the sort direction modifier from the given sort direction
117      * 
118      * @param bSortAsc
119      *            The boolean which tell if we use the ascendant or descendant sort
120      * @return 1 for the ascendant sort (which is the default) or -1 for the descendant sort
121      */
122     private int computeSortDirection( boolean bSortAsc )
123     {
124         int nSortDirection = SORT_ASCENDANT_DIRECTION;
125 
126         if ( !bSortAsc )
127         {
128             nSortDirection = SORT_DESCENDANT_DIRECTION;
129         }
130 
131         return nSortDirection;
132     }
133 
134     /**
135      * {@inheritDoc}
136      */
137     @Override
138     public int compare( DirectoryRecordItem directoryRecordItemOne, DirectoryRecordItem directoryRecordItemTwo )
139     {
140         int nComparisonResult = NumberUtils.INTEGER_ZERO;
141 
142         if ( directoryRecordItemOne == null )
143         {
144             if ( directoryRecordItemTwo != null )
145             {
146                 nComparisonResult = NumberUtils.INTEGER_MINUS_ONE;
147             }
148         }
149         else
150         {
151             if ( directoryRecordItemTwo == null )
152             {
153                 nComparisonResult = NumberUtils.INTEGER_ONE;
154             }
155             else
156             {
157                 List<RecordColumnCell> listRecordColumnCellOne = directoryRecordItemOne.getDirectoryRecordCellValues( );
158                 List<RecordColumnCell> listRecordColumnCellTwo = directoryRecordItemTwo.getDirectoryRecordCellValues( );
159 
160                 nComparisonResult = compareRecordColumnCellList( listRecordColumnCellOne, listRecordColumnCellTwo );
161             }
162         }
163 
164         return ( _nSortDirection * nComparisonResult );
165     }
166 
167     /**
168      * Compare the RecordColumnCell list with the specified key
169      * 
170      * @param listRecordColumnCellOne
171      *            The first list of RecordColumnCell to compare
172      * @param listRecordColumnCellTwo
173      *            The second list of RecordColumnCell to compare
174      * @return the result of the comparison
175      */
176     private int compareRecordColumnCellList( List<RecordColumnCell> listRecordColumnCellOne, List<RecordColumnCell> listRecordColumnCellTwo )
177     {
178         int nRecordColumnComparisonResult = NumberUtils.INTEGER_ZERO;
179 
180         if ( listRecordColumnCellOne == null )
181         {
182             if ( listRecordColumnCellTwo != null )
183             {
184                 nRecordColumnComparisonResult = NumberUtils.INTEGER_MINUS_ONE;
185             }
186         }
187         else
188         {
189             if ( listRecordColumnCellTwo == null )
190             {
191                 nRecordColumnComparisonResult = NumberUtils.INTEGER_ONE;
192             }
193             else
194             {
195                 int nlistRecordColumnCellOneSize = listRecordColumnCellOne.size( );
196                 int nlistRecordColumnCellTwoSize = listRecordColumnCellTwo.size( );
197                 int nRecordColumnCellPosition = _nCellPosition - 1;
198 
199                 if ( nlistRecordColumnCellOneSize == nlistRecordColumnCellTwoSize && nlistRecordColumnCellTwoSize > nRecordColumnCellPosition
200                         && nRecordColumnCellPosition > -1 )
201                 {
202                     RecordColumnCell recordColumnCellOne = listRecordColumnCellOne.get( nRecordColumnCellPosition );
203                     RecordColumnCell recordColumnCellTwo = listRecordColumnCellTwo.get( nRecordColumnCellPosition );
204 
205                     RecordColumnCellComparator recordColumnCellComparator = new RecordColumnCellComparator( _strSortAttributeName );
206                     nRecordColumnComparisonResult = recordColumnCellComparator.compare( recordColumnCellOne, recordColumnCellTwo );
207                 }
208             }
209         }
210 
211         return nRecordColumnComparisonResult;
212     }
213 }