Thursday, December 24, 2009

What if @DBColumn Fails on web

There was one scenario in which I had to populate a combo box (drop down) which contains all the values in the first column of a view.

But the data in the view is so large that the formula itself failed. So I had to think of other work arounds to do the same.

I tried changing the "Number of lines to display in view" in the "Domino Web Engine" Tab from 1000 to 5000 in the server document. This resulted in an other error that caused my "openView?readviewentries" url to collapse and bomb an error. Probably 'cos of a heap over flow sort of thing.

So I required a more stable solution and hence arrived at a result. Many might feel that this one is quite inefficient as I do. The problem is if you have 50000 documents in that view, this code shall ask the browser to hit the server 50 times to get all the data as read view entries by default shall return only 1000 documents and since I have experienced it, I dont recommend changing it.

Please correct me if I am wrong

The following is a fragment of code that I developed to build the combo box.
It is not so generic and hence you may have to tweak it a bit to suit your requirements

/************************************/
var MAX_DOCS_IN_THOUSANDS=50; //This means you get up to 50000 options in your combobox

var comboBox="<select id='myComboID' onchange='javascript:myOnchangeEvent()'>";

//read view entries by default shall return xml entries of a maximum of 1000 documents
//hence loop through all the documents in the view in sets of 1000s and get all the company names
for (itr=1; itr<=MAX_DOCS_IN_THOUSANDS; itr++) { //start of for loop
startIndex=(itr-1)*1000+1;
try {
response=getViewColumn(DBServer,DBPath,viewName,startIndex);
} catch (error) {
info=" Probably the server name or the path of the database that you have mentioned is wrong </b>";
break;
}

var textNodes=response.getElementsByTagName("text");
try {
if (textNodes.length>0) {
for (jtr=0; jtr<1000; jtr++)
comboBox+="<option>"+textNodes[jtr].childNodes[0].nodeValue+"</option>";
} else {
break;
}
} catch(error) {
response=null;
textNodes=null;
break;
} finally {
response=null;
textNodes=null;
}
} //end of for loop
comboBox+="</select>";
/************************************/

/************************************/
//function to get the dolumn values from the view
function getViewColumn(serverName,databasePath,viewName,startIndex) {
var xmlHttp=xmlHttpRequest();
var url;

if (startIndex==-1) {
url="http://"+serverName+"/"+databasePath.replace(/\\/gi,"/")+"/"+viewName+"?readviewentries&count=-1";
} else {
if (typeof startIndex!='number') startIndex=0;
if (startIndex==0) {
url="http://"+serverName+"/"+databasePath.replace(/\\/gi,"/")+"/"+viewName+"?readviewentries&count=-1";
} else {
url="http://"+serverName+"/"+databasePath.replace(/\\/gi,"/")+"/"+viewName+"?readviewentries&start="+startIndex+"&count=-1";
}
}
var retValue
try {
var xmlHttp=xmlHttpRequest();
xmlHttp.open("GET",url,false);
xmlHttp.send(null);
retValue=xmlHttp.responseXML;
xmlHttp=null;
} catch (error) {
error.message += " <br>- in function getViewColumn(serverName,databasePath,viewName)<br>";
throw error;
}
return retValue;
}
/************************************/

/************************************/
//function to create sml http request
function xmlHttpRequest() {
var xmlhttp;
if (window.XMLHttpRequest)
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else
{
throw new Error("Your browser does not support XMLHTTP! <br> - in function xmlHttpRequest()");
return;
}
return xmlhttp;
}
/************************************/

No comments:

Post a Comment