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;
021
022
023import java.util.Set;
024
025import org.apache.directory.api.ldap.model.cursor.SetCursor;
026import org.apache.directory.api.ldap.model.filter.ExprNode;
027import org.apache.directory.api.ldap.model.message.AliasDerefMode;
028import org.apache.directory.api.ldap.model.schema.SchemaManager;
029import org.apache.directory.server.xdbm.IndexEntry;
030
031
032/**
033 * A class containing the result of a search :
034 * <ul>
035 * <li>A set of candidate UUIDs</li>
036 * <li>A set of aliased entry if we have any</li>
037 * <li>A flag telling if we are dereferencing aliases or not</li>
038 * <li>A hierarchy of evaluators to use to validate the candidates</li>
039 * </ul>
040 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
041 */
042public class PartitionSearchResult
043{
044    /** The set of candidate UUIDs selected by the search */
045    private SetCursor<IndexEntry<String, String>> resultSet;
046
047    /** The set of candidate UUIDs */
048    private Set<String> candidateSet;
049
050    /** The flag indicating if we are dereferencing the aliases. Default to Never. */
051    private AliasDerefMode aliasDerefMode = AliasDerefMode.NEVER_DEREF_ALIASES;
052
053    /** The evaluator to validate the candidates */
054    private Evaluator<? extends ExprNode> evaluator;
055
056    /** The SchemaManager */
057    private SchemaManager schemaManager;
058
059
060    /**
061     * Create a PartitionSearchResult instance
062     * 
063     * @param schemaManager The SchemaManager instance
064     */
065    public PartitionSearchResult( SchemaManager schemaManager )
066    {
067        this.schemaManager = schemaManager;
068    }
069
070
071    /**
072     * @return the resultSet
073     */
074    public SetCursor<IndexEntry<String, String>> getResultSet()
075    {
076        return resultSet;
077    }
078
079
080    /**
081     * @param set the resultSet to set
082     */
083    public void setResultSet( Set<IndexEntry<String, String>> set )
084    {
085        resultSet = new SetCursor<>( set );
086    }
087
088
089    /**
090     * @return the candidateSet
091     */
092    public Set<String> getCandidateSet()
093    {
094        return candidateSet;
095    }
096
097
098    /**
099     * @param set the candidateSet to set
100     */
101    public void setCandidateSet( Set<String> set )
102    {
103        candidateSet = set;
104    }
105
106
107    /**
108     * @return the evaluator
109     */
110    public Evaluator<? extends ExprNode> getEvaluator()
111    {
112        return evaluator;
113    }
114
115
116    /**
117     * @param evaluator the evaluator to set
118     */
119    public void setEvaluator( Evaluator<? extends ExprNode> evaluator )
120    {
121        this.evaluator = evaluator;
122    }
123
124
125    /**
126     * @param aliasDerefMode the aliasDerefMode to set
127     */
128    public void setAliasDerefMode( AliasDerefMode aliasDerefMode )
129    {
130        this.aliasDerefMode = aliasDerefMode;
131    }
132
133
134    /**
135     * @return True if the alias is never dereferenced
136     */
137    public boolean isNeverDeref()
138    {
139        return aliasDerefMode == AliasDerefMode.NEVER_DEREF_ALIASES;
140    }
141
142
143    /**
144     * @return True if the alias is always dereferenced
145     */
146    public boolean isDerefAlways()
147    {
148        return aliasDerefMode == AliasDerefMode.DEREF_ALWAYS;
149    }
150
151
152    /**
153     * @return True if the alias is dereferenced while searching
154     */
155    public boolean isDerefInSearching()
156    {
157        return aliasDerefMode == AliasDerefMode.DEREF_IN_SEARCHING;
158    }
159
160
161    /**
162     * @return True if the alias is dereferenced while finding
163     */
164    public boolean isDerefFinding()
165    {
166        return aliasDerefMode == AliasDerefMode.DEREF_FINDING_BASE_OBJ;
167    }
168
169
170    /**
171     * @return the schemaManager
172     */
173    public SchemaManager getSchemaManager()
174    {
175        return schemaManager;
176    }
177
178
179    /**
180     * @param schemaManager the schemaManager to set
181     */
182    public void setSchemaManager( SchemaManager schemaManager )
183    {
184        this.schemaManager = schemaManager;
185    }
186
187
188    /**
189     * @see Object#toString()
190     */
191    public String toString()
192    {
193        StringBuilder sb = new StringBuilder();
194
195        sb.append( "Search result : \n" );
196        sb.append( "Alias : " ).append( aliasDerefMode ).append( "\n" );
197        sb.append( "Evaluator : " ).append( evaluator ).append( "\n" );
198
199        if ( resultSet == null )
200        {
201            sb.append( "No UUID found" );
202        }
203        else
204        {
205            sb.append( '{' );
206            boolean isFirst = true;
207
208            try
209            {
210                while ( resultSet.next() )
211                {
212                    if ( isFirst )
213                    {
214                        isFirst = false;
215                    }
216                    else
217                    {
218                        sb.append( ", " );
219                    }
220
221                    sb.append( resultSet.get().getId() );
222                }
223
224                resultSet.beforeFirst();
225            }
226            catch ( Exception e )
227            {
228                // Nothing we can do...
229            }
230
231            sb.append( '}' );
232        }
233
234        return sb.toString();
235    }
236}