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