UNPKG

gpii-universal

Version:

Cross platform, core components of the GPII personalization infrastructure.

128 lines (109 loc) 4.92 kB
/*! GPII Ontology Server Utilities Copyright 2012 OCAD University Copyright 2014 Raising the Floor - International 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 = fluid || require("infusion"), gpii = fluid.registerNamespace("gpii"), $ = fluid.registerNamespace("jQuery"); fluid.registerNamespace("gpii.ontologyHandler.utils"); fluid.registerNamespace("gpii.ontologyHandler.transforms"); /* * Function used when PUTting preferences to the preferences server. Filtering a full preferences * set by removing a subset from it which is needed when ensuring that no preferences are stored * twice on the server * * There are two rules regarding the preference sets that this function helps to adhere to: * (1) A preference should be stored in the ontology the user saves it in and * (2) no preference should be stored at one time in multiple ontologies (not taking into account * contexts). * * So if I do a PUT with preference set containing setting Xa in ontology A, and that setting * already exist in the preference set stored on server as Xb, in ontology B, we need to make * sure that Xb is deleted from the preference set. That is what this filter function does. * When storing a preference set in eg. the flat ontology, all the prefs are first translated * to other known ontologies, and then these settings are removed from the prefs set using * this filter function. Once this is done, the flat preferences can safely be stored on the * server without risking duplication. * * @source (Object) - the set of preferences from which to remove elements found in the _remove_ * object * @remove (Object) - The set of preferences to remove from the _source_ set. * @return (Object) - The _source_ set of preferences, but with all the elements present in _remove_ * removed */ gpii.ontologyHandler.utils.filter = function (source, remove) { var modified = false, sourceElement, toRemove; var ret = fluid.freshContainer(source); for (var ind in source) { sourceElement = source[ind]; if (ind in remove) { toRemove = remove[ind]; if (fluid.isPrimitive(toRemove) || $.isArray(toRemove)) { continue; } else { var res = gpii.ontologyHandler.utils.filter(sourceElement, toRemove); if (res === undefined) { continue; } ret[ind] = res; modified = true; } } else { ret[ind] = sourceElement; modified = true; } } return modified ? ret : undefined; }; gpii.ontologyHandler.utils.filterPrefs = function (fullSet, toRemove) { var cpy = fluid.copy(fullSet); var result = gpii.ontologyHandler.utils.filter(cpy, toRemove); return result === undefined ? {} : result; }; // Transforms required for application specific settings fluid.defaults("gpii.ontologyHandler.transforms.applicationISOToFlat", { gradeNames: "fluid.standardTransformFunction", invertConfiguration: "gpii.ontologyHandler.transforms.applicationISOToFlat.invert" }); gpii.ontologyHandler.transforms.applicationISOToFlat = function (preferences, transformSpec, transform) { fluid.each(preferences, function (data, appId) { var value, key; value = data.parameters; key = "http://registry\\.gpii\\.net/applications/" + fluid.pathUtil.escapeSegment(appId); fluid.model.transform.setValue(key, value, transform); }); }; gpii.ontologyHandler.transforms.applicationISOToFlat.invert = function (transformSpec) { transformSpec.type = "gpii.ontologyHandler.transforms.applicationFlatToISO"; return transformSpec; }; fluid.defaults("gpii.ontologyHandler.transforms.applicationFlatToISO", { gradeNames: "fluid.standardTransformFunction" }); gpii.ontologyHandler.transforms.applicationFlatToISO = function (preferences, transformSpec, transform) { var searchValue = "//registry.gpii.net/applications/"; fluid.each(preferences, function (preference, uri) { var appIndex = uri.indexOf(searchValue), appId; if (appIndex < 0) { return; } appId = uri.substring(appIndex + searchValue.length); preference = { parameters: preference, id: appId }; fluid.model.transform.setValue(fluid.model.composeSegments(transformSpec.outputPath, fluid.pathUtil.escapeSegment(appId)), preference, transform); }); };