KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Arrays JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Set JavaDoc;
28
29 import org.apache.directory.ldapstudio.browser.core.model.IAttribute;
30
31
32 public class SchemaUtils
33 {
34
35     public static String JavaDoc[] getAttributeTypeDescriptionNames( AttributeTypeDescription[] atds )
36     {
37
38         Set JavaDoc attributeSet = new HashSet JavaDoc();
39         for ( int i = 0; i < atds.length; i++ )
40         {
41             AttributeTypeDescription atd = atds[i];
42             attributeSet.addAll( Arrays.asList( atd.getNames() ) );
43         }
44
45         String JavaDoc[] attributes = ( String JavaDoc[] ) attributeSet.toArray( new String JavaDoc[0] );
46         Arrays.sort( attributes );
47         return attributes;
48
49     }
50
51
52     /**
53      *
54      * @return all operational attributes types
55      */

56     public static AttributeTypeDescription[] getOperationalAttributeDescriptions( Schema schema )
57     {
58         Set JavaDoc operationalAttributeSet = new HashSet JavaDoc();
59         for ( Iterator JavaDoc it = schema.getAtdMapByName().values().iterator(); it.hasNext(); )
60         {
61             AttributeTypeDescription atd = ( AttributeTypeDescription ) it.next();
62             if ( isOperational( atd ) )
63             {
64                 operationalAttributeSet.add( atd );
65             }
66         }
67
68         AttributeTypeDescription[] operationalAttributes = ( AttributeTypeDescription[] ) operationalAttributeSet
69             .toArray( new AttributeTypeDescription[0] );
70         return operationalAttributes;
71     }
72
73
74     /**
75      *
76      * @return all user attributes types
77      */

78     public static AttributeTypeDescription[] getUserAttributeDescriptions( Schema schema )
79     {
80         Set JavaDoc userAttributeSet = new HashSet JavaDoc();
81         for ( Iterator JavaDoc it = schema.getAtdMapByName().values().iterator(); it.hasNext(); )
82         {
83             AttributeTypeDescription atd = ( AttributeTypeDescription ) it.next();
84             if ( !isOperational( atd ) )
85             {
86                 userAttributeSet.add( atd );
87             }
88         }
89
90         AttributeTypeDescription[] userAttributes = ( AttributeTypeDescription[] ) userAttributeSet
91             .toArray( new AttributeTypeDescription[0] );
92         return userAttributes;
93     }
94
95
96     public static boolean isOperational( AttributeTypeDescription atd )
97     {
98         return atd.isNoUserModification()
99             || !AttributeTypeDescription.ATTRIBUTE_USAGE_USER_APPLICATIONS.equalsIgnoreCase( atd.getUsage() );
100
101         // atd.isNoUserModification()
102
// ||
103
// AttributeTypeDescription.ATTRIBUTE_USAGE_DIRECTORY_OPERATION.equalsIgnoreCase(atd.getUsage())
104
// ||
105
// AttributeTypeDescription.ATTRIBUTE_USAGE_DSA_OPERATION.equalsIgnoreCase(atd.getUsage());
106
}
107
108
109     public static boolean isModifyable( AttributeTypeDescription atd )
110     {
111
112         if ( atd == null )
113         {
114             return false;
115         }
116
117         if ( atd.isNoUserModification() )
118         {
119             return false;
120         }
121
122         // Check some default no-user-modification attributes
123
// e.g. Siemens DirX doesn't provide a good schema.
124
// TODO: make default no-user-modification attributes configurable
125
String JavaDoc[] nonModifyableAttributes = new String JavaDoc[]
126             { IAttribute.OPERATIONAL_ATTRIBUTE_CREATORS_NAME, IAttribute.OPERATIONAL_ATTRIBUTE_CREATE_TIMESTAMP,
127                 IAttribute.OPERATIONAL_ATTRIBUTE_MODIFIERS_NAME, IAttribute.OPERATIONAL_ATTRIBUTE_MODIFY_TIMESTAMP,
128                 IAttribute.OPERATIONAL_ATTRIBUTE_STRUCTURAL_OBJECT_CLASS,
129                 IAttribute.OPERATIONAL_ATTRIBUTE_GOVERNING_STRUCTURE_RULE,
130
131                 IAttribute.OPERATIONAL_ATTRIBUTE_SUBSCHEMA_SUBENTRY, IAttribute.OPERATIONAL_ATTRIBUTE_VENDOR_NAME,
132                 IAttribute.OPERATIONAL_ATTRIBUTE_VENDOR_VERSION,
133
134                 IAttribute.OPERATIONAL_ATTRIBUTE_ENTRY_UUID, IAttribute.OPERATIONAL_ATTRIBUTE_HAS_SUBORDINATES,
135                 IAttribute.OPERATIONAL_ATTRIBUTE_SUBORDINATE_COUNT, IAttribute.OPERATIONAL_ATTRIBUTE_NUM_SUBORDINATES
136
137             };
138         for ( int i = 0; i < nonModifyableAttributes.length; i++ )
139         {
140             String JavaDoc att = nonModifyableAttributes[i];
141             if ( att.equalsIgnoreCase( atd.getNumericOID() ) )
142             {
143                 return false;
144             }
145             for ( int n = 0; n < atd.getNames().length; n++ )
146             {
147                 if ( att.equalsIgnoreCase( atd.getNames()[n] ) )
148                 {
149                     return false;
150                 }
151             }
152         }
153
154         return true;
155     }
156
157 }
158
Popular Tags