Skip to content

Instantly share code, notes, and snippets.

@lpar
Created January 5, 2016 22:55
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save lpar/85a00ca286942cd164d3 to your computer and use it in GitHub Desktop.
Domino XPages directory name picker using standard Directory API, compatible with Directory Assistance Raw
/*
* © Copyright IBM Corp. 2016
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.ibm.us.meta;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import lotus.domino.Directory;
import lotus.domino.DirectoryNavigator;
import lotus.domino.NotesException;
import lotus.domino.Session;
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;
import com.ibm.xsp.model.domino.DominoUtils;
public class DirectoryNamePicker extends ValueBindingObjectImpl implements INamePickerData {
public String[] getSourceLabels () {
// TODO Auto-generated method stub
return null;
}
public boolean hasCapability (final int arg0) {
// TODO Auto-generated method stub
return false;
}
public List<IPickerEntry> loadEntries (final Object[] arg0, final String[] arg1) {
// TODO Auto-generated method stub
return null;
}
public IPickerResult readEntries (final IPickerOptions options) {
String startKey = options.getStartKey();
int count = options.getCount();
List<IPickerEntry> entries = new ArrayList<IPickerEntry>();
if (startKey != null) {
// User is performing a search
try {
entries = this.dirLookup(startKey, count);
} catch (NotesException e) {
System.err.println("Exception trying to perform directory lookup: " + e.getMessage());
e.printStackTrace();
}
}
return new SimplePickerResult(entries, -1);
}
private final static Pattern nameAbbreviate = Pattern.compile("((O|OU)=|^CN=)");
public ArrayList<IPickerEntry> dirLookup(final String search, final int limit) throws NotesException {
ArrayList<IPickerEntry> result = new ArrayList<IPickerEntry>();
Session sess = DominoUtils.getCurrentSession();
Directory dir = sess.getDirectory();
dir.setSearchAllDirectories(true);
Vector<String> itemsToFetch = new Vector<String>();
itemsToFetch.add("FullName");
Vector<String> namesToLookup = new Vector<String>();
namesToLookup.add(search);
DirectoryNavigator dirnav = dir.lookupNames("($Users)", namesToLookup, itemsToFetch, true);
int count = 0;
while (dirnav.isNameLocated()) {
while (dirnav.isMatchLocated() && count < limit) {
Vector<String> iv = dirnav.getFirstItemValue();
String notesid = iv.get(0);
Matcher m = nameAbbreviate.matcher(notesid);
String val = m.replaceAll("");
IPickerEntry entry = new SimplePickerResult.Entry(val, val);
result.add(entry);
dirnav.findNextMatch();
count += 1;
}
dirnav.findNextName();
}
dirnav.recycle();
dir.recycle();
sess.recycle();
return result;
}
}
<?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:this.data>
<xp:dominoDocument var="document1"></xp:dominoDocument>
</xp:this.data>
<xp:label for="person1">Name field with lookup dialog</xp:label>
<br />
<xp:inputText id="person1" value="#{document1.Person1}" multipleSeparator=","
multipleTrim="true" size="80">
</xp:inputText>
<xe:namePicker id="namePicker1" for="person1">
<xe:this.dataProvider>
<xe:beanNamePicker dataBean="com.ibm.us.meta.DirectoryNamePicker"
loaded="true">
</xe:beanNamePicker>
</xe:this.dataProvider>
</xe:namePicker>
</xp:view>
@PatrickKwinten
Copy link

awesome stuff!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment