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