Package org.apache.directory.studio.ldapbrowser.core.jobs

Source Code of org.apache.directory.studio.ldapbrowser.core.jobs.InitializeChildrenJob

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

package org.apache.directory.studio.ldapbrowser.core.jobs;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.directory.studio.connection.core.Connection;
import org.apache.directory.studio.connection.core.StudioProgressMonitor;
import org.apache.directory.studio.ldapbrowser.core.BrowserCoreConstants;
import org.apache.directory.studio.ldapbrowser.core.BrowserCoreMessages;
import org.apache.directory.studio.ldapbrowser.core.BrowserCorePlugin;
import org.apache.directory.studio.ldapbrowser.core.events.ChildrenInitializedEvent;
import org.apache.directory.studio.ldapbrowser.core.events.EventRegistry;
import org.apache.directory.studio.ldapbrowser.core.internal.model.AliasBaseEntry;
import org.apache.directory.studio.ldapbrowser.core.internal.model.ReferralBaseEntry;
import org.apache.directory.studio.ldapbrowser.core.internal.model.Search;
import org.apache.directory.studio.ldapbrowser.core.model.Control;
import org.apache.directory.studio.ldapbrowser.core.model.IBrowserConnection;
import org.apache.directory.studio.ldapbrowser.core.model.IEntry;
import org.apache.directory.studio.ldapbrowser.core.model.IRootDSE;
import org.apache.directory.studio.ldapbrowser.core.model.ISearch;
import org.apache.directory.studio.ldapbrowser.core.model.ISearchResult;


public class InitializeChildrenJob extends AbstractAsyncBulkJob
{

    private IEntry[] entries;


    public InitializeChildrenJob( IEntry[] entries )
    {
        this.entries = entries;
        setName( BrowserCoreMessages.jobs__init_entries_title_subonly );
    }


    protected Connection[] getConnections()
    {
        Connection[] connections = new Connection[entries.length];
        for ( int i = 0; i < connections.length; i++ )
        {
            connections[i] = entries[i].getBrowserConnection().getConnection();
        }
        return connections;
    }


    protected Object[] getLockedObjects()
    {
        List l = new ArrayList();
        l.addAll( Arrays.asList( entries ) );
        return l.toArray();
    }


    protected String getErrorMessage()
    {
        return entries.length == 1 ? BrowserCoreMessages.jobs__init_entries_error_1
            : BrowserCoreMessages.jobs__init_entries_error_n;
    }


    protected void executeBulkJob( StudioProgressMonitor monitor )
    {
        monitor.beginTask( " ", entries.length + 2 ); //$NON-NLS-1$
        monitor.reportProgress( " " ); //$NON-NLS-1$

        for ( int pi = 0; pi < entries.length && !monitor.isCanceled(); pi++ )
        {

            monitor.setTaskName( BrowserCoreMessages.bind( BrowserCoreMessages.jobs__init_entries_task, new String[]
                { this.entries[pi].getDn().toString() } ) );
            monitor.worked( 1 );

            if ( entries[pi].getBrowserConnection() != null && entries[pi].isDirectoryEntry() )
            {
                initializeChildren( entries[pi], monitor );
            }
        }
    }


    protected void runNotification()
    {
        for ( int pi = 0; pi < entries.length; pi++ )
        {
            IEntry parent = entries[pi];
            if ( parent.getBrowserConnection() != null && parent.isDirectoryEntry() )
            {
                EventRegistry.fireEntryUpdated( new ChildrenInitializedEvent( parent ), this );
            }
        }
    }


