static void importLdif()

in plugins/ldapbrowser.core/src/main/java/org/apache/directory/studio/ldapbrowser/core/jobs/ImportLdifRunnable.java [249:390]


    static void importLdif( IBrowserConnection browserConnection, LdifEnumeration enumeration, Writer logWriter,
        boolean updateIfEntryExists, boolean continueOnError, StudioProgressMonitor monitor )
    {
        if ( browserConnection == null )
        {
            return;
        }

        StudioProgressMonitor dummyMonitor = new StudioProgressMonitor( monitor );
        int importedCount = 0;
        int errorCount = 0;
        try
        {
            while ( !monitor.isCanceled() && enumeration.hasNext() )
            {
                LdifContainer container = enumeration.next();

                if ( container instanceof LdifRecord )
                {
                    LdifRecord record = ( LdifRecord ) container;
                    try
                    {
                        dummyMonitor.reset();
                        importLdifRecord( browserConnection, record, updateIfEntryExists, dummyMonitor );
                        if ( dummyMonitor.errorsReported() )
                        {
                            errorCount++;
                            logModificationError( browserConnection, logWriter, record, dummyMonitor.getException(),
                                monitor );

                            if ( !continueOnError )
                            {
                                monitor.reportError( dummyMonitor.getException() );
                                return;
                            }
                        }
                        else
                        {
                            importedCount++;
                            logModification( browserConnection, logWriter, record, monitor );

                            // update cache and adjust attribute/children initialization flags
                            Dn dn = new Dn( record.getDnLine().getValueAsString() );
                            IEntry entry = browserConnection.getEntryFromCache( dn );
                            Dn parentDn = dn.getParent();
                            IEntry parentEntry = null;
                            while ( parentEntry == null && parentDn != null )
                            {
                                parentEntry = browserConnection.getEntryFromCache( parentDn );
                                parentDn = parentDn.getParent();
                            }

                            if ( record instanceof LdifChangeDeleteRecord )
                            {
                                if ( entry != null )
                                {
                                    entry.setAttributesInitialized( false );
                                    browserConnection.uncacheEntryRecursive( entry );
                                }
                                if ( parentEntry != null )
                                {
                                    parentEntry.setChildrenInitialized( false );
                                }
                            }
                            else if ( record instanceof LdifChangeModDnRecord )
                            {
                                if ( entry != null )
                                {
                                    entry.setAttributesInitialized( false );
                                    browserConnection.uncacheEntryRecursive( entry );
                                }
                                if ( parentEntry != null )
                                {
                                    parentEntry.setChildrenInitialized( false );
                                }
                                LdifChangeModDnRecord modDnRecord = ( LdifChangeModDnRecord ) record;
                                if ( modDnRecord.getNewsuperiorLine() != null )
                                {
                                    Dn newSuperiorDn = new Dn( modDnRecord.getNewsuperiorLine()
                                        .getValueAsString() );
                                    IEntry newSuperiorEntry = browserConnection.getEntryFromCache( newSuperiorDn );
                                    if ( newSuperiorEntry != null )
                                    {
                                        newSuperiorEntry.setChildrenInitialized( false );
                                    }
                                }
                            }
                            else if ( record instanceof LdifChangeAddRecord || record instanceof LdifContentRecord )
                            {
                                if ( entry != null )
                                {
                                    entry.setAttributesInitialized( false );
                                }
                                if ( parentEntry != null )
                                {
                                    parentEntry.setChildrenInitialized( false );
                                    parentEntry.setHasChildrenHint( true );
                                }
                            }
                            else
                            {
                                if ( entry != null )
                                {
                                    entry.setAttributesInitialized( false );
                                }
                            }
                        }
                    }
                    catch ( Exception e )
                    {
                        logModificationError( browserConnection, logWriter, record, e, monitor );
                        errorCount++;

                        if ( !continueOnError )
                        {
                            monitor.reportError( e );
                            return;
                        }
                    }

                    monitor.reportProgress( BrowserCoreMessages.bind(
                        BrowserCoreMessages.ldif__imported_n_entries_m_errors, new String[]
                            { "" + importedCount, "" + errorCount } ) ); //$NON-NLS-1$ //$NON-NLS-2$
                }
                else
                {
                    logWriter.write( container.toRawString() );
                }
            }

            if ( errorCount > 0 )
            {
                monitor.reportError( BrowserCoreMessages.bind( BrowserCoreMessages.ldif__n_errors_see_logfile,
                    new String[]
                        { "" + errorCount } ) ); //$NON-NLS-1$
            }
        }
        catch ( Exception e )
        {
            monitor.reportError( e );
        }
    }