You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by se...@apache.org on 2008/08/27 01:23:09 UTC

svn commit: r689285 - in /directory/studio/trunk: ldapbrowser-core/src/main/java/org/apache/directory/studio/ldapbrowser/core/jobs/ ldapbrowser-ui/src/main/java/org/apache/directory/studio/ldapbrowser/ui/wizards/

Author: seelmann
Date: Tue Aug 26 16:23:08 2008
New Revision: 689285

URL: http://svn.apache.org/viewvc?rev=689285&view=rev
Log:
Fix for DIRSTUDIO-147 (Allow LDIF imports to overwrite existing entries)

Modified:
    directory/studio/trunk/ldapbrowser-core/src/main/java/org/apache/directory/studio/ldapbrowser/core/jobs/ExecuteLdifRunnable.java
    directory/studio/trunk/ldapbrowser-core/src/main/java/org/apache/directory/studio/ldapbrowser/core/jobs/ImportLdifJob.java
    directory/studio/trunk/ldapbrowser-ui/src/main/java/org/apache/directory/studio/ldapbrowser/ui/wizards/ImportLdifMainWizardPage.java
    directory/studio/trunk/ldapbrowser-ui/src/main/java/org/apache/directory/studio/ldapbrowser/ui/wizards/ImportLdifWizard.java

Modified: directory/studio/trunk/ldapbrowser-core/src/main/java/org/apache/directory/studio/ldapbrowser/core/jobs/ExecuteLdifRunnable.java
URL: http://svn.apache.org/viewvc/directory/studio/trunk/ldapbrowser-core/src/main/java/org/apache/directory/studio/ldapbrowser/core/jobs/ExecuteLdifRunnable.java?rev=689285&r1=689284&r2=689285&view=diff
==============================================================================
--- directory/studio/trunk/ldapbrowser-core/src/main/java/org/apache/directory/studio/ldapbrowser/core/jobs/ExecuteLdifRunnable.java (original)
+++ directory/studio/trunk/ldapbrowser-core/src/main/java/org/apache/directory/studio/ldapbrowser/core/jobs/ExecuteLdifRunnable.java Tue Aug 26 16:23:08 2008
@@ -135,7 +135,7 @@
                 }
             };
 
-            ImportLdifJob.importLdif( browserConnection, enumeration, logWriter, continueOnError, monitor );
+            ImportLdifJob.importLdif( browserConnection, enumeration, logWriter, false, continueOnError, monitor );
 
             logWriter.close();
             ldifReader.close();

Modified: directory/studio/trunk/ldapbrowser-core/src/main/java/org/apache/directory/studio/ldapbrowser/core/jobs/ImportLdifJob.java
URL: http://svn.apache.org/viewvc/directory/studio/trunk/ldapbrowser-core/src/main/java/org/apache/directory/studio/ldapbrowser/core/jobs/ImportLdifJob.java?rev=689285&r1=689284&r2=689285&view=diff
==============================================================================
--- directory/studio/trunk/ldapbrowser-core/src/main/java/org/apache/directory/studio/ldapbrowser/core/jobs/ImportLdifJob.java (original)
+++ directory/studio/trunk/ldapbrowser-core/src/main/java/org/apache/directory/studio/ldapbrowser/core/jobs/ImportLdifJob.java Tue Aug 26 16:23:08 2008
@@ -36,6 +36,7 @@
 import java.util.List;
 
 import javax.naming.InvalidNameException;
+import javax.naming.NameAlreadyBoundException;
 import javax.naming.directory.Attribute;
 import javax.naming.directory.Attributes;
 import javax.naming.directory.BasicAttribute;
@@ -50,17 +51,21 @@
 import org.apache.directory.studio.connection.core.Connection;
 import org.apache.directory.studio.connection.core.ConnectionCoreConstants;
 import org.apache.directory.studio.connection.core.DnUtils;
-import org.apache.directory.studio.connection.core.jobs.StudioProgressMonitor;
 import org.apache.directory.studio.connection.core.Connection.ReferralHandlingMethod;
+import org.apache.directory.studio.connection.core.jobs.StudioProgressMonitor;
 import org.apache.directory.studio.ldapbrowser.core.BrowserCoreMessages;
 import org.apache.directory.studio.ldapbrowser.core.events.BulkModificationEvent;
 import org.apache.directory.studio.ldapbrowser.core.events.EventRegistry;
 import org.apache.directory.studio.ldapbrowser.core.model.ConnectionException;
