/*
* GPII Solutions Registry Datasource
*
* Copyright 2016 RtF-I
*
* Licensed under the New BSD license. You may not use this file except in
* compliance with this License.
*
* The research leading to these results has received funding from the European Union's
* Seventh Framework Programme (FP7/2007-2013)
* under grant agreement no. 289016.
*
* You may obtain a copy of the License at
* https://github.com/GPII/universal/blob/master/LICENSE.txt
*/
"use strict";
var fluid = require("infusion"),
gpii = fluid.registerNamespace("gpii"),
fs = require("fs");
require("kettle");
fluid.registerNamespace("gpii.flowManager.solutionsRegistry");
fluid.defaults("gpii.flowManager.solutionsRegistry.dataSource", {
gradeNames: ["kettle.dataSource"],
members: {
fullSolutionsRegistry: null
},
readOnlyGrade: "gpii.flowManager.solutionsRegistry.dataSource",
invokers: {
getImpl: {
funcName: "gpii.flowManager.solutionsRegistry.dataSource.handle",
args: ["{that}", "{arguments}.1", "{arguments}.2"] // options, directModel
}
},
listeners: {
onCreate: "gpii.flowManager.solutionsRegistry.dataSource.loadSolutionsRegistry"
}
});
gpii.flowManager.solutionsRegistry.dataSource.loadSolutionsRegistry = function (that) {
Iif (!that.options.path) {
fluid.fail("The solutionsRegistry datasource ", that, " needs a \"path\" option pointing to the solution entries folder");
}
var url = fluid.module.resolvePath(that.options.path);
Iif (!fs.existsSync(url)) {
fluid.fail("The path provided to the solutionsRegistry datasource (", url, ") has not been found on the file system");
}
that.fullSolutionsRegistry = require(url);
};
/**
* Handler for get requests of solutions registry. It will return either a full solution registry,
* or if an 'os' is provided in the requestOptions, only the entries for that os will be returned
*
* @param that {Object} the gpii.flowManager.solutionsRegistry.dataSource
* @param requestOptions {Object} currently the only request option supported is "os". If provided,
* the returned solutions registry will be filtered by OS version
*/
gpii.flowManager.solutionsRegistry.dataSource.handle = function (that, requestOptions) {
var promise = fluid.promise();
if (requestOptions.os) { // if "os" is defined, return only solution registry entries for that OS
Eif (requestOptions.os in that.fullSolutionsRegistry) {
promise.resolve(JSON.stringify(that.fullSolutionsRegistry[requestOptions.os]));
} else {
promise.reject({
isError: true,
message: "The requested OS (" + requestOptions.os + ") was not present in the solutions registry",
statusCode: 404
});
}
} else { // if no "os" is requested, return the full solutions registry
promise.resolve(JSON.stringify(that.fullSolutionsRegistry));
}
return promise;
};
/** A mixin grade which automatically expands any %terms corresponding to module names registered in Infusion's module database */
// TODO: This is a duplicate of kettle.dataSource.file.moduleTerms - this should be rewritten after KETTLE-50 is resolved
fluid.defaults("gpii.flowManager.solutionsRegistry.dataSource.moduleTerms", {
gradeNames: "gpii.flowManager.solutionsRegistry.dataSource",
termMap: "@expand:fluid.module.terms()"
});
|