1 /** 2 * Created by milogertjejansen on 2/16/16. 3 */ 4 var REST = library.REST.REST(); 5 var myData = {}; 6 var myLimit; 7 var myOrder; 8 9 /** 10 * Adds an object to myData containing the value of the given schema/field 11 * 12 * @param theAssetId - the id of the asset to get the value of 13 */ 14 function getDiscussionInternal(theAssetId) { 15 // Try to get our file. 16 var aFile = fileManager.getFileObjectById(theAssetId); 17 if (aFile == null) { 18 try { 19 aFile = new ManagedFile(theAssetId); 20 } catch (anEr) { 21 REST.submitError(theAssetId, "The file does not exist"); 22 } 23 if (aFile == null) { 24 REST.submitError(theAssetId, "The file does not exist"); 25 return; 26 } 27 } 28 29 // Set the id. 30 myData["id"] = theAssetId; 31 myData["discussion"] = []; 32 33 // Get the discussion. 34 var aDiscussionSet = aFile.xmp.meta.getField( 35 new Property("http://brightech.com/ns/mb", "discussion") 36 ); 37 if (aDiscussionSet != null) { 38 // Set the limit if it's too large. 39 if (myLimit && myLimit > aDiscussionSet.length) { 40 myLimit = aDiscussionSet.length; 41 } 42 43 // Set the start: either 0 or the end of the array. 44 var aStartIndex = myOrder === "desc" ? aDiscussionSet.length - 1 : 0; 45 46 // Pick where to stop: either the limit requested or the discussion set length or 0, depending on the order. 47 var aLimit = !isNaN(myLimit) ? myLimit : aDiscussionSet.length; 48 49 // Set the actual array index. 50 var aPlacementIndex = 0; 51 52 // Loop through them and add a useful item to the array. 53 while (true) { 54 myData["discussion"][aPlacementIndex] = {}; 55 56 // Get the sub entries in the discussion. 57 var aSubFields = aDiscussionSet.getItemAt(aStartIndex).getFields(); 58 59 // Loop through all the sub entry's fields and place them in the appropriate array. 60 for (var j = 0; j < aSubFields.length; j++) { 61 myData["discussion"][aPlacementIndex][ 62 aSubFields[j].name 63 ] = aDiscussionSet 64 .getItemAt(aStartIndex) 65 .getField(aSubFields[j]) 66 .toString(); 67 } 68 69 aPlacementIndex++; 70 71 // Descending order. 72 aLimit--; 73 if (aLimit <= 0) { 74 break; 75 } 76 if (myOrder === "desc") { 77 aStartIndex--; 78 if (aStartIndex < 0) { 79 break; 80 } 81 } else { 82 aStartIndex++; 83 if (aStartIndex > aDiscussionSet.length - 1) { 84 break; 85 } 86 } 87 } 88 } 89 } 90 91 /** 92 * Returns the discussions for the given asset. 93 * 94 * @description Gets the discussion for the asset given by AssetID in 'id'. The list of discussions is ordered in 95 * ascending date created. 96 * @example 'MBurl'/wf/restapi/1/getDiscussion?id=AssetId 97 * @example <a target="_blank" href='http://127.0.0.1:55555/wf/restapi/1/getDiscussion?id=12345>http://127.0.0.1:55555/wf/restapi/1/getDiscussion?id=12345</a> 98 * @class Returns the discussion of the asset of the given id. 99 * @name GetDiscussion 100 * @param id Asset ID to get the discussion from. 101 * @returns A structure similar to below is returned. 102 * <pre><code>{ 103 * 'id': AssetId, 104 * 'discussion': [ 105 * { 106 * "date": "Tue, 16 Feb 2016 13:36:01 -0600", 107 "endTime": "-1.0", 108 "globalAdminEdited": "false", 109 "startTime": "-1.0", 110 "text": "Sample comment", 111 "dateEdited": "Tue, 16 Feb 2016 13:36:03 -0600", 112 "page": "1", 113 "user": "root", 114 "uid": "BFAD9D5B-3316-4D58-E50B-34C554C0FF93" 115 * }, 116 * ... 117 * ] 118 * }</code></pre> 119 */ 120 function main() { 121 var aId = REST.getParameter("id", true); 122 myLimit = parseInt(REST.getParameter("limit", false)); 123 myOrder = REST.getParameter("order", false); 124 if (myData.error != null) { 125 return REST.formatResponse(); 126 } 127 getDiscussionInternal(aId); 128 return REST.formatResponse(); 129 } 130 main(); 131