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.blog.business.portlet;
35
36 import fr.paris.lutece.portal.service.util.AppPropertiesService;
37
38 import org.apache.commons.lang3.StringUtils;
39
40 /**
41 * PortletOrder
42 */
43 public class PortletOrder
44 {
45 // CONSTANTS
46 public static final int DATE_UPDATE_PORTLET = AppPropertiesService.getPropertyInt( "blog.order.date_update_portlet", 0 );
47 public static final int PAGE_NAME = AppPropertiesService.getPropertyInt( "blog.order.page_name", 1 );
48 public static final int PAGE_ID = AppPropertiesService.getPropertyInt( "blog.order.page_id", 2 );
49 public static final int PORTLET_NAME = AppPropertiesService.getPropertyInt( "blog.order.portlet_name", 3 );
50 public static final int SORT_ASC = AppPropertiesService.getPropertyInt( "blog.order.asc", 1 );
51 private static final String SQL_ORDER_BY_PAGE_NAME = " ORDER BY f.name ";
52 private static final String SQL_ORDER_BY_PAGE_ID = " ORDER BY a.id_page ";
53 private static final String SQL_ORDER_BY_PORTLET_NAME = " ORDER BY a.name ";
54 private static final String SQL_ORDER_BY_DATE_UPDATE_PORTLET = " ORDER BY a.date_update ";
55 private static final String SQL_ASC = " ASC ";
56 private static final String SQL_DESC = " DESC ";
57
58 // VARIABLES
59 private boolean _bSortAsc;
60 private int _nTypeOrder;
61
62 /**
63 * Constructor
64 */
65 public PortletOrder( )
66 {
67 _bSortAsc = false;
68 _nTypeOrder = DATE_UPDATE_PORTLET;
69 }
70
71 /**
72 * Constructor
73 *
74 * @param bSortAsc
75 * true if it must be sorted ascendingly, false otherwise
76 * @param nTypeOrder
77 * the order type
78 */
79 public PortletOrder( boolean bSortAsc, int nTypeOrder )
80 {
81 _bSortAsc = bSortAsc;
82 _nTypeOrder = nTypeOrder;
83 }
84
85 /**
86 * Set the way of sorting the portlets
87 *
88 * @param bSortAsc
89 * true if it must be sorted ascendingly, false otherwise
90 */
91 public void setSortAsc( boolean bSortAsc )
92 {
93 _bSortAsc = bSortAsc;
94 }
95
96 /**
97 * Return true if it must be sorted ascendingly, false otherwise
98 *
99 * @return true if it must be sorted ascendingly, false otherwise
100 */
101 public boolean isSortAsc( )
102 {
103 return _bSortAsc;
104 }
105
106 /**
107 * Set the order type
108 *
109 * @param nTypeOrder
110 * the order type
111 */
112 public void setTypeOrder( int nTypeOrder )
113 {
114 _nTypeOrder = nTypeOrder;
115 }
116
117 /**
118 * Get the order type
119 *
120 * @return the order type
121 */
122 public int getTypeOrder( )
123 {
124 return _nTypeOrder;
125 }
126
127 /**
128 * Get the SQL query for ordering the portlet
129 *
130 * @return the SQL query
131 */
132 public String getSQLOrderBy( )
133 {
134 StringBuilder sbSQL = new StringBuilder( );
135
136 if ( _nTypeOrder == PAGE_NAME )
137 {
138 sbSQL.append( SQL_ORDER_BY_PAGE_NAME );
139 }
140 else
141 if ( _nTypeOrder == PAGE_ID )
142 {
143 sbSQL.append( SQL_ORDER_BY_PAGE_ID );
144 }
145 else
146 if ( _nTypeOrder == PORTLET_NAME )
147 {
148 sbSQL.append( SQL_ORDER_BY_PORTLET_NAME );
149 }
150 else
151 if ( _nTypeOrder == DATE_UPDATE_PORTLET )
152 {
153 sbSQL.append( SQL_ORDER_BY_DATE_UPDATE_PORTLET );
154 }
155
156 if ( StringUtils.isNotBlank( sbSQL.toString( ) ) )
157 {
158 sbSQL.append( _bSortAsc ? SQL_ASC : SQL_DESC );
159 }
160
161 return sbSQL.toString( );
162 }
163 }