View Javadoc
1   /*
2    * Copyright (c) 2002-2014, 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.extend.modules.actionbar.business;
35  
36  import java.util.ArrayList;
37  import java.util.List;
38  
39  import fr.paris.lutece.portal.service.plugin.Plugin;
40  import fr.paris.lutece.util.sql.DAOUtil;
41  
42  
43  /**
44   * DAO to manage ActionButton
45   */
46  public class ActionButtonDAO implements IActionButtonDAO
47  {
48      private static final String SQL_QUERY_SELECT = " SELECT id_action, name, html_content, resource_type, btn_order FROM extend_actionbar_action WHERE id_action = ? ";
49      private static final String SQL_QUERY_SELECT_BY_NAME = " SELECT id_action, name, html_content, resource_type, btn_order FROM extend_actionbar_action WHERE name = ? ";
50      private static final String SQL_QUERY_SELECT_BY_ORDER = " SELECT id_action, name, html_content, resource_type, btn_order FROM extend_actionbar_action WHERE btn_order = ? ";
51      private static final String SQL_QUERY_SELECT_ALL = " SELECT id_action, name, html_content, resource_type, btn_order FROM extend_actionbar_action ORDER BY btn_order ASC ";
52      private static final String SQL_QUERY_SELECT_ALL_BY_RESOURCE_TYPE = " SELECT id_action, name, html_content, resource_type, btn_order FROM extend_actionbar_action WHERE resource_type = '"
53              + ActionButton.EVERY_RESOURCE_TYPE + "' OR resource_type = ? ORDER BY btn_order ASC ";
54      private static final String SQL_QUERY_GET_NEW_ORDER = " SELECT MAX(btn_order) + 1 FROM extend_actionbar_action ";
55      private static final String SQL_INSERT = " INSERT INTO extend_actionbar_action( id_action, name, html_content, resource_type, btn_order ) VALUES (?,?,?,?,?) ";
56      private static final String SQL_DELETE = " DELETE FROM extend_actionbar_action WHERE id_action = ? ";
57      private static final String SQL_UPDATE = " UPDATE extend_actionbar_action SET name = ?, html_content = ?, resource_type = ? WHERE id_action = ? ";
58      private static final String SQL_QUERY_SELECT_FROM_LIST_ID = " SELECT id_action, name, html_content, resource_type, btn_order FROM extend_actionbar_action WHERE id_action IN ( ";
59      private static final String SQL_ORDER_BY_BTN_ORDER = " ORDER BY btn_order ASC ";
60      private static final String SQL_UPDATE_BTN_ORDER = " UPDATE extend_actionbar_action SET btn_order = ? WHERE id_action = ? ";
61  
62      private static final String SQL_UPDATE_FILL_BLANK_IN_ORDER = " UPDATE extend_actionbar_action SET btn_order = btn_order - 1 WHERE btn_order > ? ";
63      private static final String SQL_QUERY_GET_NEW_PRIMARY_KEY = " SELECT MAX(id_action) FROM extend_actionbar_action ";
64  
65      private static final String CONSTANT_CLOSE_PARENTHESIS = ")";
66      private static final String CONSTANT_COMMA = ",";
67      private static final String CONSTANT_QUESTION_MARK = "?";
68  
69      /**
70       * Get a new primary key for action buttons
71       * @param plugin The plugin
72       * @return The new primary key
73       */
74      private int newPrimaryKey( Plugin plugin )
75      {
76          DAOUtil daoUtil = new DAOUtil( SQL_QUERY_GET_NEW_PRIMARY_KEY, plugin );
77          daoUtil.executeQuery( );
78          int res = 1;
79          if ( daoUtil.next( ) )
80          {
81              res = daoUtil.getInt( 1 ) + 1;
82          }
83  
84          daoUtil.free( );
85  
86          return res;
87      }
88  
89      /**
90       * {@inheritDoc}
91       */
92      @Override
93      public void create( ActionButton actionButton, Plugin plugin )
94      {
95          DAOUtil daoUtil = new DAOUtil( SQL_INSERT, plugin );
96          actionButton.setIdAction( newPrimaryKey( plugin ) );
97          daoUtil.setInt( 1, actionButton.getIdAction( ) );
98          daoUtil.setString( 2, actionButton.getName( ) );
99          daoUtil.setString( 3, actionButton.getHtmlContent( ) );
100         daoUtil.setString( 4, actionButton.getResourceType( ) );
101         daoUtil.setInt( 5, getNewOrder( plugin ) );
102         daoUtil.executeUpdate( );
103         daoUtil.free( );
104     }
105 
106     /**
107      * {@inheritDoc}
108      */
109     @Override
110     public ActionButton findById( int nIdActionButton, Plugin plugin )
111     {
112         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT, plugin );
113         daoUtil.setInt( 1, nIdActionButton );
114         daoUtil.executeQuery( );
115         ActionButton action = null;
116         if ( daoUtil.next( ) )
117         {
118             action = new ActionButton( );
119             action.setIdAction( daoUtil.getInt( 1 ) );
120             action.setName( daoUtil.getString( 2 ) );
121             action.setHtmlContent( daoUtil.getString( 3 ) );
122             action.setResourceType( daoUtil.getString( 4 ) );
123             action.setOrder( daoUtil.getInt( 5 ) );
124         }
125 
126         daoUtil.free( );
127 
128         return action;
129     }
130 
131     /**
132      * {@inheritDoc}
133      */
134     @Override
135     public void update( ActionButton actionButton, Plugin plugin )
136     {
137         DAOUtil daoUtil = new DAOUtil( SQL_UPDATE, plugin );
138         daoUtil.setString( 1, actionButton.getName( ) );
139         daoUtil.setString( 2, actionButton.getHtmlContent( ) );
140         daoUtil.setString( 3, actionButton.getResourceType( ) );
141         daoUtil.setInt( 4, actionButton.getIdAction( ) );
142         daoUtil.executeUpdate( );
143         daoUtil.free( );
144     }
145 
146     /**
147      * {@inheritDoc}
148      */
149     @Override
150     public void delete( int nIdActionButton, Plugin plugin )
151     {
152         DAOUtil daoUtil = new DAOUtil( SQL_DELETE, plugin );
153         daoUtil.setInt( 1, nIdActionButton );
154         daoUtil.executeUpdate( );
155         daoUtil.free( );
156     }
157 
158     /**
159      * {@inheritDoc}
160      */
161     @Override
162     public List<ActionButton> findAll( Plugin plugin )
163     {
164         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ALL, plugin );
165         daoUtil.executeQuery( );
166         List<ActionButton> listActions = new ArrayList<ActionButton>( );
167         while ( daoUtil.next( ) )
168         {
169             ActionButtons/extend/modules/actionbar/business/ActionButton.html#ActionButton">ActionButton action = new ActionButton( );
170             action.setIdAction( daoUtil.getInt( 1 ) );
171             action.setName( daoUtil.getString( 2 ) );
172             action.setHtmlContent( daoUtil.getString( 3 ) );
173             action.setResourceType( daoUtil.getString( 4 ) );
174             action.setOrder( daoUtil.getInt( 5 ) );
175             listActions.add( action );
176         }
177 
178         daoUtil.free( );
179 
180         return listActions;
181     }
182 
183     /**
184      * {@inheritDoc}
185      */
186     @Override
187     public List<ActionButton> findAllByResourceType( String strResourceType, Plugin plugin )
188     {
189         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ALL_BY_RESOURCE_TYPE, plugin );
190         daoUtil.setString( 1, strResourceType );
191         daoUtil.executeQuery( );
192         List<ActionButton> listActions = new ArrayList<ActionButton>( );
193         while ( daoUtil.next( ) )
194         {
195             ActionButtons/extend/modules/actionbar/business/ActionButton.html#ActionButton">ActionButton action = new ActionButton( );
196             action.setIdAction( daoUtil.getInt( 1 ) );
197             action.setName( daoUtil.getString( 2 ) );
198             action.setHtmlContent( daoUtil.getString( 3 ) );
199             action.setResourceType( daoUtil.getString( 4 ) );
200             action.setOrder( daoUtil.getInt( 5 ) );
201             listActions.add( action );
202         }
203 
204         daoUtil.free( );
205 
206         return listActions;
207     }
208 
209     /**
210      * {@inheritDoc}
211      */
212     @Override
213     public ActionButton findByName( String strName, Plugin plugin )
214     {
215         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_NAME, plugin );
216         daoUtil.setString( 1, strName );
217         daoUtil.executeQuery( );
218         ActionButton action = null;
219         if ( daoUtil.next( ) )
220         {
221             action = new ActionButton( );
222             action.setIdAction( daoUtil.getInt( 1 ) );
223             action.setName( daoUtil.getString( 2 ) );
224             action.setHtmlContent( daoUtil.getString( 3 ) );
225             action.setResourceType( daoUtil.getString( 4 ) );
226             action.setOrder( daoUtil.getInt( 5 ) );
227         }
228 
229         daoUtil.free( );
230 
231         return action;
232     }
233 
234     /**
235      * {@inheritDoc}
236      */
237     @Override
238     public List<ActionButton> findActionButtons( List<Integer> listIdActions, Plugin plugin )
239     {
240         List<ActionButton> listActions = new ArrayList<ActionButton>( );
241         if ( listIdActions != null && listIdActions.size( ) > 0 )
242         {
243             StringBuilder sbSql = new StringBuilder( SQL_QUERY_SELECT_FROM_LIST_ID );
244             for ( int i = 0; i < listIdActions.size( ); i++ )
245             {
246                 sbSql.append( CONSTANT_QUESTION_MARK );
247                 if ( i + 1 < listIdActions.size( ) )
248                 {
249                     sbSql.append( CONSTANT_COMMA );
250                 }
251             }
252             sbSql.append( CONSTANT_CLOSE_PARENTHESIS );
253             sbSql.append( SQL_ORDER_BY_BTN_ORDER );
254             DAOUtil daoUtil = new DAOUtil( sbSql.toString( ), plugin );
255             int nIndex = 1;
256             for ( Integer nIdAction : listIdActions )
257             {
258                 daoUtil.setInt( nIndex++, nIdAction );
259             }
260             daoUtil.executeQuery( );
261             while ( daoUtil.next( ) )
262             {
263                 ActionButtons/extend/modules/actionbar/business/ActionButton.html#ActionButton">ActionButton action = new ActionButton( );
264                 action.setIdAction( daoUtil.getInt( 1 ) );
265                 action.setName( daoUtil.getString( 2 ) );
266                 action.setHtmlContent( daoUtil.getString( 3 ) );
267                 action.setResourceType( daoUtil.getString( 4 ) );
268                 action.setOrder( daoUtil.getInt( 5 ) );
269                 listActions.add( action );
270             }
271 
272             daoUtil.free( );
273         }
274         return listActions;
275     }
276 
277     /**
278      * {@inheritDoc}
279      */
280     @Override
281     public int getNewOrder( Plugin plugin )
282     {
283         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_GET_NEW_ORDER, plugin );
284         daoUtil.executeQuery( );
285         int nResult = 1;
286         if ( daoUtil.next( ) )
287         {
288             nResult = daoUtil.getInt( 1 );
289             if ( nResult == 0 )
290             {
291                 nResult++;
292             }
293         }
294         daoUtil.free( );
295         return nResult;
296     }
297 
298     /**
299      * {@inheritDoc}
300      */
301     @Override
302     public void updateActionButtonOrder( int nIdAction, int nNewOrder, Plugin plugin )
303     {
304         DAOUtil daoUtil = new DAOUtil( SQL_UPDATE_BTN_ORDER, plugin );
305         daoUtil.setInt( 1, nNewOrder );
306         daoUtil.setInt( 2, nIdAction );
307         daoUtil.executeUpdate( );
308         daoUtil.free( );
309     }
310 
311     /**
312      * {@inheritDoc}
313      */
314     @Override
315     public List<ActionButton> findByOrder( int nNewOrder, Plugin plugin )
316     {
317         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_ORDER, plugin );
318         daoUtil.setInt( 1, nNewOrder );
319         daoUtil.executeQuery( );
320         List<ActionButton> listAction = new ArrayList<ActionButton>( );
321         if ( daoUtil.next( ) )
322         {
323             ActionButtons/extend/modules/actionbar/business/ActionButton.html#ActionButton">ActionButton action = new ActionButton( );
324             action.setIdAction( daoUtil.getInt( 1 ) );
325             action.setName( daoUtil.getString( 2 ) );
326             action.setHtmlContent( daoUtil.getString( 3 ) );
327             action.setResourceType( daoUtil.getString( 4 ) );
328             action.setOrder( daoUtil.getInt( 5 ) );
329             listAction.add( action );
330         }
331 
332         daoUtil.free( );
333 
334         return listAction;
335     }
336 
337     /**
338      * {@inheritDoc}
339      */
340     @Override
341     public void fillBlankInOrder( int nOrder, Plugin plugin )
342     {
343         DAOUtil daoUtil = new DAOUtil( SQL_UPDATE_FILL_BLANK_IN_ORDER, plugin );
344         daoUtil.setInt( 1, nOrder );
345         daoUtil.executeUpdate( );
346         daoUtil.free( );
347     }
348 }