From b39753d0a6544f5355f63f1c9f5ca29b91a85076 Mon Sep 17 00:00:00 2001 From: Victor Woeltjen Date: Tue, 1 Sep 2015 10:57:56 -0700 Subject: [PATCH] [Entanglement] Separate out location Separate out location property from the CreationService, instead handle in the entanglement bundle; this is consistent with changes for identity management (attaching a creator to newly-created domain object models). for nasa/openmctweb#92 --- .../browse/src/creation/CreationService.js | 11 +-- .../test/creation/CreationServiceSpec.js | 10 --- platform/entanglement/bundle.json | 5 ++ .../src/services/LocatingCreationDecorator.js | 49 ++++++++++++ .../services/LocatingCreationDecoratorSpec.js | 79 +++++++++++++++++++ platform/entanglement/test/suite.json | 1 + 6 files changed, 138 insertions(+), 17 deletions(-) create mode 100644 platform/entanglement/src/services/LocatingCreationDecorator.js create mode 100644 platform/entanglement/test/services/LocatingCreationDecoratorSpec.js diff --git a/platform/commonUI/browse/src/creation/CreationService.js b/platform/commonUI/browse/src/creation/CreationService.js index 110a49c216..3dd96d289e 100644 --- a/platform/commonUI/browse/src/creation/CreationService.js +++ b/platform/commonUI/browse/src/creation/CreationService.js @@ -55,6 +55,10 @@ define( * space an object is created within (as it is possible to * have multiple persistence spaces attached.) * + * Note that the model passed in for object creation may be + * modified to attach additional initial properties associated + * with domain object creation. + * * @param {object} model the model for the newly-created * domain object * @param {DomainObject} parent the domain object which @@ -67,12 +71,6 @@ define( var persistence = parent.getCapability("persistence"), self = this; - // Store the location of an object relative to it's parent. - function addLocationToModel(modelId, model, parent) { - model.location = parent.getId(); - return model; - } - // Persist the new domain object's model; it will be fully // constituted as a domain object when loaded back, as all // domain object models are. @@ -135,7 +133,6 @@ define( // 2. Create a model with that ID in the persistence space // 3. Add that ID to return self.$q.when(uuid()).then(function (id) { - model = addLocationToModel(id, model, parent); return doPersist(persistence.getSpace(), id, model); }).then(function (id) { return addToComposition(id, parent, persistence); diff --git a/platform/commonUI/browse/test/creation/CreationServiceSpec.js b/platform/commonUI/browse/test/creation/CreationServiceSpec.js index 7f15afe061..bdcd752c6c 100644 --- a/platform/commonUI/browse/test/creation/CreationServiceSpec.js +++ b/platform/commonUI/browse/test/creation/CreationServiceSpec.js @@ -201,16 +201,6 @@ define( expect(mockLog.error).toHaveBeenCalled(); }); - it("stores location on new domainObjects", function () { - var model = { name: "my model" }, - objectPromise = creationService.createObject( - model, - mockParentObject - ); - - expect(model.location).toBe('parentId'); - }); - }); } ); diff --git a/platform/entanglement/bundle.json b/platform/entanglement/bundle.json index c50ac31baf..f85c5cdf4c 100644 --- a/platform/entanglement/bundle.json +++ b/platform/entanglement/bundle.json @@ -33,6 +33,11 @@ } ], "components": [ + { + "type": "decorator", + "provides": "creationService", + "implementation": "services/LocatingCreationDecorator.js" + } ], "controllers": [ ], diff --git a/platform/entanglement/src/services/LocatingCreationDecorator.js b/platform/entanglement/src/services/LocatingCreationDecorator.js new file mode 100644 index 0000000000..041cc38138 --- /dev/null +++ b/platform/entanglement/src/services/LocatingCreationDecorator.js @@ -0,0 +1,49 @@ +/***************************************************************************** + * 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"; + + /** + * Adds a `location` property to newly-created domain objects. + * @constructor + * @augments {platform/commonUI/browse.CreationService} + * @memberof platform/entanglement + */ + function LocatingCreationDecorator(creationService) { + this.creationService = creationService; + } + + LocatingCreationDecorator.prototype.createObject = function (model, parent) { + if (parent && parent.getId) { + model.location = parent.getId(); + } + return this.creationService.createObject(model, parent); + }; + + return LocatingCreationDecorator; + } +); + diff --git a/platform/entanglement/test/services/LocatingCreationDecoratorSpec.js b/platform/entanglement/test/services/LocatingCreationDecoratorSpec.js new file mode 100644 index 0000000000..aa3cbce68a --- /dev/null +++ b/platform/entanglement/test/services/LocatingCreationDecoratorSpec.js @@ -0,0 +1,79 @@ +/***************************************************************************** + * 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,describe,beforeEach,it,jasmine,expect */ + +define( + [ + '../../src/services/LocatingCreationDecorator' + ], + function (LocatingCreationDecorator) { + "use strict"; + + describe("LocatingCreationDecorator", function () { + var mockCreationService, + mockPromise, + mockParent, + decorator; + + beforeEach(function () { + mockCreationService = jasmine.createSpyObj( + 'creationService', + ['createObject'] + ); + mockPromise = jasmine.createSpyObj( + 'promise', + ['then'] + ); + mockParent = jasmine.createSpyObj( + 'domainObject', + ['getCapability', 'getId', 'getModel', 'hasCapability', 'useCapability'] + ); + mockCreationService.createObject.andReturn(mockPromise); + mockParent.getId.andReturn('test-id'); + decorator = new LocatingCreationDecorator(mockCreationService); + }); + + it("delegates to its decorated service", function () { + expect(decorator.createObject( + { someKey: "some value" }, + mockParent + )).toEqual(mockPromise); // promise returned by decoratee + }); + + it("adds a location property", function () { + decorator.createObject( + { someKey: "some value" }, + mockParent + ); + expect(mockCreationService.createObject).toHaveBeenCalledWith( + { + someKey: "some value", + location: "test-id" // Parent's identifier + }, + mockParent + ); + }); + + }); + } +); diff --git a/platform/entanglement/test/suite.json b/platform/entanglement/test/suite.json index fe3c32cbef..9dfceb5c0f 100644 --- a/platform/entanglement/test/suite.json +++ b/platform/entanglement/test/suite.json @@ -4,5 +4,6 @@ "services/LinkService", "services/MoveService", "services/LocationService", + "services/LocatingCreationDecorator", "capabilities/LocationCapability" ]