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.AttributeDeletedEvent; 29 import org.apache.directory.ldapstudio.browser.core.events.EntryModificationEvent; 30 import org.apache.directory.ldapstudio.browser.core.events.EventRegistry; 31 import org.apache.directory.ldapstudio.browser.core.events.ValueDeletedEvent; 32 import org.apache.directory.ldapstudio.browser.core.model.AttributeHierarchy; 33 import org.apache.directory.ldapstudio.browser.core.model.IAttribute; 34 import org.apache.directory.ldapstudio.browser.core.model.IEntry; 35 import org.apache.directory.ldapstudio.browser.core.model.IValue; 36 import org.apache.directory.ldapstudio.browser.core.model.ModelModificationException; 37 38 39 public class DeleteAttributesValueJob extends AbstractModificationJob 40 { 41 42 private IEntry entry; 43 44 private IAttribute[] attributes; 45 46 private IValue[] values; 47 48 private EntryModificationEvent event; 49 50 51 public DeleteAttributesValueJob( IAttribute attributes[], IValue[] values ) 52 { 53 this.attributes = attributes; 54 this.values = values; 55 for ( int i = 0; attributes != null && i < attributes.length; i++ ) 56 { 57 if ( this.entry == null ) 58 { 59 this.entry = attributes[i].getEntry(); 60 } 61 } 62 for ( int i = 0; values != null && i < values.length; i++ ) 63 { 64 if ( this.entry == null ) 65 { 66 this.entry = values[i].getAttribute().getEntry(); 67 } 68 } 69 70 setName( attributes.length + values.length == 1 ? BrowserCoreMessages.jobs__delete_attributes_name_1 71 : BrowserCoreMessages.jobs__delete_attributes_name_n ); 72 } 73 74 75 public DeleteAttributesValueJob( AttributeHierarchy ah ) 76 { 77 this( ah.getAttributes(), new IValue[0] ); 78 } 79 80 81 public DeleteAttributesValueJob( IValue value ) 82 { 83 this( new IAttribute[0], new IValue[] 84 { value } ); 85 } 86 87 88 protected void executeAsyncModificationJob( ExtendedProgressMonitor monitor ) throws ModelModificationException 89 { 90 91 monitor.beginTask( attributes.length + values.length == 1 ? BrowserCoreMessages.jobs__delete_attributes_task_1 92 : BrowserCoreMessages.jobs__delete_attributes_task_n, 2 ); 93 monitor.reportProgress( " " ); monitor.worked( 1 ); 95 96 for ( int i = 0; attributes != null && i < attributes.length; i++ ) 97 { 98 attributes[i].getEntry().deleteAttribute( attributes[i] ); 99 } 100 for ( int i = 0; values != null && i < values.length; i++ ) 101 { 102 values[i].getAttribute().deleteValue( values[i] ); 103 } 104 105 entry.getConnection().delete( attributes, monitor ); 106 entry.getConnection().delete( values, monitor ); 107 108 if ( values.length > 0 ) 109 { 110 this.event = new ValueDeletedEvent( entry.getConnection(), entry, values[0].getAttribute(), values[0] ); 111 } 112 else if ( attributes.length > 0 ) 113 { 114 this.event = new AttributeDeletedEvent( entry.getConnection(), entry, attributes[0] ); 115 } 116 } 117 118 119 protected IEntry getModifiedEntry() 120 { 121 return entry; 122 } 123 124 125 protected String [] getAffectedAttributeNames() 126 { 127 Set affectedAttributeNameSet = new HashSet (); 128 for ( int i = 0; i < attributes.length; i++ ) 129 { 130 affectedAttributeNameSet.add( attributes[i].getDescription() ); 131 } 132 for ( int i = 0; i < values.length; i++ ) 133 { 134 affectedAttributeNameSet.add( values[i].getAttribute().getDescription() ); 135 } 136 return ( String [] ) affectedAttributeNameSet.toArray( new String [affectedAttributeNameSet.size()] ); 137 } 138 139 140 protected void runNotification() 141 { 142 if ( this.event != null ) 143 { 144 EventRegistry.fireEntryUpdated( this.event, this ); 145 } 146 } 147 148 149 protected String getErrorMessage() 150 { 151 return attributes.length + values.length == 1 ? BrowserCoreMessages.jobs__delete_attributes_error_1 152 : BrowserCoreMessages.jobs__delete_attributes_error_n; 153 } 154 155 } 156 | Popular Tags |