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     */
020    package org.apache.directory.server.xdbm.search.impl;
021    
022    
023    import org.apache.directory.shared.ldap.filter.PresenceNode;
024    import org.apache.directory.shared.ldap.schema.AttributeType;
025    import org.apache.directory.server.xdbm.IndexEntry;
026    import org.apache.directory.server.xdbm.Store;
027    import org.apache.directory.server.xdbm.Index;
028    import org.apache.directory.server.xdbm.search.Evaluator;
029    import org.apache.directory.server.schema.registries.Registries;
030    import org.apache.directory.server.core.entry.ServerEntry;
031    import org.apache.directory.server.core.entry.ServerAttribute;
032    
033    import java.util.Iterator;
034    
035    
036    /**
037     * An Evaluator which determines if candidates are matched by GreaterEqNode
038     * assertions.
039     *
040     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
041     * @version $Rev$
042     */
043    public class PresenceEvaluator implements Evaluator<PresenceNode, ServerEntry>
044    {
045        private final PresenceNode node;
046        private final Store<ServerEntry> db;
047        private final Registries registries;
048        private final AttributeType type;
049        private final Index<String,ServerEntry> idx;
050    
051    
052        public PresenceEvaluator( PresenceNode node, Store<ServerEntry> db, Registries registries )
053            throws Exception
054        {
055            this.db = db;
056            this.node = node;
057            this.registries = registries;
058            this.type = registries.getAttributeTypeRegistry().lookup( node.getAttribute() );
059    
060            if ( db.hasUserIndexOn( node.getAttribute() ) )
061            {
062                idx = db.getPresenceIndex();
063            }
064            else
065            {
066                idx = null;
067            }
068        }
069    
070    
071        public PresenceNode getExpression()
072        {
073            return node;
074        }
075    
076    
077        public AttributeType getAttributeType()
078        {
079            return type;
080        }
081    
082    
083        // TODO - determine if comaparator and index entry should have the Value
084        // wrapper or the raw normalized value
085        public boolean evaluate( IndexEntry<?,ServerEntry> indexEntry ) throws Exception
086        {
087            if ( idx != null )
088            {
089                return idx.forward( type.getOid(), indexEntry.getId() );
090            }
091    
092            ServerEntry entry = indexEntry.getObject();
093    
094            // resuscitate the entry if it has not been and set entry in IndexEntry
095            if ( null == entry )
096            {
097                entry = db.lookup( indexEntry.getId() );
098                indexEntry.setObject( entry );
099            }
100    
101            return evaluate( entry );
102        }
103    
104    
105        // TODO - determine if comaparator and index entry should have the Value
106        // wrapper or the raw normalized value
107        public boolean evaluate( Long id ) throws Exception
108        {
109            if ( idx != null )
110            {
111                return idx.forward( type.getOid(), id );
112            }
113    
114            return evaluate( db.lookup( id ) );
115        }
116    
117    
118        // TODO - determine if comaparator and index entry should have the Value
119        // wrapper or the raw normalized value
120        public boolean evaluate( ServerEntry entry ) throws Exception
121        {
122            // get the attribute
123            ServerAttribute attr = ( ServerAttribute ) entry.get( type );
124    
125            // if the attribute exists just return true
126            if ( attr != null )
127            {
128                return true;
129            }
130    
131            // If we do not have the attribute, loop through the sub classes of
132            // the attributeType.  Perhaps the entry has an attribute value of a
133            // subtype (descendant) that will produce a match
134            if ( registries.getAttributeTypeRegistry().hasDescendants( node.getAttribute() ) )
135            {
136                // TODO check to see if descendant handling is necessary for the
137                // index so we can match properly even when for example a name
138                // attribute is used instead of more specific commonName
139                Iterator<AttributeType> descendants =
140                    registries.getAttributeTypeRegistry().descendants( node.getAttribute() );
141    
142                do
143                {
144                    AttributeType descendant = descendants.next();
145    
146                    attr = ( ServerAttribute ) entry.get( descendant );
147    
148                    if ( attr != null )
149                    {
150                        return true;
151                    }
152                }
153                while ( descendants.hasNext() );
154            }
155    
156            // we fell through so a match was not found - assertion was false.
157            return false;
158        }
159    }