|
| 1 | +const HELP_TEXT_INDENT = 2; |
| 2 | +const EXPORTED_JSON_INDENT = 2; |
| 3 | +const HEADER_COMMENTED_PROP_SKIP = "#skip"; |
| 4 | +const HEADER_COMMENTED_PROP_ERROR = "#error"; |
| 5 | +const HEADER_PROP_ACCESSION = "accession"; |
| 6 | +const HEADER_PROP_UUID = "uuid"; |
| 7 | +const DEFAULT_PROP_PRIORITY = [ |
| 8 | + HEADER_COMMENTED_PROP_SKIP, HEADER_COMMENTED_PROP_ERROR, |
| 9 | + HEADER_PROP_ACCESSION, HEADER_PROP_UUID, "aliases", "award", "lab", |
| 10 | +]; |
| 11 | +const PRIORITY_INDENTIFYING_PROP = [HEADER_PROP_ACCESSION, HEADER_PROP_UUID]; |
| 12 | +const DEFAULT_EXPORTED_JSON_FILE_PREFIX = "encode-metadata-submitter.exported"; |
| 13 | +const TOOLTIP_FOR_PROP_SKIP = "Dry-run any REST actions (GET/PUT/POST)\n\nIf recent REST action is successful (200 or 201) then it is automatically set as 1 to prevent duplicate submission/retrieval."; |
| 14 | +const TOOLTIP_FOR_PROP_ERROR = "Recent REST action + HTTP error code + JSON response\n\n-200: Successful.\n-201: Successfully POSTed.\n-409: Found a conflict when POSTing\n"; |
| 15 | + |
| 16 | + |
| 17 | +function getTooltipForCommentedProp(prop) { |
| 18 | + if (prop === HEADER_COMMENTED_PROP_SKIP) { |
| 19 | + return TOOLTIP_FOR_PROP_SKIP; |
| 20 | + } |
| 21 | + else if(prop === HEADER_COMMENTED_PROP_ERROR) { |
| 22 | + return TOOLTIP_FOR_PROP_ERROR; |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +function getNumMetadataInSheet(sheet) { |
| 27 | + return getLastRow(sheet) - HEADER_ROW; |
| 28 | +} |
| 29 | + |
| 30 | +function makeMetadataUrl(method, profileName, endpoint, identifyingVal) { |
| 31 | + switch(method) { |
| 32 | + case "GET": |
| 33 | + return `${endpoint}/${profileName}/${identifyingVal}/?format=json&frame=edit`; |
| 34 | + case "PUT": |
| 35 | + return `${endpoint}/${profileName}/${identifyingVal}`; |
| 36 | + case "POST": |
| 37 | + return `${endpoint}/${profileName}`; |
| 38 | + default: |
| 39 | + Logger.log("makeMetadataUrl: Not supported method " + method); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +function getMetadataFromPortal(identifyingVal, identifyingProp, profileName, endpoint) { |
| 44 | + var url = makeMetadataUrl("GET", profileName, endpoint, identifyingVal); |
| 45 | + var response = restGet(url); |
| 46 | + var error = response.getResponseCode(); |
| 47 | + |
| 48 | + var object = { |
| 49 | + [HEADER_COMMENTED_PROP_ERROR]: "GET" + "," + error, |
| 50 | + // [HEADER_PROP_GET_URL]: url, |
| 51 | + [HEADER_COMMENTED_PROP_SKIP]: 0, |
| 52 | + [identifyingProp]: identifyingVal |
| 53 | + }; |
| 54 | + |
| 55 | + var responseJson = JSON.parse(response.getContentText()); |
| 56 | + if (error === 200) { |
| 57 | + // if no error, merge parsed JSON to row object |
| 58 | + object[HEADER_COMMENTED_PROP_SKIP] = 1; |
| 59 | + object = {...object, ...responseJson}; |
| 60 | + } |
| 61 | + else { |
| 62 | + // if error, write helpText to provide debugging information |
| 63 | + object[HEADER_COMMENTED_PROP_ERROR] += "\n" + JSON.stringify(responseJson, null, HELP_TEXT_INDENT); |
| 64 | + } |
| 65 | + return object; |
| 66 | +} |
| 67 | + |
| 68 | +function getSortedProps(props, profile, propPriority=DEFAULT_PROP_PRIORITY) { |
| 69 | + // sort metadata's props by given profile and propPriority |
| 70 | + // - props in propPriority come first if exists |
| 71 | + // - and then props under required key in profile come next |
| 72 | + // - all the other commented (#) props come last |
| 73 | + var sortedProps = []; |
| 74 | + |
| 75 | + // priority props first |
| 76 | + for (var prop of propPriority.concat(profile["required"])) { |
| 77 | + if (props.includes(prop) && !sortedProps.includes(prop)) { |
| 78 | + sortedProps.push(prop); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + // non-commented props |
| 83 | + for (var prop of props) { |
| 84 | + if (!prop.startsWith("#") && !sortedProps.includes(prop)) { |
| 85 | + sortedProps.push(prop); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + // and then commented props |
| 90 | + for (var prop of props) { |
| 91 | + if (prop.startsWith("#") && !sortedProps.includes(prop)) { |
| 92 | + sortedProps.push(prop); |
| 93 | + } |
| 94 | + } |
| 95 | + return sortedProps; |
| 96 | +} |
| 97 | + |
| 98 | + |
| 99 | +getIdentifyingPropForProfile |
| 100 | + |
| 101 | +function updateSheetWithMetadataFromPortal(sheet, profileName, endpointForGet, endpointForProfile) { |
| 102 | + var profile = getProfile(profileName, endpointForProfile); |
| 103 | + if (!profile) { |
| 104 | + Logger.log(`Couldn't find a profile schema ${profile} for profile name ${profileName}.`) |
| 105 | + return 0; |
| 106 | + } |
| 107 | + |
| 108 | + var identifyingProp = getIdentifyingPropForProfile(profile); |
| 109 | + if (!identifyingProp) { |
| 110 | + Logger.log(`Could't find identifying property ${identifyingProp} in header row ${HEADER_ROW}.`); |
| 111 | + return -1; |
| 112 | + } |
| 113 | + var identifyingCol = findColumnByHeaderValue(sheet, identifyingProp); |
| 114 | + |
| 115 | + // also check #skip column exists. if so skip row with #skip===1 |
| 116 | + var skipCol = findColumnByHeaderValue(sheet, HEADER_COMMENTED_PROP_SKIP); |
| 117 | + |
| 118 | + // update each row if has accession value |
| 119 | + var numUpdated = 0; |
| 120 | + for (var row = HEADER_ROW + 1; row <= getLastRow(sheet); row++) { |
| 121 | + var identifyingVal = getCellValue(sheet, row, identifyingCol); |
| 122 | + if (skipCol && toBoolean(getCellValue(sheet, row, skipCol))) { |
| 123 | + continue; |
| 124 | + } |
| 125 | + if (identifyingVal) { |
| 126 | + var metadataObj = getMetadataFromPortal( |
| 127 | + identifyingVal, identifyingProp, profileName, endpointForGet |
| 128 | + ); |
| 129 | + var sortedProps = getSortedProps(Object.keys(metadataObj), profile); |
| 130 | + writeJsonToRow(sheet, metadataObj, row, sortedProps); |
| 131 | + numUpdated++; |
| 132 | + } |
| 133 | + } |
| 134 | + return numUpdated; |
| 135 | +} |
| 136 | + |
| 137 | +function exportSheetToJsonFile(sheet, profileName, endpointForProfile, keepCommentedProps, jsonFilePath) { |
| 138 | + var profile = getProfile(profileName, endpointForProfile); |
| 139 | + |
| 140 | + var result = []; |
| 141 | + for (var row = HEADER_ROW + 1; row <= getLastRow(sheet); row++) { |
| 142 | + var jsonBeforeTypeCast = rowToJson( |
| 143 | + sheet, row, keepCommentedProps=false, bypassGoogleAutoParsing=true |
| 144 | + ); |
| 145 | + var json = typeCastJsonValuesByProfile(profile, jsonBeforeTypeCast); |
| 146 | + result.push(json); |
| 147 | + } |
| 148 | + DriveApp.createFile(jsonFilePath, JSON.stringify(result, null, EXPORTED_JSON_INDENT)); |
| 149 | +} |
| 150 | + |
| 151 | +function putSheetToPortal(sheet, profileName, endpointForPut, endpointForProfile, method="PUT") { |
| 152 | + // returns actual number of submitted rows |
| 153 | + var profile = getProfile(profileName, endpointForProfile); |
| 154 | + var identifyingProp = getIdentifyingPropForProfile(profile); |
| 155 | + |
| 156 | + const numData = getNumMetadataInSheet(sheet); |
| 157 | + var numSubmitted = 0; |
| 158 | + |
| 159 | + for (var row = HEADER_ROW + 1; row <= numData + HEADER_ROW; row++) { |
| 160 | + var jsonBeforeTypeCast = rowToJson( |
| 161 | + sheet, row, keepCommentedProps=true, bypassGoogleAutoParsing=true |
| 162 | + ); |
| 163 | + |
| 164 | + // if has #skip and it is 1 then skip |
| 165 | + if (jsonBeforeTypeCast.hasOwnProperty(HEADER_COMMENTED_PROP_SKIP)) { |
| 166 | + if (toBoolean(jsonBeforeTypeCast[HEADER_COMMENTED_PROP_SKIP])) { |
| 167 | + continue; |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + var json = typeCastJsonValuesByProfile( |
| 172 | + profile, jsonBeforeTypeCast, keepCommentedProps=false |
| 173 | + ); |
| 174 | + |
| 175 | + switch(method) { |
| 176 | + case "PUT": |
| 177 | + |
| 178 | + var url = makeMetadataUrl(method, profileName, endpointForPut, json[identifyingProp]); |
| 179 | + var response = restPut(url, payloadJson=json); |
| 180 | + break; |
| 181 | + |
| 182 | + case "POST": |
| 183 | + var url = makeMetadataUrl(method, profileName, endpointForPut); |
| 184 | + var response = restPost(url, payloadJson=json); |
| 185 | + break; |
| 186 | + |
| 187 | + default: |
| 188 | + Logger.log("putSheetToPortal: Wrong REST method " + method); |
| 189 | + continue; |
| 190 | + } |
| 191 | + |
| 192 | + var error = response.getResponseCode(); |
| 193 | + var responseJson = JSON.parse(response.getContentText()); |
| 194 | + |
| 195 | + json[HEADER_COMMENTED_PROP_ERROR] = method + "," + error; |
| 196 | + |
| 197 | + switch(error) { |
| 198 | + case 200: |
| 199 | + json[HEADER_COMMENTED_PROP_SKIP] = 1; |
| 200 | + break; |
| 201 | + |
| 202 | + case 201: |
| 203 | + var identifyingVal = responseJson["@graph"][0][identifyingProp]; |
| 204 | + json[identifyingProp] = identifyingVal; |
| 205 | + json[HEADER_COMMENTED_PROP_ERROR] += "\n" + JSON.stringify(responseJson, null, HELP_TEXT_INDENT); |
| 206 | + json[HEADER_COMMENTED_PROP_SKIP] = 1; |
| 207 | + break; |
| 208 | + |
| 209 | + default: |
| 210 | + json[HEADER_COMMENTED_PROP_ERROR] += "\n" + JSON.stringify(responseJson, null, HELP_TEXT_INDENT); |
| 211 | + json[HEADER_COMMENTED_PROP_SKIP] = 0; |
| 212 | + } |
| 213 | + |
| 214 | + // rewrite data, with commented headers such as error and text, on the sheet |
| 215 | + writeJsonToRow(sheet, json, row); |
| 216 | + numSubmitted++; |
| 217 | + } |
| 218 | + return numSubmitted; |
| 219 | +} |
| 220 | + |
| 221 | +function postSheetToPortal(sheet, profileName, endpointForPost, endpointForProfile) { |
| 222 | + // returns actual number of submitted rows |
| 223 | + return putSheetToPortal(sheet, profileName, endpointForPost, endpointForProfile, method="POST"); |
| 224 | +} |
| 225 | + |
| 226 | +function addTemplateMetadataToSheet(sheet, profile) { |
| 227 | + var metadataObj = makeTemplateMetadataObjectFromProfile(profile); |
| 228 | + var sortedProps = getSortedProps(Object.keys(metadataObj), profile); |
| 229 | + addJsonToSheet(sheet, metadataObj, sortedProps); |
| 230 | +} |
0 commit comments