Question

This is a bit of a two part question.

I am using Tim Tripcony's Fancy XPage Typeahead script in a number of applications to return a typeahead list based on a number of different views in a specific database.

  1. Can the server side javascript listed in that blog be transformed into a java class to return the same results that can be picked up by the native typeahead functions in XPages.

  2. Can that class be part of an extension library that is deployed to all servers so it is available to all applications for immediate use and if so how would it be called from the XPage.

Was it helpful?

Solution

Yes, I can't think of an example of any SSJS that cannot be converted to Java, Here is Tim Tripcony's SSJS ported to Java.

import java.util.HashMap;
import java.util.Map;
import lotus.domino.*;
import com.ibm.domino.xsp.module.nsf.NotesContext;
public class TypeAhead {
    public static String directoryTypeAhead(String searchValue) {
        String returnList = "";
        try {

            Database directory = NotesContext.getCurrent().getCurrentSession().getDatabase("", "names.nsf");
            View allUsers = directory.getView("($Users)");
            Map<String, HashMap<String, String>> matches = new HashMap<String, HashMap<String, String>>();
                Map<String, String> individualMatches = new HashMap<String, String>();
            Map<String, Boolean> includeForm = new HashMap<String, Boolean>();
            includeForm.put("Person", Boolean.TRUE);
            includeForm.put("Group", Boolean.TRUE);
            ViewEntryCollection matchingEntries = allUsers.getAllEntriesByKey(searchValue, false);
            ViewEntry entry = matchingEntries.getFirstEntry();
            int resultCount = 0;
            while (entry != null) {
                Document matchDoc = entry.getDocument();
                String matchType = matchDoc.getItemValueString("Form");
                if ((Boolean)includeForm.get(matchType)) {
                    String fullName = matchDoc.getItemValue("FullName").elementAt(0).toString();
                    if (matches.get(fullName) == null) {
                        resultCount++;
                        Name matchName = NotesContext.getCurrent().getCurrentSession().createName(fullName);
                        individualMatches = new HashMap<String, String>();
                        individualMatches.put("cn", matchName.getCommon());
                        individualMatches.put("photo", matchDoc.getItemValueString("photoUrl"));
                        individualMatches.put("job", matchDoc.getItemValueString("jobTitle"));
                        individualMatches.put("email", matchDoc.getItemValueString("internetAddress"));
                        matches.put(fullName, (HashMap<String, String>) individualMatches);
                    }
                }
                if (resultCount > 9) {
                    entry = null;
                }
                else {
                    entry = matchingEntries.getNextEntry(entry);
                }
            }
            returnList = "<ul>";
            for (Map<String, String> match : matches.values()) {    
                String matchDetails = "<li><table><tr><td><img class=\"avatar\" src=\"" + match.get("photo") + "\"/></td><td valign=\"top\"><p><strong>" +  match.get("cn") + "</strong></p><p><span class=\"informal\">" + match.get("job") + "<br/>" +  match.get("email") + "</span></p></td></tr></table></li>";
                returnList += matchDetails;
            }
            returnList += "</ul>";
        } catch(Exception e) {
            System.out.println(e);
        }
        return returnList;
    }
}

As far as creating it in an extension library all you really have to do to get what I think you want is put it in a plugin Jar and create a feature and update site then you can use the new 8.5.3 functionality to replicate it out to all of your servers.

You might use this code by doing the following inside of your xpage:

<xp:inputText id="inputText1" value="#{viewScope.someVar}">
  <xp:typeAhead mode="partial" minChars="1" valueMarkup="true"
   var="searchValue"
   valueList="#{javascript:return com.tobysamples.demo.TypeAhead.directoryTypeAhead(searchValue);}">
  </xp:typeAhead></xp:inputText>

OTHER TIPS

I think it is not that hard to convert the SSJS to Java. With a managed bean you can immediately access the java method by the field. And should be a nice enhancement to the Extension Library, so everyone can benefit this cool typeahead

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top