1 20 21 package org.apache.directory.ldapstudio.browser.core.internal.model; 22 23 24 import java.util.ArrayList ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 28 import org.apache.directory.ldapstudio.browser.core.BrowserCoreMessages; 29 import org.apache.directory.ldapstudio.browser.core.events.EmptyValueAddedEvent; 30 import org.apache.directory.ldapstudio.browser.core.events.EmptyValueDeletedEvent; 31 import org.apache.directory.ldapstudio.browser.core.events.EntryModificationEvent; 32 import org.apache.directory.ldapstudio.browser.core.events.EventRegistry; 33 import org.apache.directory.ldapstudio.browser.core.events.ValueAddedEvent; 34 import org.apache.directory.ldapstudio.browser.core.events.ValueDeletedEvent; 35 import org.apache.directory.ldapstudio.browser.core.events.ValueModifiedEvent; 36 import org.apache.directory.ldapstudio.browser.core.internal.search.LdapSearchPageScoreComputer; 37 import org.apache.directory.ldapstudio.browser.core.model.IAttribute; 38 import org.apache.directory.ldapstudio.browser.core.model.IConnection; 39 import org.apache.directory.ldapstudio.browser.core.model.IEntry; 40 import org.apache.directory.ldapstudio.browser.core.model.IValue; 41 import org.apache.directory.ldapstudio.browser.core.model.ModelModificationException; 42 import org.apache.directory.ldapstudio.browser.core.model.schema.AttributeTypeDescription; 43 import org.apache.directory.ldapstudio.browser.core.model.schema.SchemaUtils; 44 import org.eclipse.search.ui.ISearchPageScoreComputer; 45 46 47 53 public class Attribute implements IAttribute 54 { 55 56 57 private static final long serialVersionUID = -5679384884002589786L; 58 59 60 private AttributeDescription attributeDescription; 61 62 63 private IEntry entry; 64 65 66 private List <IValue> valueList; 67 68 69 80 public Attribute( IEntry entry, String description ) throws ModelModificationException 81 { 82 if ( entry == null ) 83 { 84 throw new ModelModificationException( BrowserCoreMessages.model__empty_entry ); 85 } 86 if ( description == null ) 87 { 88 throw new ModelModificationException( BrowserCoreMessages.model__empty_attribute ); 89 } 90 91 this.entry = entry; 92 this.attributeDescription = new AttributeDescription( description ); 93 this.valueList = new ArrayList <IValue>(); 94 95 } 96 97 98 101 public IEntry getEntry() 102 { 103 return entry; 104 } 105 106 107 110 public boolean isConsistent() 111 { 112 if ( valueList.isEmpty() ) 113 { 114 return false; 115 } 116 117 for ( Iterator it = valueList.iterator(); it.hasNext(); ) 118 { 119 IValue value = ( IValue ) it.next(); 120 if ( value.isEmpty() ) 121 { 122 return false; 123 } 124 } 125 126 return true; 127 } 128 129 130 133 public boolean isMustAttribute() 134 { 135 if ( isObjectClassAttribute() ) 136 { 137 return true; 138 } 139 else 140 { 141 String [] mustAttributeNames = getEntry().getSubschema().getMustAttributeNames(); 142 for ( int i = 0; i < mustAttributeNames.length; i++ ) 143 { 144 String must = mustAttributeNames[i]; 145 if ( must.equalsIgnoreCase( getType() ) ) 146 { 147 return true; 148 } 149 } 150 return false; 151 } 152 } 153 154 155 158 public boolean isMayAttribute() 159 { 160 return !isObjectClassAttribute() && !isMustAttribute() && !isOperationalAttribute(); 161 } 162 163 164 167 public boolean isOperationalAttribute() 168 { 169 return getAttributeTypeDescription() == null || SchemaUtils.isOperational( getAttributeTypeDescription() ); 170 } 171 172 173 176 public boolean isObjectClassAttribute() 177 { 178 return OBJECTCLASS_ATTRIBUTE.equalsIgnoreCase( getDescription() ); 179 } 180 181 182 185 public boolean isString() 186 { 187 return !isBinary(); 188 } 189 190 191 194 public boolean isBinary() 195 { 196 return getAttributeTypeDescription().isBinary(); 197 } 198 199 200 203 public void addEmptyValue() 204 { 205 try 206 { 207 IValue emptyValue = new Value( this ); 208 valueList.add( emptyValue ); 209 this.attributeModified( new EmptyValueAddedEvent( getEntry().getConnection(), getEntry(), this, emptyValue ) ); 210 } 211 catch ( ModelModificationException mme ) 212 { 213 } 215 } 216 217 218 221 public void deleteEmptyValue() 222 { 223 for ( Iterator it = this.valueList.iterator(); it.hasNext(); ) 224 { 225 IValue value = ( IValue ) it.next(); 226 if ( value.isEmpty() ) 227 { 228 it.remove(); 229 attributeModified( new EmptyValueDeletedEvent( getEntry().getConnection(), getEntry(), this, value ) ); 230 return; 231 } 232 } 233 } 234 235 236 241 private void attributeModified( EntryModificationEvent event ) 242 { 243 EventRegistry.fireEntryUpdated( event, getEntry() ); 244 } 245 246 247 253 private void checkValue( IValue value ) throws ModelModificationException 254 { 255 if ( value == null ) 256 { 257 throw new ModelModificationException( BrowserCoreMessages.model__empty_value ); 258 } 259 if ( !value.getAttribute().equals( this ) ) 260 { 261 throw new ModelModificationException( BrowserCoreMessages.model__values_attribute_is_not_myself ); 262 } 263 } 264 265 266 272 private boolean internalDeleteValue( IValue valueToDelete ) 273 { 274 for ( Iterator it = valueList.iterator(); it.hasNext(); ) 275 { 276 IValue value = ( IValue ) it.next(); 277 if ( value.equals( valueToDelete ) ) 278 { 279 it.remove(); 280 return true; 281 } 282 } 283 return false; 284 } 285 286 287 290 public void addValue( IValue valueToAdd ) throws ModelModificationException 291 { 292 this.checkValue( valueToAdd ); 293 294 valueList.add( valueToAdd ); 295 attributeModified( new ValueAddedEvent( getEntry().getConnection(), getEntry(), this, valueToAdd ) ); 296 } 297 298 299 302 public void deleteValue( IValue valueToDelete ) throws ModelModificationException 303 { 304 this.checkValue( valueToDelete ); 305 306 if ( this.internalDeleteValue( valueToDelete ) ) 307 { 308 this.attributeModified( new ValueDeletedEvent( getEntry().getConnection(), getEntry(), this, valueToDelete ) ); 309 } 310 } 311 312 313 316 public void modifyValue( IValue oldValue, IValue newValue ) throws ModelModificationException 317 { 318 this.checkValue( oldValue ); 319 this.checkValue( newValue ); 320 321 this.internalDeleteValue( oldValue ); 322 this.valueList.add( newValue ); 323 this.attributeModified( new ValueModifiedEvent( getEntry().getConnection(), getEntry(), this, oldValue, 324 newValue ) ); 325 } 326 327 328 331 public IValue[] getValues() 332 { 333 return ( IValue[] ) valueList.toArray( new IValue[0] ); 334 } 335 336 337 340 public int getValueSize() 341 { 342 return this.valueList.size(); 343 } 344 345 346 349 public String getDescription() 350 { 351 return getAttributeDescription().getDescription(); 352 } 353 354 355 358 public String getType() 359 { 360 return getAttributeDescription().getParsedAttributeType(); 361 } 362 363 364 367 public String toString() 368 { 369 return getDescription(); 370 } 371 372 373 376 public boolean equals( Object o ) 377 { 378 if ( o == null || !( o instanceof IAttribute ) ) 380 { 381 return false; 382 } 383 IAttribute a = ( IAttribute ) o; 384 385 if ( !getEntry().equals( a.getEntry() ) ) 387 { 388 return false; 389 } 390 391 return getDescription().equals( a.getDescription() ); 393 } 394 395 396 399 public int hashCode() 400 { 401 return getDescription().hashCode(); 402 } 403 404 405 408 public byte[][] getBinaryValues() 409 { 410 List <byte[]> binaryValueList = new ArrayList <byte[]>(); 411 412 IValue[] values = getValues(); 413 for ( int i = 0; i < values.length; i++ ) 414 { 415 binaryValueList.add( values[i].getBinaryValue() ); 416 } 417 418 return binaryValueList.toArray( new byte[0][] ); 419 } 420 421 422 425 public String getStringValue() 426 { 427 if ( getValueSize() > 0 ) 428 { 429 return ( ( IValue ) valueList.get( 0 ) ).getStringValue(); 430 } 431 else 432 { 433 return null; 434 } 435 } 436 437 438 441 public String [] getStringValues() 442 { 443 List <String > stringValueList = new ArrayList <String >(); 444 445 IValue[] values = getValues(); 446 for ( int i = 0; i < values.length; i++ ) 447 { 448 stringValueList.add( values[i].getStringValue() ); 449 } 450 451 return stringValueList.toArray( new String [stringValueList.size()] ); 452 } 453 454 455 458 public AttributeTypeDescription getAttributeTypeDescription() 459 { 460 return getEntry().getConnection().getSchema().getAttributeTypeDescription( getType() ); 461 } 462 463 464 467 public Object getAdapter( Class adapter ) 468 { 469 470 Class <?> clazz = ( Class <?> ) adapter; 471 if ( clazz.isAssignableFrom( ISearchPageScoreComputer.class ) ) 472 { 473 return new LdapSearchPageScoreComputer(); 474 } 475 if ( clazz.isAssignableFrom( IConnection.class ) ) 476 { 477 return getEntry().getConnection(); 478 } 479 if ( clazz.isAssignableFrom( IEntry.class ) ) 480 { 481 return getEntry(); 482 } 483 if ( clazz.isAssignableFrom( IAttribute.class ) ) 484 { 485 return this; 486 } 487 488 return null; 489 } 490 491 492 495 public AttributeDescription getAttributeDescription() 496 { 497 return attributeDescription; 498 } 499 500 } 501 | Popular Tags |