1 var REST = library.REST.REST();
  2 var mySearch = null;
  3 
  4 /**
  5  * Builds a Boolean criterion
  6  * @returns BooleanCriterion
  7  */
  8 function buildCriterion(theJSONSearch) {
  9   var aBooleanCriterion = new BooleanCriterion(theJSONSearch.conjunction);
 10   for (var i = 0; i < theJSONSearch.criteria.length; i++) {
 11     var aJSONCriterion = theJSONSearch.criteria[i];
 12     var aCriterion;
 13     if (aJSONCriterion.conjunction) {
 14       aCriterion = buildCriterion(aJSONCriterion);
 15     } else {
 16       var aProperty = REST.getProperty(aJSONCriterion.fieldId);
 17       if (aProperty.namespace === "database") {
 18         aCriterion = new DatabaseCriterion(aProperty.name);
 19       } else {
 20         aCriterion = new XmpCriterion(aProperty);
 21       }
 22       aCriterion.setCondition(aJSONCriterion.condition);
 23       if (aProperty.name == "directory") {
 24         aCriterion.setValue(REST.getFullPath(aJSONCriterion.value));
 25       } else {
 26         aCriterion.setValue(aJSONCriterion.value);
 27       }
 28     }
 29     aBooleanCriterion.addCriterion(aCriterion);
 30   }
 31   return aBooleanCriterion;
 32 }
 33 
 34 /**
 35  * returns a Search wrapper created from the JSON search object
 36  * @param theJSONSearch the search object
 37  * @param theQuickSearch a quicksearch to include with the search object
 38  * @returns {Search}
 39  */
 40 function generateSearch(theJSONSearch, theQuickSearch) {
 41   var aSearch = new Search();
 42   var aCriteria = new Criteria("and");
 43   if (!!theQuickSearch) {
 44     var aQSCriteria = searchManager
 45         .createQuickSearch(theQuickSearch)
 46         .getCriteria();
 47     var aQSCriterion = aQSCriteria.getRootCriterion();
 48     aCriteria.addCriterion(aQSCriterion);
 49   }
 50   aCriteria.addCriterion(buildCriterion(theJSONSearch));
 51   aSearch.setCriteria(aCriteria);
 52   return aSearch;
 53 }
 54 
 55 /**
 56  * Adds a result to the result array in myData
 57  * @param theManagedFile
 58  */
 59 function search() {
 60   var aResults = REST.executeSearchWithPaging(mySearch);
 61   for (var i in aResults) {
 62     REST.push(aResults[i], {}, true);
 63   }
 64 }
 65 
 66 /**
 67  * @name Search
 68  * @class Runs a search for assets.
 69  * @description Runs a search and displays the asset info for each result
 70  * @param search a JSON search object to execute.
 71  * search =
 72  * {
 73  *      "conjunction":"or",
 74  *      "criteria":
 75  *          [
 76  *              {
 77  *                  "fieldId":"http://purl.org/dc/elements/1.1/ subject",
 78  *                  "condition":"cont",
 79  *                  "value":"newValue"
 80  *              },
 81  *              {
 82  *                  "fieldId":"directory",
 83  *                  "condition":"beg",
 84  *                  "value":"Upload"
 85  *              }
 86  *          ]
 87  *  }
 88  * @param [savedSearchId] the ID of the saved search to execute
 89  * @param [pageSize=100] the number of assets to give in the response
 90  * @param [pageIndex=0] the index of the page to return
 91  * @param [sortField] the namespace of the desired field ex "record_id" or "http://purl.org/dc/elements/1.1/ subject"
 92  * @param [sortDirection] "ASC" or "DESC".  the order in which the sortFiled should be applied
 93  * @param [verbose=false] Setting this to true will collect a variety of default values for each asset.
 94  * @param [FILTERS] Filtering can be applied to the "id", "name", "path", "height", "width", "bytes", "lastModified", "mimeType", and "resolver" properties if verbose=true.
 95  * @param [fields] An array of field id's to collect the values for each asset
 96  * @returns [{AssetData}, ... ]
 97  <br>
 98  * Supported Search Conditions
 99  * <ul>
100  <li>Container Fields</li>
101  <ul>
102  <li>cont</li>
103  <li>not_cont</li>
104  <li>not</li>
105  <li>null</li>
106  <li>cont_part</li>
107  <li>not_cont_part</li>
108  </ul>
109  <li>Integer Fields</li>
110  <ul>
111  <li>not</li>
112  <li>null</li>
113  <li>eq</li>
114  <li>ne</li>
115  <li>le</li>
116  <li>lt</li>
117  <li>ge</li>
118  <li>gt</li>
119  <li>anyval</li>
120  <li>notchanged</li>
121  </ul>
122  <li>Hierarchy Fields</li>
123  <ul>
124  <li>not</li>
125  <li>null</li>
126  <li>eq</li>
127  <li>ne</li>
128  <li>cont</li>
129  <li>not_cont</li>
130  <li>anyval</li>
131  <li>notchanged</li>
132  </ul>
133  <li>Date Fields</li>
134  <ul>
135  <li>not</li>
136  <li>null</li>
137  <li>eq</li>
138  <li>ne</li>
139  <li>le</li>
140  <li>lt</li>
141  <li>ge</li>
142  <li>gt</li>
143  <li>between</li>
144  <li>anyval</li>
145  <li>notchanged</li>
146  </ul>
147  <li>Other Fields</li>
148  <ul>
149  <li>not</li>
150  <li>null</li>
151  <li>eq</li>
152  <li>ne</li>
153  <li>beg</li>
154  <li>not_beg</li>
155  <li>end</li>
156  <li>not_end</li>
157  <li>cont</li>
158  <li>not_cont</li>
159  <li>all</li>
160  <li>any</li>
161  <li>phrase</li>
162  <li>wo</li>
163  <li>anyval</li>
164  <li>notchanged</li>
165  </ul>
166  </ul>
167  *
168  *<br>
169  * English Translations
170  * <ul>
171  *     <li>eq = Equals</li>
172  *     <li>ne = Does not equal</li>
173  *     <li>lt = Less than</li>
174  *     <li>le = Less than or equal to</li>
175  *     <li>gt = Greater than</li>
176  *     <li>ge = Greater than or equal to</li>
177  *     <li>between = Between</li>
178  *     <li>cont = Contains</li>
179  *     <li>not_cont = Does not contain</li>
180  *     <li>beg = Begins with</li>
181  *     <li>not_beg = Does not begin with</li>
182  *     <li>end = Ends with</li>
183  *     <li>not_end = does not end with</li>
184  *     <li>null = is null</li>
185  *     <li>not = is not null</li>
186  *     <li>cont_part = Contains partial value</li>
187  *     <li>not_cont_part = Does not contain partial value</li>
188  *     <li>anyval = Any value</li>
189  *     <li>all = All words</li>
190  *     <li>any = Any Words</li>
191  *     <li>phrase = Has Exact phrase</li>
192  *     <li>wo = Without word</li>
193  * </ul>
194  *
195  * @example A search for assets where the Dublin Core Keywords field contains the value "newValue"
196  * /wf/restapi/v2/search
197  *
198  * Parameters:
199  * fields = ["http://purl.org/dc/elements/1.1/ subject"]
200  * search =
201  * {
202  *      "conjunction":"or",
203  *      "criteria":
204  *          [
205  *              {
206  *                  "fieldId":"http://purl.org/dc/elements/1.1/ subject",
207  *                  "condition":"cont",
208  *                  "value":"newValue"
209  *              }
210  *          ]
211  *  }
212  *
213  Response:
214  [
215  {
216 	 "id": 201628187,
217 	 "fields": {
218 	   "http://purl.org/dc/elements/1.1/ subject": [
219 		 "newValue",
220 		 "newValue"
221 	   ]
222 	 }
223    },
224  ]
225 
226  * @example A search for assets where the Dublin Core Keywords field contains the value "newValue" or "check"
227  * /wf/restapi/v2/search
228  *
229  * Parameters:
230  * fields = ["http://purl.org/dc/elements/1.1/ subject"]
231  * search =
232  * {
233  *      "conjunction":"or",
234  *      "criteria":
235  *          [
236  *              {
237  *                  "fieldId":"http://purl.org/dc/elements/1.1/ subject",
238  *                  "condition":"cont",
239  *                  "value":"newValue"
240  *              },
241  *              {
242  *                  "fieldId":"http://purl.org/dc/elements/1.1/ subject",
243  *                  "condition":"cont",
244  *                  "value":"check"
245  *              }
246  *          ]
247  *  }
248  *
249  Response:
250  [
251  {
252 		"id": 201629260,
253 		"fields": {
254 		  "http://purl.org/dc/elements/1.1/ subject": [
255 			"check",
256 			"1",
257 			"2"
258 		  ]
259 		}
260 	},
261  {
262 	   "id": 201628187,
263 	   "fields": {
264 		 "http://purl.org/dc/elements/1.1/ subject": [
265 		   "newValue",
266 		   "newValue"
267 		 ]
268 	   }
269 	 }
270  ]
271 
272  * @example Include a quick search with the search object
273  * /wf/restapi/v2/search
274  *
275  * Parameters:
276  * quickSearch=apple
277  * verbose=true
278  * search =
279  {
280 	 "conjunction":"AND",
281 	 "criteria":[
282 		 {
283 			 "fieldId":"file_name",
284 			 "condition":"ne",
285 			 "value":"__easter_egg__"
286 		 }
287 	 ]
288 }
289  *
290  Response:
291  [
292  {
293    "id": 201631733,
294    "name": "apple.jpeg",
295    "path": "apple.jpeg",
296    "height": 240,
297    "width": 210,
298    "bytes": 40335,
299    "lastModified": 1518701186000,
300    "mimeType": "image/jpeg",
301    "previews": {
302 	 "thumbnail": "http://10.31.210.2:55555/servlet/jb.view?table=thumbnails&col=thumbnails&id=pe_323031363331373333",
303 	 "viewex": "http://10.31.210.2:55555/servlet/jb.view?table=viewex&col=viewex&id=pe_323031363331373333",
304 	 "downloadUrl": "http://10.31.210.2:55555/servlet/dload?id=pe_323031363331373333"
305    },
306    "publishedRenditions": {
307             "webPNG": "https://aws-cloud-host-s3Bucket/c1ba4a79-2a82-7823-ab28-5a0a1fa8220a/c1ba4a79-2a82-7823-ab28-5a0a1fa8220a.png",
308             "webJpeg": "https://aws-cloud-host-s3Bucket/9ff45e98-dcc0-7823-9e45-c3be6390d3bf/9ff45e98-dcc0-7823-9e45-c3be6390d3bf.jpeg",
309             "webGif": "https://aws-cloud-host-s3Bucket/11a081b2-53cf-7823-bb08-2a7f75c4ccee/11a081b2-53cf-7823-bb08-2a7f75c4ccee.gif"
310    }
311  }
312  ]
313 
314  * @example Include a saved search ID instead of a JSON search,A published renditions section will be sent in response
315  * with preset names and public S3 URLs as keys and values respectively if a license for renditions is enabled.
316  * /wf/restapi/v2/search
317  *
318  * Parameters:
319  * savedSearchId=1
320  * verbose=true
321  *
322  Response:
323  [
324  {
325 	   "savedSearchId": 201631733,
326 	   "name": "apple.jpeg",
327 	   "path": "apple.jpeg",
328 	   "height": 240,
329 	   "width": 210,
330 	   "bytes": 40335,
331 	   "lastModified": 1518701186000,
332 	   "mimeType": "image/jpeg",
333 	   "previews": {
334 		 "thumbnail": "http://10.31.210.2:55555/servlet/jb.view?table=thumbnails&col=thumbnails&id=pe_323031363331373333",
335 		 "viewex": "http://10.31.210.2:55555/servlet/jb.view?table=viewex&col=viewex&id=pe_323031363331373333",
336 		 "downloadUrl": "http://10.31.210.2:55555/servlet/dload?id=pe_323031363331373333"
337 	   },
338 	   "publishedRenditions": {
339             "webPNG": "https://aws-cloud-host-s3Bucket/c1ba4a79-2a82-7823-ab28-5a0a1fa8220a/c1ba4a79-2a82-7823-ab28-5a0a1fa8220a.png",
340             "webJpeg": "https://aws-cloud-host-s3Bucket/9ff45e98-dcc0-7823-9e45-c3be6390d3bf/9ff45e98-dcc0-7823-9e45-c3be6390d3bf.jpeg",
341             "webGif": "https://aws-cloud-host-s3Bucket/11a081b2-53cf-7823-bb08-2a7f75c4ccee/11a081b2-53cf-7823-bb08-2a7f75c4ccee.gif"
342        }
343 	 }
344  ]
345 
346  @example A search for assets where the data block filed is not null
347  /wf/restapi/v2/search
348 
349  Parameters:
350  fields = ["http://ns.mediabeacon.com/datablock/1.0/ datablock"]
351  search =
352  {
353      "conjunction":"or",
354      "criteria":
355          [
356              {
357                  "fieldId":"http://ns.mediabeacon.com/datablock/1.0/ datablock",
358                  "condition":"not"
359              }
360          ]
361  }
362 
363  Response:
364  [
365  {
366         "id": 201633044,
367         "fields": {
368             "http://ns.mediabeacon.com/datablock/1.0/ datablock": [
369                 {
370                     "http://ns.adobe.com/pdf/1.3/ PDFVersion": "1",
371                     "http://ns.adobe.com/pdf/1.3/ Keywords": "datablock0"
372                 },
373                 {
374                     "http://ns.adobe.com/pdf/1.3/ PDFVersion": "2",
375                     "http://ns.adobe.com/pdf/1.3/ Keywords": "datablock1"
376                 }
377             ]
378         }
379     }
380  ]
381 
382  @example A search for assets where the data block filed is null
383  /wf/restapi/v2/search
384 
385  Parameters:
386  fields = ["http://ns.mediabeacon.com/datablock/1.0/ datablock"]
387  search =
388  {
389      "conjunction":"or",
390      "criteria":
391          [
392              {
393                  "fieldId":"http://ns.mediabeacon.com/datablock/1.0/ datablock",
394                  "condition":"null"
395              }
396          ]
397  }
398 
399  Response:
400  [
401  {
402         "id": 201633048,
403         "fields": {
404             "http://ns.mediabeacon.com/datablock/1.0/ datablock": null
405         }
406     },
407  {
408         "id": 201633047,
409         "fields": {
410             "http://ns.mediabeacon.com/datablock/1.0/ datablock": null
411         }
412     }
413  ]
414  */
415 function main() {
416   try {
417     var aSavedSearchId = context.getParameter("savedSearchId");
418     var aSearchObject = context.getParameter("search");
419     var aQuickSearchTerm = context.getParameter("quickSearch");
420     mySearch = generateSearch(aSearchObject, aQuickSearchTerm);
421     if (!!aSavedSearchId) {
422       var aSavedSearch = searchManager.getSearch(aSavedSearchId);
423       if (!!aSearchObject || !!aQuickSearchTerm) {
424         var aSavedSearchCriterion = aSavedSearch
425             .getCriteria()
426             .getRootCriterion();
427         var aBuiltSearchCriterion = mySearch.getCriteria().getRootCriterion();
428         var aCombinedCriteria = new Criteria("and");
429         aCombinedCriteria.addCriterion(aSavedSearchCriterion);
430         aCombinedCriteria.addCriterion(aBuiltSearchCriterion);
431         mySearch.setCriteria(aCombinedCriteria);
432       }
433     }
434   } catch (anE) {
435     REST.pushError(
436         REST.errors.e400,
437         "Failed to build search with search=" + JSON.stringify(mySearch),
438         anE
439     );
440   }
441   REST.setCallback(search);
442   return REST.execute();
443 }
444 
445 main();