Working with JSON in your XPages application (3) – getEntriesByKey

Introduction

In our third example we will extend the loadPictures method in the first blog entry in this serie to detect whether or not we want to load just all the entries in a view or entries that match a key in the first sorted column.

Use case

In the following simple scenario a user makes an selection (e.g. from a combobox) and corresponding picture documents will be displayed, as in the image:

sample3json

Whenever a different value is selected the list with corresponding documents will be updated.

So how do we do this?

Switch between Notes views

The main difference is the key send if we do or don’t (empty value) want a filtering of the result list. To collect this key we provide a combobox:

</div>

The loadCategories method provides an ArrayList of String values:

public ArrayList<String> loadCategories(String ViewName) throws NotesException{
System.out.println(appRef + “.loadCategories()”);

NotesContext nct = NotesContext.getCurrent();
Session session = nct.getCurrentSession();

String ServerName = “dev1”;
String DatabaseName = session.getCurrentDatabase().getFilePath();
Database DB = session.getDatabase(ServerName, DatabaseName);

View luView = DB.getView(ViewName);
ViewNavigator vwnav = null;
ViewEntry vwentry = null;
ViewEntry vwentrytmp = null;

// all results will be added to this
ArrayList<String> categories = new ArrayList<String>();

// disable autoupdate
luView.setAutoUpdate(false);

vwnav = luView.createViewNav();

// setting buffer for fast view retrieval
vwnav.setBufferMaxEntries(400);

// perform lookup
vwentry = vwnav.getFirst();
while (vwentry != null){

if(!categories.contains(vwentry.getColumnValues().elementAt(0).toString()))
categories.add(vwentry.getColumnValues().elementAt(0).toString());
// Get entry and go recycle
vwentrytmp = vwnav.getNext(vwentry);
vwentry.recycle();
vwentry = vwentrytmp;
}
luView.setAutoUpdate(true);

return categories;
}

The xp:Combobox control is bound to a viewScope variable and it’s onChange event triggers a partial refresh on a panel:

<xp:panel id=”pictures”>
<xp:pager layout=”Previous Group Next” partialRefresh=”true” id=”pager1″ for=”repeat1″></xp:pager>
<table class=”table table-hover”>
<thead>
<tr>
<th>Thumb</th>
<th>Subject</th>
<th>Category</th>
<th>Description</th>
<th>Author</th>
</tr>
</thead>
<xp:repeat id=”repeat1″ rows=”15″ var=”pix”>

<xp:this.value><![CDATA[#{javascript:var active = viewScope.get(“activeCategory”);
if (active == “All”){
PictureProvider.loadPictures(“”);
}
else{
PictureProvider.loadPictures(active);
}}]]></xp:this.value>
<tr>
<td>
<xp:text escape=”false”>
<xp:this.value><![CDATA[#{javascript:/*
getAttachmentURL and getBaseURL from:
http://www.wissel.net/blog/d6plinks/SHWL-86QKNM
*/

function getAttachmentURL(docID:java.lang.String, attachmentName:java.lang.String) {
var base = getBaseURL();
var middle = “/xsp/.ibmmodres/domino/OpenAttachment”;
if (base.substr(0,4) == “/xsp”) {
middle += base.substr(4);
} else {
middle += base;
}
var result = base + middle + “/” + docID + “/$File/” + attachmentName + “?Open”;
return result;
}

function getBaseURL() {
var curURL = context.getUrl();
var curAdr = curURL.getAddress();
var rel = curURL.getSiteRelativeAddress(context);
var step1 = curAdr.substr(0,curAdr.indexOf(rel));

// Now cut off the http
var step2 = step1.substr(step1.indexOf(“//”)+2);
var result = step2.substr(step2.indexOf(“/”));
return result;
}
var thumb = pix.imgthumb;
var id = pix.docUNID;

return “<img src='” + getAttachmentURL(id, thumb) + “‘>”;
}]]></xp:this.value>
</xp:text>
</td>
<td><xp:text escape=”true” value=”#{pix.subject}”></xp:text></td>
<td><xp:text escape=”true” value=”#{pix.category}”></xp:text></td>
<td><xp:text escape=”true” value=”#{pix.descr}”></xp:text></td>
<td><xp:text escape=”true” value=”#{pix.author}”></xp:text>
</td>
</tr><!– /.row –>
</xp:repeat>

</table>
</xp:panel>

Depending on the value in the viewScope we call the loadPictures method empty or with a key.

private ArrayList<JsonJavaObject> loadJSONObjects(String ServerName, String DatabaseName, String ViewName, String Key, Integer ColIdx) throws NotesException {
ArrayList<JsonJavaObject> JSONObjects = new ArrayList<JsonJavaObject>();

NotesContext nct = NotesContext.getCurrent();
Session session = nct.getCurrentSession();
Database DB = session.getDatabase(ServerName, DatabaseName);

if (!(DB==null)) {
View luView = DB.getView(ViewName);

if (!(luView == null)) {
JsonJavaFactory factory = JsonJavaFactory.instanceEx;

ViewEntryCollection vec = luView.getAllEntriesByKey(Key, true);

ViewEntry entry = vec.getFirstEntry();
while (entry != null) {

Vector<?> columnValues = entry.getColumnValues();
String colJson = String.valueOf(columnValues.get(ColIdx));
JsonJavaObject json = null;

try {
json = (JsonJavaObject) JsonParser.fromJson(factory, colJson);
if (json != null) {
JSONObjects.add(json);
}

} catch (JsonException e) {
// TODO:
}

ViewEntry tempEntry = entry;
entry = vec.getNextEntry();
tempEntry.recycle();
}
luView.recycle();
}
DB.recycle();
}
return JSONObjects;
}

Similar as in the previous examples the JsonJavaObject class from the com.ibm.commons.util.io.json package is used to return an arraylist of json objects.

In the next post I will describe how we can manipulate the JSON objects.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s