+import org.apache.directory.studio.ldapbrowser.core.model.IAttribute;
 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.IValue;
+import org.apache.directory.studio.ldapbrowser.core.utils.ModelConverter;
 import org.apache.directory.studio.ldapbrowser.core.utils.Utils;
 import org.apache.directory.studio.ldifparser.LdifFormatParameters;
 import org.apache.directory.studio.ldifparser.model.LdifEnumeration;
+import org.apache.directory.studio.ldifparser.model.LdifPart;
 import org.apache.directory.studio.ldifparser.model.container.LdifChangeAddRecord;
 import org.apache.directory.studio.ldifparser.model.container.LdifChangeDeleteRecord;
 import org.apache.directory.studio.ldifparser.model.container.LdifChangeModDnRecord;
@@ -95,6 +100,9 @@
     /** The log file. */
     private File logFile;
 
+    /** The update if entry exists flag. */
+    private boolean updateIfEntryExists;
+
     /** The continue on error flag. */
     private boolean continueOnError;
 
@@ -105,14 +113,17 @@
      * @param browserConnection the browser connection
      * @param ldifFile the LDIF file
      * @param logFile the log file
+     * @param updateIfEntryExists the update if entry exists flag
      * @param continueOnError the continue on error flag
      */
-    public ImportLdifJob( IBrowserConnection browserConnection, File ldifFile, File logFile, boolean continueOnError )
+    public ImportLdifJob( IBrowserConnection browserConnection, File ldifFile, File logFile,
+        boolean updateIfEntryExists, boolean continueOnError )
     {
         this.browserConnection = browserConnection;
         this.ldifFile = ldifFile;
         this.logFile = logFile;
         this.continueOnError = continueOnError;
+        this.updateIfEntryExists = updateIfEntryExists;
 
         setName( BrowserCoreMessages.jobs__import_ldif_name );
     }
@@ -123,11 +134,13 @@
      * 
      * @param connection the connection
      * @param ldifFile the LDIF file
-     * @param continueOnError the continue on error
+     * @param updateIfEntryExists the update if entry exists flag
+     * @param continueOnError the continue on error flag
      */
-    public ImportLdifJob( IBrowserConnection connection, File ldifFile, boolean continueOnError )
+    public ImportLdifJob( IBrowserConnection connection, File ldifFile, boolean updateIfEntryExists,
+        boolean continueOnError )
     {
-        this( connection, ldifFile, null, continueOnError );
+        this( connection, ldifFile, null, updateIfEntryExists, continueOnError );
     }
 
 
@@ -192,7 +205,7 @@
                 };
             }
 
-            importLdif( browserConnection, enumeration, logWriter, continueOnError, monitor );
+            importLdif( browserConnection, enumeration, logWriter, updateIfEntryExists, continueOnError, monitor );
 
             logWriter.close();
             ldifReader.close();
@@ -228,11 +241,12 @@
      * @param browserConnection the browser connection
      * @param enumeration the LDIF enumeration
      * @param logWriter the log writer
