Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#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);
}
}