diff --git a/platform/core/bundle.json b/platform/core/bundle.json index 4b33f48f35..35b76c8a32 100644 --- a/platform/core/bundle.json +++ b/platform/core/bundle.json @@ -63,7 +63,12 @@ "provides": "modelService", "type": "provider", "implementation": "models/PersistedModelProvider.js", - "depends": [ "persistenceService", "$q", "PERSISTENCE_SPACE" ] + "depends": [ + "persistenceService", + "$q", + "PERSISTENCE_SPACE", + "ADDITIONAL_PERSISTENCE_SPACES" + ] }, { "provides": "modelService", @@ -215,6 +220,17 @@ "composition": [] } } + ], + "constants": [ + { + "key": "PERSISTENCE_SPACE", + "value": "mct" + }, + { + "key": "ADDITIONAL_PERSISTENCE_SPACES", + "value": [], + "description": "An array of additional persistence spaces to load models from." + } ] } } diff --git a/platform/core/src/models/PersistedModelProvider.js b/platform/core/src/models/PersistedModelProvider.js index 59ab020b14..3acd5a6ae6 100644 --- a/platform/core/src/models/PersistedModelProvider.js +++ b/platform/core/src/models/PersistedModelProvider.js @@ -39,23 +39,37 @@ define( * @param {PersistenceService} persistenceService the service in which * domain object models are persisted. * @param $q Angular's $q service, for working with promises - * @param {string} SPACE the name of the persistence space from which - * models should be retrieved. + * @param {string} space the name of the persistence space(s) + * from which models should be retrieved. + * @param {string} spaces additional persistence spaces to use */ - function PersistedModelProvider(persistenceService, $q, space) { + function PersistedModelProvider(persistenceService, $q, space, spaces) { this.persistenceService = persistenceService; this.$q = $q; - this.space = space; + this.spaces = [space].concat(spaces || []); + } + + // Take the most recently modified model, for cases where + // multiple persistence spaces return models. + function takeMostRecent(modelA, modelB) { + return (!modelA || modelA.modified === undefined) ? modelB : + (!modelB || modelB.modified === undefined) ? modelA : + modelA.modified > modelB.modified ? modelA : + modelB; } PersistedModelProvider.prototype.getModels = function (ids) { var persistenceService = this.persistenceService, $q = this.$q, - space = this.space; + spaces = this.spaces; - // Load a single object model from persistence + // Load a single object model from any persistence spaces function loadModel(id) { - return persistenceService.readObject(space, id); + return $q.all(spaces.map(function (space) { + return persistenceService.readObject(space, id); + })).then(function (models) { + return models.reduce(takeMostRecent); + }); } // Package the result as id->model