DakotaAlgorithmRecord DakotaAlgorithmRecord::factoryFromDakotaAlgorithm(
    const analysis::DakotaAlgorithm& dakotaAlgorithm, AnalysisRecord& analysisRecord)
{
  if (dakotaAlgorithm.optionalCast<analysis::DDACEAlgorithm>()) {
    return DDACEAlgorithmRecord(dakotaAlgorithm.cast<analysis::DDACEAlgorithm>(),
                                analysisRecord);
  }
  if (dakotaAlgorithm.optionalCast<analysis::FSUDaceAlgorithm>()) {
    return FSUDaceAlgorithmRecord(dakotaAlgorithm.cast<analysis::FSUDaceAlgorithm>(),
                                analysisRecord);
  }
  if (dakotaAlgorithm.optionalCast<analysis::PSUADEDaceAlgorithm>()) {
    return PSUADEDaceAlgorithmRecord(dakotaAlgorithm.cast<analysis::PSUADEDaceAlgorithm>(),
                                analysisRecord);
  }
  if (dakotaAlgorithm.optionalCast<analysis::ParameterStudyAlgorithm>()) {
    return ParameterStudyAlgorithmRecord(dakotaAlgorithm.cast<analysis::ParameterStudyAlgorithm>(),
                                analysisRecord);
  }
  if (dakotaAlgorithm.optionalCast<analysis::SamplingAlgorithm>()) {
    return SamplingAlgorithmRecord(dakotaAlgorithm.cast<analysis::SamplingAlgorithm>(),
                                analysisRecord);
  }

  OS_ASSERT(false);
  return DakotaAlgorithmRecord(std::shared_ptr<detail::DakotaAlgorithmRecord_Impl>());
}
boost::optional<DakotaAlgorithmRecord> DakotaAlgorithmRecord::factoryFromQuery(
    const QSqlQuery& query, ProjectDatabase& database)
{
  OptionalDakotaAlgorithmRecord result;

  int dakotaAlgorithmRecordType = query.value(AlgorithmRecordColumns::dakotaAlgorithmRecordType).toInt();

  switch (dakotaAlgorithmRecordType) {
    case DakotaAlgorithmRecordType::DDACEAlgorithmRecord :
      result = DDACEAlgorithmRecord(query, database).cast<DakotaAlgorithmRecord>();
     break;
     case DakotaAlgorithmRecordType::FSUDaceAlgorithmRecord :
      result = FSUDaceAlgorithmRecord(query, database).cast<DakotaAlgorithmRecord>();
     break;
     case DakotaAlgorithmRecordType::PSUADEDaceAlgorithmRecord :
      result = PSUADEDaceAlgorithmRecord(query, database).cast<DakotaAlgorithmRecord>();
     break;
     case DakotaAlgorithmRecordType::ParameterStudyAlgorithmRecord :
      result = ParameterStudyAlgorithmRecord(query, database).cast<DakotaAlgorithmRecord>();
     break;
     case DakotaAlgorithmRecordType::SamplingAlgorithmRecord :
      result = SamplingAlgorithmRecord(query, database).cast<DakotaAlgorithmRecord>();
     break;
    default :
      LOG(Error,"Unknown DakotaAlgorithmRecordType " << dakotaAlgorithmRecordType);
      return boost::none;
  }

  return result;
}
boost::optional<DDACEAlgorithmRecord> DDACEAlgorithmRecord::factoryFromQuery(
    const QSqlQuery& query, ProjectDatabase& database)
{
  OptionalDDACEAlgorithmRecord result;

  try {
    result = DDACEAlgorithmRecord(query,database);
  }
  catch (const std::exception& e) {
    LOG(Error,"Unable to construct DDACEAlgorithmRecord from query, because '"
        << e.what() << "'.");
  }

  return result;
}
std::vector<DDACEAlgorithmRecord> DDACEAlgorithmRecord::getDDACEAlgorithmRecords(ProjectDatabase& database) {
  std::vector<DDACEAlgorithmRecord> result;

  QSqlQuery query(*(database.qSqlDatabase()));
  query.prepare(toQString("SELECT * FROM " + AlgorithmRecord::databaseTableName() +
      " WHERE algorithmRecordType=:algorithmRecordType AND " +
      "dakotaAlgorithmRecordType=:dakotaAlgorithmRecordType"));
  query.bindValue(":algorithmRecordType", AlgorithmRecordType::DakotaAlgorithmRecord);
  query.bindValue(":dakotaAlgorithmRecordType", DakotaAlgorithmRecordType::DDACEAlgorithmRecord);
  assertExec(query);
  while (query.next()) {
    result.push_back(DDACEAlgorithmRecord(query, database));
  }

  return result;
}
boost::optional<DDACEAlgorithmRecord> DDACEAlgorithmRecord::getDDACEAlgorithmRecord(int id, ProjectDatabase& database) {
  boost::optional<DDACEAlgorithmRecord> result;

  QSqlQuery query(*(database.qSqlDatabase()));
  query.prepare(toQString("SELECT * FROM " + AlgorithmRecord::databaseTableName() +
      " WHERE algorithmRecordType=:algorithmRecordType AND " +
      "dakotaAlgorithmRecordType=:dakotaAlgorithmRecordType AND " +
      "id=:id"));
  query.bindValue(":algorithmRecordType", AlgorithmRecordType::DakotaAlgorithmRecord);
  query.bindValue(":dakotaAlgorithmRecordType", DakotaAlgorithmRecordType::DDACEAlgorithmRecord);
  query.bindValue(":id",id);
  assertExec(query);
  if (query.first()) {
    result = DDACEAlgorithmRecord(query, database);
  }

  return result;
}