function updateChangeHistory(currentDoc){
//get the database to be used when looking up the original document
var db=currentDoc.getDatabase();
//get the current user’s common name
var theUser=@Name("[CN]",@UserName());
//initialize theTimeStamp using @Now() @Command
var theTimeStamp=@Now();
//get the form name used by the current document
var theForm=currentDoc.getForm();
//check to see if this is a new document
if (currentDoc.isNewDocument()){
//if it is, record the creation
theDescription="Created";
//insert the event into the change history log
insertHistory(theUser,theTimeStamp,theDescription,currentDoc);
}else{
//get the current documents ID
docID=currentDoc.getId();
//since this is not a new document get a copy of the original document prior to any changes
theOrigDoc=db.getDocumentById(docID);
switch (theForm) {
//if the form is the project form perform the following data checks
case "ProjectEdit" :
compareStringValues("/project/name","Project Name",currentDoc,theOrigDoc,theUser,theTimeStamp,true);
compareStringValues("/project/currentStatus","Project Status",currentDoc,theOrigDoc,theUser,theTimeStamp,true);
compareStringValues("/project/manager","Project Manager",currentDoc,theOrigDoc,theUser,theTimeStamp,true);
compareDoubleValues("/project/budget","Project Budget",currentDoc,theOrigDoc,theUser,theTimeStamp);
compareStringValues("/project/statusUpdate","The Status Update was revised",currentDoc,theOrigDoc,
theUser,theTimeStamp,false);
break;
//if the form is the issue form perform the following data checks
case "IssueEdit" :
compareStringValues("/issue/type","Issue Type",currentDoc,theOrigDoc,theUser,theTimeStamp);
compareStringValues("/issue/owner","Issue Owner",currentDoc,theOrigDoc,theUser,theTimeStamp);
compareStringValues("/issue/priority","Issue Priority",currentDoc,theOrigDoc,theUser,theTimeStamp);
break;
default :
//log the error
print("<<<<<error! no change history case statement for form name="+theForm);
}
}
}
function compareStringValues(theField,theFieldDesc,currentDoc,theOrigDoc,theUser,theTimeStamp,buildDescFlag){
//initialize theDescription string variable to hold our change description
var theDescription="";
if (currentDoc.getStringValue(theField)!=theOrigDoc.getStringValue(theField)){
//build the description
if (buildDescFlag){
theDescription=theFieldDesc + " changed from "+ theOrigDoc.getStringValue(theField)
_+ " to " + currentDoc.getStringValue(theField);
//use the passed in description. pass false in when data is too long to track.
}else{
theDescription=theFieldDesc;
}
insertHistory(theUser,theTimeStamp,theDescription,currentDoc);
}
}
function compareDoubleValues(theField,theFieldDesc,currentDoc,theOrigDoc,theUser,theTimeStamp){
//initialize theDescription string variable to hold our change description
var theDescription="";
if (currentDoc.getDoubleValue(theField)!=theOrigDoc.getDoubleValue(theField)){
theDescription=theFieldDesc + " changed from "+ I18n.toString(theOrigDoc.getDoubleValue(theField))
_+ " to " + I18n.toString(currentDoc.getDoubleValue(theField));
insertHistory(theUser,theTimeStamp,theDescription,currentDoc);
}
}
function insertHistory(theUser,theTimeStamp,theDescription,currentDoc){
//create a new DOMElement object for our history
var history = new DOMElement("changeHistory");
//set the user element
history.setStringValue("userName",theUser);
//set the timeStamp
history.setDateValue("timeStamp",theTimeStamp);
//set the description of the change
history.setStringValue("description",theDescription);
//Here is where we add the history object to our current document.
currentDoc.getRootElement().addContent(1,history);
print("<<<<adding content history");
}
|