View Javadoc
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  package org.apache.directory.api.ldap.model.schema;
21  
22  
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.apache.directory.api.i18n.I18n;
27  import org.apache.directory.api.util.exception.NotImplementedException;
28  
29  
30  /**
31   * A dITStructureRule definition. A dITStructureRules is a rule governing the
32   * structure of the DIT by specifying a permitted superior to subordinate entry
33   * relationship. A structure rule relates a nameForm, and therefore a STRUCTURAL
34   * objectClass, to superior dITStructureRules. This permits entries of the
35   * STRUCTURAL objectClass identified by the nameForm to exist in the DIT as
36   * subordinates to entries governed by the indicated superior dITStructureRules.
37   * Hence dITStructureRules only apply to structural object classes.
38   * <p>
39   * According to ldapbis [MODELS]:
40   * </p>
41   * 
42   * <pre>
43   *  DIT structure rule descriptions are written according to the ABNF:
44   *  
45   *    DITStructureRuleDescription = LPAREN WSP
46   *        ruleid                    ; rule identifier
47   *        [ SP &quot;NAME&quot; SP qdescrs ]  ; short names (descriptors)
48   *        [ SP &quot;DESC&quot; SP qdstring ] ; description
49   *        [ SP &quot;OBSOLETE&quot; ]         ; not active
50   *        SP &quot;FORM&quot; SP oid          ; NameForm
51   *        [ SP &quot;SUP&quot; ruleids ]      ; superior rules
52   *        extensions WSP RPAREN     ; extensions
53   * 
54   *    ruleids = ruleid / ( LPAREN WSP ruleidlist WSP RPAREN )
55   * 
56   *    ruleidlist = ruleid *( SP ruleid )
57   * 
58   *    ruleid = number
59   * 
60   *  where:
61   *    [ruleid] is the rule identifier of this DIT structure rule;
62   *    NAME [qdescrs] are short names (descriptors) identifying this DIT
63   *        structure rule;
64   *    DESC [qdstring] is a short descriptive string;
65   *    OBSOLETE indicates this DIT structure rule use is not active;
66   *    FORM is specifies the name form associated with this DIT structure
67   *        rule;
68   *    SUP identifies superior rules (by rule id); and
69   *    [extensions] describe extensions.
70   *  
71   *  If no superior rules are identified, the DIT structure rule applies
72   *  to an autonomous administrative point (e.g. the root vertex of the
73   *  subtree controlled by the subschema) [X.501].
74   * </pre>
75   * 
76   * @see <a href="http://www.faqs.org/rfcs/rfc2252.html">RFC2252 Section 6.33</a>
77   * @see <a
78   *      href="http://www.ietf.org/internet-drafts/draft-ietf-ldapbis-models-11.txt">ldapbis
79   *      [MODELS]</a>
80   * @see DescriptionUtils#getDescription(DitStructureRule)
81   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
82   */
83  public class DitStructureRule extends AbstractSchemaObject
84  {
85      /** The mandatory serialVersionUID */
86      public static final long serialVersionUID = 1L;
87  
88      /** The rule ID. A DSR does not have an OID */
89      private int ruleId;
90  
91      /** The associated NameForm */
92      private String form;
93  
94      /** The list of superiors rules */
95      private List<Integer> superRules;
96  
97  
98      /**
99       * Creates a new instance of DitStructureRule
100      */
101     public DitStructureRule( int ruleId )
102     {
103         super( SchemaObjectType.DIT_STRUCTURE_RULE, null );
104         this.ruleId = ruleId;
105         form = null;
106         superRules = new ArrayList<Integer>();
107     }
108 
109 
110     /**
111      *  @return The associated NameForm's OID
112      */
113     public String getForm()
114     {
115         return form;
116     }
117 
118 
119     /**
120      * Sets the associated NameForm's OID
121      *
122      * @param form The NameForm's OID
123      */
124     public void setForm( String form )
125     {
126         if ( locked )
127         {
128             throw new UnsupportedOperationException( I18n.err( I18n.ERR_04441, getName() ) );
129         }
130 
131         if ( !isReadOnly )
132         {
133             this.form = form;
134         }
135     }
136 
137 
138     /**
139      * @return The Rule ID
140      */
141     public int getRuleId()
142     {
143         return ruleId;
144     }
145 
146 
147     /**
148      * Sets the rule identifier of this DIT structure rule;
149      *
150      * @param ruleId the rule identifier of this DIT structure rule;
151      */
152     public void setRuleId( int ruleId )
153     {
154         if ( locked )
155         {
156             throw new UnsupportedOperationException( I18n.err( I18n.ERR_04441, getName() ) );
157         }
158 
159         if ( !isReadOnly )
160         {
161             this.ruleId = ruleId;
162         }
163     }
164 
165 
166     /**
167      * @return The list of superiors RuleIDs
168      */
169     public List<Integer> getSuperRules()
170     {
171         return superRules;
172     }
173 
174 
175     /**
176      * Sets the list of superior RuleIds
177      * 
178      * @param superRules the list of superior RuleIds
179      */
180     public void setSuperRules( List<Integer> superRules )
181     {
182         if ( locked )
183         {
184             throw new UnsupportedOperationException( I18n.err( I18n.ERR_04441, getName() ) );
185         }
186 
187         if ( !isReadOnly )
188         {
189             this.superRules = superRules;
190         }
191     }
192 
193 
194     /**
195      * Adds a new superior RuleId
196      *
197      * @param superRule The superior RuleID to add
198      */
199     public void addSuperRule( Integer superRule )
200     {
201         if ( locked )
202         {
203             throw new UnsupportedOperationException( I18n.err( I18n.ERR_04441, getName() ) );
204         }
205 
206         superRules.add( superRule );
207     }
208 
209 
210     /**
211      * The DIT structure rule does not have an OID, so this implementations always throws an exception.
212      * 
213      * {@inheritDoc}
214      */
215     @Override
216     public String getOid()
217     {
218         throw new NotImplementedException();
219     }
220 
221 
222     /**
223      * {@inheritDoc}
224      */
225     @Override
226     public String toString()
227     {
228         return SchemaObjectRenderer.OPEN_LDAP_SCHEMA_RENDERER.render( this );
229     }
230 
231 
232     /**
233      * {@inheritDoc}
234      */
235     public DitStructureRule copy()
236     {
237         DitStructureRule copy = new DitStructureRule( ruleId );
238 
239         // Copy the SchemaObject common data
240         copy.copy( this );
241 
242         // Copy the Superiors rules
243         copy.superRules = new ArrayList<Integer>();
244 
245         // Copy the form
246         copy.form = form;
247 
248         for ( int superRule : superRules )
249         {
250             copy.superRules.add( superRule );
251         }
252 
253         return copy;
254     }
255 
256 
257     /**
258      * {@inheritDoc}
259      */
260     @Override
261     public boolean equals( Object o )
262     {
263         if ( !super.equals( o ) )
264         {
265             return false;
266         }
267 
268         if ( !( o instanceof DitStructureRule ) )
269         {
270             return false;
271         }
272 
273         @SuppressWarnings("unused")
274         DitStructureRule that = ( DitStructureRule ) o;
275 
276         // TODO : complete the test
277         return true;
278     }
279 
280 
281     /**
282      * {@inheritDoc}
283      */
284     public void clear()
285     {
286         // Clear the common elements
287         super.clear();
288 
289         // Clear the references
290         superRules.clear();
291     }
292 }