def _upsert_samples_from_csv(csv_reader):
  """Inserts/updates BiobankStoredSamples from a csv.DictReader."""
  missing_cols = set(CsvColumns.ALL) - set(csv_reader.fieldnames)
  if missing_cols:
    raise DataError(
        'CSV is missing columns %s, had columns %s.' % (missing_cols, csv_reader.fieldnames))
  samples_dao = BiobankStoredSampleDao()
  biobank_id_prefix = get_biobank_id_prefix()
  written = 0
  try:
    samples = []
    with ParticipantDao().session() as session:

      for row in csv_reader:
        sample = _create_sample_from_row(row, biobank_id_prefix)
        if sample:
          # DA-601 - Ensure biobank_id exists before accepting a sample record.
          if session.query(Participant).filter(
                      Participant.biobankId == sample.biobankId).count() < 1:
            logging.error('Bio bank Id ({0}) does not exist in the Participant table.'.
                          format(sample.biobankId))
            continue

          samples.append(sample)
          if len(samples) >= _BATCH_SIZE:
            written += samples_dao.upsert_all(samples)
            samples = []

      if samples:
        written += samples_dao.upsert_all(samples)

    return written
  except ValueError, e:
    raise DataError(e)
def _upsert_samples_from_csv(csv_reader):
    """Inserts/updates BiobankStoredSamples from a csv.DictReader."""
    missing_cols = _Columns.ALL - set(csv_reader.fieldnames)
    if missing_cols:
        raise DataError('CSV is missing columns %s, had columns %s.' %
                        (missing_cols, csv_reader.fieldnames))
    samples_dao = BiobankStoredSampleDao()
    biobank_id_prefix = get_biobank_id_prefix()
    written = 0
    try:
        samples = []
        for row in csv_reader:
            sample = _create_sample_from_row(row, biobank_id_prefix)
            if sample:
                samples.append(sample)
                if len(samples) >= _BATCH_SIZE:
                    written += samples_dao.upsert_all(samples)
                    samples = []
        if samples:
            written += samples_dao.upsert_all(samples)
        return written
    except ValueError, e:
        raise DataError(e)