1 20 21 package org.apache.directory.ldapstudio.browser.core.jobs; 22 23 24 import java.util.ArrayList ; 25 import java.util.HashSet ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.Set ; 29 30 import org.apache.directory.ldapstudio.browser.core.BrowserCoreMessages; 31 import org.apache.directory.ldapstudio.browser.core.events.EntryRenamedEvent; 32 import org.apache.directory.ldapstudio.browser.core.events.EventRegistry; 33 import org.apache.directory.ldapstudio.browser.core.events.SearchUpdateEvent; 34 import org.apache.directory.ldapstudio.browser.core.model.DN; 35 import org.apache.directory.ldapstudio.browser.core.model.IConnection; 36 import org.apache.directory.ldapstudio.browser.core.model.IEntry; 37 import org.apache.directory.ldapstudio.browser.core.model.ISearch; 38 import org.apache.directory.ldapstudio.browser.core.model.ISearchResult; 39 import org.apache.directory.ldapstudio.browser.core.model.RDN; 40 41 42 public class RenameEntryJob extends AbstractAsyncBulkJob 43 { 44 45 private IConnection connection; 46 47 private IEntry oldEntry; 48 49 private RDN newRdn; 50 51 private boolean deleteOldRdn; 52 53 private IEntry newEntry; 54 55 private Set searchesToUpdateSet = new HashSet (); 56 57 58 public RenameEntryJob( IEntry entry, RDN newRdn, boolean deleteOldRdn ) 59 { 60 this.connection = entry.getConnection(); 61 this.oldEntry = entry; 62 this.newEntry = entry; 63 this.newRdn = newRdn; 64 this.deleteOldRdn = deleteOldRdn; 65 66 setName( BrowserCoreMessages.jobs__rename_entry_name ); 67 } 68 69 70 protected IConnection[] getConnections() 71 { 72 return new IConnection[] 73 { connection }; 74 } 75 76 77 protected Object [] getLockedObjects() 78 { 79 List l = new ArrayList (); 80 l.add( oldEntry.getParententry() ); 81 return l.toArray(); 82 } 83 84 85 protected void executeBulkJob( ExtendedProgressMonitor monitor ) 86 { 87 88 monitor.beginTask( BrowserCoreMessages.bind( BrowserCoreMessages.jobs__rename_entry_task, new String [] 89 { this.oldEntry.getDn().toString() } ), 3 ); 90 monitor.reportProgress( " " ); monitor.worked( 1 ); 92 93 IEntry parent = oldEntry.getParententry(); 94 DN newDn = new DN( newRdn, parent.getDn() ); 95 96 connection.rename( oldEntry, newDn, deleteOldRdn, monitor ); 100 101 if ( !monitor.errorsReported() ) 102 { 103 parent.deleteChild( oldEntry ); 105 this.newEntry = connection.getEntry( newDn, monitor ); 106 parent.addChild( newEntry ); 107 parent.setHasMoreChildren( false ); 108 109 newEntry.setHasChildrenHint( oldEntry.hasChildren() ); 110 if ( oldEntry.isChildrenInitialized() ) 111 { 112 InitializeChildrenJob.initializeChildren( newEntry, monitor ); 113 } 114 115 ISearch[] searches = connection.getSearchManager().getSearches(); 117 for ( int j = 0; j < searches.length; j++ ) 118 { 119 ISearch search = searches[j]; 120 if ( search.getSearchResults() != null ) 121 { 122 ISearchResult[] searchResults = search.getSearchResults(); 123 for ( int k = 0; k < searchResults.length; k++ ) 124 { 125 ISearchResult result = searchResults[k]; 126 if ( oldEntry.equals( result.getEntry() ) ) 127 { 128 ISearchResult[] newsrs = new ISearchResult[searchResults.length - 1]; 129 System.arraycopy( searchResults, 0, newsrs, 0, k ); 130 System.arraycopy( searchResults, k + 1, newsrs, k, searchResults.length - k - 1 ); 131 search.setSearchResults( newsrs ); 132 searchResults = newsrs; 133 k--; 134 searchesToUpdateSet.add( search ); 135 } 136 } 137 } 138 } 139 } 140 } 141 142 143 protected void runNotification() 144 { 145 if ( oldEntry != null && newEntry != null ) 146 { 147 EventRegistry.fireEntryUpdated( new EntryRenamedEvent( oldEntry, newEntry ), this ); 148 } 149 for ( Iterator it = searchesToUpdateSet.iterator(); it.hasNext(); ) 150 { 151 ISearch search = ( ISearch ) it.next(); 152 EventRegistry.fireSearchUpdated( new SearchUpdateEvent( search, SearchUpdateEvent.EventDetail.SEARCH_PERFORMED ), this ); 153 } 154 } 155 156 157 protected String getErrorMessage() 158 { 159 return BrowserCoreMessages.jobs__rename_entry_error; 160 } 161 162 } 163 | Popular Tags |