KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Set JavaDoc;
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 JavaDoc searchesToUpdateSet = new HashSet JavaDoc();
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 JavaDoc[] getLockedObjects()
78     {
79         List JavaDoc l = new ArrayList JavaDoc();
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 JavaDoc[]
89             { this.oldEntry.getDn().toString() } ), 3 );
90         monitor.reportProgress( " " ); //$NON-NLS-1$
91
monitor.worked( 1 );
92
93         IEntry parent = oldEntry.getParententry();
94         DN newDn = new DN( newRdn, parent.getDn() );
95
96         // rename in directory
97
// TODO: use manual/simulated rename, if rename of subtree is not
98
// supported
99
connection.rename( oldEntry, newDn, deleteOldRdn, monitor );
100
101         if ( !monitor.errorsReported() )
102         {
103             // rename in parent
104
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             // rename in searches
116
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 JavaDoc 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 JavaDoc getErrorMessage()
158     {
159         return BrowserCoreMessages.jobs__rename_entry_error;
160     }
161
162 }
163
Popular Tags