KickJava   Java API By Example, From Geeks To Geeks.

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


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.EntryAddedEvent;
30 import org.apache.directory.ldapstudio.browser.core.events.EventRegistry;
31 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
32 import org.apache.directory.ldapstudio.browser.core.model.IEntry;
33
34
35 public class CreateEntryJob extends AbstractAsyncBulkJob
36 {
37
38     private IEntry[] entriesToCreate;
39
40     private IEntry[] createdEntries;
41
42
43     public CreateEntryJob( IEntry[] entriesToCreate )
44     {
45         this.entriesToCreate = entriesToCreate;
46         createdEntries = new IEntry[entriesToCreate.length];
47         setName( entriesToCreate.length == 1 ? BrowserCoreMessages.jobs__create_entry_name_1
48             : BrowserCoreMessages.jobs__create_entry_name_n );
49     }
50
51
52     protected IConnection[] getConnections()
53     {
54         IConnection[] connections = new IConnection[entriesToCreate.length];
55         for ( int i = 0; i < connections.length; i++ )
56         {
57             connections[i] = entriesToCreate[i].getConnection();
58         }
59         return connections;
60     }
61
62
63     protected Object JavaDoc[] getLockedObjects()
64     {
65         List JavaDoc l = new ArrayList JavaDoc();
66         l.addAll( Arrays.asList( entriesToCreate ) );
67         return l.toArray();
68     }
69
70
71     protected void executeBulkJob( ExtendedProgressMonitor monitor )
72     {
73
74         monitor.beginTask( entriesToCreate.length == 1 ? BrowserCoreMessages.bind(
75             BrowserCoreMessages.jobs__create_entry_task_1, new String JavaDoc[]
76                 { entriesToCreate[0].getDn().toString() } ) : BrowserCoreMessages.bind(
77             BrowserCoreMessages.jobs__create_entry_task_n, new String JavaDoc[]
78                 { Integer.toString( entriesToCreate.length ) } ), 2 + entriesToCreate.length );
79         monitor.reportProgress( " " ); //$NON-NLS-1$
80
monitor.worked( 1 );
81
82         for ( int i = 0; !monitor.isCanceled() && i < entriesToCreate.length; i++ )
83         {
84             IEntry entryToCreate = entriesToCreate[i];
85
86             entryToCreate.getConnection().create( entryToCreate, monitor );
87
88             if ( !monitor.errorsReported() )
89             {
90                 createdEntries[i] = entryToCreate.getConnection().getEntry( entryToCreate.getDn(), monitor );
91                 // createdEntries[i].getParententry().addChild(entry, this);
92
createdEntries[i].setHasChildrenHint( false );
93             }
94
95             monitor.worked( 1 );
96         }
97     }
98
99
100     protected void runNotification()
101     {
102         for ( int i = 0; i < createdEntries.length; i++ )
103         {
104             if ( createdEntries[i] != null )
105             {
106                 EventRegistry.fireEntryUpdated( new EntryAddedEvent( createdEntries[i].getConnection(),
107                     createdEntries[i] ), this );
108             }
109         }
110     }
111
112
113     protected String JavaDoc getErrorMessage()
114     {
115         return entriesToCreate.length == 1 ? BrowserCoreMessages.jobs__create_entry_error_1
116             : BrowserCoreMessages.jobs__create_entry_error_n;
117     }
118
119 }
120
Popular Tags