-
Notifications
You must be signed in to change notification settings - Fork 13
/
routes.js
267 lines (243 loc) · 7.6 KB
/
routes.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
'use strict';
// route handler for the API
var marklogic = require('marklogic');
var connection = require('./dbsettings').connection;
var db = marklogic.createDatabaseClient(connection);
var qb = marklogic.queryBuilder;
var pb = marklogic.patchBuilder;
/*
function to select all documents from the database - the query is restricted to
retrieve images from the 'image' collection. The 'image' collection consists of
documents that are describing the image itself but they have no binary data. The
binary data is only linked
e.g.
{
"filename": "IMG_6193.jpg",
"location": {
"type": "Point",
"coordinates": [
43.7385,
7.429167
]
},
"binary": "/binary/IMG_6193.jpg"
}
*/
var selectAll = function selectAll() {
return db.documents.query(
qb.where(
qb.collection('image')
)
.orderBy(
qb.sort('filename')
)
.slice(0,300) //return 300 documents "per page" (pagination)
).result();
};
/* This function selects one image from the database */
var selectOne = function selectOne(uri) {
return db.documents.read('/image/' + uri + '.json').result();
};
/* This function updates the document. From the frontend we are allowed to set/change
the title of an image.
*/
var updateDocument = function(uri, update) {
update = JSON.parse(update);
var description = update.description;
var newDocument = {};
return db.documents.read('/image/' + uri + '.json')
.result()
.then(function(document) {
if (update.title) {
var title = update.title;
document[0].content.title = title;
}
if (description) {
document[0].content.description = description;
}
newDocument = document[0].content;
document[0].collections = ['image'];
return db.documents.write(document[0])
.result();
});
};
/* This function is responsible for doing a geospatial search
Geospatial search in MarkLogic uses a geospatial object (in thise case a geo path)
and it also has support for 4 geospatial types. We have circle, square, polygon
and point. In this function we are using the geospatial circle
*/
var search = function search(arg) {
if (typeof arg === 'object') {
var radius = parseInt(arg.radius);
var lat = parseFloat(arg.lat);
var lng = parseFloat(arg.lng);
return db.documents.query(
qb.where(
qb.collection('image'),
qb.geospatial(
qb.geoProperty(qb.property('location'), qb.property('coordinates')),
qb.circle(radius, lat, lng)
)
).slice(0,300)
).result();
} else {
return db.documents.query(
qb.where(
qb.collection('image'),
qb.parsedFrom(arg)
).slice(0,300)
).result();
}
};
var semantic = function semantic(country) {
country = country.replace(' ', '_');
var query = [
'PREFIX db: <http://dbpedia.org/resource/>',
'PREFIX onto: <http://dbpedia.org/ontology/>',
'PREFIX foaf: <http://xmlns.com/foaf/0.1/>',
'PREFIX prop: <http://dbpedia.org/property/>',
'SELECT * ',
'WHERE {',
'OPTIONAL { db:' + country + ' onto:capital ?capital . } ',
'OPTIONAL { db:' + country + ' prop:imageFlag ?imageFlag . } ',
'OPTIONAL { db:' + country + ' prop:imageMap ?imageMap . } ',
'OPTIONAL { db:' + country + ' foaf:homepage ?homepagel . } ',
'OPTIONAL { db:' + country + ' onto:anthem ?anthem . } ',
'OPTIONAL { db:' + country + ' prop:areaKm ?areaKm . } ',
'OPTIONAL { db:' + country + ' prop:currencyCode ?currencyCode . } ',
'OPTIONAL { db:' + country + ' prop:timeZone ?timeZone . } ',
'OPTIONAL { db:' + country + ' onto:abstract ?abstract . }} '
];
return db.graphs.sparql('application/sparql-results+json', query.join('\n'))
.result();
};
/*
When specified the function below are making use of ExpressJS' req.params object
that contains the URL parameters that are sent with the request so:
if the route configuration contains:
/api/:id then the following URL http://localhost/api/image1234 will have a
'req.params.id' value that we can capture.
*/
/* wrapper function for selectAll() to retrieve all documents */
var apiindex = function(req, res) {
selectAll().then(function(documents) {
res.json(documents);
}).catch(function(error) {
console.log('Error: ', error);
});
};
/* wrapper function to retrieve one document information */
var apiimage = function(req, res) {
var id = req.params.id;
selectOne(id).then(function(document) {
if (document.length !== 0) {
res.json(document);
}
}).catch(function(error) {
res.status(404).end();
console.log('Error: ', error);
});
};
/* wrapper function to retrieve image data */
var apiimagedata = function(req, res) {
var id = req.params.id;
res.writeHead(200, { 'Content-type': 'image/jpeg' });
var data = [];
var buffer = [];
db.documents.read('/binary/' + id).stream('chunked').on('data', function(chunk) {
data.push(chunk);
}).on('end', function() {
buffer = Buffer.concat(data);
res.end(buffer);
});
};
/* wrapper function to update a document's title */
var apiupdate = function(req, res) {
var id = req.params.id;
var update = req.params.update;
updateDocument(id, update).then(function(response) {
res.json(200);
}).catch(function(error) {
console.log('Error: ', error);
});
};
/* wrapper function for the geospatial search */
var apisearch = function(req, res) {
//if radius exists it is a geospatial search
if (req.params.radius) {
var radius = req.params.radius;
var lat = req.params.lat;
var lng = req.params.lng;
var searchObj = {
radius: radius,
lat: lat,
lng: lng
};
search(searchObj).then(function(data) {
res.json(data);
}).catch(function(error) {
console.log('Error: ', error);
});
} else {
var term = req.params.term;
search(term).then(function(data) {
res.json(data);
}).catch(function(error) {
console.log('Error: ', error);
});
}
};
var apisemantic = function(req, res) {
var country = req.params.country;
var options = ['capital', 'imageFlag', 'imageMap', 'homepage', 'anthem', 'areaKm', 'currencyCode', 'timeZone', 'abstract'];
var data = {};
var counter = 0;
semantic(country)
.then(function(result) {
options.forEach(function(option) {
var check = '';
counter++
if (result.results.bindings[0][option]) {
if (result.results.bindings[0][option].value.indexOf('_') > -1) {
var value = result.results.bindings[0][option].value.replace(/_/g, ' ');
if (value.indexOf('http://') > -1) {
data[option] = value.split('/').pop();
}
} else if (result.results.bindings[0][option].value.indexOf('http://') > -1) {
data[option] = result.results.bindings[0][option].value.split('/').pop();
} else {
data[option] = result.results.bindings[0][option].value
}
if (counter === options.length) {
res.json(data);
}
}
});
}, function(error) {
console.log(error);
});
}
var appindex = function(req, res) {
res.render('index');
};
/* this route configuration is needed as we are using jade files */
var partials = function partials(req, res) {
var name = req.params.name;
res.render('partials/' + name);
};
/* making both the app and api functions available via exports
*/
module.exports = {
app: {
index: appindex,
partials: partials
},
api : {
index: apiindex,
image: apiimage,
imagedata: apiimagedata,
update: apiupdate,
search: apisearch,
semantic: apisemantic
}
};