/*
* $Id: AnySearchControls.java,v 1.7 2002/09/16 08:05:03 jkl Exp $
*
* Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
*
* Use is subject to license terms, as defined in
* Anvil Sofware License, Version 1.1. See LICENSE
* file, or http://njet.org/license-1.1.txt
*/
package anvil.core.naming;
import javax.naming.directory.SearchControls;
import anvil.core.Any;
import anvil.core.AnyAbstractClass;
import anvil.core.AnyMap;
import anvil.core.AnyList;
import anvil.core.AnyUtils;
import anvil.script.Context;
import anvil.core.arrays.AnyObjectArray;
/// @class SearchControls
/// This class encapsultes factors that determine scope of search
/// and what gets returned as a result of the search.
/**
* class AnySearchControls
*
* @author: Simo Tuokko
* @author: Jani Lehtim�ki
*/
public class AnySearchControls extends AnyAbstractClass
{
/// @constructor SearchControls
/// Creates search controller.
/// @synopsis SearchControls(flags...)
/// @param flags Zero or more of following expressions:
/// <dl>
/// <dt><code>"scope" => typeOfScope</code></dt>
/// <dd>OBJECT_SCOPE, ONELEVEL_SCOPE, SUBTREE_SCOPE</dd>
/// <dt><code>"countlimit" => limit</code></dt>
/// <dd>Max number of entries to return</dd>
/// <dt><code>"timelimit" => limit</code></dt>
/// <dd>Timelimit, in milliseconds</dd>
/// <dt><code>"attributes" => sequence</code></dt>
/// <dd>Sequence of names of attributes to return</dd>
/// <dt><code>"returnobject" => flag</code></dt>
/// <dd>Should the objects be returned as part of result</dd>
/// <dt><code>"dereflinks" => flag</code></dt>
/// <dd>Should the links be dereference during the search</dd>
/// <dt><code>"returnobject"</code></dt>
/// <dd>Same as <code>"returnObject" => true</code></dd>
/// <dt><code>"dereflinks"</code></dt>
/// <dd>Same as <code>"dereflinks" => true</code></dd>
/// <dt><code>sequence</code></dt>
/// <dd>Sequence of name of attributes to return</dd>
/// </dl>
public static final Any newInstance(anvil.script.Context context, Any[] parameters)
{
SearchControls controls = new SearchControls();
int n = parameters.length;
for(int i=0; i<n; i++) {
Any property = parameters[i];
if (property.isMap()) {
AnyMap map = property.toMap();
String key = map.getLeft().toString();
if (key.equalsIgnoreCase("scope")) {
controls.setSearchScope(map.getRight().toInt());
} else if (key.equalsIgnoreCase("countlimit")) {
controls.setCountLimit(map.getRight().toInt());
} else if (key.equalsIgnoreCase("timelimit")) {
controls.setTimeLimit(map.getRight().toInt());
} else if (key.equalsIgnoreCase("attributes")) {
Any right = map.getRight();
if (right.isNull()) {
controls.setReturningAttributes(null);
} else {
String[] attributes = AnyUtils.toStringArray(right);
controls.setReturningAttributes(attributes);
}
} else if (key.equalsIgnoreCase("returnobject")) {
controls.setReturningObjFlag(map.getRight().toBoolean());
} else if (key.equalsIgnoreCase("dereflinks")) {
controls.setDerefLinkFlag(map.getRight().toBoolean());
} else {
throw context.BadParameter("Invalid attribute: '"+key+"'");
}
} else if (property.isString()) {
String key = property.toString();
if (key.equalsIgnoreCase("returnobject")) {
controls.setReturningObjFlag(true);
} else if (key.equalsIgnoreCase("dereflinks")) {
controls.setDerefLinkFlag(true);
} else {
throw context.BadParameter("Invalid attribute: '"+key+"'");
}
} else if (property.isSequence() || property.isArray()) {
String[] attributes = AnyUtils.toStringArray(property);
controls.setReturningAttributes(attributes);
} else {
throw context.BadParameter("Invalid property: '"+property+"'");
}
}
return new AnySearchControls(controls);
}
public static final anvil.script.compiler.NativeClass __class__ =
new anvil.script.compiler.NativeClass("SearchControls", AnySearchControls.class,
//DOC{{
""+
" @class SearchControls\n" +
" This class encapsultes factors that determine scope of search \n" +
" and what gets returned as a result of the search. \n" +
" @constructor SearchControls\n" +
" Creates search controller.\n" +
" @synopsis SearchControls(flags...)\n" +
" @param flags Zero or more of following expressions:\n" +
" <dl>\n" +
" <dt><code>\"scope\" => typeOfScope</code></dt>\n" +
" <dd>OBJECT_SCOPE, ONELEVEL_SCOPE, SUBTREE_SCOPE</dd>\n" +
" <dt><code>\"countlimit\" => limit</code></dt>\n" +
" <dd>Max number of entries to return</dd>\n" +
" <dt><code>\"timelimit\" => limit</code></dt>\n" +
" <dd>Timelimit, in milliseconds</dd>\n" +
" <dt><code>\"attributes\" => sequence</code></dt>\n" +
" <dd>Sequence of names of attributes to return</dd>\n" +
" <dt><code>\"returnobject\" => flag</code></dt>\n" +
" <dd>Should the objects be returned as part of result</dd>\n" +
" <dt><code>\"dereflinks\" => flag</code></dt>\n" +
" <dd>Should the links be dereference during the search</dd>\n" +
" <dt><code>\"returnobject\"</code></dt>\n" +
" <dd>Same as <code>\"returnObject\" => true</code></dd>\n" +
" <dt><code>\"dereflinks\"</code></dt>\n" +
" <dd>Same as <code>\"dereflinks\" => true</code></dd>\n" +
" <dt><code>sequence</code></dt>\n" +
" <dd>Sequence of name of attributes to return</dd>\n" +
" </dl>\n" +
" @method getReturningAttributes\n" +
" Retrieves the attributes that will be returned as part of the search.\n" +
" @synopsis StringArray getReturningAttributes()\n" +
" @method getDerefLinks\n" +
" Determines whether links will be dereferenced during the search.\n" +
" @synopsis boolean getDerefLinks()\n" +
" @method getReturnObject\n" +
" Determines whether objects will be returned as part of the result.\n" +
" @synopsis boolean getReturnObject()\n" +
" @method getSearchScope\n" +
" Returns the scope of search.\n" +
" @synopsis int getSearchScope()\n" +
" @method getCountLimit\n" +
" Retrieves the maximum number of entries that will be returned as a result of the search.\n" +
" @synopsis int getCountLimit()\n" +
" @method getTimeLimit\n" +
" Retrieves the time limit of these Controls in milliseconds.\n" +
" @synopsis int getTimeLimit()\n" +
" @method setReturningAttributes\n" +
" Specifies the attributes that will be returned as part of the search.\n" +
" <code>null</code> indicates that all attributes will be returned.\n" +
" An empty sequence indicates no attributes are returned.\n" +
" @synopsis SearchControls setReturningAttributes()\n" +
" @synopsis SearchControls setReturningAttributes(sequence attributes)\n" +
" @method setDerefLinks\n" +
" Enables/disables link dereferencing during the search.\n" +
" @synopsis SearchControls setDerefLinks(boolean flag)\n" +
" @method setReturnObject\n" +
" Enables/disables returning objects returned as part of the result.\n" +
" @synopsis SearchControls setReturnObject(boolean flag)\n" +
" @method setSearchScope\n" +
" Sets the search scope.\n" +
" @synopsis SearchControls setSearchScope(int scope)\n" +
" @param scope One of <code>OBJECT_SCOPE, ONLEVEL_SCOPE, SUBTREE_SCOPE</code>\n" +
" @method setCountLimit\n" +
" Sets the maximum number of entries to be returned as\n" +
" a result of search.\n" +
" @synopsis SearchControls setCountLimit(int limit)\n" +
" @method setTimeLimit\n" +
" Sets the time limit for searches.\n" +
" @synopsis SearchControls setTimeLimit(int limit)\n" +
" @param limit Limit, in milliseconds\n"
//}}DOC
);
static {
NamingModule.class.getName();
}
private SearchControls _controls;
public AnySearchControls(SearchControls searchControls)
{
_controls = searchControls;
}
public anvil.script.ClassType classOf()
{
return __class__;
}
public Object toObject()
{
return _controls;
}
public String toString()
{
StringBuffer buffer = new StringBuffer();
buffer.append("SearchControls(");
buffer.append("scope=");
int scope = _controls.getSearchScope();
switch(scope) {
case SearchControls.OBJECT_SCOPE:
buffer.append("OBJECT");
break;
case SearchControls.ONELEVEL_SCOPE:
buffer.append("ONELEVEL");
break;
case SearchControls.SUBTREE_SCOPE:
buffer.append("SUBTREE");
break;
default:
buffer.append("INVALID");
}
buffer.append(", countlimit=");
buffer.append(_controls.getCountLimit());
buffer.append(", timelimit=");
buffer.append(_controls.getTimeLimit());
buffer.append(", attributes=");
String[] attrs = _controls.getReturningAttributes();
if (attrs != null) {
buffer.append('{');
int n = attrs.length;
for(int i=0; i<n; i++) {
if (i>0) {
buffer.append(", ");
}
buffer.append(attrs[i]);
}
buffer.append('}');
} else {
buffer.append("*");
}
buffer.append(", returnobject=");
buffer.append(_controls.getReturningObjFlag());
buffer.append(", dereflinks=");
buffer.append(_controls.getDerefLinkFlag());
buffer.append(')');
return buffer.toString();
}
public Any checkAttribute(anvil.script.Context context, String attribute)
{
return getAttribute(context, attribute);
}
public Any getReference(anvil.script.Context context, Any index)
{
return UNDEFINED;
}
public Any checkReference(anvil.script.Context context, Any index)
{
return UNDEFINED;
}
/// @method getReturningAttributes
/// Retrieves the attributes that will be returned as part of the search.
/// @synopsis StringArray getReturningAttributes()
public Any m_getReturningAttributes(anvil.script.Context context, Any[] parameters)
{
return Any.create(_controls.getReturningAttributes());
}
/// @method getDerefLinks
/// Determines whether links will be dereferenced during the search.
/// @synopsis boolean getDerefLinks()
public Any m_getDerefLinks(anvil.script.Context context, Any[] parameters)
{
return Any.create(_controls.getDerefLinkFlag());
}
/// @method getReturnObject
/// Determines whether objects will be returned as part of the result.
/// @synopsis boolean getReturnObject()
public Any m_getReturnObject(anvil.script.Context context, Any[] parameters)
{
return Any.create(_controls.getReturningObjFlag());
}
/// @method getSearchScope
/// Returns the scope of search.
/// @synopsis int getSearchScope()
public Any m_getSearchScope(anvil.script.Context context, Any[] parameters)
{
return Any.create(_controls.getSearchScope());
}
/// @method getCountLimit
/// Retrieves the maximum number of entries that will be returned as a result of the search.
/// @synopsis int getCountLimit()
public Any m_getCountLimit(anvil.script.Context context, Any[] parameters)
{
return Any.create(_controls.getCountLimit());
}
/// @method getTimeLimit
/// Retrieves the time limit of these Controls in milliseconds.
/// @synopsis int getTimeLimit()
public Any m_getTimeLimit(anvil.script.Context context, Any[] parameters)
{
return Any.create( _controls.getTimeLimit() );
}
/// @method setReturningAttributes
/// Specifies the attributes that will be returned as part of the search.
/// <code>null</code> indicates that all attributes will be returned.
/// An empty sequence indicates no attributes are returned.
/// @synopsis SearchControls setReturningAttributes()
/// @synopsis SearchControls setReturningAttributes(sequence attributes)
public Any m_setReturningAttributes(anvil.script.Context context, Any[] parameters)
{
if (parameters.length < 1) {
_controls.setReturningAttributes(null);
} else {
_controls.setReturningAttributes(AnyUtils.toStringArray(parameters[0]));
}
return this;
}
/// @method setDerefLinks
/// Enables/disables link dereferencing during the search.
/// @synopsis SearchControls setDerefLinks(boolean flag)
public Any m_setDerefLinks(anvil.script.Context context, Any[] parameters)
{
if (parameters.length < 1) {
throw parametersMissing(context, "setDerefLinks");
}
_controls.setDerefLinkFlag(parameters[0].toBoolean());
return this;
}
/// @method setReturnObject
/// Enables/disables returning objects returned as part of the result.
/// @synopsis SearchControls setReturnObject(boolean flag)
public Any m_setReturnObject(anvil.script.Context context, Any[] parameters)
{
if (parameters.length < 1) {
throw parametersMissing(context, "setReturnObject");
}
_controls.setReturningObjFlag(parameters[0].toBoolean());
return this;
}
/// @method setSearchScope
/// Sets the search scope.
/// @synopsis SearchControls setSearchScope(int scope)
/// @param scope One of <code>OBJECT_SCOPE, ONLEVEL_SCOPE, SUBTREE_SCOPE</code>
public Any m_setSearchScope(anvil.script.Context context, Any[] parameters)
{
if (parameters.length < 1) {
throw parametersMissing(context, "setSearchScope");
}
_controls.setSearchScope(parameters[0].toInt());
return this;
}
/// @method setCountLimit
/// Sets the maximum number of entries to be returned as
/// a result of search.
/// @synopsis SearchControls setCountLimit(int limit)
public Any m_setCountLimit(anvil.script.Context context, Any[] parameters)
{
if (parameters.length < 1) {
throw parametersMissing(context, "setCountLimit");
}
_controls.setCountLimit(parameters[0].toLong());
return this;
}
/// @method setTimeLimit
/// Sets the time limit for searches.
/// @synopsis SearchControls setTimeLimit(int limit)
/// @param limit Limit, in milliseconds
public Any m_setTimeLimit(anvil.script.Context context, Any[] parameters)
{
if (parameters.length < 1) {
throw parametersMissing(context, "setTimeLimit");
}
_controls.setTimeLimit(parameters[0].toInt());
return this;
}
}