001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2006-2008 Sun Microsystems, Inc.
025 *      Portions Copyright 2014-2015 ForgeRock AS
026 */
027package org.opends.server.tools;
028
029import static org.forgerock.opendj.ldap.DereferenceAliasesPolicy.*;
030import static org.forgerock.opendj.ldap.SearchScope.*;
031import static org.opends.messages.ToolMessages.*;
032
033import static com.forgerock.opendj.cli.Utils.*;
034
035import java.io.PrintStream;
036
037import org.forgerock.opendj.ldap.DereferenceAliasesPolicy;
038import org.forgerock.opendj.ldap.SearchScope;
039
040
041
042/**
043 * This class defines options for the search operations used
044 * by the ldapsearch tool.
045 */
046public class LDAPSearchOptions extends LDAPToolOptions
047{
048
049  private DereferenceAliasesPolicy dereferencePolicy =  NEVER;
050  private SearchScope searchScope = WHOLE_SUBTREE;
051  private int sizeLimit;
052  private int timeLimit;
053  private boolean typesOnly;
054  private boolean countMatchingEntries;
055
056  /**
057   * Creates the options instance.
058   */
059  public LDAPSearchOptions()
060  {
061  }
062
063  /**
064   * Set the timeLimit for the operation.
065   *
066   * @param timeLimit    The time limit for the search.
067   */
068  public void setTimeLimit(int timeLimit)
069  {
070    this.timeLimit = timeLimit;
071  }
072
073  /**
074   * Return the timeLimit value.
075   *
076   * @return  The timeLimit value.
077   */
078  public int getTimeLimit()
079  {
080    return timeLimit;
081  }
082
083  /**
084   * Set the sizeLimit for the operation.
085   *
086   * @param sizeLimit    The size limit for the search.
087   *
088   */
089
090  public void setSizeLimit(int sizeLimit)
091  {
092    this.sizeLimit = sizeLimit;
093  }
094
095  /**
096   * Return the sizeLimit value.
097   *
098   * @return  The sizeLimit value.
099   */
100  public int getSizeLimit()
101  {
102    return sizeLimit;
103  }
104
105  /**
106   * Set the search scope .
107   *
108   * @param  scope  The search scope string.
109   * @param  err    A print stream to which error messages should be written if
110   *                a problem occurs.
111   *
112   * @return  <CODE>true</CODE> if the scope was set properly, or
113   *          <CODE>false</CODE> if not.
114   */
115
116  public boolean setSearchScope(String scope, PrintStream err)
117  {
118      if(scope == null)
119      {
120        searchScope = WHOLE_SUBTREE;
121      }
122      else if(scope.equalsIgnoreCase("base"))
123      {
124        searchScope = BASE_OBJECT;
125      } else if(scope.equalsIgnoreCase("one"))
126      {
127        searchScope = SINGLE_LEVEL;
128      } else if (scope.equalsIgnoreCase("sub"))
129      {
130        searchScope = WHOLE_SUBTREE;
131      } else if (scope.equalsIgnoreCase("subordinate"))
132      {
133        searchScope = SUBORDINATES;
134      } else
135      {
136        printWrappedText(err, ERR_SEARCH_INVALID_SEARCH_SCOPE.get(scope));
137        return false;
138      }
139      return true;
140  }
141
142  /**
143   * Get the search scope value.
144   *
145   * @return  The search scope value.
146   */
147  public SearchScope getSearchScope()
148  {
149    return searchScope;
150  }
151
152  /**
153   * Set the dereference policy.
154   *
155   * @param policy  The dereference policy.
156   * @param  err    A print stream to which error messages should be written if
157   *                a problem occurs.
158   *
159   * @return  <CODE>true</CODE> if the dereference policy was set properly, or
160   *          <CODE>false</CODE> if not.
161   */
162
163  public boolean setDereferencePolicy(String policy, PrintStream err)
164  {
165      if(policy == null)
166      {
167        dereferencePolicy = NEVER;
168      } else if(policy.equals("never"))
169      {
170        dereferencePolicy = NEVER;
171      } else if(policy.equals("always"))
172      {
173        dereferencePolicy = ALWAYS;
174      } else if (policy.equals("search"))
175      {
176        dereferencePolicy = IN_SEARCHING;
177      } else if (policy.equals("find"))
178      {
179        dereferencePolicy = FINDING_BASE;
180      } else
181      {
182        printWrappedText(err, ERR_SEARCH_INVALID_DEREFERENCE_POLICY.get(policy));
183        return false;
184      }
185      return true;
186  }
187
188  /**
189   * Return the dereference policy.
190   *
191   * @return  The alias dereference policy.
192   */
193  public DereferenceAliasesPolicy getDereferencePolicy()
194  {
195    return dereferencePolicy;
196  }
197
198  /**
199   * Return only the attribute types in the search result.
200   *
201   * @return  <CODE>true</CODE> if only attribute types should be returned in
202   *          matching entries, or <CODE>false</CODE> if both types and values
203   *          should be included.
204   */
205  public boolean getTypesOnly()
206  {
207    return this.typesOnly;
208  }
209
210
211  /**
212   * Return only the attribute types in the search result.
213   *
214   * @param  typesOnly  Specifies whether only attribute types should be
215   *                    returned in matching entries, or both types and values.
216   */
217  public void setTypesOnly(boolean typesOnly)
218  {
219    this.typesOnly = typesOnly;
220  }
221
222
223  /**
224   * Indicates whether to report the number of matching entries returned by the
225   * server.
226   *
227   * @return  {@code true} if the number of matching entries should be reported,
228   *          or {@code false} if not.
229   */
230  public boolean countMatchingEntries()
231  {
232    return countMatchingEntries;
233  }
234
235
236  /**
237   * Specifies whether to report the number of matching entries returned by the
238   * server.
239   *
240   * @param  countMatchingEntries  Specifies whether to report the number of
241   *                               matching entries returned by the server.
242   */
243  public void setCountMatchingEntries(boolean countMatchingEntries)
244  {
245    this.countMatchingEntries = countMatchingEntries;
246  }
247}
248