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
#include <fstream>
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <productiondatabaseclient.h>
#include <boost/program_options.hpp>
#include "toolbox.cpp"
#include "ProdDbAccess.h"
#include "QtGui/QApplication"
namespace po=boost::program_options;
using namespace std;
using namespace ProductionDatabase;
static string fileName = "serials.dat";
static string username = "";
static string password = "";
static string newLocation = "";
static string change = "";
static bool debug = false;
static bool unknownonly = false;
void processArgumentsAndQueryMissing(int m_argc, char* m_argv[]) {
po::options_description desc("Available options");
desc.add_options()
("help", "produce help message")
("fileName", po::value<string>(), "file name for 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!")
("location", po::value<string>(), "New Location of APDs")
("debug", "emit additional messages for debugging purposes")
("unknownOnly", "only work on APDs with location 'unknown' or 'Bochum'")
("change", po::value<string>(), "Overwrite this even if 'unknownonly' is set.")
;
po::variables_map vm;
po::store(po::parse_command_line(m_argc, m_argv, desc), vm);
po::notify(vm);
if (vm.count("help")) {
cout << desc << "\n";
exit(0);
}
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("location")) {
newLocation = vm["location"].as<string>();
} else {
cout << "Where are these APDs?" << endl;
try {
cin >> newLocation;
cout << endl;
} catch (...) {
newLocation = "";
}
if (newLocation == "") {
cerr << "Invalid location!" << endl;
exit(-1);
}
}
if (vm.count("debug")) {
debug = true;
}
if (vm.count("unknownOnly")) {
unknownonly = true;
}
if (vm.count("change")) {
change = vm["change"].as<string>();
}
}
int main(int argc, char* argv[]) {
QApplication *_app = new QApplication(argc, argv);
processArgumentsAndQueryMissing(argc, argv);
ProdDbAccess *_proddb = new ProdDbAccess();
vector<string> apdSerials;
vector<string> allApdSerials;
allApdSerials = loadSerialsFromFileName(fileName);
apdSerials.clear();
for ( size_t i = 0 ; i < allApdSerials.size() ; i++ ) {
string existinglocation = _proddb->location( uint( stoul( allApdSerials[i] ) ) );
if ( existinglocation != newLocation && ( !unknownonly || existinglocation == "unknown" || existinglocation == "Bochum" || existinglocation == change ) )
apdSerials.push_back(allApdSerials[i]);
}
if ( apdSerials.size() == 0 ) {
cout << "No data to write! Exiting." << endl;
return 0;
}
cout << "Data will be written for " << apdSerials.size() << " APDs." << endl;
ProductionDatabaseClient *_proddbclient = new ProductionDatabaseClient();
if (username == "" || password == "") _proddbclient->queryCredentials();
else _proddbclient->setCredentials(username, password);
DatabaseClientResponse response = _proddbclient->checkConnectivityAndCredentials();
if (response != Successful ) {
cerr << "Connection to database failed because of error: " << response << endl;
return response;
}
if (debug) cout << "Connection successful!" << endl;
_proddbclient->storeNewAPDLocation(apdSerials,newLocation);
cout << "\nSuccessfully assigned Location '" << newLocation << "' to APDs from serial file " << fileName << "!\n\nThank you for using the apdLocationSetter :)" << endl << endl;
_app->exit(0);
return (-apdSerials.empty());
}