KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > schemas > model > AttributeType


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  *
19  */

20
21 package org.apache.directory.ldapstudio.schemas.model;
22
23
24 import java.util.ArrayList JavaDoc;
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 /**
35  * This class is the model for 'attribute type' LDAP schema
36  * elements. It is modeled after the RFC 2252 recommandation but
37  * not all of the properties are implemented in the ApacheDS
38  * Directory Server.
39  *
40  */

41 @SuppressWarnings JavaDoc("unused")//$NON-NLS-1$
42
public class AttributeType implements SchemaElement, Cloneable JavaDoc
43 {
44     private static Logger logger = Logger.getLogger( AttributeType.class );
45     private AttributeTypeLiteral literal;
46     private Schema originatingSchema;
47     private ArrayList JavaDoc<SchemaElementListener> listeners;
48     private AttributeTypeEditor editor;
49
50
51     /**
52      * Default constructor
53      * @param literal the literal given by the schema parser that represents this element
54      * @param originatingSchema the schema file that defines this element
55      */

56     public AttributeType( AttributeTypeLiteral literal, Schema originatingSchema )
57     {
58         this.literal = literal;
59         this.originatingSchema = originatingSchema;
60         listeners = new ArrayList JavaDoc<SchemaElementListener>();
61     }
62
63
64     /**
65      * @return the openldap parser literal associated with this attributeType
66      */

67     public AttributeTypeLiteral getLiteral()
68     {
69         return literal;
70     }
71
72
73     /* (non-Javadoc)
74      * @see org.apache.directory.ldapstudio.schemas.model.SchemaElement#getOriginatingSchema()
75      */

76     public Schema getOriginatingSchema()
77     {
78         return originatingSchema;
79     }
80
81
82     /**
83      * Returns the editor associated to this attributeType
84      * @return the editor
85      */

86     public AttributeTypeEditor getEditor()
87     {
88         return editor;
89     }
90
91
92     /**
93      * Sets the editor associated to this attributeType
94      * @param editor the associated editor
95      */

96     public void setEditor( AttributeTypeEditor editor )
97     {
98         this.editor = editor;
99     }
100
101
102     /**
103      * Call this method to remove the attributeType<->Editor association
104      * @param editor the associated editor
105      */

106     public void removeEditor( AttributeTypeEditor editor )
107     {
108         if ( this.editor == editor )
109             this.editor = null;
110     }
111
112
113     /**
114      * Close the editor associated to this attributeType WITHOUT applying the
115      * modifications
116      */

117     public void closeAssociatedEditor()
118     {
119         if ( editor != null )
120         {
121             // This is a trick, so you don't get asked a second time to close and save or not the editor that has already been closed.
122
editor.setDirty( false );
123
124             editor.close( false );
125             removeEditor( editor );
126         }
127     }
128
129
130     /**
131      * Checks if the attributeType has pending modifications in its editor
132      * @return
133      */

134     public boolean hasPendingModifications()
135     {
136         if ( this.editor != null )
137         {
138             return editor.isDirty();
139         }
140
141         return false;
142     }
143
144
145     /**
146      * Apply the pending modifications to the model (this instance)
147      */

148     public void applyPendingModifications()
149     {
150         if ( hasPendingModifications() )
151         {
152             editor.doSave( new NullProgressMonitor() );
153         }
154     }
155
156
157     public String JavaDoc getDescription()
158     {
159         return literal.getDescription();
160     }
161
162
163     public String JavaDoc getEquality()
164     {
165         return literal.getEquality();
166     }
167
168
169     public int getLength()
170     {
171         return literal.getLength();
172     }
173
174
175     public String JavaDoc[] getNames()
176     {
177         return literal.getNames();
178     }
179
180
181     public String JavaDoc getOid()
182     {
183         return literal.getOid();
184     }
185
186
187     public String JavaDoc getOrdering()
188     {
189         return literal.getOrdering();
190     }
191
192
193     public String JavaDoc getSubstr()
194     {
195         return literal.getSubstr();
196     }
197
198
199     public String JavaDoc getSuperior()
200     {
201         return literal.getSuperior();
202     }
203
204
205     public String JavaDoc 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 JavaDoc description )
218     {
219         literal.setDescription( description );
220     }
221
222
223     public void setEquality( String JavaDoc 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 JavaDoc[] names )
236     {
237         literal.setNames( names );
238     }
239
240
241     public void setOrdering( String JavaDoc ordering )
242     {
243         literal.setOrdering( ordering );
244     }
245
246
247     public void setSubstr( String JavaDoc substr )
248     {
249         literal.setSubstr( substr );
250     }
251
252
253     public void setSuperior( String JavaDoc superior )
254     {
255         literal.setSuperior( superior );
256     }
257
258
259     public void setSyntax( String JavaDoc 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 JavaDoc 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 JavaDoc write()
326     {
327         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
328
329         // Open the definition and OID
330
sb.append( "attributetype ( " + literal.getOid() + " \n" ); //$NON-NLS-1$ //$NON-NLS-2$
331

332         // NAME(S)
333
String JavaDoc[] names = literal.getNames();
334         sb.append( "\tNAME " ); //$NON-NLS-1$
335
if ( names.length > 1 )
336         {
337             sb.append( "( " ); //$NON-NLS-1$
338
for ( String JavaDoc name : names )
339             {
340                 sb.append( "'" + name + "' " ); //$NON-NLS-1$ //$NON-NLS-2$
341
}
342             sb.append( ") \n" ); //$NON-NLS-1$
343
}
344         else
345         {
346             sb.append( "'" + names[0] + "' \n" ); //$NON-NLS-1$ //$NON-NLS-2$
347
}
348
349         // DESC
350
if ( ( literal.getDescription() != null ) && ( literal.getDescription().length() != 0 ) )
351         {
352             sb.append( "\tDESC '" + literal.getDescription() + "' \n" ); //$NON-NLS-1$ //$NON-NLS-2$
353
}
354
355         // OBSOLETE
356
if ( literal.isObsolete() )
357         {
358             sb.append( "\tOBSOLETE \n" ); //$NON-NLS-1$
359
}
360
361         // SUP
362
if ( ( literal.getSuperior() != null ) && ( literal.getSuperior().length() != 0 ) )
363         {
364             sb.append( "\tSUP " + literal.getSuperior() + " \n" ); //$NON-NLS-1$ //$NON-NLS-2$
365
}
366
367         // EQUALITY
368
if ( ( literal.getEquality() != null ) && ( literal.getEquality().length() != 0 ) )
369         {
370             sb.append( "\tEQUALITY " + literal.getEquality() + " \n" ); //$NON-NLS-1$ //$NON-NLS-2$
371
}
372
373         // ORDERING
374
if ( ( literal.getOrdering() != null ) && ( literal.getOrdering().length() != 0 ) )
375         {
376             sb.append( "\tORDERING " + literal.getOrdering() + " \n" ); //$NON-NLS-1$ //$NON-NLS-2$
377
}
378
379         // SUBSTR
380
if ( ( literal.getSubstr() != null ) && ( literal.getSubstr().length() != 0 ) )
381         {
382             sb.append( "\tSUBSTR " + literal.getSubstr() + " \n" ); //$NON-NLS-1$ //$NON-NLS-2$
383
}
384
385         // SYNTAX
386
if ( ( literal.getSyntax() != null ) && ( literal.getSyntax().length() != 0 ) )
387         {
388             sb.append( "\tSYNTAX " + literal.getSyntax() ); //$NON-NLS-1$
389
if ( literal.getLength() > 0 )
390             {
391                 sb.append( "{" + literal.getLength() + "}" ); //$NON-NLS-1$ //$NON-NLS-2$
392
}
393             sb.append( " \n" ); //$NON-NLS-1$
394
}
395
396         // SINGLE-VALUE
397
if ( literal.isSingleValue() )
398         {
399             sb.append( "\tSINGLE-VALUE \n" ); //$NON-NLS-1$
400
}
401
402         // COLLECTIVE
403
if ( literal.isCollective() )
404         {
405             sb.append( "\tCOLLECTIVE \n" ); //$NON-NLS-1$
406
}
407
408         // NO-USER-MODIFICATION
409
if ( literal.isNoUserModification() )
410         {
411             sb.append( "\tNO-USER-MODIFICATION \n" ); //$NON-NLS-1$
412
}
413
414         // USAGE
415
UsageEnum usage = literal.getUsage();
416         if ( usage != null )
417         {
418             if ( usage == UsageEnum.DIRECTORY_OPERATION )
419             {
420                 sb.append( "\tUSAGE directoryOperation \n" ); //$NON-NLS-1$
421
}
422             else if ( usage == UsageEnum.DISTRIBUTED_OPERATION )
423             {
424                 sb.append( "\tUSAGE distributedOperation \n" ); //$NON-NLS-1$
425
}
426             else if ( usage == UsageEnum.DSA_OPERATION )
427             {
428                 sb.append( "\tUSAGE dSAOperation \n" ); //$NON-NLS-1$
429
}
430             else if ( usage == UsageEnum.USER_APPLICATIONS )
431             {
432                 // There's nothing to write, this is the default option
433
}
434         }
435
436         // Close the definition
437
sb.append( " )\n" ); //$NON-NLS-1$
438

439         return sb.toString();
440     }
441
442
443     public String JavaDoc 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 JavaDoc e )
472             {
473                 logger.debug( "error when notifying listener: " + listener ); //$NON-NLS-1$
474
}
475         }
476     }
477
478
479     /* (non-Javadoc)
480      * @see java.lang.Object#clone()
481      */

482     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc
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     /**
506      * Updates the attribute type.
507      *
508      * @param at
509      * the attribute type to get values from
510      */

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 JavaDoc e )
535         {
536             // Will never occurr.
537
}
538     }
539 }
540
Popular Tags