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.service;
35  
36  import fr.paris.lutece.plugins.directory.business.Directory;
37  import fr.paris.lutece.plugins.directory.business.DirectoryAction;
38  import fr.paris.lutece.plugins.directory.business.DirectoryXsl;
39  import fr.paris.lutece.plugins.directory.business.Entry;
40  import fr.paris.lutece.plugins.directory.business.EntryHome;
41  import fr.paris.lutece.plugins.directory.business.EntryTypeDownloadUrl;
42  import fr.paris.lutece.plugins.directory.business.EntryTypeGeolocation;
43  import fr.paris.lutece.plugins.directory.business.EntryTypeMyLuteceUser;
44  import fr.paris.lutece.plugins.directory.business.EntryTypeRemoteMyLuteceUser;
45  import fr.paris.lutece.plugins.directory.business.Field;
46  import fr.paris.lutece.plugins.directory.business.FieldHome;
47  import fr.paris.lutece.plugins.directory.business.IEntry;
48  import fr.paris.lutece.plugins.directory.business.Record;
49  import fr.paris.lutece.plugins.directory.business.RecordField;
50  import fr.paris.lutece.plugins.directory.business.RecordFieldFilter;
51  import fr.paris.lutece.plugins.directory.business.RecordFieldHome;
52  import fr.paris.lutece.plugins.directory.service.directorysearch.DirectorySearchService;
53  import fr.paris.lutece.plugins.directory.service.parameter.DirectoryParameterService;
54  import fr.paris.lutece.plugins.directory.service.parameter.EntryParameterService;
55  import fr.paris.lutece.plugins.directory.service.record.IRecordService;
56  import fr.paris.lutece.plugins.directory.service.record.RecordService;
57  import fr.paris.lutece.plugins.directory.service.security.DirectoryUserAttributesManager;
58  import fr.paris.lutece.plugins.directory.service.upload.DirectoryAsynchronousUploadHandler;
59  import fr.paris.lutece.plugins.directory.utils.DirectoryErrorException;
60  import fr.paris.lutece.plugins.directory.utils.DirectoryUtils;
61  import fr.paris.lutece.plugins.workflowcore.business.action.Action;
62  import fr.paris.lutece.plugins.workflowcore.business.state.State;
63  import fr.paris.lutece.portal.business.rbac.RBAC;
64  import fr.paris.lutece.portal.business.user.AdminUser;
65  import fr.paris.lutece.portal.service.plugin.Plugin;
66  import fr.paris.lutece.portal.service.plugin.PluginService;
67  import fr.paris.lutece.portal.service.rbac.RBACService;
68  import fr.paris.lutece.portal.service.security.LuteceUser;
69  import fr.paris.lutece.portal.service.security.SecurityService;
70  import fr.paris.lutece.portal.service.security.UserAttributesService;
71  import fr.paris.lutece.portal.service.spring.SpringContextService;
72  import fr.paris.lutece.portal.service.util.AppLogService;
73  import fr.paris.lutece.portal.service.util.AppPropertiesService;
74  import fr.paris.lutece.portal.service.workflow.WorkflowService;
75  import fr.paris.lutece.portal.service.workgroup.AdminWorkgroupService;
76  import fr.paris.lutece.util.ReferenceList;
77  
78  import org.apache.commons.lang.StringEscapeUtils;
79  import org.apache.commons.lang.StringUtils;
80  
81  import java.util.Collection;
82  import java.util.HashMap;
83  import java.util.List;
84  import java.util.Map;
85  
86  /**
87   *
88   * Directory Service
89   *
90   */
91  public class DirectoryService
92  {
93      // PROPERTIES
94      private static final String PROPERTY_ENTRY_TYPE_MYLUTECE_USER = "directory.entry_type.mylutece_user";
95      private static final String PROPERTY_ENTRY_TYPE_REMOTE_MYLUTECE_USER = "directory.entry_type.remote_mylutece_user";
96  
97      // MARKS
98      private static final String MARK_LIST_DIRECTORY_PARAM_DEFAULT_VALUES = "list_directory_param_default_values";
99      private static final String MARK_LIST_ENTRY_PARAM_DEFAULT_VALUES = "list_entry_param_default_values";
100     private static final String MARK_LIST_EXPORT_ENCODING_PARAM = "list_export_encoding_param";
101     private static final String MARK_PERMISSION_INDEX_ALL_DIRECTORY = "permission_index_all_directory";
102     private static final String MARK_PERMISSION_XSL = "right_xsl";
103     private static final String MARK_RECORD = "record";
104     private static final String MARK_MAP_ID_ENTRY_LIST_RECORD_FIELD = "map_id_entry_list_record_field";
105     private static final String MARK_WORKFLOW_STATE = "workflow_state";
106     private static final String MARK_WORKFLOW_ACTION_LIST = "workflow_action_list";
107     private static DirectoryService _singleton;
108 
109     /**
110      * Initialize the Form service
111      *
112      */
113     public void init( )
114     {
115         Directory.init( );
116     }
117 
118     /**
119      * Returns the instance of the singleton
120      *
121      * @return The instance of the singleton
122      */
123     public static DirectoryService getInstance( )
124     {
125         if ( _singleton == null )
126         {
127             _singleton = new DirectoryService( );
128         }
129 
130         return _singleton;
131     }
132 
133     /**
134      * Build the advanced parameters management
135      * 
136      * @param user
137      *            the current user
138      * @return The model for the advanced parameters
139      */
140     public Map<String, Object> getManageAdvancedParameters( AdminUser user )
141     {
142         Map<String, Object> model = new HashMap<String, Object>( );
143 
144         if ( RBACService.isAuthorized( Directory.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID, DirectoryResourceIdService.PERMISSION_MANAGE_ADVANCED_PARAMETERS,
145                 user ) )
146         {
147             ReferenceList listDirectoryParamDefaultValues = DirectoryParameterService.getService( ).findDefaultValueParameters( );
148             ReferenceList listEntryParamDefaultValues = EntryParameterService.getService( ).findAll( );
149             ReferenceList listExportEncodingParam = DirectoryParameterService.getService( ).findExportEncodingParameters( );
150 
151             model.put( MARK_LIST_DIRECTORY_PARAM_DEFAULT_VALUES, listDirectoryParamDefaultValues );
152             model.put( MARK_LIST_ENTRY_PARAM_DEFAULT_VALUES, listEntryParamDefaultValues );
153             model.put( MARK_LIST_EXPORT_ENCODING_PARAM, listExportEncodingParam );
154         }
155 
156         if ( RBACService.isAuthorized( DirectoryXsl.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID, DirectoryXslResourceIdService.PERMISSION_CREATE, user ) )
157         {
158             model.put( MARK_PERMISSION_XSL, true );
159         }
160 
161         if ( RBACService.isAuthorized( Directory.RESOURCE_TYPE, RBAC.WILDCARD_RESOURCES_ID, DirectoryResourceIdService.PERMISSION_INDEX_ALL_DIRECTORY, user ) )
162         {
163             model.put( MARK_PERMISSION_INDEX_ALL_DIRECTORY, true );
164         }
165 
166         return model;
167     }
168 
169     /**
170      * Get the records count
171      * 
172      * @param directory
173      *            the {@link Directory}
174      * @param user
175      *            the {@link AdminUser}
176      * @return the record count
177      */
178     public int getRecordsCount( Directory directory, AdminUser user )
179     {
180         Plugin plugin = PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME );
181         int nNbRecords = 0;
182         boolean bWorkflowServiceEnable = WorkflowService.getInstance( ).isAvailable( );
183         RecordFieldFilter filter = new RecordFieldFilter( );
184         filter.setIdDirectory( directory.getIdDirectory( ) );
185         filter.setWorkgroupKeyList( AdminWorkgroupService.getUserWorkgroups( user, user.getLocale( ) ) );
186 
187         if ( ( directory.getIdWorkflow( ) != DirectoryUtils.CONSTANT_ID_NULL ) && ( directory.getIdWorkflow( ) != DirectoryUtils.CONSTANT_ID_ZERO )
188                 && bWorkflowServiceEnable )
189         {
190             List<Integer> listResultRecordIds = DirectorySearchService.getInstance( ).getSearchResults( directory, null, null, null, null, filter, plugin );
191             List<Integer> listTmpResultRecordIds = WorkflowService.getInstance( ).getAuthorizedResourceList( Record.WORKFLOW_RESOURCE_TYPE,
192                     directory.getIdWorkflow( ), DirectoryUtils.CONSTANT_ID_NULL, Integer.valueOf( directory.getIdDirectory( ) ), user );
193             listResultRecordIds = DirectoryUtils.retainAllIdsKeepingFirstOrder( listResultRecordIds, listTmpResultRecordIds );
194             nNbRecords = listResultRecordIds.size( );
195         }
196         else
197         {
198             IRecordService recordService = SpringContextService.getBean( RecordService.BEAN_SERVICE );
199             nNbRecords = recordService.getCountRecord( filter, plugin );
200         }
201 
202         return nNbRecords;
203     }
204 
205     /**
206      * Get the user infos from a given id record and id entry. <br>
207      * The retrieval of the user infos depends on the entry type : <br>
208      * <ul>
209      * <li>If it is an {@link EntryTypeMyLuteceUser}, then it will use the {@link SecurityService} API</li>
210      * <li>If it is an {@link EntryTypeRemoteMyLuteceUser}, then it will use the {@link UserAttributesService} API</li>
211      * </ul>
212      * 
213      * @param strUserGuid
214      *            the user guid
215      * @param nIdEntry
216      *            the id entry
217      * @return a {@link ReferenceList}
218      */
219     public ReferenceList getUserInfos( String strUserGuid, int nIdEntry )
220     {
221         ReferenceList userInfos = null;
222 
223         if ( StringUtils.isNotBlank( strUserGuid ) )
224         {
225             Plugin plugin = PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME );
226             IEntry entry = EntryHome.findByPrimaryKey( nIdEntry, plugin );
227 
228             if ( ( entry != null ) && ( entry.getEntryType( ) != null ) )
229             {
230                 if ( ( entry.getEntryType( ).getIdType( ) == AppPropertiesService.getPropertyInt( PROPERTY_ENTRY_TYPE_REMOTE_MYLUTECE_USER, 21 ) )
231                         && DirectoryUserAttributesManager.getManager( ).isEnabled( ) )
232                 {
233                     userInfos = DirectoryUtils.convertMapToReferenceList( DirectoryUserAttributesManager.getManager( ).getAttributes( strUserGuid ) );
234                 }
235                 else
236                     if ( entry.getEntryType( ).getIdType( ) == AppPropertiesService.getPropertyInt( PROPERTY_ENTRY_TYPE_MYLUTECE_USER, 19 ) )
237                     {
238                         LuteceUser user = SecurityService.getInstance( ).getUser( strUserGuid );
239 
240                         if ( user != null )
241                         {
242                             userInfos = DirectoryUtils.convertMapToReferenceList( user.getUserInfos( ) );
243                         }
244                     }
245             }
246         }
247 
248         return userInfos;
249     }
250 
251     /**
252      * Get the user guid from a given id record and id entry. <br>
253      * Return an empty string if the entry is not an EntryTypeMyLuteceUser nor EntryTypeRemoteMyLuteceUser
254      * 
255      * @param nIdRecord
256      *            the id record
257      * @param nIdEntry
258      *            the id entry
259      * @return the user GUID
260      */
261     public String getUserGuid( int nIdRecord, int nIdEntry )
262     {
263         String strUserGuid = StringUtils.EMPTY;
264         Plugin plugin = PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME );
265         IEntry entry = EntryHome.findByPrimaryKey( nIdEntry, plugin );
266 
267         if ( ( entry != null ) && ( entry.getEntryType( ) != null ) )
268         {
269             if ( ( ( entry.getEntryType( ).getIdType( ) == AppPropertiesService.getPropertyInt( PROPERTY_ENTRY_TYPE_REMOTE_MYLUTECE_USER, 21 ) ) && DirectoryUserAttributesManager
270                     .getManager( ).isEnabled( ) )
271                     || ( entry.getEntryType( ).getIdType( ) == AppPropertiesService.getPropertyInt( PROPERTY_ENTRY_TYPE_MYLUTECE_USER, 19 ) ) )
272             {
273                 RecordFieldFilter recordFieldFilter = new RecordFieldFilter( );
274                 recordFieldFilter.setIdRecord( nIdRecord );
275                 recordFieldFilter.setIdEntry( nIdEntry );
276 
277                 List<RecordField> listRecordFields = DirectoryService.getInstance( ).getRecordFieldByFilter( recordFieldFilter );
278 
279                 if ( ( listRecordFields != null ) && !listRecordFields.isEmpty( ) && ( listRecordFields.get( 0 ) != null ) )
280                 {
281                     strUserGuid = listRecordFields.get( 0 ).getValue( );
282                 }
283             }
284         }
285 
286         return strUserGuid;
287     }
288 
289     /**
290      * Get the max number
291      * 
292      * @param entry
293      *            the entry
294      * @return the max number
295      */
296     public int getMaxNumber( IEntry entry )
297     {
298         int nMaxNumber = 1;
299 
300         if ( entry instanceof fr.paris.lutece.plugins.directory.business.EntryTypeNumbering && ( entry.getEntryType( ) != null )
301                 && ( entry.getDirectory( ) != null ) )
302         {
303             Plugin pluginDirectory = PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME );
304             nMaxNumber = RecordFieldHome.findMaxNumber( entry.getIdEntry( ), entry.getDirectory( ).getIdDirectory( ), pluginDirectory );
305         }
306 
307         return nMaxNumber;
308     }
309 
310     /**
311      * Get the number to insert to the entry type numbering.
312      * 
313      * @param entry
314      *            the entry type numbering
315      * @param strNumber
316      *            the number to insert
317      * @return the number
318      * @throws DirectoryErrorException
319      *             exception if the number already exists on an another record field
320      */
321     public int getNumber( IEntry entry, String strNumber ) throws DirectoryErrorException
322     {
323         int nNumber = DirectoryUtils.CONSTANT_ID_NULL;
324 
325         if ( entry instanceof fr.paris.lutece.plugins.directory.business.EntryTypeNumbering && ( entry.getFields( ) != null )
326                 && ( entry.getFields( ).size( ) > 0 ) && ( entry.getEntryType( ) != null ) && ( entry.getDirectory( ) != null ) )
327         {
328             nNumber = buildNumber( entry, strNumber );
329 
330             if ( nNumber != DirectoryUtils.CONSTANT_ID_NULL )
331             {
332                 Plugin pluginDirectory = PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME );
333 
334                 if ( RecordFieldHome.isNumberOnARecordField( entry.getIdEntry( ), entry.getDirectory( ).getIdDirectory( ), nNumber, pluginDirectory ) )
335                 {
336                     throw new DirectoryErrorException( entry.getTitle( ), "Directory Error - The number already exists in an " + "another record field." );
337                 }
338             }
339         }
340 
341         return nNumber;
342     }
343 
344     /**
345      * Get the list of fields from a given id entry
346      * 
347      * @param nIdEntry
348      *            the id entry
349      * @return a list of fields
350      */
351     public List<Field> getFieldsListFromIdEntry( int nIdEntry )
352     {
353         Plugin plugin = PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME );
354 
355         return FieldHome.getFieldListByIdEntry( nIdEntry, plugin );
356     }
357 
358     /**
359      * Get the model for entry for xml
360      * 
361      * @param entry
362      *            the entry
363      * @return the model
364      */
365     public Map<String, String> getModelForEntryForXml( IEntry entry )
366     {
367         Map<String, String> model = new HashMap<String, String>( );
368         model.put( Entry.ATTRIBUTE_ENTRY_ID, String.valueOf( entry.getIdEntry( ) ) );
369         model.put( Entry.ATTRIBUTE_TITLE, StringEscapeUtils.escapeXml( entry.getTitle( ) ) );
370         model.put( Entry.ATTRIBUTE_IS_SORTABLE, Boolean.toString( entry.isSortable( ) ) );
371 
372         if ( entry instanceof EntryTypeGeolocation )
373         {
374             model.put( Entry.ATTRIBUTE_SHOWXY, Boolean.toString( showXY( entry ) ) );
375         }
376 
377         return model;
378     }
379 
380     /**
381      * Check if the entry must show the X and Y or not
382      * 
383      * @param entry
384      *            The entry
385      * @return true if it must show, false otherwise
386      */
387     public boolean showXY( IEntry entry )
388     {
389         boolean bShowXY = false;
390 
391         if ( entry instanceof EntryTypeGeolocation )
392         {
393             if ( ( entry.getFields( ) == null ) || ( entry.getFields( ).size( ) == 0 ) )
394             {
395                 entry.setFields( getFieldsListFromIdEntry( entry.getIdEntry( ) ) );
396             }
397 
398             if ( ( entry.getFields( ) != null ) && !entry.getFields( ).isEmpty( ) )
399             {
400                 Field fieldShowXY = DirectoryUtils.findFieldByTitleInTheList( EntryTypeGeolocation.CONSTANT_SHOWXY, entry.getFields( ) );
401 
402                 if ( fieldShowXY != null )
403                 {
404                     bShowXY = Boolean.valueOf( fieldShowXY.getValue( ) );
405                 }
406             }
407         }
408 
409         return bShowXY;
410     }
411 
412     /**
413      * Remove asynchronous file
414      * 
415      * @param recordField
416      *            the record field
417      * @param plugin
418      *            the plugin
419      */
420     public void removeAsynchronousFile( RecordField recordField, Plugin plugin )
421     {
422         if ( ( recordField != null ) && ( recordField.getEntry( ) != null ) )
423         {
424             IEntry entry = recordField.getEntry( );
425             String strWSRestUrl = getWSRestUrl( entry, plugin );
426 
427             if ( StringUtils.isNotBlank( strWSRestUrl ) )
428             {
429                 try
430                 {
431                     DirectoryAsynchronousUploadHandler.getHandler( ).doRemoveFile( recordField, entry, strWSRestUrl );
432                 }
433                 catch( Exception e )
434                 {
435                     AppLogService.error( e );
436                 }
437             }
438         }
439     }
440 
441     /**
442      * Get the WS rest url from a given entry
443      * 
444      * @param entry
445      *            the entry
446      * @param plugin
447      *            the plugin
448      * @return the ws rest url
449      */
450     public String getWSRestUrl( IEntry entry, Plugin plugin )
451     {
452         String strWSRestUrl = StringUtils.EMPTY;
453 
454         if ( ( entry != null ) && entry instanceof EntryTypeDownloadUrl )
455         {
456             if ( entry.getFields( ) == null )
457             {
458                 entry.setFields( getFieldsListFromIdEntry( entry.getIdEntry( ) ) );
459             }
460 
461             if ( ( entry.getFields( ) != null ) && !entry.getFields( ).isEmpty( ) )
462             {
463                 Field fieldWSRestUrl = DirectoryUtils.findFieldByTitleInTheList( EntryTypeDownloadUrl.CONSTANT_WS_REST_URL, entry.getFields( ) );
464 
465                 if ( fieldWSRestUrl != null )
466                 {
467                     strWSRestUrl = fieldWSRestUrl.getValue( );
468                 }
469             }
470         }
471 
472         return strWSRestUrl;
473     }
474 
475     /**
476      * Get the resource action for a record
477      * 
478      * @param record
479      *            the record
480      * @param directory
481      *            the directory
482      * @param listEntryResultSearch
483      *            the list of entry
484      * @param adminUser
485      *            the AdminUser
486      * @param listActionsForDirectoryEnable
487      *            The list of actions to use if the record is enabled
488      * @param listActionsForDirectoryDisable
489      *            The list of actions to use if the record is disabled
490      * @param bGetFileName
491      *            True to get file names. <i>Warning :</i> The file name is fetch by a webservice call. Beware of performance.
492      * @param plugin
493      *            the plugin
494      * @return a map of string - object
495      */
496     public Map<String, Object> getResourceAction( Record record, Directory directory, List<IEntry> listEntryResultSearch, AdminUser adminUser,
497             List<DirectoryAction> listActionsForDirectoryEnable, List<DirectoryAction> listActionsForDirectoryDisable, boolean bGetFileName, Plugin plugin )
498     {
499         if ( record.isEnabled( ) )
500         {
501             record.setActions( listActionsForDirectoryEnable );
502         }
503         else
504         {
505             record.setActions( listActionsForDirectoryDisable );
506         }
507 
508         // workflow service
509         Map<String, Object> resourceActions = new HashMap<String, Object>( );
510         resourceActions.put( MARK_RECORD, record );
511         resourceActions.put( MARK_MAP_ID_ENTRY_LIST_RECORD_FIELD,
512                 DirectoryUtils.getMapIdEntryListRecordField( listEntryResultSearch, record.getIdRecord( ), plugin, bGetFileName ) );
513 
514         boolean bWorkflowServiceEnable = WorkflowService.getInstance( ).isAvailable( ) && ( directory.getIdWorkflow( ) != DirectoryUtils.CONSTANT_ID_NULL );
515 
516         if ( bWorkflowServiceEnable )
517         {
518             WorkflowService workflowService = WorkflowService.getInstance( );
519             Collection<Action> lListActions = workflowService.getActions( record.getIdRecord( ), Record.WORKFLOW_RESOURCE_TYPE, directory.getIdWorkflow( ),
520                     adminUser );
521             State state = workflowService.getState( record.getIdRecord( ), Record.WORKFLOW_RESOURCE_TYPE, directory.getIdWorkflow( ),
522                     Integer.valueOf( directory.getIdDirectory( ) ) );
523             resourceActions.put( MARK_WORKFLOW_STATE, state );
524             resourceActions.put( MARK_WORKFLOW_ACTION_LIST, lListActions );
525         }
526 
527         return resourceActions;
528     }
529 
530     /**
531      * Get the list of record fields from a given Filter
532      * 
533      * @param recordFieldFilter
534      *            the filter
535      * @return a map of (user attribute name, user attribute value)
536      */
537     public List<RecordField> getRecordFieldByFilter( RecordFieldFilter recordFieldFilter )
538     {
539         Plugin plugin = PluginService.getPlugin( DirectoryPlugin.PLUGIN_NAME );
540 
541         return RecordFieldHome.getRecordFieldList( recordFieldFilter, plugin );
542     }
543 
544     /**
545      * Build the number from a given number. This methods first checks if the number is not a type numerical (without the prefix of the entry), or checks if the
546      * number already exists on an another record field or not.
547      * 
548      * @param entry
549      *            the entry numbering
550      * @param strNumber
551      *            the number to build
552      * @return the number
553      * @throws DirectoryErrorException
554      *             exception if the directory entry type numbering does not have the same prefix as the number
555      */
556     private int buildNumber( IEntry entry, String strNumber ) throws DirectoryErrorException
557     {
558         int nNumber = DirectoryUtils.CONSTANT_ID_NULL;
559 
560         Field field = entry.getFields( ).get( 0 );
561         String strPrefix = field.getTitle( );
562 
563         String strNumberTmp = strNumber;
564 
565         if ( StringUtils.isNotBlank( strPrefix ) )
566         {
567             if ( StringUtils.isNotBlank( strNumber ) && ( strPrefix.length( ) < strNumber.length( ) ) )
568             {
569                 strNumberTmp = strNumber.substring( strPrefix.length( ), strNumber.length( ) );
570             }
571             else
572             {
573                 throw new DirectoryErrorException( entry.getTitle( ), "Directory Error - The prefix of the entry type numbering to insert is not correct." );
574             }
575         }
576 
577         if ( StringUtils.isNotBlank( strNumberTmp ) && StringUtils.isNumeric( strNumberTmp ) )
578         {
579             nNumber = Integer.parseInt( strNumberTmp );
580         }
581         else
582         {
583             throw new DirectoryErrorException( entry.getTitle( ), "Directory Error - The prefix of the entry type numbering to insert is not correct." );
584         }
585 
586         return nNumber;
587     }
588 }