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.view.editors.objectClass.ObjectClassEditor; 27 import org.apache.directory.server.core.tools.schema.ObjectClassLiteral; 28 import org.apache.directory.shared.ldap.schema.ObjectClassTypeEnum; 29 import org.apache.log4j.Logger; 30 import org.eclipse.core.runtime.NullProgressMonitor; 31 32 33 38 public class ObjectClass implements SchemaElement, Cloneable 39 { 40 private static Logger logger = Logger.getLogger( ObjectClass.class ); 41 private ObjectClassLiteral literal; 42 private Schema originatingSchema; 43 private ArrayList <SchemaElementListener> listeners; 44 private ObjectClassEditor editor; 45 46 47 52 public ObjectClass( ObjectClassLiteral literal, Schema originatingSchema ) 53 { 54 this.literal = literal; 55 this.originatingSchema = originatingSchema; 56 listeners = new ArrayList <SchemaElementListener>(); 57 } 58 59 60 63 public ObjectClassLiteral getLiteral() 64 { 65 return literal; 66 } 67 68 69 72 public Schema getOriginatingSchema() 73 { 74 return originatingSchema; 75 } 76 77 78 82 public ObjectClassEditor getEditor() 83 { 84 return editor; 85 } 86 87 88 92 public void setEditor( ObjectClassEditor editor ) 93 { 94 this.editor = editor; 95 } 96 97 98 102 public void removeEditor( ObjectClassEditor editor ) 103 { 104 if ( this.editor == editor ) 105 this.editor = null; 106 } 107 108 109 113 public void closeAssociatedEditor() 114 { 115 if ( editor != null ) 116 { 117 editor.setDirty( false ); 119 120 editor.close( false ); 121 removeEditor( editor ); 122 } 123 } 124 125 126 130 public boolean hasPendingModifications() 131 { 132 if ( this.editor != null ) 133 { 134 return editor.isDirty(); 135 } 136 137 return false; 138 } 139 140 141 144 public void applyPendingModifications() 145 { 146 if ( hasPendingModifications() ) 147 { 148 editor.doSave( new NullProgressMonitor() ); 149 } 150 } 151 152 153 public String [] getNames() 154 { 155 return literal.getNames(); 156 } 157 158 159 public String getOid() 160 { 161 return literal.getOid(); 162 } 163 164 165 public String getDescription() 166 { 167 return literal.getDescription(); 168 } 169 170 171 public String [] getSuperiors() 172 { 173 return literal.getSuperiors(); 174 } 175 176 177 public ObjectClassTypeEnum getClassType() 178 { 179 return literal.getClassType(); 180 } 181 182 183 public boolean isObsolete() 184 { 185 return literal.isObsolete(); 186 } 187 188 189 public String [] getMay() 190 { 191 return literal.getMay(); 192 } 193 194 195 public String [] getMust() 196 { 197 return literal.getMust(); 198 } 199 200 201 public void setNames( String [] names ) 202 { 203 literal.setNames( names ); 204 } 205 206 207 public void setOid( String oid ) 208 { 209 literal.setOid( oid ); 210 } 211 212 213 public void setDescription( String description ) 214 { 215 literal.setDescription( description ); 216 } 217 218 219 public void setClassType( ObjectClassTypeEnum type ) 220 { 221 literal.setClassType( type ); 222 } 223 224 225 public void setObsolete( boolean bool ) 226 { 227 literal.setObsolete( bool ); 228 } 229 230 231 public void setSuperiors( String [] superiors ) 232 { 233 literal.setSuperiors( superiors ); 234 } 235 236 237 public void setMay( String [] may ) 238 { 239 literal.setMay( may ); 240 } 241 242 243 public void setMust( String [] must ) 244 { 245 literal.setMust( must ); 246 } 247 248 249 public String write() 250 { 251 StringBuffer sb = new StringBuffer (); 252 253 sb.append( "objectclass ( " + literal.getOid() + " \n" ); 256 String [] names = literal.getNames(); 258 sb.append( "\tNAME " ); if ( names.length > 1 ) 260 { 261 sb.append( "( " ); for ( String name : names ) 263 { 264 sb.append( "'" + name + "' " ); } 266 sb.append( ") \n" ); } 268 else 269 { 270 sb.append( "'" + names[0] + "' \n" ); } 272 273 if ( ( literal.getDescription() != null ) && ( literal.getDescription().length() != 0 ) ) 275 { 276 sb.append( "\tDESC '" + literal.getDescription() + "' \n" ); } 278 279 if ( literal.isObsolete() ) 281 { 282 sb.append( "\tOBSOLETE \n" ); } 284 285 String [] superiors = literal.getSuperiors(); 287 if ( superiors.length != 0 ) 288 { 289 if ( superiors.length > 1 ) 290 { 291 sb.append( "\tSUP (" + superiors[0] ); for ( int i = 1; i < superiors.length; i++ ) 293 { 294 sb.append( " $ " + superiors[i] ); } 296 sb.append( ") \n" ); } 298 else 299 { 300 sb.append( "\tSUP " + superiors[0] + " \n" ); } 302 } 303 304 ObjectClassTypeEnum classtype = literal.getClassType(); 306 if ( classtype == ObjectClassTypeEnum.ABSTRACT ) 307 { 308 sb.append( "\tABSTRACT \n" ); } 310 else if ( classtype == ObjectClassTypeEnum.AUXILIARY ) 311 { 312 sb.append( "\tAUXILIARY \n" ); } 314 else if ( classtype == ObjectClassTypeEnum.STRUCTURAL ) 315 { 316 sb.append( "\tSTRUCTURAL \n" ); } 318 319 String [] must = literal.getMust(); 321 if ( must.length != 0 ) 322 { 323 sb.append( "\tMUST " ); if ( must.length > 1 ) 325 { 326 sb.append( "( " + must[0] + " " ); for ( int i = 1; i < must.length; i++ ) 328 { 329 sb.append( "$ " + must[i] + " " ); } 331 sb.append( ") \n" ); } 333 else if ( must.length == 1 ) 334 { 335 sb.append( must[0] + " \n" ); } 337 } 338 339 String [] may = literal.getMay(); 341 if ( may.length != 0 ) 342 { 343 sb.append( "\tMAY " ); if ( may.length > 1 ) 345 { 346 sb.append( "( " + may[0] + " " ); for ( int i = 1; i < may.length; i++ ) 348 { 349 sb.append( "$ " + may[i] + " " ); } 351 sb.append( ") \n" ); } 353 else if ( may.length == 1 ) 354 { 355 sb.append( may[0] + " \n" ); } 357 } 358 sb.append( " )\n" ); 361 return sb.toString(); 362 } 363 364 365 public String toString() 366 { 367 return getNames()[0]; 368 } 369 370 371 public void addListener( SchemaElementListener listener ) 372 { 373 if ( !listeners.contains( listener ) ) 374 listeners.add( listener ); 375 } 376 377 378 public void removeListener( SchemaElementListener listener ) 379 { 380 listeners.remove( listener ); 381 } 382 383 384 private void notifyChanged( ObjectClass oldObjectClass ) 385 { 386 for ( SchemaElementListener listener : listeners ) 387 { 388 try 389 { 390 listener.schemaElementChanged( this, new LDAPModelEvent( LDAPModelEvent.Reason.OCModified, 391 oldObjectClass, this ) ); 392 } 393 catch ( Exception e ) 394 { 395 logger.debug( "error when notifying " + listener + " of " + this.getNames()[0] + " modification" ); } 397 } 398 } 399 400 401 404 public Object clone() throws CloneNotSupportedException 405 { 406 ObjectClass oc = ( ObjectClass ) super.clone(); 407 408 oc.literal = new ObjectClassLiteral( literal.getOid() ); 409 oc.literal.setClassType( literal.getClassType() ); 410 oc.literal.setDescription( literal.getDescription() ); 411 oc.literal.setMay( literal.getMay() ); 412 oc.literal.setMust( literal.getMust() ); 413 oc.literal.setNames( literal.getNames() ); 414 oc.literal.setObsolete( literal.isObsolete() ); 415 oc.literal.setSuperiors( literal.getSuperiors() ); 416 417 return oc; 418 } 419 420 421 427 public void update( ObjectClass oc ) 428 { 429 try 430 { 431 ObjectClass oldObjectClass = ( ObjectClass ) clone(); 432 433 literal.setClassType( oc.getClassType() ); 434 literal.setDescription( oc.getDescription() ); 435 literal.setMay( oc.getMay() ); 436 literal.setMust( oc.getMust() ); 437 literal.setNames( oc.getNames() ); 438 literal.setObsolete( oc.isObsolete() ); 439 literal.setOid( oc.getOid() ); 440 literal.setSuperiors( oc.getSuperiors() ); 441 442 notifyChanged( oldObjectClass ); 443 } 444 catch ( CloneNotSupportedException e ) 445 { 446 } 448 } 449 } 450 | Popular Tags |