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.server.xdbm.search.evaluator;
021
022
023import org.apache.directory.api.ldap.model.entry.Entry;
024import org.apache.directory.api.ldap.model.exception.LdapException;
025import org.apache.directory.api.ldap.model.filter.ScopeNode;
026import org.apache.directory.server.core.api.partition.PartitionTxn;
027import org.apache.directory.server.i18n.I18n;
028import org.apache.directory.server.xdbm.IndexEntry;
029import org.apache.directory.server.xdbm.Store;
030import org.apache.directory.server.xdbm.search.Evaluator;
031
032
033/**
034 * Evaluates base level scope assertions on candidates using an entry database.
035 * 
036 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
037 */
038public class BaseLevelScopeEvaluator<E> implements Evaluator<ScopeNode>
039{
040    /** The ScopeNode containing initial search scope constraints */
041    private final ScopeNode node;
042
043    /** The entry identifier of the scope base */
044    private final String baseId;
045
046    /** True if the scope requires alias dereferencing while searching */
047    private final boolean dereferencing;
048
049    /** the entry db storing entries */
050    private final Store db;
051
052
053    /**
054     * Creates a one level scope node Evaluator for search expressions.
055     *
056     * @param db the database used to evaluate scope node
057     * @param node the scope node
058     */
059    public BaseLevelScopeEvaluator( Store db, ScopeNode node )
060    {
061        this.node = node;
062
063        this.db = db;
064        baseId = node.getBaseId();
065        dereferencing = node.getDerefAliases().isDerefInSearching() || node.getDerefAliases().isDerefAlways();
066    }
067
068
069    /**
070     * Asserts whether or not a candidate has one level scope while taking
071     * alias dereferencing into account.
072     *
073     * TODO - terribly inefficient - would benefit from exposing the id of an
074     * entry within the Entry
075     *
076     * {@inheritDoc}
077     */
078    public boolean evaluate( Entry candidate ) throws LdapException
079    {
080        throw new UnsupportedOperationException( I18n.err( I18n.ERR_721 ) );
081    }
082
083
084    /**
085     * {@inheritDoc}
086     */
087    @Override
088    public boolean evaluate( PartitionTxn partitionTxn, IndexEntry<?, String> indexEntry ) throws LdapException
089    {
090        Entry entry = indexEntry.getEntry();
091
092        // Fetch the entry
093        if ( null == entry )
094        {
095            entry = db.fetch( partitionTxn, indexEntry.getId() );
096
097            if ( null == entry )
098            {
099                // The entry is not anymore present : get out
100                return false;
101            }
102
103            indexEntry.setEntry( entry );
104        }
105
106        return true;
107    }
108
109
110    public ScopeNode getExpression()
111    {
112        return node;
113    }
114
115
116    /**
117     * Gets the id of the search base associated with the ScopeNode expression.
118     *
119     * @return identifier of the search base
120     */
121    public String getBaseId()
122    {
123        return baseId;
124    }
125
126
127    /**
128     * Gets whether or not dereferencing is enabled for this evaluator.
129     *
130     * @return true if dereferencing is enabled, false otherwise
131     */
132    public boolean isDereferencing()
133    {
134        return dereferencing;
135    }
136
137
138    /**
139     * @see Object#toString()
140     */
141    public String toString( String tabs )
142    {
143        StringBuilder sb = new StringBuilder();
144
145        sb.append( tabs ).append( "BaseLevelScopEvaluator : " ).append( node ).append( "\n" );
146
147        return sb.toString();
148    }
149
150
151    /**
152     * @see Object#toString()
153     */
154    public String toString()
155    {
156        return toString( "" );
157    }
158}