UNPKG

gpii-universal

Version:

Cross platform, core components of the GPII personalization infrastructure.

181 lines (162 loc) 6.42 kB
/** * GPII Preferences Server Data Source * * Copyright 2018 OCAD University * * Licensed under the New BSD license. You may not use this file except in * compliance with this License. * * 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"); fluid.registerNamespace("gpii.flowManager.prefsServerDataSource"); // "gpii.flowManager.prefsServerDataSource" is used by the cloud based flow manager to communicate with the // preferences server. fluid.defaults("gpii.flowManager.prefsServerDataSource", { gradeNames: ["fluid.component"], // This option should be distributed down from, e.g., gpii.flowManager.config.cloud.base prefsServerURL: null, preferencesUrlTemplate: "%prefsServerURL/preferences/%gpiiKey?merge=%merge", prefsServerReadyUrlTemplate: "%prefsServerURL/ready", // The URLs below are calculated from the flow manager base URL and their templates. preferencesUrl: { expander: { funcName: "fluid.stringTemplate", args: ["{that}.options.preferencesUrlTemplate", { prefsServerURL: "{that}.options.prefsServerURL" }] } }, prefsServerHealthUrl: { expander: { funcName: "fluid.stringTemplate", args: ["{that}.options.prefsServerReadyUrlTemplate", { prefsServerURL: "{that}.options.prefsServerURL" }] } }, distributeOptions: { preferencesUrl: { source: "{that}.options.preferencesUrl", target: "{that > preferencesDataSourceImpl}.options.url" }, prefsServerHealthUrl: { source: "{that}.options.prefsServerHealthUrl", target: "{that > prefsServerHealthImpl}.options.url" } }, components: { preferencesDataSourceImpl: { type: "kettle.dataSource.URL", options: { termMap: { gpiiKey: "%gpiiKey" }, writable: true, writeMethod: "PUT" } }, prefsServerHealthImpl: { type: "kettle.dataSource.URL" } }, invokers: { // Retrieve preferences get: { funcName: "gpii.flowManager.prefsServerDataSource.get", args: ["{that}", "{arguments}.0"] // gpiiKey }, // Update preferences update: { funcName: "gpii.flowManager.prefsServerDataSource.update", args: ["{that}", "{arguments}.0", "{arguments}.1", "{arguments}.2"] // gpiiKey, merge, preferences }, // Create GPII key and associated preferences create: { funcName: "gpii.flowManager.prefsServerDataSource.create", args: ["{that}", "{arguments}.0", "{arguments}.1"] // gpiiKey, preferences }, // Check the liveness of the preferences server: running with proper database connection isLive: { funcName: "gpii.flowManager.prefsServerDataSource.isLive", args: ["{prefsServerHealthImpl}"] } } }); /** * Retrieve user preferences from the preferences server for the given GPII key. * @param {Component} that - An instance of gpii.flowManager.prefsServerDataSource. * @param {String} gpiiKey - A GPII key. * @return {Promise} A promise whose resolved value is the user preferences. */ gpii.flowManager.prefsServerDataSource.get = function (that, gpiiKey) { return that.preferencesDataSourceImpl.get({ gpiiKey: gpiiKey }); }; /** * Update user preferences on the preferences server for the given GPII key. * @param {Component} that - An instance of gpii.flowManager.prefsServerDataSource. * @param {String} gpiiKey A GPII key. * @param {Boolean} merge The flag of whether to merge the given preferences with the existing preferences saved * in the preferences server. * @param {Object} preferences The to-be-updated preferences. * @return {Promise} A promise whose resolved value is the status of the update. */ gpii.flowManager.prefsServerDataSource.update = function (that, gpiiKey, merge, preferences) { fluid.log("prefsServerDataSource, Merging preferences for GPII key (", gpiiKey , ") with preferences: ", preferences); merge = merge || true; var mergeStr = merge.toString(); var directModel = { gpiiKey: gpiiKey, merge: mergeStr }; return that.preferencesDataSourceImpl.set(directModel, preferences, { writeMethod: "PUT", termMap: { gpiiKey: "%gpiiKey", merge: "%merge" } }); }; /** * Create the given GPII key and associated preferences on the preferences server. * @param {Component} that - An instance of gpii.flowManager.prefsServerDataSource. * @param {String} gpiiKey A GPII key. * @param {Object} preferences The preferences associated with the GPII key. * @return {Promise} A promise whose resolved value is the status of the create. */ gpii.flowManager.prefsServerDataSource.create = function (that, gpiiKey, preferences) { fluid.log("prefsServerDataSource, Creating GPII key (", gpiiKey , ") and its preferences: ", preferences); var directModel = { gpiiKey: gpiiKey }; return that.preferencesDataSourceImpl.set(directModel, preferences, { writeMethod: "PUT", termMap: { gpiiKey: "%gpiiKey" } }); }; /** * Check the liveness of the preferences server: running with proper database connection. * @param {Component} prefsServerHealthImpl - An instance of {gpii.flowManager.prefsServerDataSource}.prefsServerHealthImpl. * @return {Promise} A promise whose resolved value is the liveness status of the preferences server. The resolved value is * true if the server is alive. Otherwise, false. */ gpii.flowManager.prefsServerDataSource.isLive = function (prefsServerHealthImpl) { var promiseTogo = fluid.promise(); var isLivePromise = prefsServerHealthImpl.get(); isLivePromise.then(function (value) { // onResolve promiseTogo.resolve(value); }, function (value) { // onReject promiseTogo.resolve(value); }); return promiseTogo; };