KickJava   Java API By Example, From Geeks To Geeks.

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


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.List JavaDoc;
27
28 import org.apache.directory.ldapstudio.browser.core.BrowserCoreMessages;
29 import org.apache.directory.ldapstudio.browser.core.events.ChildrenInitializedEvent;
30 import org.apache.directory.ldapstudio.browser.core.events.EventRegistry;
31 import org.apache.directory.ldapstudio.browser.core.internal.model.Attribute;
32 import org.apache.directory.ldapstudio.browser.core.internal.model.Entry;
33 import org.apache.directory.ldapstudio.browser.core.internal.model.Search;
34 import org.apache.directory.ldapstudio.browser.core.internal.model.Value;
35 import org.apache.directory.ldapstudio.browser.core.model.IAttribute;
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 import org.apache.directory.ldapstudio.browser.core.model.IValue;
41 import org.apache.directory.ldapstudio.browser.core.model.NameException;
42 import org.apache.directory.ldapstudio.browser.core.model.RDN;
43 import org.apache.directory.ldapstudio.browser.core.model.RDNPart;
44 import org.apache.directory.ldapstudio.browser.core.model.SearchParameter;
45 import org.apache.directory.ldapstudio.browser.core.model.schema.SchemaUtils;
46
47
48 public class CopyEntriesJob extends AbstractAsyncBulkJob
49 {
50
51     private IEntry parent;
52
53     private IEntry[] entriesToCopy;
54
55     private int scope;
56
57
58     public CopyEntriesJob( final IEntry parent, final IEntry[] entriesToCopy, int scope )
59     {
60         this.parent = parent;
61         this.entriesToCopy = entriesToCopy;
62         this.scope = scope;
63         setName( entriesToCopy.length == 1 ? BrowserCoreMessages.jobs__copy_entries_name_1
64             : BrowserCoreMessages.jobs__copy_entries_name_n );
65     }
66
67
68     protected IConnection[] getConnections()
69     {
70         return new IConnection[]
71             { parent.getConnection() };
72     }
73
74
75     protected Object JavaDoc[] getLockedObjects()
76     {
77         List JavaDoc l = new ArrayList JavaDoc();
78         l.add( parent );
79         l.addAll( Arrays.asList( entriesToCopy ) );
80         return l.toArray();
81     }
82
83
84     protected void executeBulkJob( ExtendedProgressMonitor monitor )
85     {
86
87         monitor.beginTask( entriesToCopy.length == 1 ? BrowserCoreMessages.bind(
88             BrowserCoreMessages.jobs__copy_entries_task_1, new String JavaDoc[]
89                 { entriesToCopy[0].getDn().toString(), parent.getDn().toString() } ) : BrowserCoreMessages.bind(
90             BrowserCoreMessages.jobs__copy_entries_task_n, new String JavaDoc[]
91                 { Integer.toString( entriesToCopy.length ), parent.getDn().toString() } ), 2 + entriesToCopy.length );
92
93         monitor.reportProgress( " " ); //$NON-NLS-1$
94
monitor.worked( 1 );
95
96         if ( scope == ISearch.SCOPE_OBJECT || scope == ISearch.SCOPE_ONELEVEL || scope == ISearch.SCOPE_SUBTREE )
97         {
98             int num = 0;
99             for ( int i = 0; !monitor.isCanceled() && i < entriesToCopy.length; i++ )
100             {
101                 IEntry entryToCopy = entriesToCopy[i];
102
103                 if ( scope == ISearch.SCOPE_OBJECT
104                     || !parent.getDn().toString().endsWith( entryToCopy.getDn().toString() ) )
105                 {
106                     num = this.copyEntryRecursive( entryToCopy, parent, scope, num, monitor );
107                 }
108                 else
109                 {
110                     monitor.reportError( BrowserCoreMessages.jobs__copy_entries_source_and_target_are_equal );
111                 }
112             }
113         }
114     }
115
116
117     protected void runNotification()
118     {
119         EventRegistry.fireEntryUpdated( new ChildrenInitializedEvent( parent ), this );
120     }
121
122
123     protected String JavaDoc getErrorMessage()
124     {
125         return entriesToCopy.length == 1 ? BrowserCoreMessages.jobs__copy_entries_error_1
126             : BrowserCoreMessages.jobs__copy_entries_error_n;
127     }
128
129
130     private int copyEntryRecursive( IEntry entryToCopy, IEntry parent, int scope, int num,
131         ExtendedProgressMonitor monitor )
132     {
133         try
134         {
135             SearchParameter param = new SearchParameter();
136             param.setSearchBase( entryToCopy.getDn() );
137             param.setFilter( ISearch.FILTER_TRUE );
138             param.setScope( ISearch.SCOPE_OBJECT );
139             param.setAliasesDereferencingMethod( IConnection.DEREFERENCE_ALIASES_NEVER );
140             param.setReferralsHandlingMethod( IConnection.HANDLE_REFERRALS_IGNORE );
141             param.setReturningAttributes( new String JavaDoc[]
142                 { ISearch.ALL_USER_ATTRIBUTES, IAttribute.REFERRAL_ATTRIBUTE } );
143             ISearch search = new Search( entryToCopy.getConnection(), param );
144             entryToCopy.getConnection().search( search, monitor );
145
146             ISearchResult[] srs = search.getSearchResults();
147             if ( !monitor.isCanceled() && srs != null && srs.length == 1 )
148             {
149                 entryToCopy = srs[0].getEntry();
150                 IAttribute[] attributesToCopy = entryToCopy.getAttributes();
151
152                 // create new entry
153
RDN rdn = entryToCopy.getRdn();
154                 IEntry newEntry = new Entry( parent, rdn );
155
156                 // change RDN if entry already exists
157
ExtendedProgressMonitor testMonitor = new ExtendedProgressMonitor( monitor );
158                 IEntry testEntry = parent.getConnection().getEntry( newEntry.getDn(), testMonitor );
159                 if ( testEntry != null )
160                 {
161                     String JavaDoc rdnValue = rdn.getValue();
162                     String JavaDoc newRdnValue = BrowserCoreMessages.bind( BrowserCoreMessages.copy_n_of_s, "", rdnValue ); //$NON-NLS-1$
163
RDN newRdn = getNewRdn( rdn, newRdnValue );
164                     newEntry = new Entry( parent, newRdn );
165                     testEntry = parent.getConnection().getEntry( newEntry.getDn(), testMonitor );
166                     for ( int i = 2; testEntry != null; i++ )
167                     {
168                         newRdnValue = BrowserCoreMessages.bind( BrowserCoreMessages.copy_n_of_s, i + " ", rdnValue ); //$NON-NLS-1$
169
newRdn = getNewRdn( rdn, newRdnValue );
170                         newEntry = new Entry( parent, newRdn );
171                         testEntry = parent.getConnection().getEntry( newEntry.getDn(), testMonitor );
172                     }
173                 }
174
175                 // copy attributes
176
for ( int i = 0; i < attributesToCopy.length; i++ )
177                 {
178                     IAttribute attributeToCopy = attributesToCopy[i];
179
180                     if ( SchemaUtils.isModifyable( attributeToCopy.getAttributeTypeDescription() )
181                         || IAttribute.REFERRAL_ATTRIBUTE.equalsIgnoreCase( attributeToCopy.getDescription() ) )
182                     {
183                         IAttribute newAttribute = new Attribute( newEntry, attributeToCopy.getDescription() );
184                         newEntry.addAttribute( newAttribute );
185                         IValue[] valuesToCopy = attributeToCopy.getValues();
186                         for ( int j = 0; j < valuesToCopy.length; j++ )
187                         {
188                             IValue valueToCopy = valuesToCopy[j];
189                             IValue newValue = new Value( newAttribute, valueToCopy.getRawValue() );
190                             newAttribute.addValue( newValue );
191                         }
192                     }
193                 }
194
195                 // check if RDN attributes ar present
196
RDN newRdn = newEntry.getRdn();
197                 RDNPart[] oldRdnParts = rdn.getParts();
198                 for ( int i = 0; i < oldRdnParts.length; i++ )
199                 {
200                     RDNPart part = oldRdnParts[i];
201                     IAttribute rdnAttribute = newEntry.getAttribute( part.getType() );
202                     if ( rdnAttribute != null )
203                     {
204                         IValue[] values = rdnAttribute.getValues();
205                         for ( int ii = 0; ii < values.length; ii++ )
206                         {
207                             if ( part.getUnencodedValue().equals( values[ii].getRawValue() ) )
208                             {
209                                 rdnAttribute.deleteValue( values[ii] );
210                             }
211                             if ( rdnAttribute.getValueSize() == 0 )
212                             {
213                                 newEntry.deleteAttribute( rdnAttribute );
214                             }
215                         }
216                     }
217                 }
218                 RDNPart[] newRdnParts = newRdn.getParts();
219                 for ( int i = 0; i < newRdnParts.length; i++ )
220                 {
221                     RDNPart part = newRdnParts[i];
222                     IAttribute rdnAttribute = newEntry.getAttribute( part.getType() );
223                     if ( rdnAttribute == null )
224                     {
225                         rdnAttribute = new Attribute( newEntry, part.getType() );
226                         newEntry.addAttribute( rdnAttribute );
227                         rdnAttribute.addValue( new Value( rdnAttribute, part.getUnencodedValue() ) );
228                     }
229                     else
230                     {
231                         boolean mustAdd = true;
232                         IValue[] values = rdnAttribute.getValues();
233                         for ( int ii = 0; ii < values.length; ii++ )
234                         {
235                             if ( part.getUnencodedValue().equals( values[ii].getStringValue() ) )
236                             {
237                                 mustAdd = false;
238                                 break;
239                             }
240                         }
241                         if ( mustAdd )
242                         {
243                             rdnAttribute.addValue( new Value( rdnAttribute, part.getUnencodedValue() ) );
244                         }
245                     }
246                 }
247
248                 newEntry.getConnection().create( newEntry, monitor );
249                 newEntry.getParententry().addChild( newEntry );
250                 newEntry.setHasChildrenHint( false );
251
252                 num++;
253                 monitor.reportProgress( BrowserCoreMessages.bind( BrowserCoreMessages.model__copied_n_entries,
254                     new String JavaDoc[]
255                         { Integer.toString( num ) } ) );
256
257                 // check for children
258
if ( !monitor.isCanceled() && ( scope == ISearch.SCOPE_ONELEVEL || scope == ISearch.SCOPE_SUBTREE ) )
259                 {
260
261                     SearchParameter subParam = new SearchParameter();
262                     subParam.setSearchBase( entryToCopy.getDn() );
263                     subParam.setFilter( ISearch.FILTER_TRUE );
264                     subParam.setScope( ISearch.SCOPE_ONELEVEL );
265                     subParam.setReturningAttributes( ISearch.NO_ATTRIBUTES );
266                     ISearch subSearch = new Search( entryToCopy.getConnection(), subParam );
267                     entryToCopy.getConnection().search( subSearch, monitor );
268
269                     ISearchResult[] subSrs = subSearch.getSearchResults();
270                     if ( !monitor.isCanceled() && subSrs != null && subSrs.length > 0 )
271                     {
272
273                         for ( int i = 0; i < subSrs.length; i++ )
274                         {
275                             ISearchResult subSearchResult = subSrs[i];
276                             IEntry childEntry = subSearchResult.getEntry();
277
278                             if ( scope == ISearch.SCOPE_ONELEVEL )
279                             {
280                                 num = this
281                                     .copyEntryRecursive( childEntry, newEntry, ISearch.SCOPE_OBJECT, num, monitor );
282                             }
283                             else if ( scope == ISearch.SCOPE_SUBTREE )
284                             {
285                                 num = this.copyEntryRecursive( childEntry, newEntry, ISearch.SCOPE_SUBTREE, num,
286                                     monitor );
287                             }
288                         }
289
290                     }
291                 }
292             }
293         }
294         catch ( Exception JavaDoc e )
295         {
296             monitor.reportError( e );
297         }
298         return num;
299     }
300
301
302     private RDN getNewRdn( RDN rdn, String JavaDoc newRdnValue ) throws NameException
303     {
304         String JavaDoc[] names = rdn.getTypes();
305         String JavaDoc[] values = rdn.getValues();
306         values[0] = newRdnValue;
307         RDN newRdn = new RDN( names, values, true );
308         return newRdn;
309     }
310
311 }
312
Popular Tags