lib/impl/getForm.js
function populateFieldDataFromDataSources(populatedForms, cb) {
    logger.debug("populateFieldDataFromDataSources", populatedForms);
    var DataSource = models.get(connections.mongooseConnection, models.MODELNAMES.DATA_SOURCE);
    //If no data source cache data is expected, then there is no need to load the Data Source Cache Data.

    if (!options.expectDataSourceCache) {
      return cb(undefined, populatedForms);
    }
    var dataSourceIds = getDataSourceIds(populatedForms);

    logger.debug("populateFieldDataFromDataSources", {dataSourceIds: dataSourceIds});

    //If none of the forms refer to any data sources, then no need to search
    if (dataSourceIds.length === 0) {
      return cb(undefined, populatedForms);
    }

    var query = {
      _id: {
        "$in": dataSourceIds
      }
    };

    //One query to populate all data sources
    DataSource.find(query).exec(function(err, dataSources) {
      if (err) {
        logger.error("Error Finding Data Sources", {error: err, dataSourceIds:dataSourceIds});
        return cb(err);
      }

      logger.debug("populateFieldDataFromDataSources", {dataSources: dataSources});

      var validatonError = _validateReturnedDataSources(dataSourceIds, dataSources);

      if (validatonError) {
        logger.error("Error Getting Form With Data Sources", {error: validatonError});
        return cb(validatonError);
      }

      var cacheEntries = {};

      //Assigning a lookup for cache entries
      _.each(dataSources, function(dataSource) {
        cacheEntries[dataSource._id] = dataSource.cache[0].data;
      });

      //Overriding field options for a field with data source data if the field is defined as being sourced from a data source.
      populatedForms = _.map(populatedForms, function(populatedForm) {
        populatedForm.pages = _.map(populatedForm.pages, function(page) {
          page.fields =  _.map(page.fields, function(field) {
            //If it is a data source type field, then return the data source data
            if (field.dataSourceType === models.FORM_CONSTANTS.DATA_SOURCE_TYPE_DATA_SOURCE) {
              //No guarantee these are set
              field.fieldOptions = field.fieldOptions || {};
              field.fieldOptions.definition = field.fieldOptions.definition || {};

              //Setting the data source data
              field.fieldOptions.definition.options = models.convertDSCacheToFieldOptions(field.type, cacheEntries[field.dataSource]);
            }
            return field;
          });
          return page;
        });

        return populatedForm;
      });

      logger.debug("populateFieldDataFromDataSources", {populatedForms: JSON.stringify(populatedForms)});

      //Finished, return the merged forms
      return cb(undefined, populatedForms);
    });

  }
function populateFieldDataFromDataSources(populatedForms, cb) {
    logger.debug("populateFieldDataFromDataSources", populatedForms);
    var DataSource = models.get(connections.mongooseConnection, models.MODELNAMES.DATA_SOURCE);
    //If no data source cache data is expected, then there is no need to load the Data Source Cache Data.

    if (!options.expectDataSourceCache) {
      return cb(undefined, populatedForms);
    }
    var dataSourceIds = getDataSourceIds(populatedForms);

    logger.debug("populateFieldDataFromDataSources", {dataSourceIds: dataSourceIds});

    //If none of the forms refer to any data sources, then no need to search
    if (dataSourceIds.length === 0) {
      return cb(undefined, populatedForms);
    }

    var query = {
      _id: {
        "$in": dataSourceIds
      }
    };

    //One query to populate all data sources
    DataSource.find(query).exec(function(err, dataSources) {
      if (err) {
        logger.error("Error Finding Data Sources", {error: err, dataSourceIds:dataSourceIds});
        return cb(err);
      }

      logger.debug("populateFieldDataFromDataSources", {dataSources: dataSources});

      var validatonError = _validateReturnedDataSources(dataSourceIds, dataSources);

      if (validatonError) {
        logger.error("Error Getting Form With Data Sources", {error: validatonError});
        return cb(validatonError);
      }

      var cacheEntries = {};

      //Assigning a lookup for cache entries
      _.each(dataSources, function(dataSource) {
        cacheEntries[dataSource._id] = dataSource.cache[0].data;
      });

      //Overriding field options for a field with data source data if the field is defined as being sourced from a data source.
      populatedForms = _.map(populatedForms, function(populatedForm) {
        populatedForm.pages = _.map(populatedForm.pages, function(page) {
          page.fields =  _.map(page.fields, function(field) {
            //If it is a data source type field, then return the data source data
            if (field.dataSourceType === models.FORM_CONSTANTS.DATA_SOURCE_TYPE_DATA_SOURCE) {
              //No guarantee these are set
              field.fieldOptions = field.fieldOptions || {};
              field.fieldOptions.definition = field.fieldOptions.definition || {};

              //Setting the data source data
              field.fieldOptions.definition.options = models.convertDSCacheToFieldOptions(field.type, cacheEntries[field.dataSource]);
            }
            return field;
          });
          return page;
        });

        return populatedForm;
      });

      logger.debug("populateFieldDataFromDataSources", {populatedForms: JSON.stringify(populatedForms)});

      //Finished, return the merged forms
      return cb(undefined, populatedForms);
    });

  }
