001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *    http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 *
019 */
020package org.apache.directory.api.ldap.model.schema.registries.helper;
021
022
023import java.util.List;
024
025import org.apache.directory.api.ldap.model.exception.LdapException;
026import org.apache.directory.api.ldap.model.schema.DitContentRule;
027import org.apache.directory.api.ldap.model.schema.registries.AttributeTypeRegistry;
028import org.apache.directory.api.ldap.model.schema.registries.ObjectClassRegistry;
029import org.apache.directory.api.ldap.model.schema.registries.Registries;
030
031
032/**
033 * An helper class used to store all the methods associated with an DitContentRule
034 * in relation with the Registries and SchemaManager.
035 * 
036 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
037 */
038public class DitContentRuleHelper
039{
040    /**
041     * Inject the DitContentRule into the registries, updating the references to
042     * other SchemaObject
043     *
044     * @param ditContentRule The DitContentRule to add to the Registries
045     * @param errors The errors we got while adding the DitContentRule to the Registries
046     * @param registries The Registries
047     * @exception If the addition failed
048     */
049    public static void addToRegistries( DitContentRule ditContentRule, List<Throwable> errors, Registries registries )
050        throws LdapException
051    {
052        if ( registries != null )
053        {
054            try
055            {
056                ditContentRule.unlock();
057                AttributeTypeRegistry atRegistry = registries.getAttributeTypeRegistry();
058                ObjectClassRegistry ocRegistry = registries.getObjectClassRegistry();
059
060                if ( ditContentRule.getMayAttributeTypeOids() != null )
061                {
062                    ditContentRule.getMayAttributeTypes().clear();
063
064                    for ( String oid : ditContentRule.getMayAttributeTypeOids() )
065                    {
066                        ditContentRule.getMayAttributeTypes().add( atRegistry.lookup( oid ) );
067                    }
068                }
069
070                if ( ditContentRule.getMustAttributeTypeOids() != null )
071                {
072                    ditContentRule.getMustAttributeTypes().clear();
073
074                    for ( String oid : ditContentRule.getMustAttributeTypeOids() )
075                    {
076                        ditContentRule.getMustAttributeTypes().add( atRegistry.lookup( oid ) );
077                    }
078                }
079
080                if ( ditContentRule.getNotAttributeTypeOids() != null )
081                {
082                    ditContentRule.getNotAttributeTypes().clear();
083
084                    for ( String oid : ditContentRule.getNotAttributeTypeOids() )
085                    {
086                        ditContentRule.getNotAttributeTypes().add( atRegistry.lookup( oid ) );
087                    }
088                }
089
090                if ( ditContentRule.getAuxObjectClassOids() != null )
091                {
092                    ditContentRule.getAuxObjectClasses().clear();
093
094                    for ( String oid : ditContentRule.getAuxObjectClassOids() )
095                    {
096                        ditContentRule.getAuxObjectClasses().add( ocRegistry.lookup( oid ) );
097                    }
098                }
099            }
100            finally
101            {
102                ditContentRule.lock();
103            }
104        }
105    }
106}