View Javadoc
1   /*
2    * Copyright (c) 2002-2017, 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.utils.sort;
35  
36  import fr.paris.lutece.plugins.directory.business.EntryTypeGeolocation;
37  import fr.paris.lutece.plugins.directory.business.IEntry;
38  import fr.paris.lutece.plugins.directory.business.Record;
39  import fr.paris.lutece.plugins.directory.business.RecordField;
40  import fr.paris.lutece.portal.service.util.AppLogService;
41  import fr.paris.lutece.util.sort.AttributeComparator;
42  
43  import java.util.ArrayList;
44  import java.util.Collections;
45  import java.util.Comparator;
46  import java.util.Date;
47  import java.util.List;
48  
49  /**
50   *
51   * RecordComparator
52   *
53   */
54  public class RecordComparator implements Comparator<Record>
55  {
56      private static final String CONSTANT_VALUE = "value";
57      private static final String EMPTY_STRING = "";
58      private IEntry _entry;
59      private boolean _bIsAscSort;
60  
61      /**
62       * Constructor
63       * 
64       * @param entry
65       *            the entry to compare
66       * @param bIsAscSort
67       *            true if it is sorted asc, false otherwise
68       */
69      public RecordComparator( IEntry entry, boolean bIsAscSort )
70      {
71          _entry = entry;
72          _bIsAscSort = bIsAscSort;
73      }
74  
75      /**
76       * Compare two records
77       * 
78       * @param r1
79       *            Record 1
80       * @param r2
81       *            Record 2
82       * @return negative value if r1 is before r2 in the alphabetical order, 0 if r1 equals r2, positive value if r1 is after r2
83       */
84      @Override
85      public int compare( Record r1, Record r2 )
86      {
87          int nStatus = 0;
88          RecordField rf1 = getRecordFieldToCompare( r1 );
89          RecordField rf2 = getRecordFieldToCompare( r2 );
90  
91          if ( ( rf1 != null ) && ( rf2 != null ) )
92          {
93              if ( _entry instanceof fr.paris.lutece.plugins.directory.business.EntryTypeDate )
94              {
95                  try
96                  {
97                      Long lTime1 = Long.parseLong( rf1.getValue( ) );
98                      Date date1 = new Date( lTime1 );
99                      Long lTime2 = Long.parseLong( rf2.getValue( ) );
100                     Date date2 = new Date( lTime2 );
101                     nStatus = date1.compareTo( date2 );
102                 }
103                 catch( Exception e )
104                 {
105                     AppLogService.error( e );
106                 }
107             }
108             else
109                 if ( _entry instanceof fr.paris.lutece.plugins.directory.business.EntryTypeNumbering )
110                 {
111                     try
112                     {
113                         nStatus = Integer.parseInt( rf1.getValue( ) ) - Integer.parseInt( rf2.getValue( ) );
114                     }
115                     catch( Exception e )
116                     {
117                         AppLogService.error( e );
118                     }
119                 }
120                 else
121                 {
122                     if ( ( rf1.getValue( ) == null ) && ( rf2.getValue( ) != null ) )
123                     {
124                         nStatus = -1;
125                     }
126                     else
127                         if ( ( rf1.getValue( ) != null ) && ( rf2.getValue( ) == null ) )
128                         {
129                             nStatus = 1;
130                         }
131                         else
132                             if ( ( rf1.getValue( ) != null ) && ( rf2.getValue( ) != null ) )
133                             {
134                                 nStatus = rf1.getValue( ).compareToIgnoreCase( rf2.getValue( ) );
135                             }
136                 }
137         }
138 
139         if ( !_bIsAscSort )
140         {
141             nStatus = nStatus * ( -1 );
142         }
143 
144         return nStatus;
145     }
146 
147     /**
148      * Get the record field
149      * 
150      * @param r
151      *            Record
152      * @return RecordField
153      */
154     private RecordField getRecordFieldToCompare( Record r )
155     {
156         RecordField rf = null;
157 
158         if ( _entry instanceof fr.paris.lutece.plugins.directory.business.EntryTypeCheckBox
159                 || _entry instanceof fr.paris.lutece.plugins.directory.business.EntryTypeSelect )
160         {
161             List<RecordField> listRecordFields = new ArrayList<RecordField>( );
162 
163             for ( RecordField recordField : r.getListRecordField( ) )
164             {
165                 if ( recordField.getEntry( ).getIdEntry( ) == _entry.getIdEntry( ) )
166                 {
167                     if ( recordField.getValue( ) == null )
168                     {
169                         recordField.setValue( EMPTY_STRING );
170                     }
171 
172                     listRecordFields.add( recordField );
173                 }
174             }
175 
176             Collections.sort( listRecordFields, new AttributeComparator( CONSTANT_VALUE, _bIsAscSort ) );
177 
178             if ( listRecordFields.size( ) > 0 )
179             {
180                 rf = listRecordFields.get( 0 );
181             }
182         }
183         else
184             if ( _entry instanceof fr.paris.lutece.plugins.directory.business.EntryTypeGeolocation )
185             {
186                 for ( RecordField recordField : r.getListRecordField( ) )
187                 {
188                     if ( ( recordField.getEntry( ).getIdEntry( ) == _entry.getIdEntry( ) )
189                             && recordField.getField( ).getTitle( ).equals( EntryTypeGeolocation.CONSTANT_ADDRESS ) )
190                     {
191                         rf = recordField;
192                     }
193                 }
194             }
195             else
196                 if ( _entry instanceof fr.paris.lutece.plugins.directory.business.EntryTypeDate
197                         || _entry instanceof fr.paris.lutece.plugins.directory.business.EntryTypeDirectory
198                         || _entry instanceof fr.paris.lutece.plugins.directory.business.EntryTypeMail
199                         || _entry instanceof fr.paris.lutece.plugins.directory.business.EntryTypeUrl
200                         || _entry instanceof fr.paris.lutece.plugins.directory.business.EntryTypeNumbering
201                         || _entry instanceof fr.paris.lutece.plugins.directory.business.EntryTypeSQL
202                         || _entry instanceof fr.paris.lutece.plugins.directory.business.EntryTypeText
203                         || _entry instanceof fr.paris.lutece.plugins.directory.business.EntryTypeTextArea
204                         || _entry instanceof fr.paris.lutece.plugins.directory.business.EntryTypeRadioButton
205                         || _entry instanceof fr.paris.lutece.plugins.directory.business.EntryTypeRichText
206                         || _entry instanceof fr.paris.lutece.plugins.directory.business.EntryTypeInternalLink
207                         || _entry instanceof fr.paris.lutece.plugins.directory.business.EntryTypeMyLuteceUser )
208                 {
209                     for ( RecordField recordField : r.getListRecordField( ) )
210                     {
211                         if ( recordField.getEntry( ).getIdEntry( ) == _entry.getIdEntry( ) )
212                         {
213                             rf = recordField;
214                         }
215                     }
216                 }
217 
218         return rf;
219     }
220 }