View Javadoc
1   /*
2    * Copyright (c) 2002-2021, City of 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.announce.business;
35  
36  import org.apache.commons.lang3.StringUtils;
37  
38  /**
39   * Announces sort
40   */
41  public class AnnounceSort
42  {
43      /**
44       * Sort by creation date
45       */
46      public static final String SORT_DATE_CREATION = "date_creation";
47  
48      /**
49       * Sort by creation date
50       */
51      public static final String SORT_DATE_PUBLICATION = "publication_time";
52  
53      /**
54       * Sort by modification date
55       */
56      public static final String SORT_DATE_MODIFICATION = "date_modification";
57  
58      /**
59       * Sort by title
60       */
61      public static final String SORT_TITLE = "title_announce";
62  
63      /**
64       * Sort by price
65       */
66      public static final String SORT_PRICE = "price_announce";
67  
68      /**
69       * Sort by description
70       */
71      public static final String SORT_DESCRIPTION = "description_announce";
72  
73      /**
74       * Default sort to use
75       */
76      public static final AnnounceSortusiness/AnnounceSort.html#AnnounceSort">AnnounceSort DEFAULT_SORT = new AnnounceSort( SORT_DATE_MODIFICATION, false );
77      private final String _strSortColumn;
78      private final boolean _bSortAsc;
79  
80      /**
81       * Private constructor
82       * 
83       * @param strSortColumn
84       *            The name of the database column to use to sort
85       * @param bSortAsc
86       *            True to sort ascending, false otherwise
87       */
88      private AnnounceSort( String strSortColumn, boolean bSortAsc )
89      {
90          this._strSortColumn = strSortColumn;
91          this._bSortAsc = bSortAsc;
92      }
93  
94      /**
95       * Get the name of the sorted column
96       * 
97       * @return The name of the sorted column
98       */
99      public String getSortColumn( )
100     {
101         return _strSortColumn;
102     }
103 
104     /**
105      * Get the name of the sorted column
106      * 
107      * @return The name of the sorted column
108      */
109     public boolean getSortAsc( )
110     {
111         return _bSortAsc;
112     }
113 
114     /**
115      * Get an announce sort from a string of the name of the column to sort
116      * 
117      * @param strSort
118      *            The name of the column to sort
119      * @param bSortAsc
120      *            True to sort ascending false otherwise
121      * @return The corresponding sort, or the default sort if the given parameter does not match any sortable column name
122      */
123     public static AnnounceSort getAnnounceSort( String strSort, boolean bSortAsc )
124     {
125         if ( StringUtils.equals( SORT_DATE_CREATION, strSort ) || StringUtils.equals( SORT_DATE_PUBLICATION, strSort )
126                 || StringUtils.equals( SORT_DATE_MODIFICATION, strSort ) || StringUtils.equals( SORT_TITLE, strSort )
127                 || StringUtils.equals( SORT_DESCRIPTION, strSort ) || StringUtils.equals( SORT_PRICE, strSort ) )
128         {
129             return new AnnounceSort( strSort, bSortAsc );
130         }
131 
132         return DEFAULT_SORT;
133     }
134 }