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 
  9 /**
 10  * Adds a discussion to the asset for the user
 11  *
 12  * @param theJSON {"id":123456,"text":"the message to add to the discussion","user":"theUserName"}
 13  */
 14 function setDiscussion(theJSON) {
 15   theJSON.user = !theJSON.user ? context.getUser().username : theJSON.user;
 16   var aFile = fileManager.getFileObjectById(theJSON.id);
 17   if (aFile == null) {
 18     REST.pushError(REST.errors.e404, "The file does not exist:" + theJSON.id);
 19     return;
 20   }
 21 
 22   // Set up the new discussion item.
 23   var aNewDiscussion = aFile.xmp.newStructure();
 24   aNewDiscussion.addField(
 25     new Property("http://brightech.com/ns/mb", "user"),
 26     theJSON.user
 27   );
 28   aNewDiscussion.addField(
 29     new Property("http://brightech.com/ns/mb", "page"),
 30     "1"
 31   );
 32   aNewDiscussion.addField(
 33     new Property("http://brightech.com/ns/mb", "uid"),
 34     generateUUID()
 35   );
 36   aNewDiscussion.addField(
 37     new Property("http://brightech.com/ns/mb", "text"),
 38     theJSON.text
 39   );
 40   aNewDiscussion.addField(
 41     new Property("http://brightech.com/ns/mb", "date"),
 42     new Date().toISOString()
 43   );
 44 
 45   // Get the discussion field
 46   var aProp = new Property("http://brightech.com/ns/mb", "discussion");
 47   var aDiscussion = aFile.xmp.meta.getField(aProp);
 48   if (aDiscussion == null) {
 49     // Add the field itself.
 50     var aSeq = aFile.xmp.newSeq();
 51     aSeq.add(aNewDiscussion.length, aNewDiscussion);
 52     aFile.xmp.meta.addField(aProp, aSeq);
 53   } else {
 54     // Add to the existing field.
 55     aDiscussion.add(aDiscussion.length, aNewDiscussion);
 56   }
 57 
 58   // Get the discussion from the asset and format it.
 59   var aOld;
 60   var aNew;
 61   if (aDiscussion) {
 62     var aFormattedDisc = REST.formatDiscussionList(aDiscussion, 2, "desc");
 63     aOld = aFormattedDisc[1];
 64     aNew = aFormattedDisc;
 65   } else {
 66     aOld = null;
 67     aNew = aNewDiscussion;
 68   }
 69   var aChangeList = REST.addToChangeList({}, aProp, aOld, aNew);
 70 
 71   // Write the xmp out.
 72   aFile.writeXmp();
 73 
 74   // If we have a workflow that we want to trigger post-file writing do it here.
 75   var aTriggeredAdd = {};
 76   var aTriggerResponse = REST.triggerAssetBasedWorkflows(
 77     REST.AssetTriggers.Modified,
 78     aFile,
 79     aChangeList
 80   );
 81   if (aTriggerResponse) {
 82     aTriggeredAdd.triggerAssetModified = aTriggerResponse.ok;
 83   }
 84 
 85   //only respond with the last added discussion
 86   var aParams = {
 87     ids: theJSON.id,
 88     limit: 1,
 89     order: "DESC",
 90   };
 91 
 92   var aDiscussions = REST.callRESTWorkflow("getDiscussion", aParams);
 93   for (var i in aDiscussions) {
 94     REST.push(aFile, Object.assign(aTriggeredAdd, aDiscussions[i]));
 95   }
 96 }
 97 
 98 /**
 99  * @name SetDiscussion
100  * @class Adds discussions to assets
101  * @description For each asset in  "data", this endpoint writes the text to the discussion of the asset
102  * @param data an array of assets and the discussion to add. [{"id":123456,"text":"the message to add to the discussion","user":"theUserName"}, ...]
103  * @param [verbose=false] Setting this to true will collect a variety of default values for each asset.
104  * @param [fields] An array of field id's to collect the values for each asset
105  * @param [triggerAssetBasedWorkflow=true] set to false to avoid triggering other asset metadata edited workflows
106  * @returns [{assetInfo}, ... ]
107  *
108  * @example /wf/restapi/v2/setDiscussion
109  *
110  * Parameters:
111  * data=[{"id":201629375,"text":"the message to add to the discussion","user":"root"}]
112  *
113  * Response:
114  [
115 	 {
116 	   "discussion": [
117 		 {
118 		   "date": "Mon, 16 Oct 2017 14:53:23 -0500",
119 		   "endTime": "-1.0",
120 		   "globalAdminEdited": "false",
121 		   "startTime": "-1.0",
122 		   "text": "Previous Comment",
123 		   "dateEdited": "Mon, 16 Oct 2017 14:53:23 -0500",
124 		   "page": "1",
125 		   "user": "root",
126 		   "uid": "5F9A56F8-4403-7875-EFE7-70FDB94F072A"
127 		 },
128 		 {
129 		   "date": "2017-10-16T19:53:55.147Z",
130 		   "page": "1",
131 		   "text": "the message to add to the discussion",
132 		   "user": "root",
133 		   "uid": "F4CA822B-B6E3-4D77-B77A-97393BE84846"
134 		 }
135 	   ],
136 	   "id": 201629375
137 	 }
138  ]
139  */
140 function main() {
141   REST.setCallback(setDiscussion);
142   return REST.execute("data");
143 }
144 main();
145