Link to attachments in Rich Text Fields from XPages and XPinC


var debug = false; 
/**
Builds path to an attachment
*/

function getAttachmentPath(doc:NotesDocument, attachmentName, rtFieldName){
 if(rtFieldName==null || rtFieldName ==""){
  rtFieldName = "$File";
 }
 if(context.isRunningContext("Notes")){
     var dbPath = getDbPath();
  var dbPathTarget = getDbXPInCPath(doc.getParentDatabase());
     return "" + dbPath[0] + "/xsp/.ibmmodres/domino/OpenAttachment/" + dbPathTarget + "/" + doc.getUniversalID() + "/"+rtFieldName+"/" + escape(attachmentName)+ "?OpenElement";
 }
    else{
     var url = doc.getHttpURL();
  return url.replace("?OpenDocument","/$File/"+attachmentName+"?OpenElement");
    }
}

/**
Cache the XpagesInClient path variables in an applicationScope variable
*/
function getDbXPInCPath(db:NotesDatabase){
    var dbCacheName = "dbpathxpinc-"+@LeftBack(db.getFileName(),".");
    if(debug||isCacheInvalid(dbCacheName, 600)){
     synchronized(applicationScope){
      var res = db.getServer()+"!!"+db.getFilePath().replace("\\","/");
      applicationScope.put(dbCacheName,res);
     }
    }
    return applicationScope.get(dbCacheName);
}

/**
Cache the dbPath variables in an applicationScope variable
*/
function getDbPath(){
    if(debug||isCacheInvalid("dbpathweb", 600)){
     synchronized(applicationScope){
      var dbPath = @Left(context.getUrl().toString().replace("\\","/"), ".nsf") + ".nsf";
      var pos = (context.isRunningContext("Notes")) ? 4 : 3;
      var secondPathElements = dbPath.split("/");
      var secondPath = "";
      for (pos; pos<secondPathElements.length; pos++){
       if (secondPath != "")
       secondPath += "/";
       secondPath += secondPathElements[pos];
      }
      var res:Array = new Array();
      res.push(dbPath);
      res.push(secondPath);
      applicationScope.dbpathweb = res;
     }
    }
    return applicationScope.dbpathweb;
}

/**
A generic caching mechanism for each key will check to see if it is 'n' seconds
since it was last updated. Use for things that change relatively infrequently  
*/
function isCacheInvalid(key, cacheInterval){
    var currentTime = new Date().getTime();
    if (!applicationScope.containsKey(key + "_time")){
     applicationScope.put(key + "_time", currentTime);
     return true;
    }
    var diffInSecs = Math.ceil((currentTime - applicationScope.get(key + "_time")) / 1000);
    if (diffInSecs < cacheInterval) {
     return false;
    } else {
     applicationScope.put(key + "_time", currentTime);
     return true;
    }
}
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.
No comments yetLogin first to comment...