1 20 21 package org.apache.directory.ldapstudio.schemas.model; 22 23 24 import java.util.ArrayList ; 25 26 import org.apache.directory.ldapstudio.schemas.controller.SchemasViewController; 27 import org.apache.directory.ldapstudio.schemas.view.editors.attributeType.AttributeTypeEditor; 28 import org.apache.directory.server.core.tools.schema.AttributeTypeLiteral; 29 import org.apache.directory.shared.ldap.schema.UsageEnum; 30 import org.apache.log4j.Logger; 31 import org.eclipse.core.runtime.NullProgressMonitor; 32 33 34 41 @SuppressWarnings ("unused")public class AttributeType implements SchemaElement, Cloneable  43 { 44 private static Logger logger = Logger.getLogger( AttributeType.class ); 45 private AttributeTypeLiteral literal; 46 private Schema originatingSchema; 47 private ArrayList <SchemaElementListener> listeners; 48 private AttributeTypeEditor editor; 49 50 51 56 public AttributeType( AttributeTypeLiteral literal, Schema originatingSchema ) 57 { 58 this.literal = literal; 59 this.originatingSchema = originatingSchema; 60 listeners = new ArrayList <SchemaElementListener>(); 61 } 62 63 64 67 public AttributeTypeLiteral getLiteral() 68 { 69 return literal; 70 } 71 72 73 76 public Schema getOriginatingSchema() 77 { 78 return originatingSchema; 79 } 80 81 82 86 public AttributeTypeEditor getEditor() 87 { 88 return editor; 89 } 90 91 92 96 public void setEditor( AttributeTypeEditor editor ) 97 { 98 this.editor = editor; 99 } 100 101 102 106 public void removeEditor( AttributeTypeEditor editor ) 107 { 108 if ( this.editor == editor ) 109 this.editor = null; 110 } 111 112 113 117 public void closeAssociatedEditor() 118 { 119 if ( editor != null ) 120 { 121 editor.setDirty( false ); 123 124 editor.close( false ); 125 removeEditor( editor ); 126 } 127 } 128 129 130 134 public boolean hasPendingModifications() 135 { 136 if ( this.editor != null ) 137 { 138 return editor.isDirty(); 139 } 140 141 return false; 142 } 143 144 145 148 public void applyPendingModifications() 149 { 150 if ( hasPendingModifications() ) 151 { 152 editor.doSave( new NullProgressMonitor() ); 153 } 154 } 155 156 157 public String getDescription() 158 { 159 return literal.getDescription(); 160 } 161 162 163 public String getEquality() 164 { 165 return literal.getEquality(); 166 } 167 168 169 public int getLength() 170 { 171 return literal.getLength(); 172 } 173 174 175 public String [] getNames() 176 { 177 return literal.getNames(); 178 } 179 180 181 public String getOid() 182 { 183 return literal.getOid(); 184 } 185 186 187 public String getOrdering() 188 { 189 return literal.getOrdering(); 190 } 191 192 193 public String getSubstr() 194 { 195 return literal.getSubstr(); 196 } 197 198 199 public String getSuperior() 200 { 201 return literal.getSuperior(); 202 } 203 204 205 public String getSyntax() 206 { 207 return literal.getSyntax(); 208 } 209 210 211 public UsageEnum getUsage() 212 { 213 return literal.getUsage(); 214 } 215 216 217 public void setDescription( String description ) 218 { 219 literal.setDescription( description ); 220 } 221 222 223 public void setEquality( String equality ) 224 { 225 literal.setEquality( equality ); 226 } 227 228 229 public void setLength( int length ) 230 { 231 literal.setLength( length ); 232 } 233 234 235 public void setNames( String [] names ) 236 { 237 literal.setNames( names ); 238 } 239 240 241 public void setOrdering( String ordering ) 242 { 243 literal.setOrdering( ordering ); 244 } 245 246 247 public void setSubstr( String substr ) 248 { 249 literal.setSubstr( substr ); 250 } 251 252 253 public void setSuperior( String superior ) 254 { 255 literal.setSuperior( superior ); 256 } 257 258 259 public void setSyntax( String syntax ) 260 { 261 literal.setSyntax( syntax ); 262 } 263 264 265 public void setUsage( UsageEnum usage ) 266 { 267 literal.setUsage( usage ); 268 } 269 270 271 public void setOid( String oid ) 272 { 273 literal.setOid( oid ); 274 } 275 276 277 public void setObsolete( boolean bool ) 278 { 279 literal.setObsolete( bool ); 280 } 281 282 283 public void setSingleValue( boolean bool ) 284 { 285 literal.setSingleValue( bool ); 286 } 287 288 289 public void setCollective( boolean bool ) 290 { 291 literal.setCollective( bool ); 292 } 293 294 295 public void setNoUserModification( boolean bool ) 296 { 297 literal.setNoUserModification( bool ); 298 } 299 300 301 public boolean isObsolete() 302 { 303 return literal.isObsolete(); 304 } 305 306 307 public boolean isCollective() 308 { 309 return literal.isCollective(); 310 } 311 312 313 public boolean isNoUserModification() 314 { 315 return literal.isNoUserModification(); 316 } 317 318 319 public boolean isSingleValue() 320 { 321 return literal.isSingleValue(); 322 } 323 324 325 public String write() 326 { 327 StringBuffer sb = new StringBuffer (); 328 329 sb.append( "attributetype ( " + literal.getOid() + " \n" ); 332 String [] names = literal.getNames(); 334 sb.append( "\tNAME " ); if ( names.length > 1 ) 336 { 337 sb.append( "( " ); for ( String name : names ) 339 { 340 sb.append( "'" + name + "' " ); } 342 sb.append( ") \n" ); } 344 else 345 { 346 sb.append( "'" + names[0] + "' \n" ); } 348 349 if ( ( literal.getDescription() != null ) && ( literal.getDescription().length() != 0 ) ) 351 { 352 sb.append( "\tDESC '" + literal.getDescription() + "' \n" ); } 354 355 if ( literal.isObsolete() ) 357 { 358 sb.append( "\tOBSOLETE \n" ); } 360 361 if ( ( literal.getSuperior() != null ) && ( literal.getSuperior().length() != 0 ) ) 363 { 364 sb.append( "\tSUP " + literal.getSuperior() + " \n" ); } 366 367 if ( ( literal.getEquality() != null ) && ( literal.getEquality().length() != 0 ) ) 369 { 370 sb.append( "\tEQUALITY " + literal.getEquality() + " \n" ); } 372 373 if ( ( literal.getOrdering() != null ) && ( literal.getOrdering().length() != 0 ) ) 375 { 376 sb.append( "\tORDERING " + literal.getOrdering() + " \n" ); } 378 379 if ( ( literal.getSubstr() != null ) && ( literal.getSubstr().length() != 0 ) ) 381 { 382 sb.append( "\tSUBSTR " + literal.getSubstr() + " \n" ); } 384 385 if ( ( literal.getSyntax() != null ) && ( literal.getSyntax().length() != 0 ) ) 387 { 388 sb.append( "\tSYNTAX " + literal.getSyntax() ); if ( literal.getLength() > 0 ) 390 { 391 sb.append( "{" + literal.getLength() + "}" ); } 393 sb.append( " \n" ); } 395 396 if ( literal.isSingleValue() ) 398 { 399 sb.append( "\tSINGLE-VALUE \n" ); } 401 402 if ( literal.isCollective() ) 404 { 405 sb.append( "\tCOLLECTIVE \n" ); } 407 408 if ( literal.isNoUserModification() ) 410 { 411 sb.append( "\tNO-USER-MODIFICATION \n" ); } 413 414 UsageEnum usage = literal.getUsage(); 416 if ( usage != null ) 417 { 418 if ( usage == UsageEnum.DIRECTORY_OPERATION ) 419 { 420 sb.append( "\tUSAGE directoryOperation \n" ); } 422 else if ( usage == UsageEnum.DISTRIBUTED_OPERATION ) 423 { 424 sb.append( "\tUSAGE distributedOperation \n" ); } 426 else if ( usage == UsageEnum.DSA_OPERATION ) 427 { 428 sb.append( "\tUSAGE dSAOperation \n" ); } 430 else if ( usage == UsageEnum.USER_APPLICATIONS ) 431 { 432 } 434 } 435 436 sb.append( " )\n" ); 439 return sb.toString(); 440 } 441 442 443 public String toString() 444 { 445 return getNames()[0]; 446 } 447 448 449 public void addListener( SchemaElementListener listener ) 450 { 451 if ( !listeners.contains( listener ) ) 452 listeners.add( listener ); 453 } 454 455 456 public void removeListener( SchemaElementListener listener ) 457 { 458 listeners.remove( listener ); 459 } 460 461 462 private void notifyChanged( AttributeType oldAttributeType ) 463 { 464 for ( SchemaElementListener listener : listeners ) 465 { 466 try 467 { 468 listener.schemaElementChanged( this, new LDAPModelEvent( LDAPModelEvent.Reason.ATModified, 469 oldAttributeType, this ) ); 470 } 471 catch ( Exception e ) 472 { 473 logger.debug( "error when notifying listener: " + listener ); } 475 } 476 } 477 478 479 482 public Object clone() throws CloneNotSupportedException  483 { 484 AttributeType at = ( AttributeType ) super.clone(); 485 486 at.literal = new AttributeTypeLiteral( literal.getOid() ); 487 at.literal.setCollective( literal.isCollective() ); 488 at.literal.setDescription( literal.getDescription() ); 489 at.literal.setEquality( literal.getEquality() ); 490 at.literal.setLength( literal.getLength() ); 491 at.literal.setNames( literal.getNames() ); 492 at.literal.setNoUserModification( literal.isNoUserModification() ); 493 at.literal.setObsolete( literal.isObsolete() ); 494 at.literal.setOrdering( literal.getOrdering() ); 495 at.literal.setSingleValue( literal.isSingleValue() ); 496 at.literal.setSubstr( literal.getSubstr() ); 497 at.literal.setSuperior( literal.getSuperior() ); 498 at.literal.setSyntax( literal.getSyntax() ); 499 at.literal.setUsage( literal.getUsage() ); 500 501 return at; 502 } 503 504 505 511 public void update( AttributeType at ) 512 { 513 try 514 { 515 AttributeType oldAttributeType = ( AttributeType ) clone(); 516 517 literal.setCollective( at.isCollective() ); 518 literal.setDescription( at.getDescription() ); 519 literal.setEquality( at.getEquality() ); 520 literal.setLength( at.getLength() ); 521 literal.setNames( at.getNames() ); 522 literal.setNoUserModification( at.isNoUserModification() ); 523 literal.setObsolete( at.isObsolete() ); 524 literal.setOid( at.getOid() ); 525 literal.setOrdering( at.getOrdering() ); 526 literal.setSingleValue( at.isSingleValue() ); 527 literal.setSubstr( at.getSubstr() ); 528 literal.setSuperior( at.getSuperior() ); 529 literal.setSyntax( at.getSyntax() ); 530 literal.setUsage( at.getUsage() ); 531 532 notifyChanged( oldAttributeType ); 533 } 534 catch ( CloneNotSupportedException e ) 535 { 536 } 538 } 539 } 540 | Popular Tags |