KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 /**
34  * This class is the model for 'object type' LDAP schema
35  * elements it's based on the ASN.1 description.
36  *
37  */

38 public class ObjectClass implements SchemaElement, Cloneable JavaDoc
39 {
40     private static Logger logger = Logger.getLogger( ObjectClass.class );
41     private ObjectClassLiteral literal;
42     private Schema originatingSchema;
43     private ArrayList JavaDoc<SchemaElementListener> listeners;
44     private ObjectClassEditor editor;
45
46
47     /**
48      * Default constructor
49      * @param literal the literal given by the schema parser that represents this element
50      * @param originatingSchema the schema file that defines this element
51      */

52     public ObjectClass( ObjectClassLiteral literal, Schema originatingSchema )
53     {
54         this.literal = literal;
55         this.originatingSchema = originatingSchema;
56         listeners = new ArrayList JavaDoc<SchemaElementListener>();
57     }
58
59
60     /**
61      * @return the openldap parser literal associated with this objectClass
62      */

63     public ObjectClassLiteral getLiteral()
64     {
65         return literal;
66     }
67
68
69     /* (non-Javadoc)
70      * @see org.apache.directory.ldapstudio.schemas.model.SchemaElement#getOriginatingSchema()
71      */

72     public Schema getOriginatingSchema()
73     {
74         return originatingSchema;
75     }
76
77
78     /**
79      * Returns the editor associated to this objectClass
80      * @return the editor
81      */

82     public ObjectClassEditor getEditor()
83     {
84         return editor;
85     }
86
87
88     /**
89      * Sets the editor associated to this objectClass
90      * @param editor the associated editor
91      */

92     public void setEditor( ObjectClassEditor editor )
93     {
94         this.editor = editor;
95     }
96
97
98     /**
99      * Call this method to remove the objectClass<->Editor association
100      * @param editor the associated editor
101      */

102     public void removeEditor( ObjectClassEditor editor )
103     {
104         if ( this.editor == editor )
105             this.editor = null;
106     }
107
108
109     /**
110      * Close the editor associated to this objectClass WITHOUT applying the
111      * modifications
112      */

113     public void closeAssociatedEditor()
114     {
115         if ( editor != null )
116         {
117             // 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.
118
editor.setDirty( false );
119
120             editor.close( false );
121             removeEditor( editor );
122         }
123     }
124
125
126     /**
127      * Checks if the objectClass has pending modifications in its editor
128      * @return
129      */

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

144     public void applyPendingModifications()
145     {
146         if ( hasPendingModifications() )
147         {
148             editor.doSave( new NullProgressMonitor() );
149         }
150     }
151
152
153     public String JavaDoc[] getNames()
154     {
155         return literal.getNames();
156     }
157
158
159     public String JavaDoc getOid()
160     {
161         return literal.getOid();
162     }
163
164
165     public String JavaDoc getDescription()
166     {
167         return literal.getDescription();
168     }
169
170
171     public String JavaDoc[] 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 JavaDoc[] getMay()
190     {
191         return literal.getMay();
192     }
193
194
195     public String JavaDoc[] getMust()
196     {
197         return literal.getMust();
198     }
199
200
201     public void setNames( String JavaDoc[] names )
202     {
203         literal.setNames( names );
204     }
205
206
207     public void setOid( String JavaDoc oid )
208     {
209         literal.setOid( oid );
210     }
211
212
213     public void setDescription( String JavaDoc 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 JavaDoc[] superiors )
232     {
233         literal.setSuperiors( superiors );
234     }
235
236
237     public void setMay( String JavaDoc[] may )
238     {
239         literal.setMay( may );
240     }
241
242
243     public void setMust( String JavaDoc[] must )
244     {
245         literal.setMust( must );
246     }
247
248
249     public String JavaDoc write()
250     {
251         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
252
253         // Open the definition and OID
254
sb.append( "objectclass ( " + literal.getOid() + " \n" ); //$NON-NLS-1$ //$NON-NLS-2$
255

256         // NAME(S)
257
String JavaDoc[] names = literal.getNames();
258         sb.append( "\tNAME " ); //$NON-NLS-1$
259
if ( names.length > 1 )
260         {
261             sb.append( "( " ); //$NON-NLS-1$
262
for ( String JavaDoc name : names )
263             {
264                 sb.append( "'" + name + "' " ); //$NON-NLS-1$ //$NON-NLS-2$
265
}
266             sb.append( ") \n" ); //$NON-NLS-1$
267
}
268         else
269         {
270             sb.append( "'" + names[0] + "' \n" ); //$NON-NLS-1$ //$NON-NLS-2$
271
}
272
273         // DESC
274
if ( ( literal.getDescription() != null ) && ( literal.getDescription().length() != 0 ) )
275         {
276             sb.append( "\tDESC '" + literal.getDescription() + "' \n" ); //$NON-NLS-1$ //$NON-NLS-2$
277
}
278
279         // OBSOLETE
280
if ( literal.isObsolete() )
281         {
282             sb.append( "\tOBSOLETE \n" ); //$NON-NLS-1$
283
}
284
285         // SUP
286
String JavaDoc[] superiors = literal.getSuperiors();
287         if ( superiors.length != 0 )
288         {
289             if ( superiors.length > 1 )
290             {
291                 sb.append( "\tSUP (" + superiors[0] ); //$NON-NLS-1$
292
for ( int i = 1; i < superiors.length; i++ )
293                 {
294                     sb.append( " $ " + superiors[i] ); //$NON-NLS-1$
295
}
296                 sb.append( ") \n" ); //$NON-NLS-1$
297
}
298             else
299             {
300                 sb.append( "\tSUP " + superiors[0] + " \n" ); //$NON-NLS-1$ //$NON-NLS-2$
301
}
302         }
303
304         // CLASSTYPE
305
ObjectClassTypeEnum classtype = literal.getClassType();
306         if ( classtype == ObjectClassTypeEnum.ABSTRACT )
307         {
308             sb.append( "\tABSTRACT \n" ); //$NON-NLS-1$
309
}
310         else if ( classtype == ObjectClassTypeEnum.AUXILIARY )
311         {
312             sb.append( "\tAUXILIARY \n" ); //$NON-NLS-1$
313
}
314         else if ( classtype == ObjectClassTypeEnum.STRUCTURAL )
315         {
316             sb.append( "\tSTRUCTURAL \n" ); //$NON-NLS-1$
317
}
318
319         // MUST
320
String JavaDoc[] must = literal.getMust();
321         if ( must.length != 0 )
322         {
323             sb.append( "\tMUST " ); //$NON-NLS-1$
324
if ( must.length > 1 )
325             {
326                 sb.append( "( " + must[0] + " " ); //$NON-NLS-1$ //$NON-NLS-2$
327
for ( int i = 1; i < must.length; i++ )
328                 {
329                     sb.append( "$ " + must[i] + " " ); //$NON-NLS-1$ //$NON-NLS-2$
330
}
331                 sb.append( ") \n" ); //$NON-NLS-1$
332
}
333             else if ( must.length == 1 )
334             {
335                 sb.append( must[0] + " \n" ); //$NON-NLS-1$
336
}
337         }
338
339         // MAY
340
String JavaDoc[] may = literal.getMay();
341         if ( may.length != 0 )
342         {
343             sb.append( "\tMAY " ); //$NON-NLS-1$
344
if ( may.length > 1 )
345             {
346                 sb.append( "( " + may[0] + " " ); //$NON-NLS-1$ //$NON-NLS-2$
347
for ( int i = 1; i < may.length; i++ )
348                 {
349                     sb.append( "$ " + may[i] + " " ); //$NON-NLS-1$ //$NON-NLS-2$
350
}
351                 sb.append( ") \n" ); //$NON-NLS-1$
352
}
353             else if ( may.length == 1 )
354             {
355                 sb.append( may[0] + " \n" ); //$NON-NLS-1$
356
}
357         }
358         // Close the definition
359
sb.append( " )\n" ); //$NON-NLS-1$
360

361         return sb.toString();
362     }
363
364
365     public String JavaDoc 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 JavaDoc e )
394             {
395                 logger.debug( "error when notifying " + listener + " of " + this.getNames()[0] + " modification" ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
396
}
397         }
398     }
399
400
401     /* (non-Javadoc)
402      * @see java.lang.Object#clone()
403      */

404     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc
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     /**
422      * Update the object class.
423      *
424      * @param oc
425      * the object class to get values from
426      */

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 JavaDoc e )
445         {
446             // Will never occurr.
447
}
448     }
449 }
450
Popular Tags