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.directory.modules.rest.service;
35  
36  import fr.paris.lutece.plugins.directory.business.Directory;
37  import fr.paris.lutece.plugins.directory.business.DirectoryFilter;
38  import fr.paris.lutece.plugins.directory.business.DirectoryHome;
39  import fr.paris.lutece.plugins.directory.business.EntryFilter;
40  import fr.paris.lutece.plugins.directory.business.EntryHome;
41  import fr.paris.lutece.plugins.directory.business.Field;
42  import fr.paris.lutece.plugins.directory.business.FieldHome;
43  import fr.paris.lutece.plugins.directory.business.IEntry;
44  import fr.paris.lutece.plugins.directory.business.PhysicalFileHome;
45  import fr.paris.lutece.plugins.directory.business.Record;
46  import fr.paris.lutece.plugins.directory.business.RecordField;
47  import fr.paris.lutece.plugins.directory.business.RecordFieldFilter;
48  import fr.paris.lutece.plugins.directory.business.RecordFieldHome;
49  import fr.paris.lutece.plugins.directory.business.RecordHome;
50  import fr.paris.lutece.plugins.directory.modules.rest.util.constants.DirectoryRestConstants;
51  import fr.paris.lutece.plugins.directory.service.DirectoryPlugin;
52  import fr.paris.lutece.plugins.directory.utils.DirectoryErrorException;
53  import fr.paris.lutece.plugins.directory.utils.DirectoryUtils;
54  import fr.paris.lutece.plugins.directory.web.action.DirectoryAdminSearchFields;
55  import fr.paris.lutece.portal.service.plugin.Plugin;
56  import fr.paris.lutece.portal.service.plugin.PluginService;
57  import fr.paris.lutece.portal.service.util.AppException;
58  import fr.paris.lutece.portal.service.util.AppLogService;
59  import fr.paris.lutece.portal.service.util.AppPropertiesService;
60  import fr.paris.lutece.portal.service.workflow.WorkflowService;
61  import fr.paris.lutece.portal.web.upload.MultipartHttpServletRequest;
62  import fr.paris.lutece.util.http.MultipartUtil;
63  import fr.paris.lutece.util.sql.TransactionManager;
64  
65  import org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException;
66  import org.apache.commons.fileupload.FileUploadException;
67  import org.apache.commons.lang.StringUtils;
68  
69  import java.util.ArrayList;
70  import java.util.Enumeration;
71  import java.util.HashMap;
72  import java.util.Iterator;
73  import java.util.List;
74  import java.util.Map;
75  
76  import javax.servlet.ServletRequest;
77  import javax.servlet.http.HttpServletRequest;
78  
79  
80  /**
81   *
82   * DirectoryRestService
83   *
84   */
85  public class DirectoryRestService implements IDirectoryRestService
86  {
87      private static final String PARAMETER_DIRECTORY_ID = "directoryId";
88      private static final String PARAMETER_RECORD_ID = "recordId";
89  
90      /** use this parameter to avoid workflow usage */
91      private static final String PARAMETER_NO_WORKFLOW = "noWorkflow";
92  
93      // GET
94  
95      /**
96       * {@inheritDoc}
97       */
98      @Override
99      public Record getRecord( int nIdRecord, HttpServletRequest request )
100         throws DirectoryRestException, DirectoryErrorException
101     {
102         Record record = RecordHome.findByPrimaryKey( nIdRecord, getPlugin(  ) );
103 
104         List<Integer> listIdsEntry = getIdsEntry( record.getDirectory(  ).getIdDirectory(  ), request );
105 
106         return getRecord( nIdRecord, listIdsEntry,getMapFieldEntryByListIdEntry(listIdsEntry) );
107     }
108 
109     
110 
111     /**
112      * {@inheritDoc}
113      */
114     @Override
115     public List<Record> getRecordsList( int nIdDirectory, HttpServletRequest request )
116         throws DirectoryRestException, DirectoryErrorException
117     {
118         Directory directory = getDirectory( nIdDirectory );
119         Plugin pluginDirectory = PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME );
120 
121         DirectoryAdminSearchFields searchFields = new DirectoryAdminSearchFields(  );
122         Enumeration paramNames = request.getParameterNames(  );
123         HashMap<String, List<RecordField>> mapQuery = null;
124 
125         if ( paramNames.hasMoreElements(  ) )
126         {
127             mapQuery = DirectoryUtils.getSearchRecordData( request, nIdDirectory, DirectoryUtils.getPlugin(  ),
128                     request.getLocale(  ) );
129         }
130 
131         searchFields.setMapQuery( mapQuery );
132         searchFields.setSortParameters( request, directory, pluginDirectory );
133 
134         boolean bWorkflowServiceEnable = WorkflowService.getInstance(  ).isAvailable(  );
135 
136         List<Record> listRecords = null;
137         List<Integer> listIdsEntry = getIdsEntry( nIdDirectory, request );
138 
139         List<Integer> listIdsRecord = DirectoryUtils.getListResults( request, directory, bWorkflowServiceEnable, true,
140                 searchFields, null, request.getLocale(  ) );
141         
142        
143         
144         listRecords = new ArrayList<Record>(  );
145         
146         Map<Integer,Field> mapFieldEntry= getMapFieldEntryByListIdEntry(listIdsEntry);
147 
148         if ( listIdsRecord != null )
149         {
150             int nMaxNumber = AppPropertiesService.getPropertyInt( DirectoryRestConstants.PROPERTY_MAX_NUMBER_RECORDS,
151                     100 );
152 
153             for ( int i = 0; ( i < nMaxNumber ) && ( i < listIdsRecord.size(  ) ); i++ )
154             {
155                 int nIdRecord = listIdsRecord.get( i );
156                 Record record = getRecord( nIdRecord, listIdsEntry,mapFieldEntry);
157                 listRecords.add( record );
158             }
159         }
160 
161         return listRecords;
162     }
163 
164     /**
165      * {@inheritDoc}
166      */
167     @Override
168     public List<IEntry> getEntries( Record record )
169     {
170         List<IEntry> listEntries = new ArrayList<IEntry>(  );
171 
172         if ( record.getListRecordField(  ) != null )
173         {
174             Plugin pluginDirectory = PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME );
175 
176             for ( RecordField recordField : record.getListRecordField(  ) )
177             {
178                 if ( recordField.getEntry(  ) != null )
179                 {
180                     int nIdEntry = recordField.getEntry(  ).getIdEntry(  );
181                     IEntry entry = EntryHome.findByPrimaryKey( nIdEntry, pluginDirectory );
182 
183                     if ( ( entry != null ) && !listEntries.contains( entry ) )
184                     {
185                         listEntries.add( entry );
186                     }
187                 }
188             }
189         }
190 
191         return listEntries;
192     }
193 
194     /**
195      * {@inheritDoc}
196      */
197     @Override
198     public List<Integer> getIdsEntry( int nIdDirectory, HttpServletRequest request )
199     {
200         List<Integer> listIdsEntry = new ArrayList<Integer>(  );
201         String[] strIdsEntry = request.getParameterValues( DirectoryRestConstants.PARAMETER_ID_ENTRY_FILTER );
202 
203         if ( strIdsEntry != null )
204         {
205             Plugin pluginDirectory = PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME );
206 
207             for ( String strIdEntry : strIdsEntry )
208             {
209                 if ( StringUtils.isNotBlank( strIdEntry ) && StringUtils.isNumeric( strIdEntry ) )
210                 {
211                     // Check if the entry in the parameter is indeed from the directory
212                     int nIdEntry = Integer.parseInt( strIdEntry );
213                     IEntry entry = EntryHome.findByPrimaryKey( nIdEntry, pluginDirectory );
214 
215                     if ( ( entry != null ) && ( entry.getDirectory(  ) != null ) &&
216                             ( entry.getDirectory(  ).getIdDirectory(  ) == nIdDirectory ) )
217                     {
218                         listIdsEntry.add( nIdEntry );
219                     }
220                 }
221             }
222         }
223         else
224         {
225             listIdsEntry = getIdsEntry( nIdDirectory );
226         }
227 
228         return listIdsEntry;
229     }
230 
231     /**
232      * {@inheritDoc}
233      */
234     @Override
235     public List<Integer> getIdsEntry( int nIdDirectory )
236     {
237         List<Integer> listIdsEntry = new ArrayList<Integer>(  );
238 
239         EntryFilter filter = new EntryFilter(  );
240         filter.setIdDirectory( nIdDirectory );
241         filter.setIsComment( EntryFilter.FILTER_FALSE );
242         filter.setIsEntryParentNull( EntryFilter.FILTER_TRUE );
243 
244         List<IEntry> listEntryFirstLevel = EntryHome.getEntryList( filter, getPlugin(  ) );
245 
246         for ( IEntry entry : listEntryFirstLevel )
247         {
248             IEntry entryFistLevel = EntryHome.findByPrimaryKey( entry.getIdEntry(  ), getPlugin(  ) );
249 
250             if ( ( entryFistLevel != null ) && entryFistLevel.getEntryType(  ).getGroup(  ) )
251             {
252                 filter = new EntryFilter(  );
253                 filter.setIdEntryParent( entryFistLevel.getIdEntry(  ) );
254 
255                 for ( IEntry entryChildren : EntryHome.getEntryList( filter, getPlugin(  ) ) )
256                 {
257                     IEntry entryTmp = EntryHome.findByPrimaryKey( entryChildren.getIdEntry(  ), getPlugin(  ) );
258 
259                     if ( entryTmp != null )
260                     {
261                         listIdsEntry.add( entryTmp.getIdEntry(  ) );
262                     }
263                 }
264             }
265 
266             listIdsEntry.add( entry.getIdEntry(  ) );
267         }
268 
269         return listIdsEntry;
270     }
271 
272     /**
273      * {@inheritDoc}
274      */
275     @Override
276     public Directory getDirectory( int nIdDirectory )
277     {
278         Plugin plugin = PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME );
279 
280         return DirectoryHome.findByPrimaryKey( nIdDirectory, plugin );
281     }
282 
283     /**
284      * {@inheritDoc}
285      */
286     @Override
287     public List<Directory> getDirectoriesList(  )
288     {
289         Plugin plugin = PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME );
290 
291         return DirectoryHome.getDirectoryList( new DirectoryFilter(  ), plugin );
292     }
293 
294     /**
295      * {@inheritDoc}
296      */
297     @Override
298     public Map<String, List<RecordField>> getMapIdEntryListRecordField( Record record )
299     {
300         Map<String, List<RecordField>> map = new HashMap<String, List<RecordField>>(  );
301 
302         if ( ( record.getListRecordField(  ) != null ) && !record.getListRecordField(  ).isEmpty(  ) )
303         {
304             for ( RecordField recordField : record.getListRecordField(  ) )
305             {
306                 int nIdEntry = recordField.getEntry(  ).getIdEntry(  );
307                 List<RecordField> listRecordFields = map.get( Integer.toString( nIdEntry ) );
308 
309                 if ( listRecordFields == null )
310                 {
311                     listRecordFields = new ArrayList<RecordField>(  );
312                 }
313 
314                 listRecordFields.add( recordField );
315                 map.put( Integer.toString( nIdEntry ), listRecordFields );
316             }
317         }
318 
319         return map;
320     }
321 
322     // ACTION
323 
324     /**
325      * {@inheritDoc}
326      */
327     @Override
328     public Record addToDirectory( String strIdDirectory, ServletRequest request )
329         throws DirectoryErrorException
330     {
331         int nDirectoryId = Integer.parseInt( strIdDirectory );
332         Directory directory = DirectoryHome.findByPrimaryKey( nDirectoryId, getPlugin(  ) );
333 
334         Record record = new Record(  );
335         record.setDirectory( directory );
336         record.setDateCreation( DirectoryUtils.getCurrentTimestamp(  ) );
337         record.setEnabled( directory.isRecordActivated(  ) );
338 
339         List<RecordField> listRecordFields = getRecordFields( (HttpServletRequest) request, record );
340 
341         record.setListRecordField( listRecordFields );
342 
343         // do not use the workflow if creation is partial
344         String strNoWorkflowInit = request.getParameter( PARAMETER_NO_WORKFLOW );
345 
346         TransactionManager.beginTransaction( getPlugin(  ) );
347 
348         try
349         {
350             //save the Record and the RecordFiels
351             record.setIdRecord( RecordHome.create( record, getPlugin(  ) ) );
352 
353             if ( StringUtils.isBlank( strNoWorkflowInit ) && isEntrySet( listRecordFields, nDirectoryId ) )
354             {
355                 doWorkflowActions( record, directory );
356             }
357 
358             TransactionManager.commitTransaction( getPlugin(  ) );
359         }
360         catch ( Exception e )
361         {
362             TransactionManager.rollBack( getPlugin(  ) );
363             throw new AppException( e.getMessage(  ), e );
364         }
365 
366         return record;
367     }
368 
369     /**
370      * {@inheritDoc}
371      */
372     @Override
373     public Record insertOrCompleteRecord( HttpServletRequest request )
374         throws DirectoryErrorException, DirectoryRestException
375     {
376         String strIdRecord = request.getParameter( PARAMETER_RECORD_ID );
377 
378         if ( StringUtils.isNotBlank( strIdRecord ) && StringUtils.isNumeric( strIdRecord ) )
379         {
380             // strRecordId ==> update
381             if ( AppLogService.isDebugEnabled(  ) )
382             {
383                 AppLogService.debug( "Record id found, updating record " + strIdRecord );
384             }
385 
386             int nIdRecord = Integer.parseInt( strIdRecord );
387 
388             return completeRecord( nIdRecord, request );
389         }
390 
391         // strRecordId == null ==> create, strDirectoryId should not be null
392         String strDirectoryId = request.getParameter( PARAMETER_DIRECTORY_ID );
393 
394         if ( AppLogService.isDebugEnabled(  ) )
395         {
396             AppLogService.debug( "Directory id found, inserting record into directory " + strDirectoryId );
397         }
398 
399         return addToDirectory( strDirectoryId, request );
400     }
401 
402     /**
403      * {@inheritDoc}
404      */
405     @Override
406     public String deleteRecord( int nIdRecord, HttpServletRequest request )
407         throws DirectoryErrorException, DirectoryRestException
408     {
409         if ( AppLogService.isDebugEnabled(  ) )
410         {
411             AppLogService.debug( "Record id found, deleting record " + nIdRecord );
412         }
413 
414         Record record = getRecord( nIdRecord, request );
415         RecordHome.remove( record.getIdRecord(  ), getPlugin(  ) );
416         WorkflowService.getInstance(  ).doRemoveWorkFlowResource( record.getIdRecord(  ), Record.WORKFLOW_RESOURCE_TYPE );
417 
418         return "record deleted";
419     }
420 
421     /**
422      * {@inheritDoc}
423      */
424     @Override
425     public Record completeRecord( int nIdRecord, ServletRequest request )
426         throws DirectoryErrorException, DirectoryRestException
427     {
428         Record record = getRecord( nIdRecord, (HttpServletRequest) request );
429 
430         List<RecordField> listRecordFields = getRecordFields( (HttpServletRequest) request, record );
431 
432         List<RecordField> listOldRecordField = record.getListRecordField(  );
433         // remove all non-relevant old record fields (null record fields)
434         removeNullRecordFields( listOldRecordField );
435 
436         for ( RecordField oldRecordField : listOldRecordField )
437         {
438             int nIdEntry = oldRecordField.getEntry(  ).getIdEntry(  );
439 
440             // idField should be 0 IF AND ONLY IF the entry is a "file". 
441             int nIdField = ( oldRecordField.getField(  ) == null ) ? 0 : oldRecordField.getField(  ).getIdField(  );
442 
443             RecordField submitRecordField = findRecordField( nIdEntry, nIdField, listRecordFields );
444 
445             if ( submitRecordField == null )
446             {
447                 listRecordFields.add( oldRecordField );
448             }
449             else
450             {
451                 // old value is kept and NOT modified, the new value is dropped.
452                 if ( oldRecordField.getFile(  ) != null )
453                 {
454                     // entry type file
455                     removeRecordField( nIdEntry, nIdField, listRecordFields );
456                     // get old field data
457                     oldRecordField.getFile(  )
458                                   .setPhysicalFile( PhysicalFileHome.findByPrimaryKey( 
459                             oldRecordField.getFile(  ).getPhysicalFile(  ).getIdPhysicalFile(  ), getPlugin(  ) ) );
460                     listRecordFields.add( oldRecordField );
461                 }
462 
463                 if ( oldRecordField.getValue(  ) != null )
464                 {
465                     // other entries
466                     removeRecordField( nIdEntry, nIdField, listRecordFields );
467                     listRecordFields.add( oldRecordField );
468                 }
469             }
470         }
471 
472         record.setListRecordField( listRecordFields );
473 
474         RecordHome.updateWidthRecordField( record, getPlugin(  ) );
475 
476         // do not use the workflow if update is partial
477         String strNoWorkflowInit = request.getParameter( PARAMETER_NO_WORKFLOW );
478 
479         if ( StringUtils.isBlank( strNoWorkflowInit ) &&
480                 isEntrySet( listRecordFields, record.getDirectory(  ).getIdDirectory(  ) ) )
481         {
482             doWorkflowActions( record,
483                 DirectoryHome.findByPrimaryKey( record.getDirectory(  ).getIdDirectory(  ), getPlugin(  ) ) );
484         }
485 
486         return record;
487     }
488 
489     /**
490      * {@inheritDoc}
491      */
492     @Override
493     public Record updateRecord( HttpServletRequest request )
494         throws DirectoryRestException, DirectoryErrorException
495     {
496         String strIdRecord = request.getParameter( PARAMETER_RECORD_ID );
497 
498         if ( StringUtils.isNotBlank( strIdRecord ) && StringUtils.isNumeric( strIdRecord ) )
499         {
500             int nIdRecord = Integer.parseInt( strIdRecord );
501             Record record = getRecord( nIdRecord, request );
502 
503             if ( record != null )
504             {
505                 List<RecordField> listRecordFields = getRecordFields( request, record );
506 
507                 for ( RecordField recordField : listRecordFields )
508                 {
509                     int idEntry = recordField.getEntry(  ).getIdEntry(  );
510                     String strEntryId = request.getParameter( Integer.toString( idEntry ) );
511 
512                     if ( strEntryId != null )
513                     {
514                         RecordFieldFilter filter = new RecordFieldFilter(  );
515                         filter.setIdEntry( idEntry );
516                         filter.setIdRecord( record.getIdRecord(  ) );
517                         RecordFieldHome.removeByFilter( filter, getPlugin(  ) );
518                         recordField.setRecord( record );
519                         RecordFieldHome.create( recordField, getPlugin(  ) );
520                     }
521                 }
522 
523                 return record;
524             }
525         }
526 
527         return null;
528     }
529 
530     /**
531      * {@inheritDoc}
532      */
533     @Override
534     public MultipartHttpServletRequest convertRequest( HttpServletRequest request )
535         throws SizeLimitExceededException, FileUploadException
536     {
537         int nSizeThreshold = AppPropertiesService.getPropertyInt( DirectoryRestConstants.PROPERTY_MULTIPART_SIZE_THRESHOLD,
538                 -1 );
539         boolean bActivateNormalizeFileName = Boolean.getBoolean( AppPropertiesService.getProperty( 
540                     DirectoryRestConstants.PROPERTY_MULTIPART_NORMALIZE_FILE_NAME ) );
541         String strRequestSizeMax = AppPropertiesService.getProperty( DirectoryRestConstants.PROPERTY_MULTIPART_REQUEST_SIZE_MAX );
542         long nRequestSizeMax = 0;
543 
544         if ( StringUtils.isNotBlank( strRequestSizeMax ) && StringUtils.isNumeric( strRequestSizeMax ) )
545         {
546             nRequestSizeMax = Long.parseLong( strRequestSizeMax );
547         }
548 
549         return MultipartUtil.convert( nSizeThreshold, nRequestSizeMax, bActivateNormalizeFileName, request );
550     }
551 
552     // PRIVATE METHODS
553 
554     /**
555      * <code>true</code> if the entry is set, or if {@link #PROPERTY_FIELD_WORKFLOW_PREFIX} is empty, <code>false</code> otherwise.
556      * This is use to bypass workflow initialization if the field is not set.
557      * @param listRecordFields record field list
558      * @param nIdDirectory the id directory
559      * @return <code>true</code> if the entry is set, or if {@link #PROPERTY_FIELD_WORKFLOW_PREFIX} is empty, <code>false</code> otherwise.
560      * @see #PROPERTY_FIELD_WORKFLOW_PREFIX
561      */
562     private boolean isEntrySet( List<RecordField> listRecordFields, int nIdDirectory )
563     {
564         int nIdEntry = AppPropertiesService.getPropertyInt( DirectoryRestConstants.PROPERTY_FIELD_WORKFLOW_PREFIX +
565                 nIdDirectory, DirectoryUtils.CONSTANT_ID_NULL );
566 
567         if ( nIdEntry == DirectoryUtils.CONSTANT_ID_NULL )
568         {
569             return true;
570         }
571 
572         for ( RecordField recordField : listRecordFields )
573         {
574             if ( recordField.getEntry(  ).getIdEntry(  ) == nIdEntry )
575             {
576                 if ( StringUtils.isNotBlank( recordField.getValue(  ) ) )
577                 {
578                     return true;
579                 }
580             }
581         }
582 
583         return false;
584     }
585 
586     /**
587      * Inits workflow actions (if available).
588      * @param record the record
589      * @param directory the directory
590      */
591     private void doWorkflowActions( Record record, Directory directory )
592     {
593         if ( WorkflowService.getInstance(  ).isAvailable(  ) &&
594                 ( directory.getIdWorkflow(  ) != DirectoryUtils.CONSTANT_ID_NULL ) )
595         {
596             WorkflowService.getInstance(  )
597                            .getState( record.getIdRecord(  ), Record.WORKFLOW_RESOURCE_TYPE,
598                 directory.getIdWorkflow(  ), Integer.valueOf( directory.getIdDirectory(  ) ) );
599             WorkflowService.getInstance(  )
600                            .executeActionAutomatic( record.getIdRecord(  ), Record.WORKFLOW_RESOURCE_TYPE,
601                 directory.getIdWorkflow(  ), Integer.valueOf( directory.getIdDirectory(  ) ) );
602         }
603     }
604 
605     /**
606      * Gets the record fields list for the record.
607      * @param request the request
608      * @param record the record
609      * @return the record fields
610      * @throws DirectoryErrorException if occurs
611      */
612     private List<RecordField> getRecordFields( HttpServletRequest request, Record record )
613         throws DirectoryErrorException
614     {
615         List<RecordField> listRecordFields = new ArrayList<RecordField>(  );
616         EntryFilter filter = new EntryFilter(  );
617         filter.setIdDirectory( record.getDirectory(  ).getIdDirectory(  ) );
618         filter.setIsComment( EntryFilter.FILTER_FALSE );
619         filter.setIsEntryParentNull( EntryFilter.FILTER_TRUE );
620 
621         List<IEntry> listEntryFirstLevel = EntryHome.getEntryList( filter, getPlugin(  ) );
622 
623         for ( IEntry entry : listEntryFirstLevel )
624         {
625             // no directory error testing (i.e. mandatory fields can be blanks)
626             DirectoryUtils.getDirectoryRecordFieldData( record, request, entry.getIdEntry(  ), false, listRecordFields,
627                 getPlugin(  ), request.getLocale(  ) );
628         }
629 
630         return listRecordFields;
631     }
632 
633     /**
634      * Finds the record field matching the entry id and field id
635      * @param nIdEntry the entry id
636      * @param nIdField the field Id
637      * @param listRecordFields the list
638      * @return the record field found, <code>null</code> otherwise.
639      */
640     private RecordField findRecordField( int nIdEntry, int nIdField, List<RecordField> listRecordFields )
641     {
642         for ( RecordField recordField : listRecordFields )
643         {
644             if ( isSameRecordField( nIdEntry, nIdField, recordField ) )
645             {
646                 return recordField;
647             }
648         }
649 
650         return null;
651     }
652 
653     /**
654      * Removes the record field matching the entry id and field id from the list
655      * @param nIdEntry entry id
656      * @param nIdField field id
657      * @param listRecordFields record fields to check
658      */
659     private void removeRecordField( int nIdEntry, int nIdField, List<RecordField> listRecordFields )
660     {
661         Iterator<RecordField> itRecordFields = listRecordFields.iterator(  );
662 
663         while ( itRecordFields.hasNext(  ) )
664         {
665             RecordField recordField = itRecordFields.next(  );
666 
667             if ( isSameRecordField( nIdEntry, nIdField, recordField ) )
668             {
669                 itRecordFields.remove(  );
670 
671                 break;
672             }
673         }
674     }
675 
676     /**
677      * Removes <code>null</code> record fields (i.e. with no file nor field nor value associated)
678      * @param listRecordFields recordfield list.
679      */
680     private void removeNullRecordFields( List<RecordField> listRecordFields )
681     {
682         Iterator<RecordField> itRecordFields = listRecordFields.iterator(  );
683 
684         while ( itRecordFields.hasNext(  ) )
685         {
686             RecordField recordField = itRecordFields.next(  );
687 
688             if ( ( recordField.getField(  ) == null ) && ( recordField.getFile(  ) == null ) &&
689                     ( recordField.getValue(  ) == null ) )
690             {
691                 itRecordFields.remove(  );
692             }
693         }
694     }
695 
696     /**
697      * Checks if the record field has the same entry id and field id
698      * @param nIdEntry the entry id
699      * @param nIdField the field id
700      * @param recordField the record field
701      * @return boolean.
702      */
703     private boolean isSameRecordField( int nIdEntry, int nIdField, RecordField recordField )
704     {
705         if ( recordField.getEntry(  ).getIdEntry(  ) == nIdEntry )
706         {
707             int nIdFieldFound = ( recordField.getField(  ) == null ) ? 0 : recordField.getField(  ).getIdField(  );
708 
709             if ( nIdFieldFound == nIdField )
710             {
711                 return true;
712             }
713         }
714 
715         return false;
716     }
717 
718     /**
719      * Get the plugin
720      * @return the plugin
721      */
722     private Plugin getPlugin(  )
723     {
724         return PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME );
725     }
726     /**
727      * Gets the record
728      * @param nIdRecord resource id
729      * @param listIdsEntry the list of ids entry
730      * @param mapFieldEntry the map of field entry
731      * @return the record
732      * @throws DirectoryRestException if occurs
733      * @throws DirectoryErrorException if occurs
734      */
735     private Record getRecord( int nIdRecord, List<Integer> listIdsEntry, Map<Integer,Field> mapFieldEntry )
736         throws DirectoryRestException, DirectoryErrorException
737     {
738         Record record = RecordHome.findByPrimaryKey( nIdRecord, getPlugin(  ) );
739         
740         List<RecordField> listRecordField = RecordFieldHome.getRecordFieldSpecificList( listIdsEntry, nIdRecord,
741                 getPlugin(  ),mapFieldEntry );
742         record.setListRecordField( listRecordField );
743 
744         return record;
745     }
746     
747     
748     /**
749      * return a map containing all fields containing in the list of entry 
750      * @param listIdsEntry a list of id entry
751      * @return a map containing all fields containing in the list of entry 
752      */
753     private  Map<Integer,Field>  getMapFieldEntryByListIdEntry(List<Integer> listIdsEntry)
754     {
755 	    Map<Integer,Field> mapFieldEntry=new HashMap<Integer, Field>();
756 	    
757 	    for ( Integer idEntry : listIdsEntry )
758 	    {    
759 	   	 
760 	   	 
761 	   	 List<Field> listField=FieldHome.getFieldListByIdEntry(idEntry, DirectoryUtils.getPlugin(  ));
762 	    	for(Field field:listField)
763 	    	{
764 	    		mapFieldEntry.put(field.getIdField(), field);
765 	    	}
766 	 
767 	    }
768 	    return mapFieldEntry;
769     }
770 }