1 /**
  2  * @fileOverview
  3  * @author <a href="https://www.labkey.org">LabKey</a> (<a href="mailto:info@labkey.com">info@labkey.com</a>)
  4  * @license Copyright (c) 2018 LabKey Corporation
  5  * <p/>
  6  * Licensed under the Apache License, Version 2.0 (the "License");
  7  * you may not use this file except in compliance with the License.
  8  * You may obtain a copy of the License at
  9  * <p/>
 10  * http://www.apache.org/licenses/LICENSE-2.0
 11  * <p/>
 12  * Unless required by applicable law or agreed to in writing, software
 13  * distributed under the License is distributed on an "AS IS" BASIS,
 14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  * See the License for the specific language governing permissions and
 16  * limitations under the License.
 17  * <p/>
 18  */
 19 LABKEY.Assay = new function(impl) {
 20 
 21     /**
 22      * Documentation specified in core/Assay.js -- search for "@name importRun"
 23      */
 24     impl.importRun = function(config) {
 25         if (!window.FormData)
 26             throw new Error("modern browser required");
 27 
 28         if (!config.assayId)
 29             throw new Error("assayId required");
 30 
 31         var files = [];
 32         if (config.files) {
 33             for (var i = 0; i < config.files.length; i++) {
 34                 var f = config.files[i];
 35                 if (f instanceof window.File) {
 36                     files.push(f);
 37                 }
 38                 else if (f.tagName == "INPUT") {
 39                     for (var j = 0; j < f.files.length; j++) {
 40                         files.push(f.files[j]);
 41                     }
 42                 }
 43             }
 44         }
 45 
 46         if (files.length == 0 && !config.runFilePath && !config.dataRows)
 47             throw new Error("At least one of 'file', 'runFilePath', or 'dataRows' is required");
 48 
 49         if ((files.length > 0 ? 1 : 0) + (config.runFilePath ? 1 : 0) + (config.dataRows ? 1 : 0) > 1)
 50             throw new Error("Only one of 'file', 'runFilePath', or 'dataRows' is allowed");
 51 
 52         var formData = new FormData();
 53         formData.append("assayId", config.assayId);
 54         if (config.name)
 55             formData.append("name", config.name);
 56         if (config.comment)
 57             formData.append("comment", config.comment);
 58         if (config.batchId)
 59             formData.append("batchId", config.batchId);
 60 
 61         if (config.properties) {
 62             for (var key in config.properties) {
 63                 if (LABKEY.Utils.isObject(config.properties[key]))
 64                     formData.append("properties['" + key + "']", JSON.stringify(config.properties[key]));
 65                 else
 66                     formData.append("properties['" + key + "']", config.properties[key]);
 67             }
 68         }
 69 
 70         if (config.batchProperties) {
 71             for (var key in config.batchProperties) {
 72                 if (LABKEY.Utils.isObject(config.batchProperties[key]))
 73                     formData.append("batchProperties['" + key + "']", JSON.stringify(config.batchProperties[key]));
 74                 else
 75                     formData.append("batchProperties['" + key + "']", config.batchProperties[key]);
 76             }
 77         }
 78 
 79         if (config.dataRows)
 80             formData.append("dataRows", JSON.stringify(config.dataRows));
 81 
 82         if (config.plateMetadata)
 83             formData.append("plateMetadata", JSON.stringify(config.plateMetadata));
 84 
 85         if (config.runFilePath)
 86             formData.append("runFilePath", config.runFilePath);
 87 
 88         if (files && files.length > 0) {
 89             formData.append("file", files[0]);
 90             for (var i = 1; i < files.length; i++) {
 91                 formData.append("file" + i, files[i]);
 92             }
 93         }
 94 
 95         LABKEY.Ajax.request({
 96             method: 'POST',
 97             url: LABKEY.ActionURL.buildURL("assay", "importRun.api", config.containerPath),
 98             success: LABKEY.Utils.getCallbackWrapper(LABKEY.Utils.getOnSuccess(config), config.scope, false),
 99             failure: LABKEY.Utils.getCallbackWrapper(LABKEY.Utils.getOnFailure(config), config.scope, true),
100             form: formData
101         });
102     };
103 
104     return impl;
105 
106 }(LABKEY.Assay || new function() { return {}; });