    public static void initializeChildren( IEntry parent, StudioProgressMonitor monitor )
    {

        if ( parent instanceof IRootDSE )
        {
            // special handling for Root DSE
            return;
        }
        else
        {
            monitor.reportProgress( BrowserCoreMessages.bind( BrowserCoreMessages.jobs__init_entries_progress_sub,
                new String[]
                    { parent.getDn().toString() } ) );

            // clear old children
            IEntry[] oldChildren = parent.getChildren();
            for ( int i = 0; oldChildren != null && i < oldChildren.length; i++ )
            {
                if ( oldChildren[i] != null )
                {
                    parent.deleteChild( oldChildren[i] );
                }
            }
            parent.setChildrenInitialized( false );
           
            // determine alias and referral handling
            int scope = ISearch.SCOPE_ONELEVEL;
            int derefAliasMethod = parent.getBrowserConnection().getAliasesDereferencingMethod();
            int handleReferralsMethod = parent.getBrowserConnection().getReferralsHandlingMethod();
            if ( BrowserCorePlugin.getDefault().getPluginPreferences().getBoolean(
                BrowserCoreConstants.PREFERENCE_SHOW_ALIAS_AND_REFERRAL_OBJECTS ) )
            {
                scope = ( parent.isAlias() || parent.isReferral() ) ? ISearch.SCOPE_OBJECT : ISearch.SCOPE_ONELEVEL;
                derefAliasMethod = parent.isAlias() ? IBrowserConnection.DEREFERENCE_ALIASES_FINDING
                    : IBrowserConnection.DEREFERENCE_ALIASES_NEVER;
                handleReferralsMethod = parent.isReferral() ? IBrowserConnection.HANDLE_REFERRALS_FOLLOW
                    : IBrowserConnection.HANDLE_REFERRALS_IGNORE;
            }

            // get children,
            ISearch search = new Search( null, parent.getBrowserConnection(), parent.getDn(), parent.getChildrenFilter(),
                ISearch.NO_ATTRIBUTES, scope, parent.getBrowserConnection().getCountLimit(),
                parent.getBrowserConnection().getTimeLimit(), derefAliasMethod, handleReferralsMethod, BrowserCorePlugin
                    .getDefault().getPluginPreferences().getBoolean( BrowserCoreConstants.PREFERENCE_CHECK_FOR_CHILDREN ),
                BrowserCorePlugin.getDefault().getPluginPreferences().getBoolean(
                    BrowserCoreConstants.PREFERENCE_SHOW_ALIAS_AND_REFERRAL_OBJECTS ), null );
            parent.getBrowserConnection().search( search, monitor );
            ISearchResult[] srs = search.getSearchResults();
            monitor.reportProgress( BrowserCoreMessages.bind( BrowserCoreMessages.jobs__init_entries_progress_subcount,
                new String[]
                    { srs == null ? Integer.toString( 0 ) : Integer.toString( srs.length ), parent.getDn().toString() } ) );

            // fill children in search result
            if ( srs != null && srs.length > 0 )
            {

                /*
                 * clearing old children before filling new subenties is
                 * necessary to handle aliases and referrals.
                 */
                IEntry[] connChildren = parent.getChildren();
                for ( int i = 0; connChildren != null && i < connChildren.length; i++ )
                {
                    if ( connChildren[i] != null )
                    {
                        parent.deleteChild( connChildren[i] );
                    }
                }
                parent.setChildrenInitialized( false );

                for ( int i = 0; srs != null && i < srs.length; i++ )
                {
                    if ( parent.isReferral() )
                    {
                        ReferralBaseEntry referralBaseEntry = new ReferralBaseEntry( srs[i].getEntry().getBrowserConnection(),
                            srs[i].getEntry().getDn() );
                        parent.addChild( referralBaseEntry );
                        // System.out.println("Ref: " +
                        // referralBaseEntry.getUrl());
                    }
                    else if ( parent.isAlias() )
                    {
                        AliasBaseEntry aliasBaseEntry = new AliasBaseEntry( srs[i].getEntry().getBrowserConnection(), srs[i]
                            .getEntry().getDn() );
                        parent.addChild( aliasBaseEntry );
                        // System.out.println("Ali: " +
                        // aliasBaseEntry.getUrl());
                    }
                    else
                    {
                        parent.addChild( srs[i].getEntry() );
                    }
                }
            }
            else
            {
                parent.setHasChildrenHint( false );
            }

            // get subentries
            ISearch subSearch = new Search( null, parent.getBrowserConnection(), parent.getDn(), parent.getChildrenFilter()!=null?parent.getChildrenFilter():ISearch.FILTER_SUBENTRY,
                ISearch.NO_ATTRIBUTES, scope, parent.getBrowserConnection().getCountLimit(),
                parent.getBrowserConnection().getTimeLimit(), derefAliasMethod, handleReferralsMethod, BrowserCorePlugin
                    .getDefault().getPluginPreferences().getBoolean( BrowserCoreConstants.PREFERENCE_CHECK_FOR_CHILDREN ),
                BrowserCorePlugin.getDefault().getPluginPreferences().getBoolean(
                    BrowserCoreConstants.PREFERENCE_SHOW_ALIAS_AND_REFERRAL_OBJECTS ), new Control[]
                    { Control.SUBENTRIES_CONTROL } );
            if ( BrowserCorePlugin.getDefault().getPluginPreferences().getBoolean(
                BrowserCoreConstants.PREFERENCE_FETCH_SUBENTRIES ) )
            {
                parent.getBrowserConnection().search( subSearch, monitor );
                ISearchResult[] subSrs = subSearch.getSearchResults();
                monitor.reportProgress( BrowserCoreMessages.bind( BrowserCoreMessages.jobs__init_entries_progress_subcount,
                    new String[]
                        { subSrs == null ? Integer.toString( 0 ) : Integer.toString( subSrs.length ),
                            parent.getDn().toString() } ) );
                // fill children in search result
                if ( subSrs != null && subSrs.length > 0 )
                {

                    for ( int i = 0; subSrs != null && i < subSrs.length; i++ )
                    {
                        parent.addChild( subSrs[i].getEntry() );
                    }
                }
            }

            // check exceeded limits / canceled
            parent.setHasMoreChildren( search.isCountLimitExceeded() || subSearch.isCountLimitExceeded()
                || monitor.isCanceled() );
           
            // set initialized state
            parent.setChildrenInitialized( true );
        }
    }

}
TOP

Related Classes of org.apache.directory.studio.ldapbrowser.core.jobs.InitializeChildrenJob

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.