|
| 1 | +(function (angular) { |
| 2 | + 'use strict'; |
| 3 | + /** |
| 4 | + * @memberof spApp |
| 5 | + * @ngdoc controller |
| 6 | + * @name AddAPointsCtrl |
| 7 | + * @description |
| 8 | + * Add points to the map using spatial-service's sandbox services |
| 9 | + */ |
| 10 | + angular.module('add-points-ctrl', ['map-service', 'layers-service', 'predefined-areas-service']) |
| 11 | + .controller('AddPointsCtrl', ['LayoutService', '$scope', 'MapService', '$timeout', 'LayersService', |
| 12 | + '$uibModalInstance', 'PredefinedAreasService', 'BiocacheService', 'LoggerService', 'data', |
| 13 | + function (LayoutService, $scope, MapService, $timeout, LayersService, $uibModalInstance, PredefinedAreasService, BiocacheService, LoggerService, inputData) { |
| 14 | + |
| 15 | + $scope.inputData = inputData; |
| 16 | + |
| 17 | + $scope.step = 'default'; |
| 18 | + $scope.method = 'existing'; |
| 19 | + |
| 20 | + $scope.errorMsg = ''; |
| 21 | + |
| 22 | + $scope.datasetName = 'my dataset name'; |
| 23 | + $scope.message = ''; |
| 24 | + $scope.status = 'queued'; |
| 25 | + $scope.statusUrl = ''; |
| 26 | + $scope.dataResourceUid = ''; |
| 27 | + |
| 28 | + $scope.maxFileSize = $SH.maxUploadSize; |
| 29 | + |
| 30 | + $scope.uploadingFile = false; |
| 31 | + $scope.uploadProgress = 0; |
| 32 | + |
| 33 | + $scope.file = null; |
| 34 | + |
| 35 | + $scope.uploadsList = []; |
| 36 | + $scope.searchUploads = ''; |
| 37 | + $scope.sortType = 'date'; |
| 38 | + $scope.sortReverse = true; |
| 39 | + |
| 40 | + LayoutService.addToSave($scope); |
| 41 | + |
| 42 | + $scope.init = function () { |
| 43 | + // get a list of all prior uploads |
| 44 | + if ($SH.userId) { |
| 45 | + // find existing old sandbox uploads |
| 46 | + if ($SH.sandboxServiceUrl && $SH.sandboxUrl) { |
| 47 | + BiocacheService.userUploads($SH.userId, $SH.sandboxServiceUrl).then(function (data) { |
| 48 | + |
| 49 | + if (data.totalRecords === 0) { |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + // add bs and ws to each item |
| 54 | + var items = data.facetResults[0].fieldResult; |
| 55 | + items.forEach(function (item) { |
| 56 | + item.bs = $SH.sandboxServiceUrl; |
| 57 | + item.ws = $SH.sandboxUrl; |
| 58 | + // get dataset_name and last_load_date |
| 59 | + BiocacheService.searchForOccurrences({ |
| 60 | + qid: item.fq, // skip qid registration for this one-off query |
| 61 | + bs: item.bs, |
| 62 | + ws: item.ws |
| 63 | + }, [], 0, 0, 'dataset_name,last_processed_date').then(function (data) { |
| 64 | + if (data.totalRecords > 0) { |
| 65 | + // handle facets returning in a different order |
| 66 | + var order = data.facetResults[0].fieldName === 'dataset_name' ? 0 : 1; |
| 67 | + item.label = data.facetResults[order === 0 ? 0 : 1].fieldResult[0].label; |
| 68 | + item.date = data.facetResults[order === 0 ? 1 : 0].fieldResult[0].label; |
| 69 | + |
| 70 | + // format the date so that it is sortable. It is currently a string, e.g. "2010-11-01T00:00:00Z" |
| 71 | + item.date = new Date(item.date).toISOString().slice(0, 10); |
| 72 | + |
| 73 | + item.addedToMap = false; |
| 74 | + |
| 75 | + item.old = true; |
| 76 | + |
| 77 | + $scope.uploadsList.push(item); |
| 78 | + } |
| 79 | + }); |
| 80 | + }); |
| 81 | + }); |
| 82 | + } |
| 83 | + |
| 84 | + // find existing spatial-service sandbox uploads |
| 85 | + BiocacheService.userUploads($SH.userId, $SH.sandboxSpatialServiceUrl).then(function (data) { |
| 86 | + if (data.totalRecords === 0) { |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + // add bs and ws to each item |
| 91 | + var items = data.facetResults[0].fieldResult; |
| 92 | + items.forEach(function (item) { |
| 93 | + item.bs = $SH.sandboxSpatialServiceUrl; |
| 94 | + item.ws = $SH.sandboxSpatialUiUrl; |
| 95 | + // get dataset_name and last_load_date |
| 96 | + BiocacheService.searchForOccurrences({ |
| 97 | + qid: item.fq, // skip qid registration for this one-off query |
| 98 | + bs: item.bs, |
| 99 | + ws: item.ws |
| 100 | + }, [], 0, 0, 'datasetName,lastProcessedDate').then(function (data) { |
| 101 | + if (data.totalRecords > 0) { |
| 102 | + // handle facets returning in a different order |
| 103 | + var order = data.facetResults[0].fieldName === 'datasetName' ? 0 : 1; |
| 104 | + item.label = data.facetResults[order === 0 ? 0 : 1].fieldResult[0].label; |
| 105 | + item.date = data.facetResults[order === 0 ? 1 : 0].fieldResult[0].label; |
| 106 | + |
| 107 | + // format the date so that it is sortable. It is currently a string, e.g. "2010-11-01T00:00:00Z" |
| 108 | + item.date = new Date(item.date).toISOString().slice(0, 10); |
| 109 | + |
| 110 | + item.addedToMap = false; |
| 111 | + |
| 112 | + item.old = false; |
| 113 | + |
| 114 | + $scope.uploadsList.push(item); |
| 115 | + } |
| 116 | + }); |
| 117 | + }); |
| 118 | + }); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + $scope.uploadFile = function (newFiles) { |
| 123 | + |
| 124 | + if (newFiles == null || newFiles.length == 0) { |
| 125 | + return |
| 126 | + } |
| 127 | + |
| 128 | + var file = newFiles[0] |
| 129 | + |
| 130 | + if (file.$error) { |
| 131 | + if (file.$errorMessages.maxSize) { |
| 132 | + bootbox.alert($i18n(476, "The uploaded file is too large. Max file size:") + " " + Math.floor($scope.maxFileSize / 1024 / 1024) + "MB"); |
| 133 | + return |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + $scope.file = file; |
| 138 | + |
| 139 | + // remove file extension and add date/time |
| 140 | + var dateTime = new Date().toLocaleString(); |
| 141 | + var newName = file.name.replace(/\.[^/.]+$/, "") + " " + dateTime; |
| 142 | + $scope.datasetName = newName.substring(0, 200); // limit to 200 characters |
| 143 | + }; |
| 144 | + |
| 145 | + $scope.checkStatus = function() { |
| 146 | + LayersService.getSandboxUploadStatus($scope.statusUrl).then(function (data) { |
| 147 | + $scope.status = data.status; |
| 148 | + $scope.message = data.message; |
| 149 | + |
| 150 | + if ($scope.status === 'running') { |
| 151 | + $timeout(function () { |
| 152 | + $scope.checkStatus(); |
| 153 | + }, 3000); // wait 3 seconds before checking status |
| 154 | + } else if ($scope.status === 'finished') { |
| 155 | + // successful |
| 156 | + } |
| 157 | + }, function (error) { |
| 158 | + if (!error.handled) { |
| 159 | + $scope.status = 'error'; |
| 160 | + if (error.data.error) { |
| 161 | + $scope.message = error.data.error; |
| 162 | + } else { |
| 163 | + $scope.message = "status code: " + error.status; |
| 164 | + } |
| 165 | + } |
| 166 | + }); |
| 167 | + } |
| 168 | + |
| 169 | + // currently removing the auto close step, so that the user can read the last status message |
| 170 | + $scope.addToMapAndClose = function () { |
| 171 | + var q = { |
| 172 | + q: ['dataResourceUid:"' + $scope.dataResourceUid + '"'], |
| 173 | + name: $scope.datasetName, |
| 174 | + bs: $SH.sandboxSpatialServiceUrl, |
| 175 | + ws: $SH.sandboxSpatialUiUrl |
| 176 | + }; |
| 177 | + |
| 178 | + if (!$scope.logged) { |
| 179 | + $scope.logged = true |
| 180 | + |
| 181 | + LoggerService.log("Create", "Points", {query: q, name: $scope.datasetName}) |
| 182 | + } |
| 183 | + |
| 184 | + BiocacheService.newLayer(q, undefined, q.name).then(function (data) { |
| 185 | + if (data != null) { |
| 186 | + MapService.add(data); |
| 187 | + } |
| 188 | + $scope.$close(); |
| 189 | + }); |
| 190 | + }; |
| 191 | + |
| 192 | + $scope.ok = function () { |
| 193 | + if ($scope.errorMsg || $scope.status === 'error') { |
| 194 | + $scope.$close(); |
| 195 | + } else if ($scope.status === 'finished') { |
| 196 | + $scope.addToMapAndClose(); |
| 197 | + } else { |
| 198 | + $scope.step = 'uploading'; |
| 199 | + $scope.uploadingFile = true; |
| 200 | + |
| 201 | + LayersService.uploadSandboxFile($scope.file, $scope.datasetName, $scope.file.name).then(function (response) { |
| 202 | + if (response.data.error) { |
| 203 | + $scope.status = 'error'; |
| 204 | + $scope.uploadingFile = false; |
| 205 | + $scope.errorMsg = response.data.error; |
| 206 | + return |
| 207 | + } else { |
| 208 | + $scope.status = 'starting'; |
| 209 | + $scope.statusUrl = response.data.statusUrl; |
| 210 | + $scope.message = response.data.message; |
| 211 | + $scope.dataResourceUid = response.data.dataResourceUid; |
| 212 | + |
| 213 | + $timeout(function () { |
| 214 | + $scope.checkStatus(); |
| 215 | + }, 3000); // wait 3 seconds before checking status |
| 216 | + } |
| 217 | + |
| 218 | + $scope.uploadingFile = false; |
| 219 | + }, function (error) { |
| 220 | + $scope.errorMsg = "Unexpected error."; |
| 221 | + if (!error.handled) { |
| 222 | + if (error.status == 500) { |
| 223 | + $scope.errorMsg = "Unexpected error: the uploaded file may be broken or unrecognised."; |
| 224 | + } else { |
| 225 | + if (error.data.error) { |
| 226 | + $scope.errorMsg = error.data.error; |
| 227 | + } else { |
| 228 | + $scope.errorMsg = $i18n(540,"An error occurred. Please try again and if the same error occurs, send an email to support@ala.org.au and include the URL to this page, the error message and what steps you performed that triggered this error."); |
| 229 | + } |
| 230 | + } |
| 231 | + } |
| 232 | + $scope.uploadingFile = false; |
| 233 | + |
| 234 | + }, function (evt) { |
| 235 | + $scope.uploadProgress = parseInt(100.0 * evt.loaded / evt.total); |
| 236 | + }); |
| 237 | + } |
| 238 | + } |
| 239 | + |
| 240 | + $scope.isDisabled = function () { |
| 241 | + if ($scope.step === 'uploading') { |
| 242 | + return $scope.status !== 'finished' && $scope.status !== 'error' |
| 243 | + } else { |
| 244 | + return $scope.file == null || $scope.uploadingFile; |
| 245 | + } |
| 246 | + } |
| 247 | + |
| 248 | + $scope.isLoggedIn = $scope.isLoggedIn = $SH.userId !== undefined && $SH.userId !== null && $SH.userId.length > 0; |
| 249 | + $scope.isNotLoggedIn = !$scope.isLoggedIn; |
| 250 | + |
| 251 | + $scope.addToMap = function (item) { |
| 252 | + item.addedToMap = true; |
| 253 | + |
| 254 | + BiocacheService.registerLayer(item.bs, item.ws, [item.fq], undefined, undefined, true, true, item.label).then(function (data) { |
| 255 | + if (data != null) { |
| 256 | + MapService.add(data); |
| 257 | + } |
| 258 | + }); |
| 259 | + } |
| 260 | + |
| 261 | + $scope.delete = function (item) { |
| 262 | + bootbox.confirm("Are you sure you want to delete \"" + item.label + "\?", function (result) { |
| 263 | + if (result) { |
| 264 | + // extract id from item.fq with the content `data_resource_uid:"39632cdd-4e1f-41d8-922a-c09a68270b2d"` |
| 265 | + var dataResourceUid = item.fq.split('"')[1]; |
| 266 | + |
| 267 | + LayersService.deleteSandboxUpload(dataResourceUid).then(function (data) { |
| 268 | + if (data != null) { |
| 269 | + $scope.uploadsList = $scope.uploadsList.filter(function (i) { |
| 270 | + return i.fq !== item.fq; |
| 271 | + }); |
| 272 | + } |
| 273 | + }); |
| 274 | + } |
| 275 | + }); |
| 276 | + } |
| 277 | + |
| 278 | + $scope.download = function (item) { |
| 279 | + var url = item.ws + "/occurrences/search?q=" + item.fq; |
| 280 | + var a = document.createElement('a'); |
| 281 | + a.target = '_blank'; |
| 282 | + a.href = url; |
| 283 | + a.click(); |
| 284 | + } |
| 285 | + |
| 286 | + $scope.searchFilter = function(item) { |
| 287 | + if (!$scope.searchUploads) { |
| 288 | + return true; |
| 289 | + } |
| 290 | + var searchText = $scope.searchUploads.toLowerCase(); |
| 291 | + return item.label.toLowerCase().includes(searchText); |
| 292 | + }; |
| 293 | + |
| 294 | + $scope.showFinished = function() { |
| 295 | + return st |
| 296 | + } |
| 297 | + |
| 298 | + $scope.init(); |
| 299 | + |
| 300 | + |
| 301 | + }]) |
| 302 | + |
| 303 | +}(angular)); |
0 commit comments