+     * @param updateIfEntryExists the update if entry exists flag
      * @param continueOnError the continue on error flag
      * @param monitor the progress monitor
      */
     static void importLdif( IBrowserConnection browserConnection, LdifEnumeration enumeration, Writer logWriter,
-        boolean continueOnError, StudioProgressMonitor monitor )
+        boolean updateIfEntryExists, boolean continueOnError, StudioProgressMonitor monitor )
     {
         if ( browserConnection == null )
         {
@@ -254,7 +268,7 @@
                     try
                     {
                         dummyMonitor.reset();
-                        importLdifRecord( browserConnection, record, dummyMonitor );
+                        importLdifRecord( browserConnection, record, updateIfEntryExists, dummyMonitor );
                         if ( dummyMonitor.errorsReported() )
                         {
                             errorCount++;
@@ -316,6 +330,10 @@
                             }
                             else if ( record instanceof LdifChangeAddRecord || record instanceof LdifContentRecord )
                             {
+                                if ( entry != null )
+                                {
+                                    entry.setAttributesInitialized( false );
+                                }
                                 if ( parentEntry != null )
                                 {
                                     parentEntry.setChildrenInitialized( false );
@@ -371,12 +389,13 @@
      * 
      * @param browserConnection the browser connection
      * @param record the LDIF record
+     * @param updateIfEntryExists the update if entry exists flag
      * @param monitor the progress monitor
      * 
      * @throws ConnectionException the connection exception
      */
-    static void importLdifRecord( IBrowserConnection browserConnection, LdifRecord record, StudioProgressMonitor monitor )
-        throws ConnectionException
+    static void importLdifRecord( IBrowserConnection browserConnection, LdifRecord record, boolean updateIfEntryExists,
+        StudioProgressMonitor monitor ) throws ConnectionException
     {
         if ( !record.isValid() )
         {
@@ -385,38 +404,44 @@
 
         String dn = record.getDnLine().getValueAsString();
 
-        if ( record instanceof LdifContentRecord )
+        if ( record instanceof LdifContentRecord || record instanceof LdifChangeAddRecord )
         {
-            LdifContentRecord attrValRecord = ( LdifContentRecord ) record;
-            LdifAttrValLine[] attrVals = attrValRecord.getAttrVals();
-            Attributes jndiAttributes = new BasicAttributes();
-            for ( int ii = 0; ii < attrVals.length; ii++ )
+            LdifAttrValLine[] attrVals;
+            IEntry dummyEntry;
+            if ( record instanceof LdifContentRecord )
             {
-                String attributeName = attrVals[ii].getUnfoldedAttributeDescription();
-                Object realValue = attrVals[ii].getValueAsObject();
-
-                if ( jndiAttributes.get( attributeName ) != null )
+                LdifContentRecord attrValRecord = ( LdifContentRecord ) record;
+                attrVals = attrValRecord.getAttrVals();
+                try
                 {
-                    jndiAttributes.get( attributeName ).add( realValue );
+                    dummyEntry = ModelConverter.ldifContentRecordToEntry( attrValRecord, browserConnection );
                 }
-                else
+                catch ( InvalidNameException e )
                 {
-                    jndiAttributes.put( attributeName, realValue );
+                    monitor.reportError( e );
+                    return;
+                }
+            }
+            else
+            {
+                LdifChangeAddRecord changeAddRecord = ( LdifChangeAddRecord ) record;
+                attrVals = changeAddRecord.getAttrVals();
+                try
+                {
+                    dummyEntry = ModelConverter.ldifChangeAddRecordToEntry( changeAddRecord, browserConnection );
+                }
+                catch ( InvalidNameException e )
+                {
+                    monitor.reportError( e );
+                    return;
                 }
             }
 
-            browserConnection.getConnection().getJNDIConnectionWrapper().createEntry( dn, jndiAttributes,
-                ReferralHandlingMethod.IGNORE, getControls( attrValRecord ), monitor, null );
-        }
-        else if ( record instanceof LdifChangeAddRecord )
-        {
-            LdifChangeAddRecord changeAddRecord = ( LdifChangeAddRecord ) record;
-            LdifAttrValLine[] attrVals = changeAddRecord.getAttrVals();
             Attributes jndiAttributes = new BasicAttributes();
-            for ( int ii = 0; ii < attrVals.length; ii++ )
+            for ( LdifAttrValLine attrVal : attrVals )
             {
-                String attributeName = attrVals[ii].getUnfoldedAttributeDescription();
-                Object realValue = attrVals[ii].getValueAsObject();
+                String attributeName = attrVal.getUnfoldedAttributeDescription();
+                Object realValue = attrVal.getValueAsObject();
 
                 if ( jndiAttributes.get( attributeName ) != null )
                 {
@@ -429,7 +454,39 @@
             }
 
             browserConnection.getConnection().getJNDIConnectionWrapper().createEntry( dn, jndiAttributes,
-                ReferralHandlingMethod.IGNORE, getControls( changeAddRecord ), monitor, null );
+                ReferralHandlingMethod.IGNORE, getControls( record ), monitor, null );
+
+            if ( monitor.errorsReported() && updateIfEntryExists
+                && monitor.getException() instanceof NameAlreadyBoundException )
+            {
+                // creation failed with Error 68, now try to update the existing entry
+                monitor.reset();
+
+                List<ModificationItem> mis = new ArrayList<ModificationItem>();
+                for ( IAttribute attribute : dummyEntry.getAttributes() )
+                {
+                    Attribute jndiAttribute = new BasicAttribute( attribute.getDescription() );
+                    boolean isLdifPart = false;
+                    for ( IValue value : attribute.getValues() )
+                    {
+                        if ( value.getRawValue() instanceof LdifPart )
+                        {
+                            isLdifPart = true;
+                            break;
+                        }
+                        jndiAttribute.add( value.getRawValue() );
+                    }
+                    if ( !isLdifPart )
+                    {
+                        ModificationItem mi = new ModificationItem( DirContext.REPLACE_ATTRIBUTE, jndiAttribute );
+                        mis.add( mi );
+                    }
+                }
+
+                browserConnection.getConnection().getJNDIConnectionWrapper().modifyEntry( dn,
+                    mis.toArray( new ModificationItem[0] ), ReferralHandlingMethod.IGNORE, getControls( record ),
+                    monitor, null );
+            }
         }
         else if ( record instanceof LdifChangeDeleteRecord )
         {
@@ -520,7 +577,8 @@
             for ( int i = 0; i < controlLines.length; i++ )
             {
                 LdifControlLine line = controlLines[i];
-                controls[i] = new BasicControl( line.getUnfoldedOid(), line.isCritical(), line.getControlValueAsBinary() );
+                controls[i] = new BasicControl( line.getUnfoldedOid(), line.isCritical(), line
+                    .getControlValueAsBinary() );
             }
         }
         return controls;

Modified: directory/studio/trunk/ldapbrowser-ui/src/main/java/org/apache/directory/studio/ldapbrowser/ui/wizards/ImportLdifMainWizardPage.java
URL: http://svn.apache.org/viewvc/directory/studio/trunk/ldapbrowser-ui/src/main/java/org/apache/directory/studio/ldapbrowser/ui/wizards/ImportLdifMainWizardPage.java?rev=689285&r1=689284&r2=689285&view=diff
==============================================================================
--- directory/studio/trunk/ldapbrowser-ui/src/main/java/org/apache/directory/studio/ldapbrowser/ui/wizards/ImportLdifMainWizardPage.java (original)
+++ directory/studio/trunk/ldapbrowser-ui/src/main/java/org/apache/directory/studio/ldapbrowser/ui/wizards/ImportLdifMainWizardPage.java Tue Aug 26 16:23:08 2008
@@ -51,6 +51,10 @@
     public static final String CONTINUE_ON_ERROR_DIALOGSETTING_KEY = ImportLdifMainWizardPage.class.getName()
         + ".continueOnError";
 
+    /** The update if entry exists flag key */
+    public static final String UPDATE_IF_ENTRY_EXISTS_DIALOGSETTING_KEY = ImportLdifMainWizardPage.class.getName()
+        + ".updateIfEntryExists";
+
     /** The valid extension. */
     private static final String[] EXTENSIONS = new String[]
         { "*.ldif", "*.*" };
@@ -82,6 +86,9 @@
     /** The overwrite logfile button. */
     private Button overwriteLogfileButton;
 
+    /** The update if entry exists button. */
+    private Button updateIfEntryExistsButton;
+
     /** The continue on error button. */
     private Button continueOnErrorButton;
 
@@ -290,8 +297,33 @@
             }
         } );
 
-        // Continue
-        continueOnErrorButton = BaseWidgetUtils.createCheckbox( composite, "Continue on error", 3 );
+        // Options
+        Composite optionsOuterComposite = BaseWidgetUtils.createColumnContainer( composite, 1, 3 );
+        Group optionsGroup = BaseWidgetUtils.createGroup( optionsOuterComposite, "Options", 1 );
+        Composite optionsContainer = BaseWidgetUtils.createColumnContainer( optionsGroup, 3, 1 );
+
+        updateIfEntryExistsButton = BaseWidgetUtils.createCheckbox( optionsContainer, "Update if entry already exists",
+            3 );
+        updateIfEntryExistsButton
+            .setToolTipText( "This options applies for LDIF content records and LDIF add records. "
+                + "If enabled and the entry to add already exists it will be updated with the attributes defined in the LDIF." );
+        if ( BrowserUIPlugin.getDefault().getDialogSettings().get( UPDATE_IF_ENTRY_EXISTS_DIALOGSETTING_KEY ) == null )
+        {
+            BrowserUIPlugin.getDefault().getDialogSettings().put( UPDATE_IF_ENTRY_EXISTS_DIALOGSETTING_KEY, false );
+        }
+        updateIfEntryExistsButton.setSelection( BrowserUIPlugin.getDefault().getDialogSettings().getBoolean(
+            UPDATE_IF_ENTRY_EXISTS_DIALOGSETTING_KEY ) );
+        wizard.setUpdateIfEntryExists( updateIfEntryExistsButton.getSelection() );
+        updateIfEntryExistsButton.addSelectionListener( new SelectionAdapter()
+        {
+            public void widgetSelected( SelectionEvent event )
+            {
+                wizard.setUpdateIfEntryExists( updateIfEntryExistsButton.getSelection() );
+                validate();
+            }
+        } );
+
+        continueOnErrorButton = BaseWidgetUtils.createCheckbox( optionsContainer, "Continue on error", 3 );
         if ( BrowserUIPlugin.getDefault().getDialogSettings().get( CONTINUE_ON_ERROR_DIALOGSETTING_KEY ) == null )
         {
             BrowserUIPlugin.getDefault().getDialogSettings().put( CONTINUE_ON_ERROR_DIALOGSETTING_KEY, false );
@@ -309,7 +341,6 @@
         } );
 
         setControl( composite );
-        // nameText.setFocus();
     }
 
 
@@ -319,6 +350,8 @@
     public void saveDialogSettings()
     {
         ldifFileBrowserWidget.saveDialogSettings();
+        BrowserUIPlugin.getDefault().getDialogSettings().put( UPDATE_IF_ENTRY_EXISTS_DIALOGSETTING_KEY,
+            updateIfEntryExistsButton.getSelection() );
         BrowserUIPlugin.getDefault().getDialogSettings().put( CONTINUE_ON_ERROR_DIALOGSETTING_KEY,
             continueOnErrorButton.getSelection() );
     }

Modified: directory/studio/trunk/ldapbrowser-ui/src/main/java/org/apache/directory/studio/ldapbrowser/ui/wizards/ImportLdifWizard.java
URL: http://svn.apache.org/viewvc/directory/studio/trunk/ldapbrowser-ui/src/main/java/org/apache/directory/studio/ldapbrowser/ui/wizards/ImportLdifWizard.java?rev=689285&r1=689284&r2=689285&view=diff
==============================================================================
--- directory/studio/trunk/ldapbrowser-ui/src/main/java/org/apache/directory/studio/ldapbrowser/ui/wizards/ImportLdifWizard.java (original)
+++ directory/studio/trunk/ldapbrowser-ui/src/main/java/org/apache/directory/studio/ldapbrowser/ui/wizards/ImportLdifWizard.java Tue Aug 26 16:23:08 2008
@@ -63,6 +63,9 @@
     /** The log filename. */
     private String logFilename;
 
+    /** The update if entry exists flag. */
+    private boolean updateIfEntryExists;
+
     /** The continue on error flag. */
     private boolean continueOnError;
 
@@ -164,7 +167,6 @@
      */
     public boolean performFinish()
     {
-
         mainPage.saveDialogSettings();
 
         if ( ldifFilename != null && !"".equals( ldifFilename ) )
@@ -174,11 +176,12 @@
             if ( enableLogging )
             {
                 File logFile = new File( logFilename );
-                new ImportLdifJob( importConnection, ldifFile, logFile, continueOnError ).execute();
+                new ImportLdifJob( importConnection, ldifFile, logFile, updateIfEntryExists, continueOnError )
+                    .execute();
             }
             else
             {
-                new ImportLdifJob( importConnection, ldifFile, continueOnError ).execute();
+                new ImportLdifJob( importConnection, ldifFile, updateIfEntryExists, continueOnError ).execute();
             }
 
             return true;
@@ -221,6 +224,17 @@
 
 
     /**
+     * Sets the update if entry exists flag.
+     * 
+     * @param updateIfEntryExists the update if entry exists flag
+     */
+    public void setUpdateIfEntryExists( boolean updateIfEntryExists )
+    {
+        this.updateIfEntryExists = updateIfEntryExists;
+    }
+
+
+    /**
      * Sets the continue on error flag.
      * 
      * @param continueOnError the continue on error flag