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.NameForm;
027import org.apache.directory.api.ldap.model.schema.ObjectClass;
028import org.apache.directory.api.ldap.model.schema.registries.AttributeTypeRegistry;
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 NameForm
034 * in relation with the Registries and SchemaManager.
035 * 
036 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
037 */
038public final class NameFormHelper
039{
040    private NameFormHelper()
041    {
042    }
043
044
045    /**
046     * Inject the NameForm into the registries, updating the references to
047     * other SchemaObject
048     *
049     * @param nameForm The NameForm to add to the Registries
050     * @param errors The errors we got while adding the NameForm to the Registries
051     * @param registries The Registries
052     * @throws LdapException If the addition failed
053     */
054    public static void addToRegistries( NameForm nameForm, List<Throwable> errors, Registries registries )
055        throws LdapException
056    {
057        if ( registries != null )
058        {
059            try
060            {
061                nameForm.unlock();
062
063                AttributeTypeRegistry atRegistry = registries.getAttributeTypeRegistry();
064
065                ObjectClass structuralObjectClass = registries.getObjectClassRegistry().lookup(
066                    nameForm.getStructuralObjectClassOid() );
067                nameForm.setStructuralObjectClass( structuralObjectClass );
068
069                nameForm.getMayAttributeTypes().clear();
070
071                for ( String oid : nameForm.getMayAttributeTypeOids() )
072                {
073                    nameForm.getMayAttributeTypes().add( atRegistry.lookup( oid ) );
074                }
075
076                nameForm.getMustAttributeTypes().clear();
077
078                for ( String oid : nameForm.getMustAttributeTypeOids() )
079                {
080                    nameForm.getMustAttributeTypes().add( atRegistry.lookup( oid ) );
081                }
082            }
083            finally
084            {
085                nameForm.lock();
086            }
087        }
088    }
089}