KickJava   Java API By Example, From Geeks To Geeks.

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


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.LinkedHashSet JavaDoc;
27 import java.util.List JavaDoc;
28
29 import org.apache.directory.ldapstudio.browser.core.BrowserCoreMessages;
30 import org.apache.directory.ldapstudio.browser.core.events.AttributesInitializedEvent;
31 import org.apache.directory.ldapstudio.browser.core.events.EventRegistry;
32 import org.apache.directory.ldapstudio.browser.core.internal.model.RootDSE;
33 import org.apache.directory.ldapstudio.browser.core.internal.model.Search;
34 import org.apache.directory.ldapstudio.browser.core.model.IAttribute;
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.schema.AttributeTypeDescription;
39 import org.apache.directory.ldapstudio.browser.core.model.schema.SchemaUtils;
40
41
42 public class InitializeAttributesJob extends AbstractAsyncBulkJob
43 {
44
45     private IEntry[] entries;
46
47     private boolean initOperationalAttributes;
48
49
50     public InitializeAttributesJob( IEntry[] entries, boolean initOperationalAttributes )
51     {
52         this.entries = entries;
53         this.initOperationalAttributes = initOperationalAttributes;
54         setName( BrowserCoreMessages.jobs__init_entries_title_attonly );
55     }
56
57
58     protected IConnection[] getConnections()
59     {
60         IConnection[] connections = new IConnection[entries.length];
61         for ( int i = 0; i < connections.length; i++ )
62         {
63             connections[i] = entries[i].getConnection();
64         }
65         return connections;
66     }
67
68
69     protected Object JavaDoc[] getLockedObjects()
70     {
71         List JavaDoc l = new ArrayList JavaDoc();
72         l.addAll( Arrays.asList( entries ) );
73         return l.toArray();
74     }
75
76
77     protected String JavaDoc getErrorMessage()
78     {
79         return entries.length == 1 ? BrowserCoreMessages.jobs__init_entries_error_1
80             : BrowserCoreMessages.jobs__init_entries_error_n;
81     }
82
83
84     protected void executeBulkJob( ExtendedProgressMonitor monitor )
85     {
86         monitor.beginTask( " ", entries.length + 2 ); //$NON-NLS-1$
87
monitor.reportProgress( " " ); //$NON-NLS-1$
88

89         for ( int pi = 0; pi < entries.length && !monitor.isCanceled(); pi++ )
90         {
91             monitor.setTaskName( BrowserCoreMessages.bind( BrowserCoreMessages.jobs__init_entries_task, new String JavaDoc[]
92                 { this.entries[pi].getDn().toString() } ) );
93             monitor.worked( 1 );
94             if ( entries[pi].getConnection() != null && entries[pi].getConnection().isOpened()
95                 && entries[pi].isDirectoryEntry() )
96             {
97                 initializeAttributes( entries[pi], initOperationalAttributes, monitor );
98             }
99         }
100     }
101
102
103     protected void runNotification()
104     {
105         for ( int pi = 0; pi < entries.length; pi++ )
106         {
107             IEntry parent = entries[pi];
108             if ( parent.getConnection() != null && entries[pi].getConnection().isOpened() && parent.isDirectoryEntry() )
109             {
110                 EventRegistry.fireEntryUpdated( new AttributesInitializedEvent( parent ), this );
111             }
112         }
113     }
114
115
116     public static void initializeAttributes( IEntry entry, boolean initOperationalAttributes,
117         ExtendedProgressMonitor monitor )
118     {
119
120         // get user attributes or both user and operational attributes
121
String JavaDoc[] returningAttributes = null;
122         LinkedHashSet JavaDoc raSet = new LinkedHashSet JavaDoc();
123         raSet.add( ISearch.ALL_USER_ATTRIBUTES );
124         if ( initOperationalAttributes )
125         {
126             AttributeTypeDescription[] opAtds = SchemaUtils.getOperationalAttributeDescriptions( entry.getConnection()
127                 .getSchema() );
128             String JavaDoc[] attributeTypeDescriptionNames = SchemaUtils.getAttributeTypeDescriptionNames( opAtds );
129             raSet.addAll( Arrays.asList( attributeTypeDescriptionNames ) );
130             raSet.add( ISearch.ALL_OPERATIONAL_ATTRIBUTES );
131         }
132         if ( entry instanceof RootDSE )
133         {
134             raSet.add( ISearch.ALL_USER_ATTRIBUTES );
135             raSet.add( ISearch.ALL_OPERATIONAL_ATTRIBUTES );
136         }
137         if ( entry.isReferral() )
138         {
139             raSet.add( IAttribute.REFERRAL_ATTRIBUTE );
140         }
141         returningAttributes = ( String JavaDoc[] ) raSet.toArray( new String JavaDoc[raSet.size()] );
142
143         initializeAttributes( entry, returningAttributes, monitor );
144     }
145
146
147     public static void initializeAttributes( IEntry entry, String JavaDoc[] attributes, ExtendedProgressMonitor monitor )
148     {
149
150         monitor.reportProgress( BrowserCoreMessages.bind( BrowserCoreMessages.jobs__init_entries_progress_att,
151             new String JavaDoc[]
152                 { entry.getDn().toString() } ) );
153
154         // entry.setAttributesInitialized(false, entry.getConnection());
155

156         // search
157
ISearch search = new Search( null, entry.getConnection(), entry.getDn(), ISearch.FILTER_TRUE, attributes,
158             ISearch.SCOPE_OBJECT, 0, 0, IConnection.DEREFERENCE_ALIASES_NEVER, IConnection.HANDLE_REFERRALS_IGNORE,
159             false, false, null );
160         entry.getConnection().search( search, monitor );
161
162         // set initialized state
163
entry.setAttributesInitialized( true );
164     }
165
166 }
167
Popular Tags