function populateFieldDataFromDataSources ( populatedForms , cb ) { logger . debug ( "populateFieldDataFromDataSources" , populatedForms ) ; var DataSource = models . get ( connections . mongooseConnection , models . MODELNAMES . DATA_SOURCE ) ; //If no data source cache data is expected, then there is no need to load the Data Source Cache Data. if ( ! options . expectDataSourceCache ) { return cb ( undefined , populatedForms ) ; } var dataSourceIds = getDataSourceIds ( populatedForms ) ; logger . debug ( "populateFieldDataFromDataSources" , { dataSourceIds : dataSourceIds } ) ; //If none of the forms refer to any data sources, then no need to search if ( dataSourceIds . length === 0 ) { return cb ( undefined , populatedForms ) ; } var query = { _id : { "$in" : dataSourceIds } } ; //One query to populate all data sources DataSource . find ( query ) . exec ( function ( err , dataSources ) { if ( err ) { logger . error ( "Error Finding Data Sources" , { error : err , dataSourceIds : dataSourceIds } ) ; return cb ( err ) ; } logger . debug ( "populateFieldDataFromDataSources" , { dataSources : dataSources } ) ; var validatonError = _validateReturnedDataSources ( dataSourceIds , dataSources ) ; if ( validatonError ) { logger . error ( "Error Getting Form With Data Sources" , { error : validatonError } ) ; return cb ( validatonError ) ; } var cacheEntries = { } ; //Assigning a lookup for cache entries _ . each ( dataSources , function ( dataSource ) { cacheEntries [ dataSource . _id ] = dataSource . cache [ 0 ] . data ; } ) ; //Overriding field options for a field with data source data if the field is defined as being sourced from a data source. populatedForms = _ . map ( populatedForms , function ( populatedForm ) { populatedForm . pages = _ . map ( populatedForm . pages , function ( page ) { page . fields = _ . map ( page . fields , function ( field ) { //If it is a data source type field, then return the data source data if ( field . dataSourceType === models . FORM_CONSTANTS . DATA_SOURCE_TYPE_DATA_SOURCE ) { //No guarantee these are set field . fieldOptions = field . fieldOptions || { } ; field . fieldOptions . definition = field . fieldOptions . definition || { } ; //Setting the data source data field . fieldOptions . definition . options = models . convertDSCacheToFieldOptions ( field . type , cacheEntries [ field . dataSource ] ) ; } return field ; } ) ; return page ; } ) ; return populatedForm ; } ) ; logger . debug ( "populateFieldDataFromDataSources" , { populatedForms : JSON . stringify ( populatedForms ) } ) ; //Finished, return the merged forms return cb ( undefined , populatedForms ) ; } ) ; }
Any fields that require data source data need to be populated
Any fields that require data source data need to be populated