* @param selection the current selection
* @return a prototype search
*/
public static ISearch getExampleSearch( ISelection selection )
{
ISearch exampleSearch = new Search();
String oldName = exampleSearch.getSearchParameter().getName();
exampleSearch.getSearchParameter().setName( null );
exampleSearch.setScope( SearchScope.SUBTREE );
if ( selection != null && !selection.isEmpty() && selection instanceof StructuredSelection )
{
Object[] objects = ( ( IStructuredSelection ) selection ).toArray();
Comparator<Object> comparator = new Comparator<Object>()
{
public int compare( Object o1, Object o2 )
{
if ( ( o1 instanceof IValue ) && !( o2 instanceof IValue ) )
{
return -1;
}
else if ( !( o1 instanceof IValue ) && ( o2 instanceof IValue ) )
{
return 1;
}
else if ( ( o1 instanceof IAttribute ) && !( o2 instanceof IAttribute ) )
{
return -1;
}
else if ( !( o1 instanceof IAttribute ) && ( o2 instanceof IAttribute ) )
{
return 1;
}
else if ( ( o1 instanceof AttributeHierarchy ) && !( o2 instanceof AttributeHierarchy ) )
{
return -1;
}
else if ( !( o1 instanceof AttributeHierarchy ) && ( o2 instanceof AttributeHierarchy ) )
{
return 1;
}
return 0;
}
};
Arrays.sort( objects, comparator );
Object obj = objects[0];
if ( obj instanceof ISearch )
{
ISearch search = ( ISearch ) obj;
exampleSearch = ( ISearch ) search.clone();
exampleSearch.setName( null );
}
else if ( obj instanceof IEntry )
{
IEntry entry = ( IEntry ) obj;
exampleSearch.setBrowserConnection( entry.getBrowserConnection() );
exampleSearch.setSearchBase( entry.getDn() );
exampleSearch.setFilter( entry.getChildrenFilter() );
}
else if ( obj instanceof ISearchResult )
{
ISearchResult searchResult = ( ISearchResult ) obj;
exampleSearch.setBrowserConnection( searchResult.getEntry().getBrowserConnection() );
exampleSearch.setSearchBase( searchResult.getEntry().getDn() );
}
else if ( obj instanceof IBookmark )
{
IBookmark bookmark = ( IBookmark ) obj;
exampleSearch.setBrowserConnection( bookmark.getBrowserConnection() );
exampleSearch.setSearchBase( bookmark.getDn() );
}
else if ( obj instanceof AttributeHierarchy || obj instanceof IAttribute || obj instanceof IValue )
{
IEntry entry = null;
Set<String> filterSet = new LinkedHashSet<String>();
for ( int i = 0; i < objects.length; i++ )
{
Object object = objects[i];
if ( object instanceof AttributeHierarchy )
{
AttributeHierarchy ah = ( AttributeHierarchy ) object;
for ( IAttribute attribute : ah )
{
entry = attribute.getEntry();
IValue[] values = attribute.getValues();
for ( int v = 0; v < values.length; v++ )
{
filterSet.add( LdapFilterUtils.getFilter( values[v] ) );
}
}
}
else if ( object instanceof IAttribute )
{
IAttribute attribute = ( IAttribute ) object;
entry = attribute.getEntry();
IValue[] values = attribute.getValues();
for ( int v = 0; v < values.length; v++ )
{
filterSet.add( LdapFilterUtils.getFilter( values[v] ) );
}
}
else if ( object instanceof IValue )
{
IValue value = ( IValue ) object;
entry = value.getAttribute().getEntry();
filterSet.add( LdapFilterUtils.getFilter( value ) );
}
}
exampleSearch.setBrowserConnection( entry.getBrowserConnection() );
exampleSearch.setSearchBase( entry.getDn() );
StringBuffer filter = new StringBuffer();
if ( filterSet.size() > 1 )
{
filter.append( "(&" ); //$NON-NLS-1$
for ( Iterator<String> filterIterator = filterSet.iterator(); filterIterator.hasNext(); )
{
filter.append( filterIterator.next() );
}
filter.append( ")" ); //$NON-NLS-1$
}
else if ( filterSet.size() == 1 )
{
filter.append( filterSet.toArray()[0] );
}
else
{
filter.append( ISearch.FILTER_TRUE );
}
exampleSearch.setFilter( filter.toString() );
}
else if ( obj instanceof IBrowserConnection )
{
IBrowserConnection connection = ( IBrowserConnection ) obj;
exampleSearch.setBrowserConnection( connection );
if ( connection.getRootDSE().getChildrenCount() > 0 )
{
exampleSearch.setSearchBase( connection.getRootDSE().getChildren()[0].getDn() );
}
else
{
exampleSearch.setSearchBase( connection.getRootDSE().getDn() );
}
}
else if ( obj instanceof BrowserCategory )
{
BrowserCategory cat = ( BrowserCategory ) obj;
exampleSearch.setBrowserConnection( cat.getParent() );
if ( cat.getParent().getRootDSE().getChildrenCount() > 0 )
{
exampleSearch.setSearchBase( cat.getParent().getRootDSE().getChildren()[0].getDn() );
}
else
{
exampleSearch.setSearchBase( cat.getParent().getRootDSE().getDn() );
}
}
}
exampleSearch.getSearchParameter().setName( oldName );
return exampleSearch;
}