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;
    }
}
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.