Skip to content
Snippets Groups Projects
apdbatchsetter.cpp 4.16 KiB
Newer Older
Jan Reher's avatar
Jan Reher committed
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>

#include <productiondatabaseclient.h>
#include <boost/program_options.hpp>
#include "toolbox.cpp"

using namespace std;
using namespace ProductionDatabase;
namespace po = boost::program_options;

static bool irradiatedAPDs = false;
static string fileName = "serials.dat";
static uint batchNo = 0;
static string username = "";
static string password = "";
static ProductionDatabaseClient * proddb;

void processArgumentsAndQueryMissing(int m_argc, char* m_argv[]);
po::options_description setupDescription();
po::variables_map setupVariableMap(int argc, char* argv[], po::options_description desc);
void processVariablesAndQueryMissing(po::variables_map vm, po::options_description desc);
bool checkDatabaseConnectivityAndCredentials();
void getCredentials();
bool isResponseGood(DatabaseClientResponse response);
void getIrradiationStatus(string type);
void queryBatchNumber();

int main(int argc, char* argv[]) {
  proddb = new ProductionDatabaseClient();
  processArgumentsAndQueryMissing(argc, argv);
  vector<string> apdSerials = loadSerialsFromFileName(fileName);
  if (!checkDatabaseConnectivityAndCredentials()) return 1;
  proddb->assignBatchNumberToAPDs(apdSerials,batchNo,irradiatedAPDs);
  cout  << "\nSuccessfully assigned Batch " << batchNo << " to APDs from serial file " << fileName << "." << endl << endl;
  return (-apdSerials.empty());
}

bool checkDatabaseConnectivityAndCredentials(){
  getCredentials();
  DatabaseClientResponse response = proddb->checkConnectivityAndCredentials();
  return isResponseGood(response);
}

void getCredentials() {
  if (username == "" || password == "") proddb->queryCredentials();
  else proddb->setCredentials(username, password);
}

bool isResponseGood(DatabaseClientResponse response) {
  if (response != Successful ) {
    cerr << "Connection to database failed because of error: " << response << endl;
    return false;
  }
  return true;
}

void processArgumentsAndQueryMissing(int m_argc, char* m_argv[]) {
  po::options_description desc = setupDescription();
  po::variables_map vm = setupVariableMap(m_argc, m_argv, desc);
  processVariablesAndQueryMissing(vm, desc);
}

po::options_description setupDescription() {
  po::options_description desc("Available options");
  desc.add_options()
      ("help", "produce help message")
      ("type", po::value<string>(), "(required) type of APDs [new, irr]")
      ("batch", po::value<int>(), "(required) batch number to be set")
      ("fileName", po::value<string>(), "file name or serial numbers [serials.dat]")
      ("user", po::value<string>(), "User name to connect to DB. Only used when combined with pass!")
      ("pass", po::value<string>(), "Password to connect to DB. Only used when combined with user!")
      ;
  return desc;
}

po::variables_map setupVariableMap(int argc, char* argv[], po::options_description desc) {
  po::variables_map vm;
  po::store(po::parse_command_line(argc, argv, desc), vm);
  po::notify(vm);
  return vm;
}

void processVariablesAndQueryMissing(po::variables_map vm, po::options_description desc) {
  if (vm.count("help")) {
    cout << desc << "\n";
    exit(0);
  }
  if (vm.count("batch")) {
    batchNo = uint(vm["batch"].as<int>());
  } else {
    queryBatchNumber();
  }
  if (vm.count("fileName")) {
    fileName = vm["fileName"].as<string>();
    cout << "Reading serials from " << fileName << endl;
  }
  if (vm.count("user")) {
    username = vm["user"].as<string>();
  }
  if (vm.count("pass")) {
    password = vm["pass"].as<string>();
  }
  if (vm.count("type")) {
    getIrradiationStatus(vm["type"].as<string>());
  } else {
    irradiatedAPDs = (batchNo >= 10000);
  }
}

void queryBatchNumber() {
  cout << "Which Batch do these APDs belong to?" << endl;
  try {
    cin >> batchNo;
    cout << endl;
  } catch (...) {
    batchNo = 0;
  }
  if (batchNo <= 0) {
    cerr << "Invalid batch number!" << endl;
    exit(-1);
  }
}

void getIrradiationStatus(string type) {
  if (type == "new") {
    irradiatedAPDs = false;
  }
  else if (type == "irr") {
    irradiatedAPDs = true;
  } else {
    cerr << "Invalid type: " << type << "! Possible APD types: 'new' or 'irr'" << endl;
    exit(0);
  }
}