lib/impl/dataSources/updateCache.js
function updateDataSourceEntry(params, dataSourceData, callback) {
  var dataToUpdate = dataSourceData.data;
  var dataSourceDocument = dataSourceData.document;

  logger.debug("updateDataSourceEntry ", params, dataSourceData);

  //If there is no cache entry, create a new one to validate.
  var cacheElement = dataSourceDocument.cache[0];

  if (!cacheElement) {
    dataSourceDocument.cache.push({});
  }

  cacheElement = dataSourceDocument.cache[0];

  //Assigning the last attempt to update the data source
  cacheElement.updateTimestamp = params.currentTime;

  var existingData = cacheElement.data;
  var existingHash = cacheElement.dataHash;

  logger.debug("updateDataSourceEntry ", {dataToUpdate: dataToUpdate, cacheElement: cacheElement, existingData: existingData, existingHash: existingHash});

  if (dataSourceData.dataError || dataSourceData.error) {
    cacheElement.currentStatus = {
      status: "error",
      error: dataSourceData.dataError || dataSourceData.error
    };
  } else if (dataSourceData.dataHash !== cacheElement.dataHash) {
    //If the hashes are different, need to update the data set and hash
    cacheElement.data = dataToUpdate;
    cacheElement.dataHash = dataSourceData.dataHash;
    dataSourceData.dataChanged = true;
  }

  logger.debug("updateDataSourceEntry ", {cacheElementBeforeValidation: cacheElement});

  async.waterfall([
    function validateDataPassed(cb) {
      //Validating That the Data That Was Passed is correct.
      dataSourceDocument.save(function(err) {
        if (err) {
          logger.warn("Error Validating Data Source ", {error: err});
          //Not Valid, don't try to save it, mark it as an error state
          dataSourceData.error = buildErrorResponse({
            error: err,
            userDetail: "Invalid Data For Cache Update.",
            systemDetail: err.message,
            code: ERROR_CODES.FH_FORMS_INVALID_PARAMETERS
          });

          //If there is a validation error, save it with the exiting data set Data Source for viewing later.
          cacheElement.data = existingData ? existingData : [];
          cacheElement.dataHash = existingHash;
          cacheElement.currentStatus = {
            status: "error",
            error: dataSourceData.error
          };
        }

        if (!dataSourceData.error && !dataSourceData.dataError) {
          //marking the status as ok.
          cacheElement.currentStatus = {
            status: "ok",
            error: null
          };
          //Resetting the backOffIndex as it is a valid Data Source update.
          cacheElement.backOffIndex = 0;
          cacheElement.markModified('currentStatus');
        } else {
          //The data source encountered an error. Increment the backOffIndex.
          cacheElement.backOffIndex = cacheElement.backOffIndex ? cacheElement.backOffIndex + 1 : 1;
        }

        //Mark The Submission As Refreshed
        cacheElement.lastRefreshed = params.currentTime;

        logger.debug("updateDataSourceEntry ", {cacheElementAfterValidate: cacheElement});

        cb();
      });
    },
    async.apply(saveAuditLogEntryAndUpdateDataSource, _.extend({
      cacheElement: cacheElement,
      dataSourceDocument: dataSourceDocument,
      dataSourceData: dataSourceData
    }, params))
  ], callback);
}
function updateDataSourceEntry(params, dataSourceData, callback) {
  var dataToUpdate = dataSourceData.data;
  var dataSourceDocument = dataSourceData.document;

  logger.debug("updateDataSourceEntry ", params, dataSourceData);

  //If there is no cache entry, create a new one to validate.
  var cacheElement = dataSourceDocument.cache[0];

  if (!cacheElement) {
    dataSourceDocument.cache.push({});
  }

  cacheElement = dataSourceDocument.cache[0];

  //Assigning the last attempt to update the data source
  cacheElement.updateTimestamp = params.currentTime;

  var existingData = cacheElement.data;
  var existingHash = cacheElement.dataHash;

  logger.debug("updateDataSourceEntry ", {dataToUpdate: dataToUpdate, cacheElement: cacheElement, existingData: existingData, existingHash: existingHash});

  if (dataSourceData.dataError || dataSourceData.error) {
    cacheElement.currentStatus = {
      status: "error",
      error: dataSourceData.dataError || dataSourceData.error
    };
  } else if (dataSourceData.dataHash !== cacheElement.dataHash) {
    //If the hashes are different, need to update the data set and hash
    cacheElement.data = dataToUpdate;
    cacheElement.dataHash = dataSourceData.dataHash;
    dataSourceData.dataChanged = true;
  }

  logger.debug("updateDataSourceEntry ", {cacheElementBeforeValidation: cacheElement});

  async.waterfall([
    function validateDataPassed(cb) {
      //Validating That the Data That Was Passed is correct.
      dataSourceDocument.save(function(err) {
        if (err) {
          logger.warn("Error Validating Data Source ", {error: err});
          //Not Valid, don't try to save it, mark it as an error state
          dataSourceData.error = buildErrorResponse({
            error: err,
            userDetail: "Invalid Data For Cache Update.",
            systemDetail: err.message,
            code: ERROR_CODES.FH_FORMS_INVALID_PARAMETERS
          });

          //If there is a validation error, save it with the exiting data set Data Source for viewing later.
          cacheElement.data = existingData ? existingData : [];
          cacheElement.dataHash = existingHash;
          cacheElement.currentStatus = {
            status: "error",
            error: dataSourceData.error
          };
        }

        if (!dataSourceData.error && !dataSourceData.dataError) {
          //marking the status as ok.
          cacheElement.currentStatus = {
            status: "ok",
            error: null
          };
          //Resetting the backOffIndex as it is a valid Data Source update.
          cacheElement.backOffIndex = 0;
          cacheElement.markModified('currentStatus');
        } else {
          //The data source encountered an error. Increment the backOffIndex.
          cacheElement.backOffIndex = cacheElement.backOffIndex ? cacheElement.backOffIndex + 1 : 1;
        }

        //Mark The Submission As Refreshed
        cacheElement.lastRefreshed = params.currentTime;

        logger.debug("updateDataSourceEntry ", {cacheElementAfterValidate: cacheElement});

        cb();
      });
    },
    async.apply(saveAuditLogEntryAndUpdateDataSource, _.extend({
      cacheElement: cacheElement,
      dataSourceDocument: dataSourceDocument,
      dataSourceData: dataSourceData
    }, params))
  ], callback);
}
function updateDataSourceEntry ( params , dataSourceData , callback ) { var dataToUpdate = dataSourceData . data ; var dataSourceDocument = dataSourceData . document ; logger . debug ( "updateDataSourceEntry " , params , dataSourceData ) ; //If there is no cache entry, create a new one to validate. var cacheElement = dataSourceDocument . cache [ 0 ] ; if ( ! cacheElement ) { dataSourceDocument . cache . push ( { } ) ; } cacheElement = dataSourceDocument . cache [ 0 ] ; //Assigning the last attempt to update the data source cacheElement . updateTimestamp = params . currentTime ; var existingData = cacheElement . data ; var existingHash = cacheElement . dataHash ; logger . debug ( "updateDataSourceEntry " , { dataToUpdate : dataToUpdate , cacheElement : cacheElement , existingData : existingData , existingHash : existingHash } ) ; if ( dataSourceData . dataError || dataSourceData . error ) { cacheElement . currentStatus = { status : "error" , error : dataSourceData . dataError || dataSourceData . error } ; } else if ( dataSourceData . dataHash !== cacheElement . dataHash ) { //If the hashes are different, need to update the data set and hash cacheElement . data = dataToUpdate ; cacheElement . dataHash = dataSourceData . dataHash ; dataSourceData . dataChanged = true ; } logger . debug ( "updateDataSourceEntry " , { cacheElementBeforeValidation : cacheElement } ) ; async . waterfall ( [ function validateDataPassed ( cb ) { //Validating That the Data That Was Passed is correct. dataSourceDocument . save ( function ( err ) { if ( err ) { logger . warn ( "Error Validating Data Source " , { error : err } ) ; //Not Valid, don't try to save it, mark it as an error state dataSourceData . error = buildErrorResponse ( { error : err , userDetail : "Invalid Data For Cache Update." , systemDetail : err . message , code : ERROR_CODES . FH_FORMS_INVALID_PARAMETERS } ) ; //If there is a validation error, save it with the exiting data set Data Source for viewing later. cacheElement . data = existingData ? existingData : [ ] ; cacheElement . dataHash = existingHash ; cacheElement . currentStatus = { status : "error" , error : dataSourceData . error } ; } if ( ! dataSourceData . error && ! dataSourceData . dataError ) { //marking the status as ok. cacheElement . currentStatus = { status : "ok" , error : null } ; //Resetting the backOffIndex as it is a valid Data Source update. cacheElement . backOffIndex = 0 ; cacheElement . markModified ( 'currentStatus' ) ; } else { //The data source encountered an error. Increment the backOffIndex. cacheElement . backOffIndex = cacheElement . backOffIndex ? cacheElement . backOffIndex + 1 : 1 ; } //Mark The Submission As Refreshed cacheElement . lastRefreshed = params . currentTime ; logger . debug ( "updateDataSourceEntry " , { cacheElementAfterValidate : cacheElement } ) ; cb ( ) ; } ) ; } , async . apply ( saveAuditLogEntryAndUpdateDataSource , _ . extend ( { cacheElement : cacheElement , dataSourceDocument : dataSourceDocument , dataSourceData : dataSourceData } , params ) ) ] , callback ) ; }
Update A Single Data Source Data Entry.

@param params
- currentTime
@param dataSourceData
@param callback
Update A Single Data Source Data Entry .