-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.cpp
300 lines (238 loc) · 8.71 KB
/
service.cpp
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#include <sstream>
#include "myexceptions.h"
#include "recording.h"
#include "service.h"
#include <QDebug>
using namespace std;
Service::Service(Repository* repository) {
auto memory_repository = dynamic_cast<MemoryRepository*>(repository);
if (memory_repository) {
this->repository = memory_repository;
this->has_file_repository = false;
qDebug() << "It is a memory repository!";
} else {
this->file_repository = dynamic_cast<FileRepository*>(repository);
this->has_file_repository = true;
}
}
bool Service::does_service_have_file_repository() {
return has_file_repository;
}
string Service::get_file_repository_filename() {
return file_repository->get_filename();
}
void Service::add(string title, string location, string time_of_creation, string times_accessed, string footage_preview) {
/**
*
* Adds a new recording to the repository.
*
* Input:
* - title, location, time_of_creation, times_accessed, footage_preview: strings
**/
validator.validate_time_of_creation(time_of_creation);
validator.validate_times_accessed(times_accessed);
Recording recording(title, location, time_of_creation, stoi(times_accessed), footage_preview);
if (!has_file_repository) {
repository->add(recording);
} else {
file_repository->add(recording);
}
}
vector<Recording> Service::get_repository_container() {
/**
* Gets the contents of the custom-made std::vector from the repository.
*
**/
if (!has_file_repository) {
return repository->get_container();
} else {
return file_repository->get_container();
}
}
bool Service::search(string title) {
if (!has_file_repository) {
return repository->search(title);
} else {
return file_repository->search(title);
}
}
void Service::remove(string title) {
/**
*
* Removes a recording from the repository by its title.
*
* Input:
* - title: string
**/
if (!has_file_repository) {
repository->remove(title);
} else {
file_repository->remove(title);
}
}
void Service::update(string title, string location, string time_of_creation, string times_accessed, string footage_preview) {
/**
*
* Updates the details of a recording in the repository.
* In case the recording with the specified title does not exist, the method throws
* a RepositoryException.
*
* Input:
* - title, location, time_of_creation, times_accessed, footage_preview: strings
* Throws:
* - RepositoryException: in case the recording with the given title doesn't exist
**/
validator.validate_time_of_creation(time_of_creation);
validator.validate_times_accessed(times_accessed);
if (!has_file_repository && !repository->search(title)) {
RepositoryException re("RepositoryException: The element cannot be updated because it doesn't exist!\n");
throw re;
} else if (has_file_repository && !file_repository->search(title)) {
RepositoryException re("RepositoryException: The element cannot be updated because it doesn't exist!\n");
throw re;
}
if (!has_file_repository) {
repository->remove(title);
} else {
file_repository->remove(title);
}
Recording recording(title, location, time_of_creation, stoi(times_accessed), footage_preview);
if (!has_file_repository)
repository->add(recording);
else
file_repository->add(recording);
}
void Service::set_selected_recording_index(int new_index) {
if (!has_file_repository) {
repository->set_selected_recording_index(new_index);
} else {
file_repository->set_selected_recording_index(new_index);
}
}
int Service::get_selected_recording_index() {
if (!has_file_repository) {
return repository->get_selected_recording_index();
} else {
return file_repository->get_selected_recording_index();
}
}
string Service::next() {
if (!has_file_repository)
return repository->next();
else
return file_repository->next();
}
void Service::save(string title) {
if (!has_file_repository)
repository->save(title);
else
file_repository->save(title); /// has to be fixed
}
void Service::set_current_recording(int position) {
if (!has_file_repository) {
repository->set_current_recording(position);
} else {
file_repository->set_current_recording(position);
}
}
vector<Recording> Service::get_watchlist() {
if (!has_file_repository)
return repository->get_watchlist();
else
return file_repository->get_watchlist();
}
Service::~Service() {
}
void Service::set_file_repository_filename(string new_filename) {
file_repository->set_filename(new_filename);
}
void Service::set_watchlist_filename(string file_path) {
file_repository->set_watchlist_filename(file_path);
if (file_path.find(".html") != std::string::npos) {
file_repository->update_watchlist_html_file();
} else {
file_repository->update_watchlist_csv_file();
}
}
void Service::remove_from_watchlist(int element_index) {
if (!has_file_repository) {
repository->remove_from_watchlist(element_index);
} else {
file_repository->remove_from_watchlist(element_index);
}
}
void Service::push_to_undo_stack(vector<string> operation) {
undo_stack.push(operation);
}
vector<string> Service::pop_from_undo_stack() {
vector<string> operation = undo_stack.top();
undo_stack.pop();
return operation;
}
void Service::undo() {
if (undo_stack.empty())
return;
vector<string> operation = this->pop_from_undo_stack();
if (operation[0] == "add") {
this->add(operation[1], operation[2], operation[3], operation[4], operation[5]);
vector<string> redo_operation = {"delete", operation[1], operation[2], operation[3], operation[4], operation[5]};
redo_stack.push(redo_operation);
} else if (operation[0] == "delete") {
this->remove(operation[1]);
vector<string> redo_operation = {"add", operation[1], operation[2], operation[3], operation[4], operation[5]};
redo_stack.push(redo_operation);
} else if (operation[0] == "update") {
vector<Recording> container = this->get_repository_container();
Recording searched_recording;
for (auto recording: container) {
if (recording.get_title() == operation[1]) {
searched_recording = recording;
break;
}
}
this->update(operation[1], operation[2], operation[3], operation[4], operation[5]);
vector<string> redo_operation = {"update", searched_recording.get_title(),
searched_recording.get_location(),
searched_recording.get_time_of_creation(),
std::to_string(searched_recording.get_times_accessed()),
searched_recording.get_footage_preview()};
redo_stack.push(redo_operation);
}
}
void Service::redo() {
if (redo_stack.empty())
return;
vector<string> operation = redo_stack.top();
redo_stack.pop();
if (operation[0] == "add") {
this->add(operation[1], operation[2], operation[3], operation[4], operation[5]);
vector<string> redo_operation = {"delete", operation[1], operation[2], operation[3], operation[4], operation[5]};
undo_stack.push(redo_operation);
} else if (operation[0] == "delete") {
this->remove(operation[1]);
vector<string> redo_operation = {"add", operation[1], operation[2], operation[3], operation[4], operation[5]};
for (auto token: redo_operation) {
qDebug() << QString::fromStdString(token);
}
undo_stack.push(redo_operation);
} else if (operation[0] == "update") {
vector<Recording> container = this->get_repository_container();
Recording searched_recording;
for (auto recording: container) {
if (recording.get_title() == operation[1]) {
searched_recording = recording;
break;
}
}
this->update(operation[1], operation[2], operation[3], operation[4], operation[5]);
vector<string> redo_operation = {"update", searched_recording.get_title(),
searched_recording.get_location(),
searched_recording.get_time_of_creation(),
std::to_string(searched_recording.get_times_accessed()),
searched_recording.get_footage_preview()};
undo_stack.push(redo_operation);
}
}
Repository* Service::get_pointer_to_repo() {
return repository;
}