forked from University/epr24pr5-ojanssen2
161 lines
4.3 KiB
C++
161 lines
4.3 KiB
C++
#include "Task.h"
|
|
#include "Util.h"
|
|
#include "Manager.h"
|
|
#include "Error.h"
|
|
|
|
using err::Error;
|
|
using util::Util;
|
|
using util::Manager;
|
|
|
|
namespace models
|
|
{
|
|
Task::Task(const int& id, const string& name, const string& description, const vector<int>& children):
|
|
id(id), name(name), description(description), children(children)
|
|
{
|
|
}
|
|
|
|
Task::Task(): id(0), name(""), description(""), children({})
|
|
{
|
|
}
|
|
|
|
Task::Task(const AddTask& t): id(0), name(t.get_name()), description(t.get_description()), children(t.get_children())
|
|
{
|
|
Manager* mgr = Manager::get_instance();
|
|
this->id = mgr->get_task_id();
|
|
}
|
|
|
|
|
|
ostream& Task::write(ostream& stream) const
|
|
{
|
|
stream << *this;
|
|
return stream;
|
|
}
|
|
|
|
int Task::get_id() const { return this->id; }
|
|
|
|
/**
|
|
* Diese Methode überprüft ob der Task Aktiv ist ( Nicht Nachfolger einer anderen Klass ist)
|
|
* @return bool
|
|
*/
|
|
bool Task::is_active() const {
|
|
|
|
const Manager * mgr = Manager::get_instance();
|
|
for (const Task * task : mgr->get_tasks()) {
|
|
for (const int child_id : task->get_children()) {
|
|
// Aktiver Task ist Child von Einem andern Task -> Nicht aktiv
|
|
if (this->id == child_id) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
//Kein Child-> Aktiv
|
|
return true;
|
|
}
|
|
|
|
string Task::get_name() const
|
|
{
|
|
return this->name;
|
|
}
|
|
|
|
string Task::get_description() const
|
|
{
|
|
return this->description;
|
|
}
|
|
|
|
vector<int> Task::get_children() const { return this->children; }
|
|
|
|
|
|
AddTask::AddTask(): Task() {}
|
|
|
|
AddTask::AddTask(Task& t): Task(t)
|
|
{
|
|
}
|
|
|
|
ostream& operator<<(ostream& os, const Task& t)
|
|
{
|
|
os << t.get_id() << " %" << t.get_name() << "% %" << t.get_description() << "%";
|
|
// Fügt Children Task mit passenden Leerzeichen hinzu
|
|
if (!t.children.empty())
|
|
os << " ";
|
|
for (size_t i = 0; i < t.children.size(); ++i)
|
|
{
|
|
os << t.children[i];
|
|
if (i < t.children.size() - 1)
|
|
{
|
|
os << " ";
|
|
}
|
|
}
|
|
|
|
os << "\n";
|
|
return os;
|
|
}
|
|
|
|
istream& operator>>(istream& is, Task& t)
|
|
{
|
|
int id;
|
|
is >> id;
|
|
if (!is)
|
|
throw Error(602, "Datei hat ein unbekanntes Format.");
|
|
|
|
try
|
|
{
|
|
string name = Util::read_string_between_percent(is);
|
|
if (name.empty())
|
|
throw Error(102, "Ein Parameter eines Befehls konnte nicht gelesen werden");
|
|
string description = Util::read_string_between_percent(is);
|
|
vector<int> children = Task::read_children(is);
|
|
t = {id, name, description, children};
|
|
return is;
|
|
}
|
|
catch (Error& err)
|
|
{
|
|
if (err.get_nr() != 700)
|
|
throw;
|
|
throw Error(602, "Datei hat ein unbekanntes Format.");
|
|
}
|
|
}
|
|
|
|
ostream& operator<<(ostream& os, const AddTask& t)
|
|
{
|
|
os << t.get_name() << " " << t.get_description() << "\n";
|
|
return os;
|
|
}
|
|
|
|
istream& operator>>(istream& is, AddTask& t)
|
|
{
|
|
try
|
|
{
|
|
const string name = Util::read_string_between_percent(is);
|
|
if (name.empty())
|
|
throw Error(102, "Ein Parameter eines Befehls konnte nicht gelesen werden");
|
|
const string description = Util::read_string_between_percent(is);
|
|
const vector<int> children = Task::read_children(is);
|
|
Task at = {0, name, description, children};
|
|
t = at;
|
|
return is;
|
|
}
|
|
catch (Error& err)
|
|
{
|
|
if (err.get_nr() != 700)
|
|
throw;
|
|
throw Error(102, "Ein Parameter eines Befehls konnte nicht gelesen werden");
|
|
}
|
|
}
|
|
|
|
vector<int> Task::read_children(istream &is) {
|
|
vector<int> nums;
|
|
int num;
|
|
|
|
// Read integers directly from the stream until we hit an error (non-integer or EOF)
|
|
while (is >> num) {
|
|
nums.push_back(num);
|
|
}
|
|
// Stream wurde bad durch was anderes als EOF?
|
|
if (!is.eof()) {
|
|
is.clear();
|
|
throw Error(703, "Could not convert input to integer.");
|
|
}
|
|
|
|
return nums;
|
|
}
|
|
} // models
|