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.comparators;
021
022
023import org.apache.directory.api.i18n.I18n;
024import org.apache.directory.api.ldap.model.exception.LdapException;
025import org.apache.directory.api.ldap.model.exception.LdapInvalidDnException;
026import org.apache.directory.api.ldap.model.name.Dn;
027import org.apache.directory.api.ldap.model.schema.LdapComparator;
028import org.apache.directory.api.ldap.model.schema.SchemaManager;
029
030
031/**
032 * A comparator that sorts OIDs based on their numeric id value.  Needs a 
033 * OidRegistry to properly do it's job.  Public method to set the oid 
034 * registry will be used by the server after instantiation in deserialization.
035 *
036 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
037 */
038public class UniqueMemberComparator extends LdapComparator<String>
039{
040    /** The serial version UID */
041    private static final long serialVersionUID = 2L;
042
043    /** A reference to the schema manager */
044    private SchemaManager schemaManager;
045
046
047    /**
048     * The IntegerComparator constructor. Its OID is the IntegerOrderingMatch matching
049     * rule OID.
050     */
051    public UniqueMemberComparator( String oid )
052    {
053        super( oid );
054    }
055
056
057    /**
058     * Implementation of the Compare method
059     */
060    public int compare( String dnstr1, String dnstr2 )
061    {
062        int dash1 = dnstr1.lastIndexOf( '#' );
063        int dash2 = dnstr2.lastIndexOf( '#' );
064
065        if ( ( dash1 == -1 ) && ( dash2 == -1 ) )
066        {
067            // no UID part
068            try
069            {
070                Dn dn1 = getDn( dnstr1 );
071                Dn dn2 = getDn( dnstr2 );
072
073                if ( dn1.equals( dn2 ) )
074                {
075                    return 0;
076                }
077                else
078                {
079                    return -1;
080                }
081            }
082            catch ( LdapInvalidDnException ne )
083            {
084                return -1;
085            }
086        }
087        else
088        {
089            // Now, check that we don't have another '#'
090            if ( dnstr1.indexOf( '#' ) != dash1 )
091            {
092                // Yes, we have one : this is not allowed, it should have been
093                // escaped.
094                return -1;
095            }
096
097            if ( dnstr2.indexOf( '#' ) != dash1 )
098            {
099                // Yes, we have one : this is not allowed, it should have been
100                // escaped.
101                return 1;
102            }
103
104            Dn dn1 = null;
105            Dn dn2 = null;
106
107            // This is an UID if the '#' is immediatly
108            // followed by a BitString, except if the '#' is
109            // on the last position
110            String uid1 = dnstr1.substring( dash1 + 1 );
111
112            if ( dash1 > 0 )
113            {
114                try
115                {
116                    dn1 = new Dn( dnstr1.substring( 0, dash1 ) );
117                }
118                catch ( LdapException ne )
119                {
120                    return -1;
121                }
122            }
123            else
124            {
125                return -1;
126            }
127
128            // This is an UID if the '#' is immediatly
129            // followed by a BitString, except if the '#' is
130            // on the last position
131            String uid2 = dnstr2.substring( dash2 + 1 );
132
133            if ( dash2 > 0 )
134            {
135                try
136                {
137                    dn2 = new Dn( dnstr1.substring( 0, dash2 ) );
138                }
139                catch ( LdapException ne )
140                {
141                    return 1;
142                }
143            }
144            else
145            {
146                return 1;
147            }
148
149            if ( dn1.equals( dn2 ) )
150            {
151                return uid1.compareTo( uid2 );
152            }
153
154            return -1;
155        }
156    }
157
158
159    /**
160     * {@inheritDoc}
161     */
162    public void setSchemaManager( SchemaManager schemaManager )
163    {
164        this.schemaManager = schemaManager;
165    }
166
167
168    public Dn getDn( Object obj ) throws LdapInvalidDnException
169    {
170        Dn dn = null;
171
172        if ( obj instanceof Dn )
173        {
174            dn = ( Dn ) obj;
175
176            dn = ( dn.isSchemaAware() ? dn : dn.apply( schemaManager ) );
177        }
178        else if ( obj instanceof String )
179        {
180            dn = new Dn( schemaManager, ( String ) obj );
181        }
182        else
183        {
184            throw new IllegalStateException( I18n.err( I18n.ERR_04218, ( obj == null ? null : obj.getClass() ) ) );
185        }
186
187        return dn;
188    }
189}