1 20 21 package org.apache.directory.ldapstudio.browser.core.jobs; 22 23 24 import java.util.HashSet ; 25 import java.util.Set ; 26 27 import org.apache.directory.ldapstudio.browser.core.BrowserCoreMessages; 28 import org.apache.directory.ldapstudio.browser.core.events.EventRegistry; 29 import org.apache.directory.ldapstudio.browser.core.events.ValueRenamedEvent; 30 import org.apache.directory.ldapstudio.browser.core.internal.model.Attribute; 31 import org.apache.directory.ldapstudio.browser.core.internal.model.Value; 32 import org.apache.directory.ldapstudio.browser.core.model.IAttribute; 33 import org.apache.directory.ldapstudio.browser.core.model.IEntry; 34 import org.apache.directory.ldapstudio.browser.core.model.IValue; 35 import org.apache.directory.ldapstudio.browser.core.model.ModelModificationException; 36 37 38 public class RenameValuesJob extends AbstractModificationJob 39 { 40 41 private IEntry entry; 42 43 private IValue[] oldValues; 44 45 private String newAttributeName; 46 47 private ValueRenamedEvent event; 48 49 50 public RenameValuesJob( IEntry entry, IValue[] oldValues, String newAttributeName ) 51 { 52 this.entry = entry; 53 this.oldValues = oldValues; 54 this.newAttributeName = newAttributeName; 55 56 setName( oldValues.length == 1 ? BrowserCoreMessages.jobs__rename_value_name_1 57 : BrowserCoreMessages.jobs__rename_value_name_n ); 58 } 59 60 61 protected void executeAsyncModificationJob( ExtendedProgressMonitor monitor ) throws ModelModificationException 62 { 63 64 monitor.beginTask( oldValues.length == 1 ? BrowserCoreMessages.jobs__rename_value_task_1 65 : BrowserCoreMessages.jobs__rename_value_task_n, 2 ); 66 monitor.reportProgress( " " ); monitor.worked( 1 ); 68 69 for ( int i = 0; i < oldValues.length; i++ ) 70 { 71 if ( oldValues[i].getAttribute().getEntry() != this.entry ) 72 { 73 return; 74 } 75 } 76 77 IValue[] newValues = new IValue[oldValues.length]; 78 for ( int i = 0; i < oldValues.length; i++ ) 79 { 80 81 IAttribute newAttribute = entry.getAttribute( newAttributeName ); 82 if ( newAttribute == null ) 83 { 84 newAttribute = new Attribute( entry, newAttributeName ); 85 entry.addAttribute( newAttribute ); 86 } 87 88 newValues[i] = new Value( newAttribute, oldValues[i].getRawValue() ); 89 newAttribute.addValue( newValues[i] ); 90 91 oldValues[i].getAttribute().deleteValue( oldValues[i] ); 92 93 if ( this.event == null ) 94 { 95 this.event = new ValueRenamedEvent( entry.getConnection(), entry, oldValues[0], newValues[0] ); 96 } 97 } 98 99 if ( !monitor.errorsReported() ) 100 { 101 entry.getConnection().create( newValues, monitor ); 102 } 103 if ( !monitor.errorsReported() ) 104 { 105 entry.getConnection().delete( oldValues, monitor ); 106 } 107 } 108 109 110 protected IEntry getModifiedEntry() 111 { 112 return this.entry; 113 } 114 115 116 protected String [] getAffectedAttributeNames() 117 { 118 Set affectedAttributeNameSet = new HashSet (); 119 affectedAttributeNameSet.add( newAttributeName ); 120 for ( int i = 0; i < oldValues.length; i++ ) 121 { 122 affectedAttributeNameSet.add( oldValues[i].getAttribute().getDescription() ); 123 } 124 return ( String [] ) affectedAttributeNameSet.toArray( new String [affectedAttributeNameSet.size()] ); 125 } 126 127 128 protected void runNotification() 129 { 130 if ( this.event != null ) 131 { 132 EventRegistry.fireEntryUpdated( this.event, this ); 133 } 134 } 135 136 137 protected String getErrorMessage() 138 { 139 return oldValues.length == 1 ? BrowserCoreMessages.jobs__rename_value_error_1 140 : BrowserCoreMessages.jobs__rename_value_error_n; 141 } 142 143 } 144 | Popular Tags |