1 var REST = library.REST.REST();
  2 var myData = {};
  3 var myProperties = [];
  4 
  5 /**
  6  * Builds a Boolean criterion
  7  * @returns BooleanCriterion
  8  */
  9 function buildCriterion(theJSONSearch) {
 10   try {
 11     var aBooleanCriterion = new BooleanCriterion(theJSONSearch.conjunction);
 12     for (var i = 0; i < theJSONSearch.criteria.length; i++) {
 13       var aJSONCriterion = theJSONSearch.criteria[i];
 14       var aCriterion;
 15       if (aJSONCriterion.conjunction) {
 16         aCriterion = buildCriterion(aJSONCriterion);
 17       } else {
 18         if (!aJSONCriterion.hasOwnProperty("schema")) {
 19           aCriterion = new DatabaseCriterion(aJSONCriterion.field);
 20         } else {
 21           aCriterion = new XmpCriterion(
 22             new Property(aJSONCriterion.schema, aJSONCriterion.field)
 23           );
 24         }
 25         aCriterion.setCondition(aJSONCriterion.condition);
 26         aCriterion.setValue(aJSONCriterion.value);
 27       }
 28       aBooleanCriterion.addCriterion(aCriterion);
 29     }
 30   } catch (err) {
 31     REST.submitError("format", err.toString());
 32   }
 33   return aBooleanCriterion;
 34 }
 35 
 36 /**
 37  * returns a Search wrapper created from the JSON search object
 38  * @param theJSONSearch
 39  * @returns SearchWrapper
 40  */
 41 function generateSearch(theJSONSearch) {
 42   var aSearch = new Search();
 43   var aCriteria = new Criteria("and");
 44   aCriteria.addCriterion(buildCriterion(theJSONSearch));
 45   aSearch.setCriteria(aCriteria);
 46   return aSearch;
 47 }
 48 
 49 /**
 50  * Assembles the return fields. This should either be in the 'fields' parameter or in the search criterion if the fields
 51  * parameter is undefined.
 52  *
 53  * @param theFields the fields to return;
 54  */
 55 function assembleReturnFields(theFields) {
 56   for (var i in theFields) {
 57     if (!theFields.hasOwnProperty(i)) {
 58       continue;
 59     }
 60 
 61     var aField = theFields[i];
 62     if (aField.conjunction) {
 63       aCriterion = assembleReturnFields(aField.criteria);
 64     } else {
 65       if (!aField.hasOwnProperty("schema")) {
 66         myProperties[myProperties.length] = new Property(
 67           "database",
 68           aField.field
 69         );
 70       } else {
 71         myProperties[myProperties.length] = new Property(
 72           aField.schema,
 73           aField.field
 74         );
 75       }
 76     }
 77   }
 78 }
 79 
 80 /**
 81  * Adds a result to the result array in myData
 82  * @param theManagedFile
 83  */
 84 function formatResults(theManagedFile) {
 85   var aResult = {};
 86   aResult.name = theManagedFile.name;
 87   for (var i = 0; i < myProperties.length; i++) {
 88     var aProperty = myProperties[i];
 89     if (aProperty.namespace === "database") {
 90       aResult[myProperties[i].name] = theManagedFile[aProperty.name];
 91     } else {
 92       var aVal = theManagedFile.xmp.meta.getField(myProperties[i]).toString();
 93       try {
 94         aVal = JSON.parse(aVal);
 95       } catch (theE) {
 96         // If it errors out, we don't really care. We just want .
 97       }
 98       aResult[myProperties[i].name] = aVal;
 99     }
100   }
101   myData[theManagedFile.assetId] = aResult;
102 }
103 
104 /**
105  * Builds an Advanced Search and returns the search results.
106  * @description Applies the configurations provided in 'search' to the Advanced Search, and returns the search results as AssetID's and field values. These values are parsed into a text object keyed by AssetID from the JSON file in which they are contained, and by default are displayed to the user.
107  * @example /wf/restapi/1/search?search={conjunction:'and/or',criteria:[{schema:'Schema1',field:'Field1',condition:'(see below)',value:'Value1'},{schema:'Schema2',field:'Field2',condition:'(see below)',value:'Value2'}]}
108  * @example <a target="_blank" href=http://127.0.0.1:55555/wf/restapi/1/search?search={"conjunction":"and","criteria":[{"schema":"http://purl.org/dc/elements/1.1/","field":"subject","condition":"not","value":""}]}>http://127.0.0.1:55555/wf/restapi/1/search?search={"conjunction":"and","criteria":[{"schema":"http://purl.org/dc/elements/1.1/","field":"subject","condition":"not","value":""}]}</a>
109  * @class Builds an Advanced Search and returns the search results.
110  * @name Search
111  * @param search Used to fill out the Advanced Search parameters. The object structure is {conjunction:'..',criteria:[{schema:'..',field:'..',condition:'..',value:'..'}, {conjunction:'..',criteria:[]},... ]}
112  * @param fields The fields to grab. This is optional and will grab the fields in the search if this is not defined.
113  * @param pageSize default 100
114  * @param pageNumber default 0
115  * @param sortDirection "ASC" or "DESC"
116  * @param sortField the namespace of the desired field ex "record_id" or  "http://purl.org/dc/elements/1.1/ subject"
117  <br>
118  * Supported Search Conditions
119  * <ul>
120  <li>Container Fields</li>
121  <ul>
122  <li>cont</li>
123  <li>not_cont</li>
124  <li>not</li>
125  <li>null</li>
126  <li>cont_part</li>
127  <li>not_cont_part</li>
128  </ul>
129  <li>Integer Fields</li>
130  <ul>
131  <li>not</li>
132  <li>null</li>
133  <li>eq</li>
134  <li>ne</li>
135  <li>le</li>
136  <li>lt</li>
137  <li>ge</li>
138  <li>gt</li>
139  <li>anyval</li>
140  <li>notchanged</li>
141  </ul>
142  <li>Hierarchy Fields</li>
143  <ul>
144  <li>not</li>
145  <li>null</li>
146  <li>eq</li>
147  <li>ne</li>
148  <li>cont</li>
149  <li>not_cont</li>
150  <li>anyval</li>
151  <li>notchanged</li>
152  </ul>
153  <li>Date Fields</li>
154  <ul>
155  <li>not</li>
156  <li>null</li>
157  <li>eq</li>
158  <li>ne</li>
159  <li>le</li>
160  <li>lt</li>
161  <li>ge</li>
162  <li>gt</li>
163  <li>between</li>
164  <li>anyval</li>
165  <li>notchanged</li>
166  </ul>
167  <li>Other Fields</li>
168  <ul>
169  <li>not</li>
170  <li>null</li>
171  <li>eq</li>
172  <li>ne</li>
173  <li>beg</li>
174  <li>not_beg</li>
175  <li>end</li>
176  <li>not_end</li>
177  <li>cont</li>
178  <li>not_cont</li>
179  <li>bulkIs</li>
180  <li>bulkStart</li>
181  <li>all</li>
182  <li>any</li>
183  <li>phrase</li>
184  <li>wo</li>
185  <li>anyval</li>
186  <li>notchanged</li>
187  </ul>
188  </ul>
189  *
190  *<br>
191  * English Translations
192  * <ul>
193  *     <li>ne = Does not equal</li>
194  *     <li>lt = Less than</li>
195  *     <li>le = Less than or equal to</li>
196  *     <li>gt = Greater than</li>
197  *     <li>ge = Greater than or equal to</li>
198  *     <li>between = Between</li>
199  *     <li>cont = Contains</li>
200  *     <li>not_cont = Does not contain</li>
201  *     <li>beg = Begins with</li>
202  *     <li>not_beg = Does not begin with</li>
203  *     <li>end = Ends with</li>
204  *     <li>not_end = does not end with</li>
205  *     <li>null = is null</li>
206  *     <li>not = is not null</li>
207  *     <li>cont_part = Contains partial value</li>
208  *     <li>not_cont_part = Does not contain partial value</li>
209  *     <li>anyval = Any value</li>
210  *     <li>all = All words</li>
211  *     <li>any = Any Words</li>
212  *     <li>phrase = Has Exact phrase</li>
213  *     <li>wo = Without word</li>
214  * </ul>
215  *
216  * @returns ( {"AssetID": {"name": "Name","Field1": "Asset Value for Field1","Field2": "Asset Value for Field2"},...}  )
217  */
218 function main() {
219   var aJSONSearch = REST.getParameter("search", true);
220   var aReturnFields = REST.getParameter("fields", false);
221   try {
222     var aSearch = generateSearch(aJSONSearch);
223   } catch (anE) {
224     REST.submitError("search", anE);
225   }
226 
227   try {
228     assembleReturnFields(aReturnFields ? aReturnFields : aJSONSearch.criteria);
229   } catch (anE) {
230     REST.submitError("return fields", anE);
231   }
232   if (myData.hasOwnProperty("error")) {
233     return REST.formatResponse();
234   }
235   REST.executeWithPaging(aSearch, formatResults);
236   return REST.formatResponse();
237 }
238 
239 main();
240