This repository has been archived by the owner on Apr 30, 2021. It is now read-only.
/
GetThemeFeaturesByID.js
73 lines (60 loc) · 2.29 KB
/
GetThemeFeaturesByID.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//4.3.2014 - Ryan Whitley
var flow = require('flow');
var common = require("../../../common"),
settings = require('../../../private/settings'),
shortid = require('shortid');
var operation = {};
/* METADATA */
operation.name = "GetThemeFeaturesByID";
operation.description = "Gets theme-based features and properties based on GADM ID and level.";
operation.inputs = {};
operation.outputImage = false;
operation.inputs["ids"] = {}; //comma separated list of ids
operation.inputs["theme"] = {}; //string - theme name
operation.Query = "SELECT ST_AsGeoJSON(geom) as geom, theme_count, level FROM vw_theme_{{theme}}_gadm WHERE stack_guid IN ({{ids}})";
operation.execute = flow.define(
function (args, callback) {
this.args = args;
this.callback = callback;
//Step 1
//Generate UniqueID for this Task
operation.id = shortid.generate();
//See if inputs are set. Incoming arguments should contain the same properties as the input parameters.
if (operation.isInputValid(args) === true) {
operation.inputs["ids"] = args.ids;
operation.inputs["theme"] = args.theme.toLowerCase();
//need to wrap ids in single quotes
//Execute the query
var query;
query = { text: operation.Query.replace('{{theme}}', operation.inputs["theme"]).split("{{ids}}").join(operation.wrapIdsInQuotes(args.ids)) };
common.executePgQuery(query, this);//Flow to next function when done.
}
else {
//Invalid arguments
//return message
callback("Missing or invalid required arguments: theme or ids"); //err is first argument
}
},
function (err, results) {
//Step 2 - get the results and pass back to calling function
this.callback(err, results);
}
)
//Make sure arguments are tight before executing
operation.isInputValid = function (input) {
//Test to see if inputs are specified
var isValid = false;
if (input) {
if (input["ids"] && input["theme"]) {
//It's got everything we need.
return true;
}
}
return isValid;
}
operation.wrapIdsInQuotes = function(ids){
return ids.split(',').map(function(item){
return "'" + item + "'";
});
}
module.exports = operation;