Daniel@0: "use strict"; Daniel@0: Daniel@0: App.module("DataModule", function(DataModule, App, Backbone, Marionette, $, _, Logger) { Daniel@0: // Prevent auto start Daniel@0: //CollectionDefinitionProvider.startWithParent = false; Daniel@0: Daniel@0: // Define private variables Daniel@0: var logger = null; Daniel@0: Daniel@0: /** Daniel@0: * Module initializer Daniel@0: * Daniel@0: */ Daniel@0: DataModule.addInitializer(function(){ Daniel@0: Daniel@0: logger = Logger.get("App.DataModule"); Daniel@0: Daniel@0: DataModule.DynamicDefinitionForCollection = DataModule.DynamicDefinition.extend({}); Daniel@0: DataModule.DynamicDefinitionProviderForCollections = DataModule.DynamicDefinitionProvider.extend({ Daniel@0: Daniel@0: options: { Daniel@0: DynamicDefinition: DataModule.DynamicDefinitionForCollection, Daniel@0: apiMethod: "getCollectionId", Daniel@0: customAttributesWhenNotApplicable: {id: undefined}, Daniel@0: customAttributesWhileBeingUpdated: {id: null, sampleSize: null, fullSize: null}, Daniel@0: customAttributesWhenFaulty: {id: false, sampleSize: false, fullSize: false}, Daniel@0: Daniel@0: customAttributesWhenEmpty: {id: "", sampleSize: 0, fullSize: 0} Daniel@0: }, Daniel@0: Daniel@0: definitionIsFaulty: function(definition) { Daniel@0: return definition.id === false; Daniel@0: }, Daniel@0: Daniel@0: definitionIsBeingUpdated: function(definition) { Daniel@0: return definition.id === null; Daniel@0: }, Daniel@0: Daniel@0: configParametersToRequestParameters: function(config) { Daniel@0: var rawConfigParameters = config.attributes.parameters.attributes; Daniel@0: var requestParameters = {}; Daniel@0: var library = _.str.trim(rawConfigParameters.library).replace(/ /g, "").replace(/,/g, ";"); Daniel@0: if (library) { Daniel@0: requestParameters.library = library; Daniel@0: } else { Daniel@0: return null; Daniel@0: } Daniel@0: requestParameters.sv = 0; Daniel@0: var year = _.str.trim(rawConfigParameters.year).replace(/ /g, ""); Daniel@0: if (year) { Daniel@0: requestParameters.year = year; Daniel@0: } Daniel@0: Daniel@0: //FIXME (hard-coded sample size) Daniel@0: requestParameters.sample = 1000; Daniel@0: Daniel@0: _.each(["genre", "composer", "performer", "title", "place", "collection"], function(field) { Daniel@0: var f = _.str.trim(rawConfigParameters[field]); Daniel@0: if (f) { Daniel@0: requestParameters[field] = f; Daniel@0: }; Daniel@0: }); Daniel@0: Daniel@0: return requestParameters; Daniel@0: }, Daniel@0: Daniel@0: apiResponseToAttributes: function(data, query) { Daniel@0: var attributes = {}; Daniel@0: Daniel@0: if (data && data.cid) { Daniel@0: attributes.id = data.cid; Daniel@0: attributes.sampleSize = data.size; Daniel@0: attributes.fullSize = data.full_size; Daniel@0: } else if (data.errors && data.errors[0] && _.isString(data.errors[0].desc) && data.errors[0].desc.indexOf("empty_dataset") > 0) { Daniel@0: attributes = _.clone(this.options.customAttributesWhenEmpty); Daniel@0: } else { Daniel@0: attributes = _.clone(this.options.customAttributesWhenFaulty); Daniel@0: attributes.errors = data.errors; Daniel@0: logger.warn("An error occured when attempting to obtain collection definition. Query / response:", query, data); Daniel@0: }; Daniel@0: attributes.query = query; Daniel@0: return attributes; Daniel@0: } Daniel@0: }); Daniel@0: }); Daniel@0: }, Logger);