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;
}
std::vector<ParameterStudyAlgorithmRecord> ParameterStudyAlgorithmRecord::getParameterStudyAlgorithmRecords(
    ProjectDatabase& database) {
  std::vector<ParameterStudyAlgorithmRecord> result;

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

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

  try {
    result = ParameterStudyAlgorithmRecord(query,database);
  }

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

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

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

  return result;
}