From d1c0d81120cd145dad583b054150b991fd5ac377 Mon Sep 17 00:00:00 2001 From: Victor Woeltjen Date: Thu, 17 Sep 2015 13:37:57 -0700 Subject: [PATCH] [Entanglement] Contextualize objects by location Add a context capability to domain objects based on their location at the time they are loaded. nasa/openmctweb#84 --- platform/entanglement/bundle.json | 6 ++ .../src/services/LocatingObjectDecorator.js | 88 +++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 platform/entanglement/src/services/LocatingObjectDecorator.js diff --git a/platform/entanglement/bundle.json b/platform/entanglement/bundle.json index f85c5cdf4c..61c3d90539 100644 --- a/platform/entanglement/bundle.json +++ b/platform/entanglement/bundle.json @@ -37,6 +37,12 @@ "type": "decorator", "provides": "creationService", "implementation": "services/LocatingCreationDecorator.js" + }, + { + "type": "decorator", + "provides": "objectService", + "implementation": "services/LocatingObjectDecorator.js", + "depends": ["contextualize", "$q", "$log"] } ], "controllers": [ diff --git a/platform/entanglement/src/services/LocatingObjectDecorator.js b/platform/entanglement/src/services/LocatingObjectDecorator.js new file mode 100644 index 0000000000..1993c9cc94 --- /dev/null +++ b/platform/entanglement/src/services/LocatingObjectDecorator.js @@ -0,0 +1,88 @@ +/***************************************************************************** + * Open MCT Web, Copyright (c) 2014-2015, United States Government + * as represented by the Administrator of the National Aeronautics and Space + * Administration. All rights reserved. + * + * Open MCT Web is licensed under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0. + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + * Open MCT Web includes source code licensed under additional open source + * licenses. See the Open Source Licenses file (LICENSES.md) included with + * this source code distribution or the Licensing information page available + * at runtime from the About dialog for additional information. + *****************************************************************************/ + +/*global define */ + +define( + function () { + "use strict"; + + /** + * Ensures that domain objects are loaded with a context capability + * that reflects their location. + * @constructor + * @implements {ObjectService} + * @memberof platform/entanglement + */ + function LocatingObjectDecorator(contextualize, $q, $log, objectService) { + this.contextualize = contextualize; + this.$log = $log; + this.objectService = objectService; + this.$q = $q; + } + + LocatingObjectDecorator.prototype.getObjects = function (ids) { + var $q = this.$q, + $log = this.$log, + contextualize = this.contextualize, + objectService = this.objectService, + result = {}; + + function loadObjectInContext(id, exclude) { + function attachContextById(domainObject, locationId) { + return loadObjectInContext(locationId, exclude) + .then(function (parent) { + return contextualize(domainObject, parent); + }); + } + + function attachContextForLocation(domainObject) { + var model = domainObject && domainObject.getModel(), + location = (model || {}).location; + + // Don't pursue a context if we encounter this + // object again during this sequence of invocations. + exclude[id] = true; + + return location ? + attachContextById(domainObject, location) : + domainObject; + } + + return objectService.getObjects([id]).then(function (objects) { + return exclude[id] ? + objects[id] : // Don't loop indefinitely. + attachContextForLocation(objects[id]); + }); + } + + ids.forEach(function (id) { + result[id] = loadObjectInContext(id, {}); + }); + + return $q.all(result); + }; + + return LocatingObjectDecorator; + } +); +