View Javadoc
1   /*
2    * Copyright (c) 2002-2022, 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.genericattributes.business;
35  
36  import java.sql.Statement;
37  import java.util.ArrayList;
38  import java.util.HashMap;
39  import java.util.List;
40  import java.util.Map;
41  import java.util.stream.Collectors;
42  
43  import org.apache.commons.collections.CollectionUtils;
44  import org.apache.commons.lang3.StringUtils;
45  
46  import fr.paris.lutece.portal.service.plugin.Plugin;
47  import fr.paris.lutece.util.sql.DAOUtil;
48  
49  /**
50   * This class provides Data Access methods for Entry objects
51   */
52  public final class EntryDAO implements IEntryDAO
53  {
54      // Constants
55      private static final String SQL_QUERY_NUMBER_CONDITIONAL_QUESTION = "SELECT COUNT(e2.id_entry) "
56              + "FROM genatt_entry e1,genatt_field f1,genatt_entry e2 WHERE e1.id_entry=? AND e1.id_entry=f1.id_entry and e2.id_field_depend=f1.id_field ";
57      private static final String SQL_QUERY_SELECT_LIST = "ent.id_type,typ.title,typ.is_group,typ.is_comment,typ.class_name,typ.is_mylutece_user,typ.icon_name,"
58              + "ent.id_entry,ent.id_resource,ent.resource_type,ent.id_parent,ent.code,ent.title,ent.help_message, ent.comment,ent.mandatory,ent.fields_in_line,"
59              + "ent.pos,ent.id_field_depend,ent.field_unique, ent.css_class, ent.pos_conditional, ent.error_message, "
60              + "ent.is_only_display_back, ent.is_indexed ";
61      private static final String SQL_QUERY_SELECT_ENTRY_ATTRIBUTES = "SELECT " + SQL_QUERY_SELECT_LIST
62              + "FROM genatt_entry ent,genatt_entry_type typ WHERE ent.id_type=typ.id_type ";
63      private static final String SQL_QUERY_FIND_BY_PRIMARY_KEY = SQL_QUERY_SELECT_ENTRY_ATTRIBUTES + " AND ent.id_entry = ? ";
64      private static final String SQL_QUERY_INSERT = "INSERT INTO genatt_entry ( id_resource,resource_type,id_type,id_parent,code,title,help_message, comment,mandatory,fields_in_line,"
65              + "pos,id_field_depend,field_unique,css_class, pos_conditional, error_message, is_only_display_back, is_indexed ) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
66      private static final String SQL_QUERY_DELETE = "DELETE FROM genatt_entry WHERE id_entry = ? ";
67      private static final String SQL_QUERY_UPDATE = "UPDATE genatt_entry SET id_entry=?,id_resource=?,resource_type=?,id_type=?,id_parent=?,code=?,title=?,help_message=?,"
68              + "comment=?,mandatory=?, fields_in_line=?,pos=?,id_field_depend=?,field_unique=?,css_class=?, pos_conditional=?, "
69              + "error_message=?, is_only_display_back = ?, is_indexed = ? WHERE id_entry=?";
70      private static final String SQL_QUERY_SELECT_ENTRY_BY_FILTER = SQL_QUERY_SELECT_ENTRY_ATTRIBUTES;
71      private static final String SQL_QUERY_SELECT_NUMBER_ENTRY_BY_FILTER = "SELECT COUNT(ent.id_entry) "
72              + "FROM genatt_entry ent,genatt_entry_type typ WHERE ent.id_type=typ.id_type ";
73      private static final String SQL_QUERY_NEW_POSITION = "SELECT MAX(pos) " + "FROM genatt_entry WHERE id_resource=? AND resource_type=?";
74      private static final String SQL_QUERY_NEW_POSITION_CONDITIONAL_QUESTION = "SELECT MAX(pos_conditional) FROM genatt_entry WHERE id_field_depend=?";
75      private static final String SQL_FILTER_ID_RESOURCE = " AND ent.id_resource = ? ";
76      private static final String SQL_FILTER_RESOURCE_TYPE = " AND ent.resource_type = ? ";
77      private static final String SQL_FILTER_ID_PARENT = " AND ent.id_parent = ? ";
78      private static final String SQL_FILTER_ID_PARENT_IS_NULL = " AND ent.id_parent IS NULL ";
79      private static final String SQL_FILTER_IS_GROUP = " AND typ.is_group = ? ";
80      private static final String SQL_FILTER_IS_COMMENT = " AND typ.is_comment = ? ";
81      private static final String SQL_FILTER_ID_FIELD_DEPEND = " AND ent.id_field_depend = ? ";
82      private static final String SQL_FILTER_ID_FIELD_DEPEND_IS_NULL = " AND ent.id_field_depend IS NULL ";
83      private static final String SQL_FILTER_ID_TYPE = " AND ent.id_type = ? ";
84      private static final String SQL_FILTER_IS_ONLY_DISPLAY_IN_BACK = " AND ent.is_only_display_back = ? ";
85      private static final String SQL_FILTER_IS_INDEXED = " AND ent.is_indexed = ? ";
86      private static final String SQL_ORDER_BY_POSITION = " ORDER BY ent.pos, ent.pos_conditional ";
87      private static final String SQL_GROUP_BY_POSITION = " GROUP BY ent.pos, ent.pos_conditional ";
88      private static final String SQL_GROUP_BY_ENTRY_ENTRY_TYPE = "GROUP BY " + SQL_QUERY_SELECT_LIST;
89      private static final String SQL_QUERY_ENTRIES_PARENT_NULL = SQL_QUERY_SELECT_ENTRY_ATTRIBUTES
90              + " AND id_parent IS NULL AND id_resource=? AND resource_type = ?" + SQL_FILTER_ID_FIELD_DEPEND_IS_NULL + " ORDER BY ent.pos";
91      private static final String SQL_QUERY_ENTRY_CONDITIONAL_WITH_ORDER_BY_FIELD = SQL_QUERY_SELECT_ENTRY_ATTRIBUTES
92              + " AND pos_conditional = ?  AND ent.id_field_depend = ? AND id_resource=? ";
93      private static final String SQL_QUERY_DECREMENT_ORDER_CONDITIONAL = "UPDATE genatt_entry SET pos_conditional = pos_conditional - 1 WHERE pos_conditional > ? AND id_field_depend=? AND id_resource=? AND resource_type=? ";
94      private static final int CONSTANT_ZERO = 0;
95      private static final String SQL_QUERY_SELECT_ENTRY_BY_FORM = "SELECT id_entry, title FROM genatt_entry WHERE id_resource = ? AND title IS NOT NULL ORDER BY id_entry ";
96      private static final String SQL_QUERY_SELECT_ENTRY_VALUE = "SELECT title FROM genatt_response INNER JOIN genatt_field ON genatt_response.id_field = genatt_field.id_field "
97              + "	WHERE genatt_response.id_entry = ? AND genatt_response.id_response = ? AND title IS NOT NULL  ORDER BY genatt_response.id_entry ";
98      private static final String SQL_QUERY_FIND_BY_PRIMARY_KEY_LIST = SQL_QUERY_SELECT_ENTRY_ATTRIBUTES + " AND ent.id_entry IN ( ";
99  
100     /**
101      * {@inheritDoc}
102      */
103     @Override
104     public int insert( Entry entry, Plugin plugin )
105     {
106         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, Statement.RETURN_GENERATED_KEYS, plugin ) )
107         {
108             int nIndex = 1;
109             daoUtil.setInt( nIndex++, entry.getIdResource( ) );
110             daoUtil.setString( nIndex++, entry.getResourceType( ) );
111             daoUtil.setInt( nIndex++, entry.getEntryType( ).getIdType( ) );
112 
113             if ( entry.getParent( ) != null )
114             {
115                 daoUtil.setInt( nIndex++, entry.getParent( ).getIdEntry( ) );
116             }
117             else
118             {
119                 daoUtil.setIntNull( nIndex++ );
120             }
121 
122             daoUtil.setString( nIndex++, entry.getCode( ) );
123             daoUtil.setString( nIndex++, trimEntryTitle( entry ) );
124             daoUtil.setString( nIndex++, entry.getHelpMessage( ) );
125             daoUtil.setString( nIndex++, entry.getComment( ) );
126             daoUtil.setBoolean( nIndex++, entry.isMandatory( ) );
127             daoUtil.setBoolean( nIndex++, entry.isFieldInLine( ) );
128 
129             daoUtil.setInt( nIndex++, newPosition( entry, plugin ) );
130 
131             if ( entry.getFieldDepend( ) != null )
132             {
133                 daoUtil.setInt( nIndex++, entry.getFieldDepend( ).getIdField( ) );
134             }
135             else
136             {
137                 daoUtil.setIntNull( nIndex++ );
138             }
139 
140             daoUtil.setBoolean( nIndex++, entry.isUnique( ) );
141 
142             daoUtil.setString( nIndex++, ( entry.getCSSClass( ) == null ) ? StringUtils.EMPTY : entry.getCSSClass( ) );
143             daoUtil.setInt( nIndex++, newPositionConditional( entry, plugin ) );
144             daoUtil.setString( nIndex++, entry.getErrorMessage( ) );
145             daoUtil.setBoolean( nIndex++, entry.isOnlyDisplayInBack( ) );
146             daoUtil.setBoolean( nIndex++, entry.isIndexed( ) );
147 
148             daoUtil.executeUpdate( );
149 
150             if ( daoUtil.nextGeneratedKey( ) )
151             {
152                 entry.setIdEntry( daoUtil.getGeneratedKeyInt( 1 ) );
153             }
154         }
155 
156         return entry.getIdEntry( );
157     }
158 
159     /**
160      * {@inheritDoc}
161      */
162     @Override
163     public Entry load( int nId, Plugin plugin )
164     {
165         Entry entry = null;
166         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_FIND_BY_PRIMARY_KEY, plugin ) )
167         {
168             daoUtil.setInt( 1, nId );
169             daoUtil.executeQuery( );
170 
171             if ( daoUtil.next( ) )
172             {
173                 entry = getEntryValues( daoUtil );
174             }
175 
176         }
177         if ( entry != null )
178         {
179             entry.setNumberConditionalQuestion( numberConditionalQuestion( entry.getIdEntry( ), plugin ) );
180         }
181         return entry;
182     }
183 
184     @Override
185     public List<Entry> loadMultiple( List<Integer> idList, Plugin plugin )
186     {
187         List<Entry> list = new ArrayList<>( );
188         if ( CollectionUtils.isEmpty( idList ) )
189         {
190             return list;
191         }
192         String query = SQL_QUERY_FIND_BY_PRIMARY_KEY_LIST + idList.stream( ).distinct( ).map( i -> "?" ).collect( Collectors.joining( "," ) ) + " )";
193 
194         try ( DAOUtil daoUtil = new DAOUtil( query, plugin ) )
195         {
196             for ( int i = 0; i < idList.size( ); i++ )
197             {
198                 daoUtil.setInt( i + 1, idList.get( i ) );
199             }
200             daoUtil.executeQuery( );
201 
202             while ( daoUtil.next( ) )
203             {
204                 list.add( getEntryValues( daoUtil ) );
205             }
206         }
207         return list;
208     }
209 
210     /**
211      * {@inheritDoc}
212      */
213     @Override
214     public void delete( int nIdEntry, Plugin plugin )
215     {
216         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin ) )
217         {
218             daoUtil.setInt( 1, nIdEntry );
219             daoUtil.executeUpdate( );
220         }
221     }
222 
223     /**
224      * {@inheritDoc}
225      */
226     @Override
227     public void store( Entry entry, Plugin plugin )
228     {
229         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin ) )
230         {
231             int nIndex = 1;
232             daoUtil.setInt( nIndex++, entry.getIdEntry( ) );
233             daoUtil.setInt( nIndex++, entry.getIdResource( ) );
234             daoUtil.setString( nIndex++, entry.getResourceType( ) );
235             daoUtil.setInt( nIndex++, entry.getEntryType( ).getIdType( ) );
236 
237             if ( entry.getParent( ) != null )
238             {
239                 daoUtil.setInt( nIndex++, entry.getParent( ).getIdEntry( ) );
240             }
241             else
242             {
243                 daoUtil.setIntNull( nIndex++ );
244             }
245 
246             daoUtil.setString( nIndex++, entry.getCode( ) );
247             daoUtil.setString( nIndex++, trimEntryTitle( entry ) );
248             daoUtil.setString( nIndex++, entry.getHelpMessage( ) );
249             daoUtil.setString( nIndex++, entry.getComment( ) );
250             daoUtil.setBoolean( nIndex++, entry.isMandatory( ) );
251             daoUtil.setBoolean( nIndex++, entry.isFieldInLine( ) );
252 
253             if ( entry.getFieldDepend( ) == null )
254             {
255                 daoUtil.setInt( nIndex++, entry.getPosition( ) );
256             }
257             else
258             {
259                 daoUtil.setInt( nIndex++, CONSTANT_ZERO );
260             }
261 
262             if ( entry.getFieldDepend( ) != null )
263             {
264                 daoUtil.setInt( nIndex++, entry.getFieldDepend( ).getIdField( ) );
265             }
266             else
267             {
268                 daoUtil.setIntNull( nIndex++ );
269             }
270 
271             daoUtil.setBoolean( nIndex++, entry.isUnique( ) );
272             daoUtil.setString( nIndex++, ( entry.getCSSClass( ) == null ) ? StringUtils.EMPTY : entry.getCSSClass( ) );
273 
274             if ( entry.getFieldDepend( ) != null )
275             {
276                 daoUtil.setInt( nIndex++, entry.getPosition( ) );
277             }
278             else
279             {
280                 daoUtil.setInt( nIndex++, CONSTANT_ZERO );
281             }
282 
283             daoUtil.setString( nIndex++, entry.getErrorMessage( ) );
284             daoUtil.setBoolean( nIndex++, entry.isOnlyDisplayInBack( ) );
285             daoUtil.setBoolean( nIndex++, entry.isIndexed( ) );
286 
287             daoUtil.setInt( nIndex++, entry.getIdEntry( ) );
288 
289             daoUtil.executeUpdate( );
290         }
291     }
292 
293     /**
294      * {@inheritDoc}
295      */
296     @Override
297     public List<Entry> selectEntryListByFilter( EntryFilter filter, Plugin plugin )
298     {
299         List<Entry> entryList = new ArrayList<>( );
300 
301         StringBuilder sbSQL = new StringBuilder( SQL_QUERY_SELECT_ENTRY_BY_FILTER );
302         appendFilter( sbSQL, filter.containsIdResource( ), SQL_FILTER_ID_RESOURCE );
303         appendFilter( sbSQL, filter.containsResourceType( ), SQL_FILTER_RESOURCE_TYPE );
304         appendFilter( sbSQL, filter.containsIdEntryParent( ), SQL_FILTER_ID_PARENT );
305         appendFilter( sbSQL, filter.containsEntryParentNull( ), SQL_FILTER_ID_PARENT_IS_NULL );
306         appendFilter( sbSQL, filter.containsIdIsGroup( ), SQL_FILTER_IS_GROUP );
307         appendFilter( sbSQL, filter.containsIdField( ), SQL_FILTER_ID_FIELD_DEPEND );
308         appendFilter( sbSQL, filter.containsFieldDependNull( ), SQL_FILTER_ID_FIELD_DEPEND_IS_NULL );
309         appendFilter( sbSQL, filter.containsIdEntryType( ), SQL_FILTER_ID_TYPE );
310         appendFilter( sbSQL, filter.containsIdIsComment( ), SQL_FILTER_IS_COMMENT );
311         appendFilter( sbSQL, filter.containsIsOnlyDisplayInBack( ), SQL_FILTER_IS_ONLY_DISPLAY_IN_BACK );
312         appendFilter( sbSQL, filter.containsIsIndexed( ), SQL_FILTER_IS_INDEXED );
313 
314         sbSQL.append( SQL_GROUP_BY_ENTRY_ENTRY_TYPE );
315         sbSQL.append( SQL_ORDER_BY_POSITION );
316 
317         try ( DAOUtil daoUtil = new DAOUtil( sbSQL.toString( ), plugin ) )
318         {
319             int nIndex = 1;
320 
321             if ( filter.containsIdResource( ) )
322             {
323                 daoUtil.setInt( nIndex++, filter.getIdResource( ) );
324             }
325 
326             if ( filter.containsResourceType( ) )
327             {
328                 daoUtil.setString( nIndex++, filter.getResourceType( ) );
329             }
330 
331             if ( filter.containsIdEntryParent( ) )
332             {
333                 daoUtil.setInt( nIndex++, filter.getIdEntryParent( ) );
334             }
335 
336             if ( filter.containsIdIsGroup( ) )
337             {
338                 daoUtil.setBoolean( nIndex++, filter.getIdIsGroup( ) != 0 );
339             }
340 
341             if ( filter.containsIdField( ) )
342             {
343                 daoUtil.setInt( nIndex++, filter.getIdFieldDepend( ) );
344             }
345 
346             if ( filter.containsIdEntryType( ) )
347             {
348                 daoUtil.setInt( nIndex++, filter.getIdEntryType( ) );
349             }
350 
351             if ( filter.containsIdIsComment( ) )
352             {
353                 daoUtil.setBoolean( nIndex++, filter.getIdIsComment( ) != 0 );
354             }
355 
356             if ( filter.containsIsOnlyDisplayInBack( ) )
357             {
358                 daoUtil.setBoolean( nIndex++, filter.getIsOnlyDisplayInBack( ) != 0 );
359             }
360 
361             if ( filter.containsIsIndexed( ) )
362             {
363                 daoUtil.setBoolean( nIndex++, filter.getIsIndexed( ) != 0 );
364             }
365 
366             daoUtil.executeQuery( );
367 
368             while ( daoUtil.next( ) )
369             {
370                 entryList.add( getEntryValues( daoUtil ) );
371             }
372 
373         }
374         for ( Entry entryCreated : entryList )
375         {
376             entryCreated.setNumberConditionalQuestion( numberConditionalQuestion( entryCreated.getIdEntry( ), plugin ) );
377         }
378         return entryList;
379     }
380 
381     private void appendFilter( StringBuilder sb, boolean append, String value )
382     {
383         sb.append( append ? value : StringUtils.EMPTY );
384     }
385 
386     /**
387      * {@inheritDoc}
388      */
389     @Override
390     public int selectNumberEntryByFilter( EntryFilter filter, Plugin plugin )
391     {
392         int nNumberEntry = 0;
393         StringBuilder sbSQL = new StringBuilder( SQL_QUERY_SELECT_NUMBER_ENTRY_BY_FILTER );
394         appendFilter( sbSQL, filter.containsIdResource( ), SQL_FILTER_ID_RESOURCE );
395         appendFilter( sbSQL, filter.containsIdEntryParent( ), SQL_FILTER_ID_PARENT );
396         appendFilter( sbSQL, filter.containsEntryParentNull( ), SQL_FILTER_ID_PARENT_IS_NULL );
397         appendFilter( sbSQL, filter.containsIdIsGroup( ), SQL_FILTER_IS_GROUP );
398         appendFilter( sbSQL, filter.containsIdIsComment( ), SQL_FILTER_IS_COMMENT );
399         appendFilter( sbSQL, filter.containsIdField( ), SQL_FILTER_ID_FIELD_DEPEND );
400         appendFilter( sbSQL, filter.containsIdEntryType( ), SQL_FILTER_ID_TYPE );
401         appendFilter( sbSQL, filter.containsIsOnlyDisplayInBack( ), SQL_FILTER_IS_ONLY_DISPLAY_IN_BACK );
402         appendFilter( sbSQL, filter.containsIsIndexed( ), SQL_FILTER_IS_INDEXED );
403 
404         sbSQL.append( SQL_GROUP_BY_POSITION );
405         sbSQL.append( SQL_ORDER_BY_POSITION );
406 
407         try ( DAOUtil daoUtil = new DAOUtil( sbSQL.toString( ), plugin ) )
408         {
409             int nIndex = 1;
410 
411             if ( filter.containsIdResource( ) )
412             {
413                 daoUtil.setInt( nIndex++, filter.getIdResource( ) );
414             }
415 
416             if ( filter.containsIdEntryParent( ) )
417             {
418                 daoUtil.setInt( nIndex++, filter.getIdEntryParent( ) );
419             }
420 
421             if ( filter.containsIdIsGroup( ) )
422             {
423                 daoUtil.setBoolean( nIndex++, filter.getIdIsGroup( ) != 0 );
424             }
425 
426             if ( filter.containsIdIsComment( ) )
427             {
428                 daoUtil.setBoolean( nIndex++, filter.getIdIsComment( ) != 0 );
429             }
430 
431             if ( filter.containsIdField( ) )
432             {
433                 daoUtil.setInt( nIndex++, filter.getIdFieldDepend( ) );
434             }
435 
436             if ( filter.containsIdEntryType( ) )
437             {
438                 daoUtil.setInt( nIndex++, filter.getIdEntryType( ) );
439             }
440 
441             if ( filter.containsIsOnlyDisplayInBack( ) )
442             {
443                 daoUtil.setBoolean( nIndex++, filter.getIsOnlyDisplayInBack( ) != 0 );
444             }
445 
446             if ( filter.containsIsIndexed( ) )
447             {
448                 daoUtil.setBoolean( nIndex++, filter.getIsIndexed( ) != 0 );
449             }
450 
451             daoUtil.executeQuery( );
452 
453             if ( daoUtil.next( ) )
454             {
455                 nNumberEntry = daoUtil.getInt( 1 );
456             }
457 
458         }
459 
460         return nNumberEntry;
461     }
462 
463     /**
464      * {@inheritDoc}
465      */
466     @Override
467     public List<Entry> findEntriesWithoutParent( Plugin plugin, int nIdResource, String strResourceType )
468     {
469         List<Entry> listResult = new ArrayList<>( );
470         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_ENTRIES_PARENT_NULL, plugin ) )
471         {
472             daoUtil.setInt( 1, nIdResource );
473             daoUtil.setString( 2, strResourceType );
474             daoUtil.executeQuery( );
475 
476             while ( daoUtil.next( ) )
477             {
478                 listResult.add( getEntryValues( daoUtil ) );
479             }
480 
481         }
482         for ( Entry entryCreated : listResult )
483         {
484             entryCreated.setNumberConditionalQuestion( numberConditionalQuestion( entryCreated.getIdEntry( ), plugin ) );
485         }
486         return listResult;
487     }
488 
489     /**
490      * {@inheritDoc}
491      */
492     @Override
493     public Entry findByOrderAndIdFieldAndIdResource( Plugin plugin, int nOrder, int nIdField, int nIdResource, String strResourceType )
494     {
495         Entry entry = null;
496         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_ENTRY_CONDITIONAL_WITH_ORDER_BY_FIELD, plugin ) )
497         {
498             daoUtil.setInt( 1, nOrder );
499             daoUtil.setInt( 2, nIdField );
500             daoUtil.setInt( 3, nIdResource );
501             daoUtil.executeQuery( );
502 
503             if ( daoUtil.next( ) )
504             {
505                 entry = getEntryValues( daoUtil );
506             }
507 
508         }
509         if ( entry != null )
510         {
511             entry.setNumberConditionalQuestion( numberConditionalQuestion( entry.getIdEntry( ), plugin ) );
512         }
513         return entry;
514     }
515 
516     /**
517      * {@inheritDoc}
518      */
519     @Override
520     public void decrementOrderByOne( Plugin plugin, int nOrder, int nIdField, int nIdResource, String strResourceType )
521     {
522         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DECREMENT_ORDER_CONDITIONAL, plugin ) )
523         {
524             daoUtil.setInt( 1, nOrder );
525             daoUtil.setInt( 2, nIdField );
526             daoUtil.setInt( 3, nIdResource );
527             daoUtil.setString( 4, strResourceType );
528             daoUtil.executeUpdate( );
529         }
530     }
531 
532     /**
533      * Generates a new entry position
534      * 
535      * @param plugin
536      *            the plugin
537      * @param entry
538      *            the entry
539      * @return the new entry position
540      */
541     private int newPosition( Entry entry, Plugin plugin )
542     {
543         int nPos;
544 
545         if ( entry.getFieldDepend( ) == null )
546         {
547             try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_POSITION, plugin ) )
548             {
549                 daoUtil.setInt( 1, entry.getIdResource( ) );
550                 daoUtil.setString( 2, entry.getResourceType( ) );
551                 daoUtil.executeQuery( );
552 
553                 if ( !daoUtil.next( ) )
554                 {
555                     // if the table is empty
556                     nPos = 1;
557                 }
558 
559                 nPos = daoUtil.getInt( 1 ) + 1;
560             }
561         }
562         else
563         {
564             // case of conditional question only
565             nPos = 0;
566         }
567 
568         return nPos;
569     }
570 
571     /**
572      * Generates a new entry position
573      * 
574      * @param plugin
575      *            the plugin
576      * @param entry
577      *            the entry
578      * @return the new entry position
579      */
580     private int newPositionConditional( Entry entry, Plugin plugin )
581     {
582         int nPos;
583 
584         if ( entry.getFieldDepend( ) != null )
585         {
586             // case of conditional question only
587             try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_POSITION_CONDITIONAL_QUESTION, plugin ) )
588             {
589 
590                 daoUtil.setInt( 1, entry.getFieldDepend( ).getIdField( ) );
591                 daoUtil.executeQuery( );
592 
593                 if ( daoUtil.next( ) )
594                 {
595                     // if the table is empty
596                     nPos = daoUtil.getInt( 1 ) + 1;
597                 }
598                 else
599                 {
600                     nPos = 1;
601                 }
602 
603             }
604         }
605         else
606         {
607             nPos = 0;
608         }
609 
610         return nPos;
611     }
612 
613     /**
614      * Return the number of conditional question who are associate to the entry
615      * 
616      * @param nIdEntry
617      *            the id of the entry
618      * @param plugin
619      *            the plugin
620      * @return the number of conditional question
621      */
622     private int numberConditionalQuestion( int nIdEntry, Plugin plugin )
623     {
624         int nNumberConditionalQuestion = 0;
625         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NUMBER_CONDITIONAL_QUESTION, plugin ) )
626         {
627             daoUtil.setInt( 1, nIdEntry );
628             daoUtil.executeQuery( );
629 
630             if ( daoUtil.next( ) )
631             {
632                 nNumberConditionalQuestion = daoUtil.getInt( 1 );
633             }
634         }
635         return nNumberConditionalQuestion;
636     }
637 
638     /**
639      * Get values of an entry from the current row of a daoUtil. The class to daoUtil.next( ) will NOT be made by this method.
640      * 
641      * @param daoUtil
642      *            The DAOUtil
643      * @return The entry, or null if the entry was not found
644      */
645     private Entry getEntryValues( DAOUtil daoUtil )
646     {
647         Entry entry;
648 
649         int nIndex = 1;
650         EntryTypericattributes/business/EntryType.html#EntryType">EntryType entryType = new EntryType( );
651         entryType.setIdType( daoUtil.getInt( nIndex++ ) );
652         entryType.setTitle( daoUtil.getString( nIndex++ ) );
653         entryType.setGroup( daoUtil.getBoolean( nIndex++ ) );
654         entryType.setComment( daoUtil.getBoolean( nIndex++ ) );
655         entryType.setBeanName( daoUtil.getString( nIndex++ ) );
656         entryType.setMyLuteceUser( daoUtil.getBoolean( nIndex++ ) );
657         entryType.setIconName( daoUtil.getString( nIndex++ ) );
658 
659         entry = new Entry( );
660 
661         entry.setEntryType( entryType );
662         entry.setIdEntry( daoUtil.getInt( nIndex++ ) );
663 
664         entry.setIdResource( daoUtil.getInt( nIndex++ ) );
665         entry.setResourceType( daoUtil.getString( nIndex++ ) );
666 
667         if ( daoUtil.getObject( nIndex++ ) != null )
668         {
669             Entrynericattributes/business/Entry.html#Entry">Entry entryParent = new Entry( );
670             entryParent.setIdEntry( daoUtil.getInt( nIndex - 1 ) );
671             entry.setParent( entryParent );
672         }
673 
674         entry.setCode( daoUtil.getString( nIndex++ ) );
675         entry.setTitle( daoUtil.getString( nIndex++ ) );
676         entry.setHelpMessage( daoUtil.getString( nIndex++ ) );
677         entry.setComment( daoUtil.getString( nIndex++ ) );
678         entry.setMandatory( daoUtil.getBoolean( nIndex++ ) );
679         entry.setFieldInLine( daoUtil.getBoolean( nIndex++ ) );
680         entry.setPosition( daoUtil.getInt( nIndex++ ) );
681 
682         if ( daoUtil.getObject( nIndex++ ) != null )
683         {
684             Fieldnericattributes/business/Field.html#Field">Field fieldDepend = new Field( );
685             fieldDepend.setIdField( daoUtil.getInt( nIndex - 1 ) );
686             entry.setFieldDepend( fieldDepend );
687         }
688 
689         entry.setUnique( daoUtil.getBoolean( nIndex++ ) );
690         entry.setCSSClass( daoUtil.getString( nIndex++ ) );
691 
692         if ( daoUtil.getInt( nIndex++ ) > 0 )
693         {
694             entry.setPosition( daoUtil.getInt( nIndex - 1 ) );
695         }
696 
697         entry.setErrorMessage( daoUtil.getString( nIndex++ ) );
698         entry.setOnlyDisplayInBack( daoUtil.getBoolean( nIndex++ ) );
699         entry.setIndexed( daoUtil.getBoolean( nIndex ) );
700 
701         return entry;
702     }
703 
704     /**
705      * Return the trim of the title of the entry or null if the entry doesn't have a title
706      * 
707      * @param entry
708      *            The entry to retrieve the title from
709      * @return the trim of the title of the entry or null if the entry doesn't have a title
710      */
711     private String trimEntryTitle( Entry entry )
712     {
713         String strEntryTitle = entry.getTitle( );
714 
715         if ( strEntryTitle != null )
716         {
717             strEntryTitle = strEntryTitle.trim( );
718         }
719 
720         return strEntryTitle;
721     }
722 
723     /**
724      * {@inheritDoc}
725      */
726     @Override
727     public Map<Integer, String> findEntryByForm( Plugin plugin, int nIdForm )
728     {
729         Map<Integer, String> listResult = new HashMap<>( );
730 
731         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ENTRY_BY_FORM, plugin ) )
732         {
733             daoUtil.setInt( 1, nIdForm );
734             daoUtil.executeQuery( );
735 
736             while ( daoUtil.next( ) )
737             {
738                 listResult.put( daoUtil.getInt( 1 ), daoUtil.getString( 2 ) );
739             }
740 
741         }
742 
743         return listResult;
744     }
745 
746     /**
747      * {@inheritDoc}
748      */
749     @Override
750     public String getEntryValueByIdResponse( Plugin plugin, int nIdEntry, int nIdResponse )
751     {
752         String val = null;
753         try ( DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_ENTRY_VALUE, plugin ) )
754         {
755             daoUtil.setInt( 1, nIdEntry );
756             daoUtil.setInt( 2, nIdResponse );
757             daoUtil.executeQuery( );
758 
759             if ( daoUtil.next( ) )
760             {
761                 val = daoUtil.getString( 1 );
762             }
763 
764         }
765 
766         return val;
767     }
768 }