Sample about NamePicker Data Provider (dataBean)

// Demo XPages - Name : xspSelectName

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core" xmlns:xe="http://www.ibm.com/xsp/coreex">
	<xp:panel style="padding:100px;">
		<xp:panel style="padding:10px; height:50px; width:100%; border:1px solid #000">
			<xe:namePicker
				id="namePicker1"
				pickerText="Select User"
				for="djName">
				<xe:this.dataProvider>
					<xe:beanNamePicker
						dataBean="org.openntf.ferhat.DominoDirectoryNamePicker"
						loaded="true">
					</xe:beanNamePicker>
				</xe:this.dataProvider>
			</xe:namePicker>
			<xp:br></xp:br>
			<xp:br></xp:br>
			<xe:djextNameTextBox
				id="djName"
				multipleSeparator=","
				multipleTrim="true">
			</xe:djextNameTextBox>
		</xp:panel>
	</xp:panel>
</xp:view>

// Class
// Package : org.openntf.ferhat
// Name : DominoDirectoryNamePicker

/**
 * DominoDirectoryNamePicker V2.0 (Updated at May 31, 2012 12:09 AM)
 * 
 * Author : Ferhat BULUT
 * Email  : ferhat@bestcoder.net
 * Blog   : http://www.bestcoder.net
 * 
 * This Java class generates custom data for ExtLib NamePicker after search.
 * 
 */
package org.openntf.ferhat;

import java.util.ArrayList;
import java.util.List;

import javax.faces.context.FacesContext;

import lotus.domino.Database;
import lotus.domino.Document;
import lotus.domino.DocumentCollection;
import lotus.domino.NotesException;

import com.ibm.commons.util.QuickSort;
import com.ibm.commons.util.StringUtil;
import com.ibm.xsp.FacesExceptionEx;
import com.ibm.xsp.complex.ValueBindingObjectImpl;
import com.ibm.xsp.extlib.component.picker.data.INamePickerData;
import com.ibm.xsp.extlib.component.picker.data.IPickerEntry;
import com.ibm.xsp.extlib.component.picker.data.IPickerOptions;
import com.ibm.xsp.extlib.component.picker.data.IPickerResult;
import com.ibm.xsp.extlib.component.picker.data.SimplePickerResult;

public class DominoDirectoryNamePicker extends ValueBindingObjectImpl implements INamePickerData {

	static String oldStartKey = "";
	static String[] entryList = null;

	public DominoDirectoryNamePicker() {
		//
	}
	
	public IPickerResult readEntries(IPickerOptions options) {
		List<IPickerEntry> entries = new ArrayList<IPickerEntry>();
		String startKey = options.getStartKey();
		int i = 0;

		System.out.println("DominoDirectoryNamePicker > readEntries >>> startKey:" + startKey);

		if (startKey != null && !startKey.equals(oldStartKey)) {
			
			try {
				FacesContext context = FacesContext.getCurrentInstance();
				Database currentDatabase = com.ibm.xsp.model.domino.DominoUtils.getCurrentDatabase(context);
				if (currentDatabase.isFTIndexed()) {
					String ftSearchKey = "[Form] = \"Contact\" AND ([FirstName] CONTAINS \"" + startKey + "\" OR [LastName] CONTAINS \"" + startKey + "\")";
					DocumentCollection docCollection = currentDatabase.FTSearch(ftSearchKey, 50, Database.FT_SCORES, Database.FT_DATABASE | Database.FT_FUZZY);
					
					if (docCollection.getCount() > 0) {
						oldStartKey = startKey;
						entryList = new String[docCollection.getCount()];
						i = 0;
						Document contactDoc = docCollection.getFirstDocument();
						while (contactDoc != null) {
							String label = contactDoc.getItemValueString("FirstName") + " " + contactDoc.getItemValueString("LastName");
							String value = contactDoc.getItemValueString("FirstName") + " " + contactDoc.getItemValueString("LastName") + " (" + contactDoc.getItemValueString("EMail") + ")";
							entryList[i] = label + "~" + value;
							Document tmpDoc = docCollection.getNextDocument(contactDoc);
							contactDoc.recycle();
							contactDoc = tmpDoc;
							i++;
						}
						
						(new QuickSort.StringArray(entryList) {
							@Override
		                    public int compare(String arg0, String arg1) {
								return StringUtil.compareToIgnoreCase(arg0, arg1);
		                    }
						}).sort();
						
						for(i = 0; i < entryList.length; i++) {
							String[] entryValues = entryList[i].split("~");
							entries.add(new SimplePickerResult.Entry(entryValues[1], entryValues[0]));
						}
					}
					docCollection.recycle();
				}
				
			} catch (NotesException e) {
				throw new FacesExceptionEx("NotesException - " + e.getMessage());
			}
		} else {
			if (entryList != null) {
				entries.clear();
				for(i = 0; i < entryList.length; i++) {
					String[] entryValues = entryList[i].split("~");
					IPickerEntry pickerEntry = new SimplePickerResult.Entry(entryValues[1], entryValues[0]);
					entries.add(pickerEntry);
				}
			}
		}
		
		return new SimplePickerResult(entries,-1);
	}

	public String[] getSourceLabels() {
		return null;
	}
	
	public boolean hasCapability(int capability) {
		return false;
	}
	
	public List<IPickerEntry> loadEntries(Object[] ids, String[] attributeNames) {
	    return null;
    }
}





DominoDirectoryNamePicker V2.0 (Updated at May 31, 2012 12:09 AM)

Updated some codes about Dialog usage. There is a problem for "hit to ENTER key" to make search. Hit to ENTER, values duplicated because readEntries function executed twice. When you click on Search Button, no problem.

Updates are about, when you search same value twice, to fill list with same values. etc. I'm sure, you can handle it :)

----

DominoDirectoryNamePicker V1.0

If you need to customize NamePicker data, this XSnippet can help you to do that.

I needed to use ExtLib NamePicker but our user directory is not domino directory db. It's custom database. Also we need to add search capability for First Name, Middle Name and Last Name at once.

So I need to customize NamePicker data source using bean. ( tried, tested, worked :) )

In this XSnippet, I used Full-Text search in our organization database to match users by FirstName, Middle Name and Last Name. At one search. Used nameAggregator but no success unfortunately. Because I need to use "CN (ID)" format as a value for selected items.

I hope it an help your needs.

For sample database; http://www.bestcoder.net/files/NamePickerExt.zip

Enjoy!

Ferhat BULUT
www.bestcoder.net

Java
Ferhat BULUT
May 30, 2012 4:46 PM
Rating
569

All code submitted to OpenNTF XSnippets, whether submitted as a "Snippet" or in the body of a Comment, is provided under the Apache License Version 2.0. See Terms of Use for full details.



1 comment(s)Login first to comment...
Ferhat BULUT
(at 16:48 on 30.05.2012)
I forgot to notice; Used XPagesExt.nsf Design (contact form & view) & Sample Data to create sample database.