All files / universal/gpii/node_modules/ontologyHandler/src ontologyHandlerUtilities.js

100% Statements 45/45
100% Branches 16/16
100% Functions 7/7
100% Lines 45/45

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                    9x 9x 9x   9x 9x                                                   9x 38x       38x 38x 67x   67x 34x   34x 21x   13x 13x 6x     7x 7x     33x 33x     38x     9x 25x 25x 25x       9x         9x 7x   7x 7x 7x       9x 321x 321x     9x       9x 1411x 1411x 4475x   4475x 3753x   722x 722x       722x        
/*!
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 preferences 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 NP set. That is what this filter function does.
 * When storing a preferences 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.
 * @returns (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);
    });
};