-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileManager.cpp
More file actions
248 lines (221 loc) · 10.2 KB
/
Copy pathFileManager.cpp
File metadata and controls
248 lines (221 loc) · 10.2 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#include "../include/FileManager.h"
#include "../include/PrintedBook.h"
#include "../include/DigitalBook.h"
#include "../include/Admin.h"
#include "../include/Librarian.h"
#include "../include/Member.h"
#include "../include/Utils.h"
FileManager::FileManager(std::string booksPath, std::string usersPath,
std::string txPath, std::string finesPath, std::string logPath)
: booksFilePath(booksPath), usersFilePath(usersPath),
transactionsFilePath(txPath), finesFilePath(finesPath), logFilePath(logPath) {}
bool FileManager::loadAllData(Database& db) {
bool b1 = loadUsers(db);
bool b2 = loadBooks(db);
bool b3 = loadTransactions(db);
bool b4 = loadFines(db);
logEvent("Loaded system data from persistent disk storage.");
return b1 && b2 && b3 && b4;
}
bool FileManager::saveAllData(const Database& db) {
bool b1 = saveUsers(db);
bool b2 = saveBooks(db);
bool b3 = saveTransactions(db);
bool b4 = saveFines(db);
logEvent("Saved all database records to persistent storage.");
return b1 && b2 && b3 && b4;
}
bool FileManager::loadBooks(Database& db) {
std::ifstream file(booksFilePath);
if (!file.is_open()) return false;
std::string line;
while (std::getline(file, line)) {
if (line.empty() || line[0] == '#') continue;
auto tokens = Utils::split(line, ',');
if (tokens.size() < 11) continue;
std::string type = tokens[0];
std::string isbn = tokens[1];
std::string title = tokens[2];
std::string author = tokens[3];
std::string category = tokens[4];
int year = std::stoi(tokens[5]);
int totalCopies = std::stoi(tokens[6]);
int availCopies = std::stoi(tokens[7]);
if (type == "Printed") {
std::string shelf = tokens[8];
int pages = std::stoi(tokens[9]);
std::string cond = tokens[10];
auto book = std::make_shared<PrintedBook>(isbn, title, author, category, year, totalCopies, shelf, pages, cond);
book->setAvailableCopies(availCopies);
db.addBook(book);
} else if (type == "Digital") {
double sizeMB = std::stod(tokens[8]);
std::string format = tokens[9];
std::string url = tokens[10];
auto book = std::make_shared<DigitalBook>(isbn, title, author, category, year, totalCopies, sizeMB, format, url);
book->setAvailableCopies(availCopies);
db.addBook(book);
}
}
file.close();
return true;
}
bool FileManager::saveBooks(const Database& db) {
std::ofstream file(booksFilePath);
if (!file.is_open()) return false;
file << "#Type,ISBN,Title,Author,Category,Year,TotalCopies,AvailableCopies,Extra1,Extra2,Extra3\n";
for (const auto& pair : db.books) {
const auto& b = pair.second;
if (b->getBookType() == "Printed") {
auto pb = std::dynamic_pointer_cast<PrintedBook>(b);
file << "Printed," << pb->getIsbn() << "," << pb->getTitle() << "," << pb->getAuthor() << ","
<< pb->getCategory() << "," << pb->getPublicationYear() << "," << pb->getTotalCopies() << ","
<< pb->getAvailableCopies() << "," << pb->getShelfLocation() << "," << pb->getPageCount() << ","
<< pb->getCondition() << "\n";
} else if (b->getBookType() == "Digital") {
auto dbk = std::dynamic_pointer_cast<DigitalBook>(b);
file << "Digital," << dbk->getIsbn() << "," << dbk->getTitle() << "," << dbk->getAuthor() << ","
<< dbk->getCategory() << "," << dbk->getPublicationYear() << "," << dbk->getTotalCopies() << ","
<< dbk->getAvailableCopies() << "," << dbk->getFileSizeMB() << "," << dbk->getFileFormat() << ","
<< dbk->getDownloadUrl() << "\n";
}
}
file.close();
return true;
}
bool FileManager::loadUsers(Database& db) {
std::ifstream file(usersFilePath);
if (!file.is_open()) return false;
std::string line;
while (std::getline(file, line)) {
if (line.empty() || line[0] == '#') continue;
auto tokens = Utils::split(line, ',');
if (tokens.size() < 6) continue;
std::string roleStr = tokens[0];
std::string uId = tokens[1];
std::string passHash = tokens[2];
std::string name = tokens[3];
std::string email = tokens[4];
std::string phone = tokens[5];
if (roleStr == "Admin") {
std::string level = (tokens.size() > 6) ? tokens[6] : "SystemAdmin";
auto admin = std::make_shared<Admin>(uId, "", name, email, phone, level);
admin->setPassword(passHash); // Keep original hash
admin->setPassword(passHash); // Overwrite hash directly
// Direct set password hash override:
// Since User has passwordHash protected, User constructor hashes raw password, so we set raw pass hash directly:
struct UserAccess : public User {
using User::passwordHash;
};
std::static_pointer_cast<UserAccess>(admin)->passwordHash = passHash;
db.addUser(admin);
} else if (roleStr == "Librarian") {
std::string empId = (tokens.size() > 6) ? tokens[6] : "EMP-" + uId;
std::string shift = (tokens.size() > 7) ? tokens[7] : "Day";
auto lib = std::make_shared<Librarian>(uId, "", name, email, phone, empId, shift);
struct UserAccess : public User { using User::passwordHash; };
std::static_pointer_cast<UserAccess>(lib)->passwordHash = passHash;
db.addUser(lib);
} else if (roleStr == "Member") {
int limit = (tokens.size() > 6) ? std::stoi(tokens[6]) : 5;
double fines = (tokens.size() > 7) ? std::stod(tokens[7]) : 0.0;
auto mem = std::make_shared<Member>(uId, "", name, email, phone, limit);
mem->setUnpaidFines(fines);
struct UserAccess : public User { using User::passwordHash; };
std::static_pointer_cast<UserAccess>(mem)->passwordHash = passHash;
db.addUser(mem);
}
}
file.close();
return true;
}
bool FileManager::saveUsers(const Database& db) {
std::ofstream file(usersFilePath);
if (!file.is_open()) return false;
file << "#Role,UserId,PasswordHash,Name,Email,Phone,Extra1,Extra2\n";
for (const auto& pair : db.users) {
const auto& u = pair.second;
if (u->getRole() == UserRole::ADMIN) {
auto adm = std::dynamic_pointer_cast<Admin>(u);
file << "Admin," << adm->getUserId() << "," << adm->getPasswordHash() << "," << adm->getName() << ","
<< adm->getEmail() << "," << adm->getPhone() << "," << adm->getAdminLevel() << "\n";
} else if (u->getRole() == UserRole::LIBRARIAN) {
auto lib = std::dynamic_pointer_cast<Librarian>(u);
file << "Librarian," << lib->getUserId() << "," << lib->getPasswordHash() << "," << lib->getName() << ","
<< lib->getEmail() << "," << lib->getPhone() << "," << lib->getEmployeeId() << "," << lib->getShiftHours() << "\n";
} else if (u->getRole() == UserRole::MEMBER) {
auto mem = std::dynamic_pointer_cast<Member>(u);
file << "Member," << mem->getUserId() << "," << mem->getPasswordHash() << "," << mem->getName() << ","
<< mem->getEmail() << "," << mem->getPhone() << "," << mem->getMaxBorrowLimit() << "," << mem->getUnpaidFines() << "\n";
}
}
file.close();
return true;
}
bool FileManager::loadTransactions(Database& db) {
std::ifstream file(transactionsFilePath);
if (!file.is_open()) return false;
std::string line;
while (std::getline(file, line)) {
if (line.empty() || line[0] == '#') continue;
auto tokens = Utils::split(line, ',');
if (tokens.size() < 7) continue;
Transaction tx(tokens[0], tokens[1], tokens[2], tokens[3], tokens[4], tokens[5], tokens[6]);
db.addTransaction(tx);
// Update member borrowed books list if status is ISSUED or RENEWED
if (tokens[6] == "ISSUED" || tokens[6] == "RENEWED") {
auto u = db.getUser(tokens[1]);
if (u && u->getRole() == UserRole::MEMBER) {
auto mem = std::dynamic_pointer_cast<Member>(u);
if (mem) mem->recordBorrow(tokens[2]);
}
}
}
file.close();
return true;
}
bool FileManager::saveTransactions(const Database& db) {
std::ofstream file(transactionsFilePath);
if (!file.is_open()) return false;
file << "#TransactionId,MemberId,ISBN,IssueDate,DueDate,ReturnDate,Status\n";
for (const auto& tx : db.transactions) {
file << tx.getTransactionId() << "," << tx.getMemberId() << "," << tx.getIsbn() << ","
<< tx.getIssueDate() << "," << tx.getDueDate() << "," << tx.getReturnDate() << ","
<< tx.getStatus() << "\n";
}
file.close();
return true;
}
bool FileManager::loadFines(Database& db) {
std::ifstream file(finesFilePath);
if (!file.is_open()) return false;
std::string line;
while (std::getline(file, line)) {
if (line.empty() || line[0] == '#') continue;
auto tokens = Utils::split(line, ',');
if (tokens.size() < 7) continue;
Fine fine(tokens[0], tokens[1], tokens[2], std::stod(tokens[3]), tokens[4], (tokens[5] == "1" || tokens[5] == "true"), tokens[6]);
db.addFine(fine);
}
file.close();
return true;
}
bool FileManager::saveFines(const Database& db) {
std::ofstream file(finesFilePath);
if (!file.is_open()) return false;
file << "#FineId,MemberId,TransactionId,Amount,Reason,IsPaid,DateIssued\n";
for (const auto& f : db.fines) {
file << f.getFineId() << "," << f.getMemberId() << "," << f.getTransactionId() << ","
<< f.getAmount() << "," << f.getReason() << "," << (f.getIsPaid() ? "1" : "0") << ","
<< f.getDateIssued() << "\n";
}
file.close();
return true;
}
void FileManager::logEvent(const std::string& eventMessage) {
std::ofstream file(logFilePath, std::ios::app);
if (file.is_open()) {
file << "[" << Utils::getCurrentDateStr() << "] " << eventMessage << "\n";
file.close();
}
}