KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > core > jobs > MoveEntriesJob


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  */

20
21 package org.apache.directory.ldapstudio.browser.core.jobs;
22
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Set JavaDoc;
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 JavaDoc searchesToUpdateSet = new HashSet JavaDoc();
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 JavaDoc[] getLockedObjects()
75     {
76         List JavaDoc l = new ArrayList JavaDoc();
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 JavaDoc[]
89                 {} ), 3 );
90         monitor.reportProgress( " " ); //$NON-NLS-1$
91
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             // move in directory
107
// TODO: use manual/simulated move, if move of subtree is not
108
// supported
109
int errorStatusSize1 = monitor.getErrorStatus( "" ).getChildren().length; //$NON-NLS-1$
110
connection.move( oldEntry, newParent.getDn(), monitor );
111             int errorStatusSize2 = monitor.getErrorStatus( "" ).getChildren().length; //$NON-NLS-1$
112

113             if ( errorStatusSize1 == errorStatusSize2 )
114             {
115                 // move in parent
116
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                 // move in searches
129
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 JavaDoc 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 JavaDoc 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