KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > directory > ldapstudio > browser > core > model > schema > Subschema


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.model.schema;
22
23
24 import java.io.Serializable JavaDoc;
25 import java.util.Arrays JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.TreeSet JavaDoc;
28
29 import org.apache.directory.ldapstudio.browser.core.model.IAttribute;
30 import org.apache.directory.ldapstudio.browser.core.model.IConnection;
31 import org.apache.directory.ldapstudio.browser.core.model.IEntry;
32
33
34 public class Subschema implements Serializable JavaDoc
35 {
36
37     private static final long serialVersionUID = 7821844589084867562L;
38
39     private String JavaDoc[] objectClassNames;
40
41     private Schema schema;
42
43     private Set JavaDoc allAttributeNameSet;
44
45
46     protected Subschema()
47     {
48     }
49
50
51     public Subschema( IEntry entry )
52     {
53         if ( entry.getAttribute( IAttribute.OBJECTCLASS_ATTRIBUTE ) != null )
54         {
55             this.objectClassNames = entry.getAttribute( IAttribute.OBJECTCLASS_ATTRIBUTE ).getStringValues();
56         }
57         else
58         {
59             this.objectClassNames = new String JavaDoc[0];
60         }
61         this.schema = entry.getConnection().getSchema();
62     }
63
64
65     public Subschema( String JavaDoc[] objectClassNames, IConnection connection )
66     {
67         this.objectClassNames = objectClassNames;
68         this.schema = connection.getSchema();
69     }
70
71
72     /**
73      * Returns the names of this and all super object classes.
74      *
75      * @return
76      */

77     public String JavaDoc[] getObjectClassNames()
78     {
79         return this.objectClassNames;
80     }
81
82
83     /**
84      * Returns the must attribute names of this and all super object
85      * classes.
86      *
87      * @return
88      */

89     public String JavaDoc[] getMustAttributeNames()
90     {
91         Set JavaDoc mustAttrSet = new TreeSet JavaDoc();
92         for ( int i = 0; i < this.objectClassNames.length; i++ )
93         {
94             this.fetchMust( this.objectClassNames[i], mustAttrSet );
95         }
96         return ( String JavaDoc[] ) mustAttrSet.toArray( new String JavaDoc[0] );
97     }
98
99
100     /**
101      * Returns the must attribute types of this and all super object
102      * classes.
103      *
104      * @return
105      */

106     public AttributeTypeDescription[] getMustAttributeTypeDescriptions()
107     {
108         String JavaDoc[] musts = getMustAttributeNames();
109         AttributeTypeDescription[] atds = new AttributeTypeDescription[musts.length];
110         for ( int i = 0; i < musts.length; i++ )
111         {
112             AttributeTypeDescription atd = getSchema().getAttributeTypeDescription( musts[i] );
113             atds[i] = atd;
114         }
115         return atds;
116     }
117
118
119     private void fetchMust( String JavaDoc ocName, Set JavaDoc attributeSet )
120     {
121         // add own must attributes
122
ObjectClassDescription ocd = this.getSchema().getObjectClassDescription( ocName );
123         attributeSet.addAll( Arrays.asList( ocd.getMustAttributeTypeDescriptionNames() ) );
124
125         // add must attributes of super object classes
126
if ( ocd.getSuperiorObjectClassDescriptionNames() != null )
127         {
128             for ( int k = 0; k < ocd.getSuperiorObjectClassDescriptionNames().length; k++ )
129             {
130                 fetchMust( ocd.getSuperiorObjectClassDescriptionNames()[k], attributeSet );
131             }
132         }
133     }
134
135
136     /**
137      * Returns the may attribute names of this and all super object classes.
138      *
139      * @return
140      */

141     public String JavaDoc[] getMayAttributeNames()
142     {
143         Set JavaDoc mayAttrSet = new TreeSet JavaDoc();
144         for ( int i = 0; i < this.objectClassNames.length; i++ )
145         {
146             this.fetchMay( this.objectClassNames[i], mayAttrSet );
147         }
148         return ( String JavaDoc[] ) mayAttrSet.toArray( new String JavaDoc[0] );
149     }
150
151
152     /**
153      * Returns the may attribute types of this and all super object classes.
154      *
155      * @return
156      */

157     public AttributeTypeDescription[] getMayAttributeTypeDescriptions()
158     {
159         String JavaDoc[] mays = getMayAttributeNames();
160         AttributeTypeDescription[] atds = new AttributeTypeDescription[mays.length];
161         for ( int i = 0; i < mays.length; i++ )
162         {
163             AttributeTypeDescription atd = getSchema().getAttributeTypeDescription( mays[i] );
164             atds[i] = atd;
165         }
166         return atds;
167     }
168
169
170     private void fetchMay( String JavaDoc ocName, Set JavaDoc attributeSet )
171     {
172         // add own may attributes
173
ObjectClassDescription ocd = this.getSchema().getObjectClassDescription( ocName );
174         attributeSet.addAll( Arrays.asList( ocd.getMayAttributeTypeDescriptionNames() ) );
175
176         // add may attributes of super object classes
177
if ( ocd.getSuperiorObjectClassDescriptionNames() != null )
178         {
179             for ( int k = 0; k < ocd.getSuperiorObjectClassDescriptionNames().length; k++ )
180             {
181                 fetchMay( ocd.getSuperiorObjectClassDescriptionNames()[k], attributeSet );
182             }
183         }
184     }
185
186
187     /**
188      * Returns the must and may attribute names of this and all super object
189      * classes.
190      *
191      * @return
192      */

193     public String JavaDoc[] getAllAttributeNames()
194     {
195         return ( String JavaDoc[] ) getAllAttributeNameSet().toArray( new String JavaDoc[0] );
196     }
197
198
199     public Set JavaDoc getAllAttributeNameSet()
200     {
201         if ( this.allAttributeNameSet == null )
202         {
203             this.allAttributeNameSet = new TreeSet JavaDoc();
204             this.allAttributeNameSet.addAll( Arrays.asList( this.getMustAttributeNames() ) );
205             this.allAttributeNameSet.addAll( Arrays.asList( this.getMayAttributeNames() ) );
206         }
207
208         return this.allAttributeNameSet;
209     }
210
211
212     private Schema getSchema()
213     {
214         return schema;
215         // return
216
// BrowserCorePlugin.getDefault().getConnectionManager().getConnection(this.connectionName).getSchema();
217
}
218
219 }
220
Popular Tags