KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > core > internal > model > Attribute


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.browser.core.internal.model;
22
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
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 /**
48  * Default implementation of IAttribute.
49  *
50  * @author <a HREF="mailto:dev@directory.apache.org">Apache Directory Project</a>
51  * @version $Rev$, $Date$
52  */

53 public class Attribute implements IAttribute
54 {
55
56     /** The serialVersionUID. */
57     private static final long serialVersionUID = -5679384884002589786L;
58
59     /** The attribute description */
60     private AttributeDescription attributeDescription;
61
62     /** The entry this attribute belongs to */
63     private IEntry entry;
64
65     /** The values */
66     private List JavaDoc<IValue> valueList;
67
68
69     /**
70      * Creates an new instance of Attribute with the given description
71      * and no value.
72      *
73      * @param entry
74      * The entry of this attribute, mustn't be null
75      * @param description
76      * The attribute descrption, mustn't be null.
77      * @throws ModelModificationException
78      * if the attribute name is null or empty.
79      */

80     public Attribute( IEntry entry, String JavaDoc 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 JavaDoc<IValue>();
94
95     }
96
97
98     /**
99      * {@inheritDoc}
100      */

101     public IEntry getEntry()
102     {
103         return entry;
104     }
105
106
107     /**
108      * {@inheritDoc}
109      */

110     public boolean isConsistent()
111     {
112         if ( valueList.isEmpty() )
113         {
114             return false;
115         }
116
117         for ( Iterator JavaDoc 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     /**
131      * {@inheritDoc}
132      */

133     public boolean isMustAttribute()
134     {
135         if ( isObjectClassAttribute() )
136         {
137             return true;
138         }
139         else
140         {
141             String JavaDoc[] mustAttributeNames = getEntry().getSubschema().getMustAttributeNames();
142             for ( int i = 0; i < mustAttributeNames.length; i++ )
143             {
144                 String JavaDoc must = mustAttributeNames[i];
145                 if ( must.equalsIgnoreCase( getType() ) )
146                 {
147                     return true;
148                 }
149             }
150             return false;
151         }
152     }
153
154
155     /**
156      * {@inheritDoc}
157      */

158     public boolean isMayAttribute()
159     {
160         return !isObjectClassAttribute() && !isMustAttribute() && !isOperationalAttribute();
161     }
162
163
164     /**
165      * {@inheritDoc}
166      */

167     public boolean isOperationalAttribute()
168     {
169         return getAttributeTypeDescription() == null || SchemaUtils.isOperational( getAttributeTypeDescription() );
170     }
171
172
173     /**
174      * {@inheritDoc}
175      */

176     public boolean isObjectClassAttribute()
177     {
178         return OBJECTCLASS_ATTRIBUTE.equalsIgnoreCase( getDescription() );
179     }
180
181
182     /**
183      * {@inheritDoc}
184      */

185     public boolean isString()
186     {
187         return !isBinary();
188     }
189
190
191     /**
192      * {@inheritDoc}
193      */

194     public boolean isBinary()
195     {
196         return getAttributeTypeDescription().isBinary();
197     }
198
199
200     /**
201      * {@inheritDoc}
202      */

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             // Shouldn't occur
214
}
215     }
216
217
218     /**
219      * {@inheritDoc}
220      */

221     public void deleteEmptyValue()
222     {
223         for ( Iterator JavaDoc 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     /**
237      * Fires an EntryModificationEvent.
238      *
239      * @param event the EntryModificationEvent
240      */

241     private void attributeModified( EntryModificationEvent event )
242     {
243         EventRegistry.fireEntryUpdated( event, getEntry() );
244     }
245
246
247     /**
248      * Checks if the given value is valid.
249      *
250      * @param value the value to check
251      * @throws ModelModificationException if the value is not valid
252      */

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     /**
267      * Deletes the given value from value list.
268      *
269      * @param valueToDelete the value to delete
270      * @return true if deleted
271      */

272     private boolean internalDeleteValue( IValue valueToDelete )
273     {
274         for ( Iterator JavaDoc 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     /**
288      * {@inheritDoc}
289      */

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     /**
300      * {@inheritDoc}
301      */

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     /**
314      * {@inheritDoc}
315      */

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     /**
329      * {@inheritDoc}
330      */

331     public IValue[] getValues()
332     {
333         return ( IValue[] ) valueList.toArray( new IValue[0] );
334     }
335
336
337     /**
338      * {@inheritDoc}
339      */

340     public int getValueSize()
341     {
342         return this.valueList.size();
343     }
344
345
346     /**
347      * {@inheritDoc}
348      */

349     public String JavaDoc getDescription()
350     {
351         return getAttributeDescription().getDescription();
352     }
353
354
355     /**
356      * {@inheritDoc}
357      */

358     public String JavaDoc getType()
359     {
360         return getAttributeDescription().getParsedAttributeType();
361     }
362
363
364     /**
365      * {@inheritDoc}
366      */

367     public String JavaDoc toString()
368     {
369         return getDescription();
370     }
371
372
373     /**
374      * {@inheritDoc}
375      */

376     public boolean equals( Object JavaDoc o )
377     {
378         // check argument
379
if ( o == null || !( o instanceof IAttribute ) )
380         {
381             return false;
382         }
383         IAttribute a = ( IAttribute ) o;
384
385         // compare entries
386
if ( !getEntry().equals( a.getEntry() ) )
387         {
388             return false;
389         }
390
391         // compare attribute description
392
return getDescription().equals( a.getDescription() );
393     }
394
395
396     /**
397      * {@inheritDoc}
398      */

399     public int hashCode()
400     {
401         return getDescription().hashCode();
402     }
403
404
405     /**
406      * {@inheritDoc}
407      */

408     public byte[][] getBinaryValues()
409     {
410         List JavaDoc<byte[]> binaryValueList = new ArrayList JavaDoc<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     /**
423      * {@inheritDoc}
424      */

425     public String JavaDoc 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     /**
439      * {@inheritDoc}
440      */

441     public String JavaDoc[] getStringValues()
442     {
443         List JavaDoc<String JavaDoc> stringValueList = new ArrayList JavaDoc<String JavaDoc>();
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 JavaDoc[stringValueList.size()] );
452     }
453
454
455     /**
456      * {@inheritDoc}
457      */

458     public AttributeTypeDescription getAttributeTypeDescription()
459     {
460         return getEntry().getConnection().getSchema().getAttributeTypeDescription( getType() );
461     }
462
463
464     /**
465      * {@inheritDoc}
466      */

467     public Object JavaDoc getAdapter( Class JavaDoc adapter )
468     {
469
470         Class JavaDoc<?> clazz = ( Class JavaDoc<?> ) 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     /**
493      * {@inheritDoc}
494      */

495     public AttributeDescription getAttributeDescription()
496     {
497         return attributeDescription;
498     }
499
500 }
501
Popular Tags