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.HashSet ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Set ; 30 31 import org.apache.directory.ldapstudio.browser.core.BrowserCoreMessages; 32 import org.apache.directory.ldapstudio.browser.core.events.EntryMovedEvent; 33 import org.apache.directory.ldapstudio.browser.core.events.EventRegistry; 34 import org.apache.directory.ldapstudio.browser.core.events.SearchUpdateEvent; 35 import org.apache.directory.ldapstudio.browser.core.model.DN; 36 import org.apache.directory.ldapstudio.browser.core.model.IConnection; 37 import org.apache.directory.ldapstudio.browser.core.model.IEntry; 38 import org.apache.directory.ldapstudio.browser.core.model.ISearch; 39 import org.apache.directory.ldapstudio.browser.core.model.ISearchResult; 40 41 42 public class MoveEntriesJob extends AbstractAsyncBulkJob 43 { 44 45 private IConnection connection; 46 47 private IEntry[] oldEntries; 48 49 private IEntry newParent; 50 51 private IEntry[] newEntries; 52 53 private Set searchesToUpdateSet = new HashSet (); 54 55 56 public MoveEntriesJob( IEntry[] entries, IEntry newParent ) 57 { 58 this.connection = newParent.getConnection(); 59 this.oldEntries = entries; 60 this.newParent = newParent; 61 62 setName( entries.length == 1 ? BrowserCoreMessages.jobs__move_entry_name_1 63 : BrowserCoreMessages.jobs__move_entry_name_n ); 64 } 65 66 67 protected IConnection[] getConnections() 68 { 69 return new IConnection[] 70 { connection }; 71 } 72 73 74 protected Object [] getLockedObjects() 75 { 76 List l = new ArrayList (); 77 l.add( newParent ); 78 l.addAll( Arrays.asList( oldEntries ) ); 79 return l.toArray(); 80 } 81 82 83 protected void executeBulkJob( ExtendedProgressMonitor monitor ) 84 { 85 86 monitor.beginTask( BrowserCoreMessages.bind( 87 oldEntries.length == 1 ? BrowserCoreMessages.jobs__move_entry_task_1 88 : BrowserCoreMessages.jobs__move_entry_task_n, new String [] 89 {} ), 3 ); 90 monitor.reportProgress( " " ); monitor.worked( 1 ); 92 93 this.newEntries = new IEntry[oldEntries.length]; 94 for ( int i = 0; i < oldEntries.length; i++ ) 95 { 96 this.newEntries[i] = oldEntries[i]; 97 } 98 99 for ( int i = 0; i < oldEntries.length; i++ ) 100 { 101 102 IEntry oldEntry = oldEntries[i]; 103 IEntry oldParent = oldEntry.getParententry(); 104 DN newDn = new DN( oldEntry.getRdn(), newParent.getDn() ); 105 106 int errorStatusSize1 = monitor.getErrorStatus( "" ).getChildren().length; connection.move( oldEntry, newParent.getDn(), monitor ); 111 int errorStatusSize2 = monitor.getErrorStatus( "" ).getChildren().length; 113 if ( errorStatusSize1 == errorStatusSize2 ) 114 { 115 oldParent.deleteChild( oldEntry ); 117 IEntry newEntry = connection.getEntry( newDn, monitor ); 118 this.newEntries[i] = newEntry; 119 newParent.addChild( newEntry ); 120 newParent.setHasMoreChildren( false ); 121 122 newEntry.setHasChildrenHint( oldEntry.hasChildren() ); 123 if ( oldEntry.isChildrenInitialized() ) 124 { 125 InitializeChildrenJob.initializeChildren( newEntry, monitor ); 126 } 127 128 ISearch[] searches = connection.getSearchManager().getSearches(); 130 for ( int j = 0; j < searches.length; j++ ) 131 { 132 ISearch search = searches[j]; 133 if ( search.getSearchResults() != null ) 134 { 135 ISearchResult[] searchResults = search.getSearchResults(); 136 for ( int k = 0; k < searchResults.length; k++ ) 137 { 138 ISearchResult result = searchResults[k]; 139 if ( oldEntry.equals( result.getEntry() ) ) 140 { 141 ISearchResult[] newsrs = new ISearchResult[searchResults.length - 1]; 142 System.arraycopy( searchResults, 0, newsrs, 0, k ); 143 System.arraycopy( searchResults, k + 1, newsrs, k, searchResults.length - k - 1 ); 144 search.setSearchResults( newsrs ); 145 searchResults = newsrs; 146 k--; 147 searchesToUpdateSet.add( search ); 148 } 149 } 150 } 151 } 152 } 153 } 154 } 155 156 157 protected void runNotification() 158 { 159 for ( int i = 0; i < newEntries.length; i++ ) 160 { 161 if ( oldEntries[i] != null && newEntries[i] != null ) 162 { 163 EventRegistry.fireEntryUpdated( new EntryMovedEvent( oldEntries[i], newEntries[i] ), this ); 164 } 165 } 166 for ( Iterator it = searchesToUpdateSet.iterator(); it.hasNext(); ) 167 { 168 ISearch search = ( ISearch ) it.next(); 169 EventRegistry.fireSearchUpdated( new SearchUpdateEvent( search, SearchUpdateEvent.EventDetail.SEARCH_PERFORMED ), this ); 170 } 171 } 172 173 174 protected String getErrorMessage() 175 { 176 return oldEntries.length == 1 ? BrowserCoreMessages.jobs__move_entry_error_1 177 : BrowserCoreMessages.jobs__move_entry_error_n; 178 } 179 180 } 181 | Popular Tags |