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.ValueAddedEvent; 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 CreateValuesJob extends AbstractModificationJob 39 { 40 41 private IEntry entry; 42 43 private String [] attributeDescriptions; 44 45 private Object [] rawValues; 46 47 private ValueAddedEvent event; 48 49 50 public CreateValuesJob( IEntry entry, String [] attributeDescriptions, Object [] rawValues ) 51 { 52 this.entry = entry; 53 this.attributeDescriptions = attributeDescriptions; 54 this.rawValues = rawValues; 55 56 setName( rawValues.length == 1 ? BrowserCoreMessages.jobs__create_values_name_1 57 : BrowserCoreMessages.jobs__create_values_name_n ); 58 } 59 60 61 public CreateValuesJob( IAttribute attribute, Object newValue ) 62 { 63 this( attribute.getEntry(), new String [] 64 { attribute.getDescription() }, new Object [] 65 { newValue } ); 66 } 67 68 69 protected void executeAsyncModificationJob( ExtendedProgressMonitor monitor ) throws ModelModificationException 70 { 71 72 monitor.beginTask( rawValues.length == 1 ? BrowserCoreMessages.jobs__create_values_task_1 73 : BrowserCoreMessages.jobs__create_values_task_n, 2 ); 74 monitor.reportProgress( " " ); monitor.worked( 1 ); 76 77 IValue[] newValues = new IValue[rawValues.length]; 78 for ( int i = 0; i < newValues.length; i++ ) 79 { 80 IAttribute attribute = entry.getAttribute( attributeDescriptions[i] ); 81 if ( attribute == null ) 82 { 83 attribute = new Attribute( entry, attributeDescriptions[i] ); 91 entry.addAttribute( attribute ); 92 } 93 94 newValues[i] = new Value( attribute, rawValues[i] ); 95 attribute.addValue( newValues[i] ); 96 97 if ( this.event == null ) 98 { 99 event = new ValueAddedEvent( entry.getConnection(), entry, attribute, newValues[i] ); 100 } 101 } 102 103 entry.getConnection().create( newValues, monitor ); 104 } 105 106 107 protected IEntry getModifiedEntry() 108 { 109 return entry; 110 } 111 112 113 protected String [] getAffectedAttributeNames() 114 { 115 Set affectedAttributeNameSet = new HashSet (); 116 for ( int i = 0; i < attributeDescriptions.length; i++ ) 117 { 118 affectedAttributeNameSet.add( attributeDescriptions[i] ); 119 } 120 return ( String [] ) affectedAttributeNameSet.toArray( new String [affectedAttributeNameSet.size()] ); 121 } 122 123 124 protected void runNotification() 125 { 126 if ( this.event != null ) 127 { 128 EventRegistry.fireEntryUpdated( this.event, this ); 129 } 130 } 131 132 133 protected String getErrorMessage() 134 { 135 return attributeDescriptions.length == 1 ? BrowserCoreMessages.jobs__create_values_error_1 136 : BrowserCoreMessages.jobs__create_values_error_n; 137 } 138 139 } 140 | Popular Tags |