public boolean performFinish()

in plugins/ldapbrowser.ui/src/main/java/org/apache/directory/studio/ldapbrowser/ui/wizards/BatchOperationWizard.java [323:536]


    public boolean performFinish()
    {
        if ( this.applyOnPage != null )
        {
            this.applyOnPage.saveDialogSettings();
            this.finishPage.saveDialogSettings();

            // get LDIF
            String ldifFragment = ""; //$NON-NLS-1$
            if ( typePage.getOperationType() == BatchOperationTypeWizardPage.OPERATION_TYPE_CREATE_LDIF )
            {
                ldifFragment = this.ldifPage.getLdifFragment();
            }
            else if ( typePage.getOperationType() == BatchOperationTypeWizardPage.OPERATION_TYPE_MODIFY )
            {
                ldifFragment = this.modifyPage.getLdifFragment();
            }
            if ( typePage.getOperationType() == BatchOperationTypeWizardPage.OPERATION_TYPE_DELETE )
            {
                ldifFragment = "changetype: delete" + BrowserCoreConstants.LINE_SEPARATOR; //$NON-NLS-1$
            }

            // get DNs
            Dn[] dns = applyOnPage.getApplyOnDns();
            if ( dns == null )
            {
                if ( applyOnPage.getApplyOnSearch() != null )
                {
                    ISearch search = applyOnPage.getApplyOnSearch();
                    if ( search.getBrowserConnection() != null )
                    {
                        search.setSearchResults( null );
                        SearchRunnable runnable = new SearchRunnable( new ISearch[]
                            { search } );
                        IStatus status = RunnableContextRunner.execute( runnable, getContainer(), true );
                        if ( status.isOK() )
                        {
                            ISearchResult[] srs = search.getSearchResults();
                            dns = new Dn[srs.length];
                            for ( int i = 0; i < srs.length; i++ )
                            {
                                dns[i] = srs[i].getDn();
                            }
                        }
                    }
                }
            }

            if ( dns != null )
            {
                StringBuffer ldif = new StringBuffer();
                for ( int i = 0; i < dns.length; i++ )
                {
                    ldif.append( "dn: " ); //$NON-NLS-1$
                    ldif.append( dns[i].getName() );
                    ldif.append( BrowserCoreConstants.LINE_SEPARATOR );
                    ldif.append( ldifFragment );
                    ldif.append( BrowserCoreConstants.LINE_SEPARATOR );
                }

                if ( finishPage.getExecutionMethod() == BatchOperationFinishWizardPage.EXECUTION_METHOD_LDIF_EDITOR )
                {
                    // Opening an LDIF Editor with the LDIF content
                    try
                    {
                        IEditorInput input = new NonExistingLdifEditorInput();
                        IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
                            .openEditor( input, LdifEditor.getId() );
                        IDocumentProvider documentProvider = ( ( LdifEditor ) editor ).getDocumentProvider();
                        if ( documentProvider != null )
                        {
                            IDocument document = documentProvider.getDocument( input );
                            if ( document != null )
                            {
                                document.set( ldif.toString() );
                            }
                        }
                    }
                    catch ( PartInitException e )
                    {
                        return false;
                    }

                    return true;
                }
                else if ( finishPage.getExecutionMethod() == BatchOperationFinishWizardPage.EXECUTION_METHOD_LDIF_FILE ) // TODO
                {
                    // Saving the LDIF to a file

                    // Getting the shell
                    Shell shell = Display.getDefault().getActiveShell();

                    // detect IDE or RCP:
                    // check if perspective org.eclipse.ui.resourcePerspective is available
                    boolean isIDE = CommonUIUtils.isIDEEnvironment();

                    if ( isIDE )
                    {
                        // Asking the user for the location where to 'save as' the file
                        SaveAsDialog dialog = new SaveAsDialog( shell );

                        if ( dialog.open() != Dialog.OK )
                        {
                            return false;
                        }

                        // Getting if the resulting file
                        IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile( dialog.getResult() );

                        try
                        {
                            // Creating the file if it does not exist
                            if ( !file.exists() )
                            {
                                file.create( new ByteArrayInputStream( "".getBytes() ), true, null ); //$NON-NLS-1$
                            }

                            // Saving the LDIF to the file in the workspace
                            file.setContents( new ByteArrayInputStream( ldif.toString().getBytes() ), true, true,
                                new NullProgressMonitor() );
                        }
                        catch ( Exception e )
                        {
                            return false;
                        }
                    }
                    else
                    {
                        boolean canOverwrite = false;
                        String path = null;

                        while ( !canOverwrite )
                        {
                            // Open FileDialog
                            FileDialog dialog = new FileDialog( shell, SWT.SAVE );
                            path = dialog.open();
                            if ( path == null )
                            {
                                return false;
                            }

                            // Check whether file exists and if so, confirm overwrite
                            final File externalFile = new File( path );
                            if ( externalFile.exists() )
                            {
                                String question = NLS.bind( Messages
                                    .getString( "BatchOperationWizard.TheFileAlreadyExistsReplace" ), path ); //$NON-NLS-1$
                                MessageDialog overwriteDialog = new MessageDialog( shell, Messages
                                    .getString( "BatchOperationWizard.Question" ), null, question, //$NON-NLS-1$
                                    MessageDialog.QUESTION, new String[]
                                        {
                                            IDialogConstants.YES_LABEL,
                                            IDialogConstants.NO_LABEL,
                                            IDialogConstants.CANCEL_LABEL }, 0 );
                                int overwrite = overwriteDialog.open();
                                switch ( overwrite )
                                {
                                    case 0: // Yes
                                        canOverwrite = true;
                                        break;
                                    case 1: // No
                                        break;
                                    case 2: // Cancel
                                    default:
                                        return false;
                                }
                            }
                            else
                            {
                                canOverwrite = true;
                            }
                        }

                        // Saving the LDIF to the file on disk
                        try
                        {
                            BufferedWriter outFile = new BufferedWriter( new FileWriter( path ) );
                            outFile.write( ldif.toString() );
                            outFile.close();
                        }
                        catch ( Exception e )
                        {
                            return false;
                        }
                    }

                    return true;
                }
                else if ( finishPage.getExecutionMethod() == BatchOperationFinishWizardPage.EXECUTION_METHOD_LDIF_CLIPBOARD )
                {
                    // Copying the LDIF to the clipboard
                    CopyAction.copyToClipboard( new Object[]
                        { ldif.toString() }, new Transfer[]
                        { TextTransfer.getInstance() } );

                    return true;
                }
                else if ( finishPage.getExecutionMethod() == BatchOperationFinishWizardPage.EXECUTION_METHOD_ON_CONNECTION )
                {
                    // Executing the LDIF on the connection
                    ExecuteLdifRunnable runnable = new ExecuteLdifRunnable( getConnection(), ldif.toString(), true,
                        finishPage.getContinueOnError() );
                    StudioBrowserJob job = new StudioBrowserJob( runnable );
                    job.execute();

                    return true;
                }
            }

            return false;
        }

        return true;
    }