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.business;
35  
36  import fr.paris.lutece.plugins.directory.utils.DirectoryUtils;
37  import fr.paris.lutece.portal.service.plugin.Plugin;
38  import fr.paris.lutece.portal.service.util.AppLogService;
39  import fr.paris.lutece.util.sql.DAOUtil;
40  
41  import java.util.ArrayList;
42  import java.util.List;
43  
44  /**
45   * This class provides Data Access methods for Response objects
46   */
47  public final class RecordFieldDAO implements IRecordFieldDAO
48  {
49      // Constants
50      private static final String SQL_QUERY_NEW_PK = "SELECT MAX( id_record_field ) FROM directory_record_field";
51      private static final String SQL_QUERY_FIND_BY_PRIMARY_KEY = "SELECT "
52              + "drf.id_record_field,drf.id_record,drf.record_field_value,type.class_name,ent.id_entry,ent.title,ent.display_width,ent.display_height, "
53              + "drf.id_field,drf.id_file FROM directory_record_field drf,directory_entry ent,directory_entry_type type  "
54              + "WHERE drf.id_record_field=? and drf.id_entry =ent.id_entry and ent.id_type=type.id_type ";
55      private static final String SQL_QUERY_INSERT = "INSERT INTO directory_record_field( "
56              + "id_record_field,id_record,record_field_value,id_entry,id_field,id_file) VALUES(?,?,?,?,?,?)";
57      private static final String SQL_QUERY_DELETE = "DELETE FROM directory_record_field WHERE id_record_field = ? ";
58      private static final String SQL_QUERY_DELETE_BY_LIST_RECORD_ID = "DELETE FROM directory_record_field WHERE id_record IN ( ?";
59      private static final String SQL_QUERY_UPDATE = "UPDATE  directory_record_field SET "
60              + "id_record_field=?,id_record=?,record_field_value=?,id_entry=?,id_field=?,id_file=? WHERE id_record_field=?";
61      private static final String SQL_QUERY_SELECT_RECORD_FIELD_BY_FILTER = "SELECT "
62              + "drf.id_record_field,drf.id_record,drf.record_field_value,type.class_name,type.id_type,ent.id_entry,ent.title,ent.display_width"
63              + ",ent.display_height,drf.id_field,drf.id_file " + "FROM directory_record_field drf,directory_entry ent,directory_entry_type type ";
64      private static final String SQL_QUERY_SELECT_FULL_RECORD_FIELD_LIST = "SELECT drf.id_record_field,drf.id_record,drf.record_field_value,type.class_name,ent.id_entry,ent.title,ent.display_width,ent.display_height,"
65              + " fil.id_file,fil.title,fil.id_physical_file,fil.file_size,fil.mime_type,"
66              + " dfield.id_field,dfield.id_entry,dfield.title,dfield.default_value,dfield.height,dfield.width,dfield.is_default_value,dfield.max_size_enter,dfield.field_position,dfield.value_type_date,dfield.role_key,dfield.workgroup_key"
67              + " FROM directory_record_field drf "
68              + " INNER JOIN directory_entry ent ON (drf.id_entry=ent.id_entry)"
69              + " INNER JOIN directory_entry_type type ON (ent.id_type=type.id_type) "
70              + " LEFT JOIN directory_file fil ON (drf.id_file=fil.id_file)"
71              + " LEFT JOIN directory_field dfield ON (drf.id_field=dfield.id_field) ";
72      private static final String SQL_QUERY_SELECT_VALUES_RECORD_FIELD_LIST = "SELECT id_record_field, record_field_value FROM directory_record_field drf ";
73      private static final String SQL_QUERY_SELECT_FULL_RECORD_FIELD_LIST_WITH_RECORD = "SELECT drf.id_record_field,drf.id_record,drf.record_field_value,type.class_name,ent.id_entry,ent.title,ent.display_width,ent.display_height,"
74              + " fil.id_file,fil.title,fil.id_physical_file,fil.file_size,fil.mime_type,"
75              + " dfield.id_field,dfield.id_entry,dfield.title,dfield.default_value,dfield.height,dfield.width,dfield.is_default_value,dfield.max_size_enter,dfield.field_position,dfield.value_type_date,dfield.role_key,dfield.workgroup_key,"
76              + " dr.date_creation, dr.id_directory, dr.is_enabled, dr.role_key, dr.workgroup_key, dr.date_modification "
77              + " FROM directory_record_field drf "
78              + " INNER JOIN directory_entry ent ON (drf.id_entry=ent.id_entry)"
79              + " INNER JOIN directory_entry_type type ON (ent.id_type=type.id_type) "
80              + " INNER JOIN directory_record dr ON (dr.id_record = drf.id_record) "
81              + " LEFT JOIN directory_file fil ON (drf.id_file=fil.id_file)"
82              + " LEFT JOIN directory_field dfield ON (drf.id_field=dfield.id_field) ";
83      private static final String SQL_QUERY_COUNT_RECORD_FIELD_BY_FILTER = "SELECT COUNT(drf.id_record_field) "
84              + "FROM directory_record_field drf,directory_entry ent,directory_entry_type type ";
85  
86      // Special query in order to sort numerically and not alphabetically (thus avoiding list like 1, 10, 11, 2, ... instead of 1, 2, ..., 10, 11)
87      private static final String SQL_QUERY_SELECT_MAX_NUMBER = " SELECT drf.record_field_value FROM directory_record_field drf "
88              + " INNER JOIN directory_record dr ON drf.id_record = dr.id_record " + " INNER JOIN directory_entry ent ON drf.id_entry = ent.id_entry "
89              + " WHERE ent.id_entry = ? AND dr.id_directory = ? ORDER BY CAST(drf.record_field_value AS DECIMAL) DESC LIMIT 1 ";
90      private static final String SQL_QUERY_SELECT_BY_RECORD_FIELD_VALUE = " SELECT drf.id_record_field FROM directory_record_field drf "
91              + " INNER JOIN directory_record dr ON drf.id_record = dr.id_record " + " INNER JOIN directory_entry ent ON drf.id_entry = ent.id_entry "
92              + " WHERE ent.id_entry = ? AND dr.id_directory = ? AND drf.record_field_value = ? ";
93      private static final String SQL_FILTER_ID_RECORD = " drf.id_record = ? ";
94      private static final String SQL_FILTER_ID_RECORD_IN = " drf.id_record IN ( ? ";
95      private static final String SQL_FILTER_ID_FIELD = " drf.id_field = ? ";
96      private static final String SQL_FILTER_ID_ENTRY = "  drf.id_entry = ? ";
97      private static final String SQL_FILTER_CONTAINS_FILE = "  drf.id_file IS NOT NULL ";
98      private static final String SQL_FILTER_ID_ENTRY_IN = " AND drf.id_entry IN ( ?";
99      private static final String SQL_FILTER_ADITIONAL_PARAMETER = ",?";
100     private static final String SQL_FILTER_CLOSE_PARENTHESIS = " ) ";
101     private static final String SQL_FILTER_IS_ENTRY_SHOWN_IN_RESULT_LIST = "  ent.is_shown_in_result_list=?";
102     private static final String SQL_FILTER_IS_ENTRY_SHOWN_IN_RESULT_RECORD = "  ent.is_shown_in_result_record=?";
103     private static final String SQL_FILTER_COMMA = ",";
104     private static final String SQL_ORDER_BY_ID_RECORD_FIELD = " ORDER BY ent.entry_position ";
105     private static final String SQL_ORDER_BY_FIELD_POSITION = " dfield.field_position ";
106     private static final String SQL_FILTER_ASSOCIATION_ON_ID_ENTRY = " drf.id_entry =ent.id_entry ";
107     private static final String SQL_FILTER_ASSOCIATION_ON_ID_TYPE = " ent.id_type=type.id_type ";
108     private static final String SQL_WHERE = " WHERE ";
109     private static final String SQL_UPDATE_VALUE_RECORD_FIELD = "UPDATE directory_record_field SET record_field_value = ? WHERE id_record_field = ? ";
110 
111     // Security on files
112     private static final String SQL_QUERY_FIND_BY_FILE = "SELECT "
113             + "drf.id_record_field,drf.id_record,drf.record_field_value,type.class_name,ent.id_entry,ent.title,ent.display_width,ent.display_height, "
114             + "drf.id_field,drf.id_file FROM directory_record_field drf,directory_entry ent,directory_entry_type type  "
115             + "WHERE drf.id_file=? and drf.id_entry =ent.id_entry and ent.id_type=type.id_type ";
116 
117     /**
118      * Generates a new primary key
119      *
120      * @param plugin
121      *            the plugin
122      * @return The new primary key
123      */
124     private int newPrimaryKey( Plugin plugin )
125     {
126         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_NEW_PK, plugin );
127         daoUtil.executeQuery( );
128 
129         int nKey;
130 
131         if ( !daoUtil.next( ) )
132         {
133             // if the table is empty
134             nKey = 1;
135         }
136 
137         nKey = daoUtil.getInt( 1 ) + 1;
138         daoUtil.free( );
139 
140         return nKey;
141     }
142 
143     /**
144      * {@inheritDoc}
145      */
146     @Override
147     public synchronized void insert( RecordField recordField, Plugin plugin )
148     {
149         recordField.setIdRecordField( newPrimaryKey( plugin ) );
150 
151         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_INSERT, plugin );
152 
153         daoUtil.setInt( 1, recordField.getIdRecordField( ) );
154         daoUtil.setInt( 2, recordField.getRecord( ).getIdRecord( ) );
155         daoUtil.setString( 3, recordField.getValue( ) );
156         // daoUtil.setBytes( 3 , recordField.getValue().getBytes() );
157         daoUtil.setInt( 4, recordField.getEntry( ).getIdEntry( ) );
158 
159         if ( recordField.getField( ) != null )
160         {
161             daoUtil.setInt( 5, recordField.getField( ).getIdField( ) );
162         }
163         else
164         {
165             daoUtil.setIntNull( 5 );
166         }
167 
168         if ( recordField.getFile( ) != null )
169         {
170             daoUtil.setInt( 6, recordField.getFile( ).getIdFile( ) );
171         }
172         else
173         {
174             daoUtil.setIntNull( 6 );
175         }
176 
177         daoUtil.executeUpdate( );
178 
179         daoUtil.free( );
180     }
181 
182     /**
183      * {@inheritDoc}
184      */
185     @Override
186     public RecordField load( int nIdRecordField, Plugin plugin )
187     {
188         return load( nIdRecordField, SQL_QUERY_FIND_BY_PRIMARY_KEY, plugin );
189     }
190 
191     /**
192      * {@inheritDoc}
193      */
194     @Override
195     public RecordField loadByFile( int nIdRecordField, Plugin plugin )
196     {
197         return load( nIdRecordField, SQL_QUERY_FIND_BY_FILE, plugin );
198     }
199 
200     private RecordField load( int nIdRecordField, String strSQL, Plugin plugin )
201     {
202         boolean bException = false;
203         RecordField recordField = null;
204         File file = null;
205         IEntry entry = null;
206         EntryType entryType = null;
207         Field field = null;
208         Record record = null;
209         DAOUtil daoUtil = new DAOUtil( strSQL, plugin );
210         daoUtil.setInt( 1, nIdRecordField );
211         daoUtil.executeQuery( );
212 
213         if ( daoUtil.next( ) )
214         {
215             recordField = new RecordField( );
216             recordField.setIdRecordField( daoUtil.getInt( 1 ) );
217             record = new Record( );
218             record.setIdRecord( daoUtil.getInt( 2 ) );
219             recordField.setRecord( record );
220             recordField.setValue( daoUtil.getString( 3 ) );
221 
222             /**
223              * if( daoUtil.getBytes( 3 ) != null ) { recordField.setValue( new String( daoUtil.getBytes( 3 ) ) ); }
224              **/
225             entryType = new EntryType( );
226             entryType.setClassName( daoUtil.getString( 4 ) );
227 
228             try
229             {
230                 entry = (IEntry) Class.forName( entryType.getClassName( ) ).newInstance( );
231             }
232             catch( ClassNotFoundException e )
233             {
234                 // class doesn't exist
235                 AppLogService.error( e );
236                 bException = true;
237             }
238             catch( InstantiationException e )
239             {
240                 // Class is abstract or is an interface or haven't accessible builder
241                 AppLogService.error( e );
242                 bException = true;
243             }
244             catch( IllegalAccessException e )
245             {
246                 // can't access to the class
247                 AppLogService.error( e );
248                 bException = true;
249             }
250 
251             if ( bException )
252             {
253                 daoUtil.free( );
254 
255                 return null;
256             }
257 
258             entry.setEntryType( entryType );
259             entry.setIdEntry( daoUtil.getInt( 5 ) );
260             entry.setTitle( daoUtil.getString( 6 ) );
261             entry.setDisplayWidth( daoUtil.getInt( 7 ) );
262             entry.setDisplayHeight( daoUtil.getInt( 8 ) );
263 
264             recordField.setEntry( entry );
265 
266             if ( daoUtil.getObject( 9 ) != null )
267             {
268                 field = new Field( );
269                 field.setIdField( daoUtil.getInt( 9 ) );
270                 recordField.setField( field );
271             }
272 
273             if ( daoUtil.getObject( 10 ) != null )
274             {
275                 file = new File( );
276                 file.setIdFile( daoUtil.getInt( 10 ) );
277                 recordField.setFile( file );
278             }
279         }
280 
281         daoUtil.free( );
282 
283         return recordField;
284     }
285 
286     /**
287      * {@inheritDoc}
288      */
289     @Override
290     public void delete( int nIdRecordField, Plugin plugin )
291     {
292         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_DELETE, plugin );
293         daoUtil.setInt( 1, nIdRecordField );
294         daoUtil.executeUpdate( );
295         daoUtil.free( );
296     }
297 
298     /**
299      * {@inheritDoc}
300      */
301     @Override
302     public void deleteByListRecordId( List<Integer> lListRecordId, Plugin plugin )
303     {
304         int nListIdSize = lListRecordId.size( );
305 
306         if ( nListIdSize > 0 )
307         {
308             StringBuffer sbSQL = new StringBuffer( SQL_QUERY_DELETE_BY_LIST_RECORD_ID );
309 
310             for ( int i = 1; i < nListIdSize; i++ )
311             {
312                 sbSQL.append( SQL_FILTER_ADITIONAL_PARAMETER );
313             }
314 
315             sbSQL.append( SQL_FILTER_CLOSE_PARENTHESIS );
316 
317             DAOUtil daoUtil = new DAOUtil( sbSQL.toString( ), plugin );
318 
319             for ( int i = 0; i < nListIdSize; i++ )
320             {
321                 daoUtil.setInt( i + 1, lListRecordId.get( i ) );
322             }
323 
324             daoUtil.executeUpdate( );
325             daoUtil.free( );
326         }
327     }
328 
329     /**
330      * {@inheritDoc}
331      */
332     @Override
333     public void store( RecordField recordField, Plugin plugin )
334     {
335         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_UPDATE, plugin );
336 
337         daoUtil.setInt( 1, recordField.getIdRecordField( ) );
338         daoUtil.setInt( 2, recordField.getRecord( ).getIdRecord( ) );
339         daoUtil.setString( 3, recordField.getValue( ) );
340 
341         // daoUtil.setBytes( 3 , recordField.getValue().getBytes() );
342         daoUtil.setInt( 4, recordField.getEntry( ).getIdEntry( ) );
343 
344         if ( recordField.getField( ) != null )
345         {
346             daoUtil.setInt( 5, recordField.getField( ).getIdField( ) );
347         }
348         else
349         {
350             daoUtil.setIntNull( 5 );
351         }
352 
353         if ( recordField.getFile( ) != null )
354         {
355             daoUtil.setInt( 6, recordField.getFile( ).getIdFile( ) );
356         }
357         else
358         {
359             daoUtil.setIntNull( 6 );
360         }
361 
362         daoUtil.setInt( 7, recordField.getIdRecordField( ) );
363         daoUtil.executeUpdate( );
364         daoUtil.free( );
365     }
366 
367     /**
368      * {@inheritDoc}
369      */
370     @Override
371     public List<RecordField> getRecordFieldListByRecordIdList( List<Integer> lIdRecordList, Plugin plugin )
372     {
373         boolean bException = false;
374         List<RecordField> recordFieldList = new ArrayList<RecordField>( );
375         int nIdRecordListSize = lIdRecordList.size( );
376 
377         if ( nIdRecordListSize > 0 )
378         {
379             RecordField recordField;
380             IEntry entry = null;
381             EntryType entryType = null;
382             Field field = null;
383             File file = null;
384             Record record = null;
385             Directory directory = null;
386 
387             StringBuffer sbSQL = new StringBuffer( SQL_QUERY_SELECT_FULL_RECORD_FIELD_LIST_WITH_RECORD );
388 
389             sbSQL.append( SQL_WHERE );
390 
391             for ( int i = 0; i < nIdRecordListSize; i++ )
392             {
393                 if ( i < 1 )
394                 {
395                     sbSQL.append( SQL_FILTER_ID_RECORD_IN );
396                 }
397                 else
398                 {
399                     sbSQL.append( SQL_FILTER_ADITIONAL_PARAMETER );
400                 }
401             }
402 
403             sbSQL.append( SQL_FILTER_CLOSE_PARENTHESIS + SQL_ORDER_BY_ID_RECORD_FIELD );
404 
405             DAOUtil daoUtil = new DAOUtil( sbSQL.toString( ), plugin );
406 
407             for ( int i = 0; i < nIdRecordListSize; i++ )
408             {
409                 daoUtil.setInt( i + 1, lIdRecordList.get( i ) );
410             }
411 
412             daoUtil.executeQuery( );
413 
414             while ( daoUtil.next( ) )
415             {
416                 recordField = new RecordField( );
417                 recordField.setIdRecordField( daoUtil.getInt( 1 ) ); // drf.id_record_field
418                 record = new Record( );
419                 record.setIdRecord( daoUtil.getInt( 2 ) ); // drf.id_record
420 
421                 record.setDateCreation( daoUtil.getTimestamp( 26 ) ); // dr.date_creation
422                 directory = new Directory( );
423                 directory.setIdDirectory( daoUtil.getInt( 27 ) ); // dr.id_directory
424                 record.setDirectory( directory );
425                 record.setEnabled( daoUtil.getBoolean( 28 ) ); // dr.is_enabled
426                 record.setRoleKey( daoUtil.getString( 29 ) ); // dr.role_key
427                 record.setWorkgroup( daoUtil.getString( 30 ) ); // dr.workgroup_key
428                 record.setDateModification( daoUtil.getTimestamp( 31 ) );
429 
430                 recordField.setRecord( record );
431                 recordField.setValue( daoUtil.getString( 3 ) ); // drf.record_field_value
432 
433                 entryType = new EntryType( );
434                 entryType.setClassName( daoUtil.getString( 4 ) ); // type.class_name
435 
436                 try
437                 {
438                     entry = (IEntry) Class.forName( entryType.getClassName( ) ).newInstance( );
439                 }
440                 catch( ClassNotFoundException e )
441                 {
442                     // class doesn't exist
443                     AppLogService.error( e );
444                     bException = true;
445                 }
446                 catch( InstantiationException e )
447                 {
448                     // Class is abstract or is an interface or haven't accessible builder
449                     AppLogService.error( e );
450                     bException = true;
451                 }
452                 catch( IllegalAccessException e )
453                 {
454                     // can't access to rhe class
455                     AppLogService.error( e );
456                     bException = true;
457                 }
458 
459                 if ( bException )
460                 {
461                     daoUtil.free( );
462 
463                     return null;
464                 }
465 
466                 entry.setEntryType( entryType );
467                 entry.setIdEntry( daoUtil.getInt( 5 ) ); // ent.id_entry
468                 entry.setTitle( daoUtil.getString( 6 ) ); // ent.title
469                 entry.setDisplayWidth( daoUtil.getInt( 7 ) ); // ent.display_width
470                 entry.setDisplayHeight( daoUtil.getInt( 8 ) ); // ent.display_height
471                 recordField.setEntry( entry );
472 
473                 if ( daoUtil.getObject( 14 ) != null ) // field.id_field
474                 {
475                     field = new Field( );
476                     field.setIdField( daoUtil.getInt( 14 ) ); // field.id_field
477 
478                     Entry entryField = new Entry( );
479                     entryField.setIdEntry( daoUtil.getInt( 15 ) ); // field.id_entry
480                     field.setEntry( entryField );
481 
482                     field.setTitle( daoUtil.getString( 16 ) ); // field.id_entry
483                     field.setValue( daoUtil.getString( 17 ) ); // field.default_value
484                     field.setHeight( daoUtil.getInt( 18 ) ); // field.height
485                     field.setWidth( daoUtil.getInt( 19 ) ); // field.width
486                     field.setDefaultValue( daoUtil.getBoolean( 20 ) ); // field.default_value
487                     field.setMaxSizeEnter( daoUtil.getInt( 21 ) ); // field.max_size_enter
488                     field.setPosition( daoUtil.getInt( 22 ) ); // field.field_position
489                     field.setValueTypeDate( daoUtil.getDate( 23 ) ); // field.value_type_date
490                     field.setRoleKey( daoUtil.getString( 24 ) ); // field.role_key
491                     field.setWorkgroup( daoUtil.getString( 25 ) ); // field.workgroup_key
492 
493                     recordField.setField( field );
494                 }
495 
496                 if ( daoUtil.getObject( 9 ) != null ) // fil.id_file
497                 {
498                     file = new File( );
499                     file.setIdFile( daoUtil.getInt( 9 ) ); // fil.id_file
500                     file.setTitle( daoUtil.getString( 10 ) ); // fil.title
501 
502                     PhysicalFile pf = new PhysicalFile( );
503                     pf.setIdPhysicalFile( daoUtil.getInt( 11 ) ); // fil.id_physical_file
504                     file.setPhysicalFile( pf );
505                     file.setSize( daoUtil.getInt( 12 ) ); // fil.file_size
506                     file.setMimeType( daoUtil.getString( 13 ) ); // fil.mime_type
507                     recordField.setFile( file );
508                 }
509 
510                 recordFieldList.add( recordField );
511             }
512 
513             daoUtil.free( );
514         }
515 
516         return recordFieldList;
517     }
518 
519     /**
520      * {@inheritDoc}
521      */
522     @Override
523     public List<RecordField> selectSpecificList( List<Integer> lEntryId, Integer nIdRecord, Plugin plugin )
524     {
525         boolean bException = false;
526         List<RecordField> recordFieldList = new ArrayList<RecordField>( );
527         RecordField recordField;
528         IEntry entry = null;
529         EntryType entryType = null;
530         Field field = null;
531         File file = null;
532         Record record = null;
533 
534         StringBuffer sbSQL = new StringBuffer( SQL_QUERY_SELECT_FULL_RECORD_FIELD_LIST );
535 
536         sbSQL.append( SQL_WHERE + SQL_FILTER_ID_RECORD );
537 
538         int nListEntryIdSize = lEntryId.size( );
539 
540         if ( nListEntryIdSize > 0 )
541         {
542             for ( int i = 0; i < nListEntryIdSize; i++ )
543             {
544                 if ( i < 1 )
545                 {
546                     sbSQL.append( SQL_FILTER_ID_ENTRY_IN );
547                 }
548                 else
549                 {
550                     sbSQL.append( SQL_FILTER_ADITIONAL_PARAMETER );
551                 }
552             }
553 
554             sbSQL.append( SQL_FILTER_CLOSE_PARENTHESIS );
555         }
556 
557         sbSQL.append( SQL_ORDER_BY_ID_RECORD_FIELD + SQL_FILTER_COMMA + SQL_ORDER_BY_FIELD_POSITION );
558 
559         DAOUtil daoUtil = new DAOUtil( sbSQL.toString( ), plugin );
560         daoUtil.setInt( 1, nIdRecord );
561 
562         if ( nListEntryIdSize > 0 )
563         {
564             for ( int i = 0; i < nListEntryIdSize; i++ )
565             {
566                 daoUtil.setInt( i + 2, lEntryId.get( i ) );
567             }
568         }
569 
570         daoUtil.executeQuery( );
571 
572         while ( daoUtil.next( ) )
573         {
574             recordField = new RecordField( );
575             recordField.setIdRecordField( daoUtil.getInt( 1 ) ); // drf.id_record_field
576             record = new Record( );
577             record.setIdRecord( daoUtil.getInt( 2 ) ); // drf.id_record
578             recordField.setRecord( record );
579             recordField.setValue( daoUtil.getString( 3 ) ); // drf.record_field_value
580 
581             entryType = new EntryType( );
582             entryType.setClassName( daoUtil.getString( 4 ) ); // type.class_name
583 
584             try
585             {
586                 entry = (IEntry) Class.forName( entryType.getClassName( ) ).newInstance( );
587             }
588             catch( ClassNotFoundException e )
589             {
590                 // class doesn't exist
591                 AppLogService.error( e );
592                 bException = true;
593             }
594             catch( InstantiationException e )
595             {
596                 // Class is abstract or is an interface or haven't accessible builder
597                 AppLogService.error( e );
598                 bException = true;
599             }
600             catch( IllegalAccessException e )
601             {
602                 // can't access to rhe class
603                 AppLogService.error( e );
604                 bException = true;
605             }
606 
607             if ( bException )
608             {
609                 daoUtil.free( );
610 
611                 return null;
612             }
613 
614             entry.setEntryType( entryType );
615             entry.setIdEntry( daoUtil.getInt( 5 ) ); // ent.id_entry
616             entry.setTitle( daoUtil.getString( 6 ) ); // ent.title
617             entry.setDisplayWidth( daoUtil.getInt( 7 ) ); // ent.display_width
618             entry.setDisplayHeight( daoUtil.getInt( 8 ) ); // ent.display_height
619             recordField.setEntry( entry );
620 
621             if ( daoUtil.getObject( 14 ) != null ) // field.id_field
622             {
623                 field = new Field( );
624                 field.setIdField( daoUtil.getInt( 14 ) ); // field.id_field
625 
626                 Entry entryField = new Entry( );
627                 entryField.setIdEntry( daoUtil.getInt( 15 ) ); // field.id_entry
628                 field.setEntry( entryField );
629 
630                 field.setTitle( daoUtil.getString( 16 ) ); // field.id_entry
631                 field.setValue( daoUtil.getString( 17 ) ); // field.default_value
632                 field.setHeight( daoUtil.getInt( 18 ) ); // field.height
633                 field.setWidth( daoUtil.getInt( 19 ) ); // field.width
634                 field.setDefaultValue( daoUtil.getBoolean( 20 ) ); // field.default_value
635                 field.setMaxSizeEnter( daoUtil.getInt( 21 ) ); // field.max_size_enter
636                 field.setPosition( daoUtil.getInt( 22 ) ); // field.field_position
637                 field.setValueTypeDate( daoUtil.getDate( 23 ) ); // field.value_type_date
638                 field.setRoleKey( daoUtil.getString( 24 ) ); // field.role_key
639                 field.setWorkgroup( daoUtil.getString( 25 ) ); // field.workgroup_key
640 
641                 recordField.setField( field );
642             }
643 
644             if ( daoUtil.getObject( 9 ) != null ) // fil.id_file
645             {
646                 file = new File( );
647                 file.setIdFile( daoUtil.getInt( 9 ) ); // fil.id_file
648                 file.setTitle( daoUtil.getString( 10 ) ); // fil.title
649 
650                 PhysicalFile pf = new PhysicalFile( );
651                 pf.setIdPhysicalFile( daoUtil.getInt( 11 ) ); // fil.id_physical_file
652                 file.setPhysicalFile( pf );
653                 file.setSize( daoUtil.getInt( 12 ) ); // fil.file_size
654                 file.setMimeType( daoUtil.getString( 13 ) ); // fil.mime_type
655                 recordField.setFile( file );
656             }
657 
658             recordFieldList.add( recordField );
659         }
660 
661         daoUtil.free( );
662 
663         return recordFieldList;
664     }
665 
666     /**
667      * {@inheritDoc}
668      */
669     @Override
670     public List<RecordField> selectListByFilter( RecordFieldFilter filter, Plugin plugin )
671     {
672         boolean bException = false;
673         List<RecordField> recordFieldList = new ArrayList<RecordField>( );
674         RecordField recordField;
675         IEntry entry = null;
676         EntryType entryType = null;
677         Field field = null;
678         File file = null;
679         Record record = null;
680 
681         List<String> listStrFilter = new ArrayList<String>( );
682         listStrFilter.add( SQL_FILTER_ASSOCIATION_ON_ID_ENTRY );
683         listStrFilter.add( SQL_FILTER_ASSOCIATION_ON_ID_TYPE );
684 
685         if ( filter.containsIdRecord( ) )
686         {
687             listStrFilter.add( SQL_FILTER_ID_RECORD );
688         }
689 
690         if ( filter.containsIdField( ) )
691         {
692             listStrFilter.add( SQL_FILTER_ID_FIELD );
693         }
694 
695         if ( filter.containsIdEntry( ) )
696         {
697             listStrFilter.add( SQL_FILTER_ID_ENTRY );
698         }
699 
700         if ( filter.containsIsEntryShownInResultList( ) )
701         {
702             listStrFilter.add( SQL_FILTER_IS_ENTRY_SHOWN_IN_RESULT_LIST );
703         }
704 
705         if ( filter.containsIsEntryShownInResultRecord( ) )
706         {
707             listStrFilter.add( SQL_FILTER_IS_ENTRY_SHOWN_IN_RESULT_RECORD );
708         }
709 
710         if ( filter.containsFile( ) )
711         {
712             listStrFilter.add( SQL_FILTER_CONTAINS_FILE );
713         }
714 
715         String strSQL = DirectoryUtils.buildRequetteWithFilter( SQL_QUERY_SELECT_RECORD_FIELD_BY_FILTER, listStrFilter, SQL_ORDER_BY_ID_RECORD_FIELD );
716 
717         DAOUtil daoUtil = new DAOUtil( strSQL, plugin );
718         int nIndex = 1;
719 
720         if ( filter.containsIdRecord( ) )
721         {
722             daoUtil.setInt( nIndex, filter.getIdRecord( ) );
723             nIndex++;
724         }
725 
726         if ( filter.containsIdField( ) )
727         {
728             daoUtil.setInt( nIndex, filter.getIdField( ) );
729             nIndex++;
730         }
731 
732         if ( filter.containsIdEntry( ) )
733         {
734             daoUtil.setInt( nIndex, filter.getIdEntry( ) );
735             nIndex++;
736         }
737 
738         if ( filter.containsIsEntryShownInResultList( ) )
739         {
740             daoUtil.setBoolean( nIndex, filter.getIsEntryShownInResultList( ) == RecordFieldFilter.FILTER_TRUE );
741             nIndex++;
742         }
743 
744         if ( filter.containsIsEntryShownInResultRecord( ) )
745         {
746             daoUtil.setBoolean( nIndex, filter.getIsEntryShownInResultRecord( ) == RecordFieldFilter.FILTER_TRUE );
747             nIndex++;
748         }
749 
750         daoUtil.executeQuery( );
751 
752         while ( daoUtil.next( ) )
753         {
754             recordField = new RecordField( );
755             recordField.setIdRecordField( daoUtil.getInt( 1 ) );
756             record = new Record( );
757             record.setIdRecord( daoUtil.getInt( 2 ) );
758             recordField.setRecord( record );
759             recordField.setValue( daoUtil.getString( 3 ) );
760             /**
761              * if( daoUtil.getBytes( 3 ) != null ) { recordField.setValue( new String( daoUtil.getBytes( 3 ) ) ); }
762              **/
763             entryType = new EntryType( );
764             entryType.setClassName( daoUtil.getString( 4 ) );
765 
766             try
767             {
768                 entry = (IEntry) Class.forName( entryType.getClassName( ) ).newInstance( );
769             }
770             catch( ClassNotFoundException e )
771             {
772                 // class doesn't exist
773                 AppLogService.error( e );
774                 bException = true;
775             }
776             catch( InstantiationException e )
777             {
778                 // Class is abstract or is an interface or haven't accessible builder
779                 AppLogService.error( e );
780                 bException = true;
781             }
782             catch( IllegalAccessException e )
783             {
784                 // can't access to rhe class
785                 AppLogService.error( e );
786                 bException = true;
787             }
788 
789             if ( bException )
790             {
791                 daoUtil.free( );
792 
793                 return null;
794             }
795 
796             entryType.setIdType( daoUtil.getInt( 5 ) );
797             entry.setEntryType( entryType );
798             entry.setIdEntry( daoUtil.getInt( 6 ) );
799             entry.setTitle( daoUtil.getString( 7 ) );
800             entry.setDisplayWidth( daoUtil.getInt( 8 ) );
801             entry.setDisplayHeight( daoUtil.getInt( 9 ) );
802             recordField.setEntry( entry );
803 
804             if ( daoUtil.getObject( 10 ) != null )
805             {
806                 field = new Field( );
807                 field.setIdField( daoUtil.getInt( 10 ) );
808                 recordField.setField( field );
809             }
810 
811             if ( daoUtil.getObject( 11 ) != null )
812             {
813                 file = new File( );
814                 file.setIdFile( daoUtil.getInt( 11 ) );
815                 recordField.setFile( file );
816             }
817 
818             recordFieldList.add( recordField );
819         }
820 
821         daoUtil.free( );
822 
823         return recordFieldList;
824     }
825 
826     /**
827      * {@inheritDoc}
828      */
829     @Override
830     public int getCountByFilter( RecordFieldFilter filter, Plugin plugin )
831     {
832         int nCount = 0;
833 
834         List<String> listStrFilter = new ArrayList<String>( );
835         listStrFilter.add( SQL_FILTER_ASSOCIATION_ON_ID_ENTRY );
836         listStrFilter.add( SQL_FILTER_ASSOCIATION_ON_ID_TYPE );
837 
838         if ( filter.containsIdRecord( ) )
839         {
840             listStrFilter.add( SQL_FILTER_ID_RECORD );
841         }
842 
843         if ( filter.containsIdField( ) )
844         {
845             listStrFilter.add( SQL_FILTER_ID_FIELD );
846         }
847 
848         if ( filter.containsIdEntry( ) )
849         {
850             listStrFilter.add( SQL_FILTER_ID_ENTRY );
851         }
852 
853         if ( filter.containsIsEntryShownInResultList( ) )
854         {
855             listStrFilter.add( SQL_FILTER_IS_ENTRY_SHOWN_IN_RESULT_LIST );
856         }
857 
858         if ( filter.containsIsEntryShownInResultRecord( ) )
859         {
860             listStrFilter.add( SQL_FILTER_IS_ENTRY_SHOWN_IN_RESULT_RECORD );
861         }
862 
863         String strSQL = DirectoryUtils.buildRequetteWithFilter( SQL_QUERY_COUNT_RECORD_FIELD_BY_FILTER, listStrFilter, null );
864 
865         DAOUtil daoUtil = new DAOUtil( strSQL, plugin );
866         int nIndex = 1;
867 
868         if ( filter.containsIdRecord( ) )
869         {
870             daoUtil.setInt( nIndex, filter.getIdRecord( ) );
871             nIndex++;
872         }
873 
874         if ( filter.containsIdField( ) )
875         {
876             daoUtil.setInt( nIndex, filter.getIdField( ) );
877             nIndex++;
878         }
879 
880         if ( filter.containsIdEntry( ) )
881         {
882             daoUtil.setInt( nIndex, filter.getIdEntry( ) );
883             nIndex++;
884         }
885 
886         if ( filter.containsIsEntryShownInResultList( ) )
887         {
888             daoUtil.setBoolean( nIndex, filter.getIsEntryShownInResultList( ) == RecordFieldFilter.FILTER_TRUE );
889             nIndex++;
890         }
891 
892         if ( filter.containsIsEntryShownInResultRecord( ) )
893         {
894             daoUtil.setBoolean( nIndex, filter.getIsEntryShownInResultRecord( ) == RecordFieldFilter.FILTER_TRUE );
895             nIndex++;
896         }
897 
898         daoUtil.executeQuery( );
899 
900         if ( daoUtil.next( ) )
901         {
902             nCount = daoUtil.getInt( 1 );
903         }
904 
905         daoUtil.free( );
906 
907         return nCount;
908     }
909 
910     /**
911      * {@inheritDoc}
912      */
913     @Override
914     public int getMaxNumber( int nIdEntry, int nIdDirectory, Plugin plugin )
915     {
916         int nIndex = 1;
917         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_MAX_NUMBER, plugin );
918         daoUtil.setInt( nIndex++, nIdEntry );
919         daoUtil.setInt( nIndex++, nIdDirectory );
920         daoUtil.executeQuery( );
921 
922         int nKey = 1;
923 
924         if ( daoUtil.next( ) )
925         {
926             nKey = daoUtil.getInt( 1 ) + 1;
927         }
928 
929         daoUtil.free( );
930 
931         return nKey;
932     }
933 
934     /**
935      * {@inheritDoc}
936      */
937     @Override
938     public boolean isNumberOnARecordField( int nIdEntry, int nIdDirectory, int nNumber, Plugin plugin )
939     {
940         int nIndex = 1;
941         DAOUtil daoUtil = new DAOUtil( SQL_QUERY_SELECT_BY_RECORD_FIELD_VALUE, plugin );
942         daoUtil.setInt( nIndex++, nIdEntry );
943         daoUtil.setInt( nIndex++, nIdDirectory );
944         daoUtil.setInt( nIndex++, nNumber );
945         daoUtil.executeQuery( );
946 
947         boolean isOn = false;
948 
949         if ( daoUtil.next( ) )
950         {
951             isOn = true;
952         }
953 
954         daoUtil.free( );
955 
956         return isOn;
957     }
958 
959     /**
960      * {@inheritDoc}
961      */
962     @Override
963     public List<RecordField> selectValuesList( List<Integer> lEntryId, Integer nIdRecord, Plugin plugin )
964     {
965         List<RecordField> recordFieldList = new ArrayList<RecordField>( );
966         RecordField recordField;
967         StringBuffer sbSQL = new StringBuffer( SQL_QUERY_SELECT_VALUES_RECORD_FIELD_LIST );
968 
969         sbSQL.append( SQL_WHERE + SQL_FILTER_ID_RECORD );
970 
971         int nListEntryIdSize = lEntryId.size( );
972 
973         if ( nListEntryIdSize > 0 )
974         {
975             for ( int i = 0; i < nListEntryIdSize; i++ )
976             {
977                 if ( i < 1 )
978                 {
979                     sbSQL.append( SQL_FILTER_ID_ENTRY_IN );
980                 }
981                 else
982                 {
983                     sbSQL.append( SQL_FILTER_ADITIONAL_PARAMETER );
984                 }
985             }
986 
987             sbSQL.append( SQL_FILTER_CLOSE_PARENTHESIS );
988         }
989 
990         DAOUtil daoUtil = new DAOUtil( sbSQL.toString( ), plugin );
991         daoUtil.setInt( 1, nIdRecord );
992 
993         if ( nListEntryIdSize > 0 )
994         {
995             for ( int i = 0; i < nListEntryIdSize; i++ )
996             {
997                 daoUtil.setInt( i + 2, lEntryId.get( i ) );
998             }
999         }
1000 
1001         daoUtil.executeQuery( );
1002 
1003         while ( daoUtil.next( ) )
1004         {
1005             recordField = new RecordField( );
1006             recordField.setIdRecordField( daoUtil.getInt( 1 ) ); // drf.id_record_field
1007             recordField.setValue( daoUtil.getString( 2 ) ); // drf.record_field_value
1008 
1009             recordFieldList.add( recordField );
1010         }
1011 
1012         daoUtil.free( );
1013 
1014         return recordFieldList;
1015     }
1016 
1017     /**
1018      * {@inheritDoc}
1019      */
1020     @Override
1021     public void updateValue( String strNewValue, Integer nIdRecordField, Plugin plugin )
1022     {
1023         DAOUtil daoUtil = new DAOUtil( SQL_UPDATE_VALUE_RECORD_FIELD, plugin );
1024         daoUtil.setString( 1, strNewValue );
1025         daoUtil.setInt( 2, nIdRecordField );
1026         daoUtil.executeUpdate( );
1027         daoUtil.free( );
1028     }
1029 }