1 /**
  2  * Created by milogertjejansen on 2016-02-08.
  3  *
  4  * Workflow to add new discussion items to an asset using the REST interface.
  5  */
  6 
  7 var REST = library.REST.REST();
  8 var myData = {};
  9 
 10 /**
 11  *  writes aVal to the field adds a object to myData containing the written metadata
 12  *
 13  * @param theId the asset to add the discussion to.
 14  * @param theContent the content of the discussion item to be added.
 15  */
 16 function setDiscussionInternal(theId, theContent, theUser) {
 17   var aFile = fileManager.getFileObjectById(theId);
 18   if (aFile == null) {
 19     REST.submitError(theId, "The file does not exist");
 20     return;
 21   }
 22 
 23   // Set up the new discussion item.
 24   var aNewDiscussion = aFile.xmp.newStructure();
 25   aNewDiscussion.addField(
 26     new Property("http://brightech.com/ns/mb", "user"),
 27     theUser === "" ? context.getUser().username : theUser
 28   );
 29   aNewDiscussion.addField(
 30     new Property("http://brightech.com/ns/mb", "page"),
 31     "1"
 32   );
 33   aNewDiscussion.addField(
 34     new Property("http://brightech.com/ns/mb", "uid"),
 35     generateUUID()
 36   );
 37   aNewDiscussion.addField(
 38     new Property("http://brightech.com/ns/mb", "text"),
 39     theContent
 40   );
 41   aNewDiscussion.addField(
 42     new Property("http://brightech.com/ns/mb", "date"),
 43     new Date().toISOString()
 44   );
 45 
 46   myData.id = theId;
 47   myData.content = theContent;
 48   myData.user = theUser === "" ? context.getUser().username : theUser;
 49 
 50   // Get the discussion field
 51   var aProp = new Property("http://brightech.com/ns/mb", "discussion");
 52   var aDiscussion = aFile.xmp.meta.getField(aProp);
 53   if (aDiscussion == null) {
 54     // Add the field itself.
 55     var aSeq = aFile.xmp.newSeq();
 56     aSeq.add(aNewDiscussion.length, aNewDiscussion);
 57     aFile.xmp.meta.addField(aProp, aSeq);
 58     myData.new = true;
 59   } else {
 60     // Add to the existing field.
 61     aDiscussion.add(aDiscussion.length, aNewDiscussion);
 62     myData.new = false;
 63   }
 64 
 65   // Write the xmp out.
 66   aFile.writeXmp();
 67 }
 68 
 69 /**
 70  * Adds a new discussion item to an asset.
 71  *
 72  * @description Finds the asset 'id' and adds a new discussion item with 'content.' The user and the date are provided
 73  * in the form of an api key and when the call was issued.
 74  * @example 'MBurl'/wf/restapi/1/setDiscussion?id=AssetID&content='Discussion content.'
 75  * @example <a target="_blank" href='http://127.0.0.1:55555/wf/restapi/1/setDiscussion?id=12345&content="Uploaded patch for head."'>http://127.0.0.1:55555/wf/restapi/1/setDiscussion?id=12345&content="Uploaded patch for head."</a>
 76  * @class Adds a new discussion item to an asset.
 77  * @name SetDiscussion
 78  * @param id the asset to add the discussion to.
 79  * @param content the content of the discussion item to be added.
 80  * @returns ( {'AssetID': "New Field Value",...} )
 81  */
 82 function main() {
 83   var aId = REST.getParameter("id", true);
 84   var aContent = REST.getParameter("content", true);
 85   var aUser = REST.getParameter("user", false);
 86   if (myData.error != null) {
 87     return REST.formatResponse();
 88   }
 89   setDiscussionInternal(aId, aContent, aUser);
 90   return REST.formatResponse();
 91 }
 92 main();
 93