1 20 21 package org.apache.directory.ldapstudio.browser.core.jobs; 22 23 24 import java.util.ArrayList ; 25 import java.util.Arrays ; 26 import java.util.List ; 27 28 import org.apache.directory.ldapstudio.browser.core.BrowserCoreMessages; 29 import org.apache.directory.ldapstudio.browser.core.events.EntryAddedEvent; 30 import org.apache.directory.ldapstudio.browser.core.events.EventRegistry; 31 import org.apache.directory.ldapstudio.browser.core.model.IConnection; 32 import org.apache.directory.ldapstudio.browser.core.model.IEntry; 33 34 35 public class CreateEntryJob extends AbstractAsyncBulkJob 36 { 37 38 private IEntry[] entriesToCreate; 39 40 private IEntry[] createdEntries; 41 42 43 public CreateEntryJob( IEntry[] entriesToCreate ) 44 { 45 this.entriesToCreate = entriesToCreate; 46 createdEntries = new IEntry[entriesToCreate.length]; 47 setName( entriesToCreate.length == 1 ? BrowserCoreMessages.jobs__create_entry_name_1 48 : BrowserCoreMessages.jobs__create_entry_name_n ); 49 } 50 51 52 protected IConnection[] getConnections() 53 { 54 IConnection[] connections = new IConnection[entriesToCreate.length]; 55 for ( int i = 0; i < connections.length; i++ ) 56 { 57 connections[i] = entriesToCreate[i].getConnection(); 58 } 59 return connections; 60 } 61 62 63 protected Object [] getLockedObjects() 64 { 65 List l = new ArrayList (); 66 l.addAll( Arrays.asList( entriesToCreate ) ); 67 return l.toArray(); 68 } 69 70 71 protected void executeBulkJob( ExtendedProgressMonitor monitor ) 72 { 73 74 monitor.beginTask( entriesToCreate.length == 1 ? BrowserCoreMessages.bind( 75 BrowserCoreMessages.jobs__create_entry_task_1, new String [] 76 { entriesToCreate[0].getDn().toString() } ) : BrowserCoreMessages.bind( 77 BrowserCoreMessages.jobs__create_entry_task_n, new String [] 78 { Integer.toString( entriesToCreate.length ) } ), 2 + entriesToCreate.length ); 79 monitor.reportProgress( " " ); monitor.worked( 1 ); 81 82 for ( int i = 0; !monitor.isCanceled() && i < entriesToCreate.length; i++ ) 83 { 84 IEntry entryToCreate = entriesToCreate[i]; 85 86 entryToCreate.getConnection().create( entryToCreate, monitor ); 87 88 if ( !monitor.errorsReported() ) 89 { 90 createdEntries[i] = entryToCreate.getConnection().getEntry( entryToCreate.getDn(), monitor ); 91 createdEntries[i].setHasChildrenHint( false ); 93 } 94 95 monitor.worked( 1 ); 96 } 97 } 98 99 100 protected void runNotification() 101 { 102 for ( int i = 0; i < createdEntries.length; i++ ) 103 { 104 if ( createdEntries[i] != null ) 105 { 106 EventRegistry.fireEntryUpdated( new EntryAddedEvent( createdEntries[i].getConnection(), 107 createdEntries[i] ), this ); 108 } 109 } 110 } 111 112 113 protected String getErrorMessage() 114 { 115 return entriesToCreate.length == 1 ? BrowserCoreMessages.jobs__create_entry_error_1 116 : BrowserCoreMessages.jobs__create_entry_error_n; 117 } 118 119 } 120 | Popular Tags |