00001 #include "taskcreator.h"
00002
00003 #include <controldata.h>
00004 #include <models/taskmodel.h>
00005 #include <models/resourcemodel.h>
00006 #include <models/userrequirementsmodel.h>
00007 #include <models/resourcerequirementsmodel.h>
00008
00009 #include <domain/taskmanagerdata.h>
00010 #include <domain/schedule.h>
00011 #include <domain/user.h>
00012 #include <domain/usertype.h>
00013 #include <domain/project.h>
00014
00015 #include <tools/functions.h>
00016 #include <tools/stringexception.h>
00017 #include <tools/invaliddataexception.h>
00018
00019 #include <QDebug>
00020
00021 using namespace control;
00022
00023 ControllerInterface::ActionType TaskCreator::actionType() const
00024 {
00025 return CreateAction;
00026 }
00027
00028 ControllerInterface::DataType TaskCreator::dataType() const
00029 {
00030 return TaskData;
00031 }
00032
00033 QString TaskCreator::description() const
00034 {
00035 return "Create task";
00036 }
00037
00038 bool TaskCreator::userIsValid() const
00039 {
00040 return userHasLoggedIn() && !userIsAdmin();
00041 }
00042
00043 void TaskCreator::execute()
00044 {
00045
00046 QList<domain::TaskType*> types = data->taskTypes();
00047
00048 if (types.isEmpty())
00049 {
00050 ui->showError("No known task types, load a theme first");
00051 return;
00052 }
00053
00054 QMap<QString, domain::TaskType*> typeMap;
00055
00056 Q_FOREACH (domain::TaskType* type, types)
00057 typeMap[type->name()] = type;
00058
00059 QString msg = "What type of task would you like to create?";
00060 data::StringInputData typeInput = ui->getChoice(typeMap.keys(), msg);
00061
00062 if (!typeInput.processData)
00063 return;
00064
00065 domain::TaskType* type = typeMap[typeInput];
00066 if (!type->userCanCreate(user()->userType()))
00067 {
00068 ui->showError(QString("User of type \"%1\" is not allowed "
00069 "to create a task of type \"%2\"")
00070 .arg(user()->userType()->name())
00071 .arg(type->name()));
00072
00073 return;
00074 }
00075
00076 QList<const domain::Task*> taskList = tools::makeConstList(data->tasks());
00077
00078 Q_FOREACH (const domain::Task* task, taskList)
00079 {
00080 if (task->user() != user())
00081 taskList.removeAll(task);
00082 }
00083
00084 domain::Task* task;
00085
00086 try
00087 {
00088 data::TaskRepresentation rep;
00089 TaskModel tasks(data, taskList);
00090 rep.subTasks = &tasks;
00091 rep.type = data::TaskTypeDetails(type);
00092 UserRequirementsModel userReqs(type);
00093 rep.userReqs = &userReqs;
00094 ResourceRequirementsModel resourceReqs(type);
00095 rep.resourceReqs = &resourceReqs;
00096 data::TaskDetails details = ui->getTaskDetails(rep);
00097
00098 if (!details.processData)
00099 return;
00100
00101 domain::Schedule schedule(details.duration, details.startTime,
00102 details.endTime);
00103
00104 task = data->createTask(type, user(), schedule);
00105
00106 try
00107 {
00108
00109 Q_FOREACH (QModelIndex index, details.selectedTasks.selection)
00110 {
00111 domain::Task* dep = data->task(tasks.taskForIndex(index));
00112 task->addSubTask(dep);
00113 }
00114
00115 Q_FOREACH (QString field, details.fields.keys())
00116 task->setField(field, details.fields[field]);
00117 }
00118 catch (tools::InvalidDataException& e)
00119 {
00120 data->removeData(task);
00121 throw e;
00122 }
00123 }
00124 catch (tools::StringException e)
00125 {
00126 ui->showError(e.message());
00127 return;
00128 }
00129
00130 QMap<QString, domain::Project*> projectsMap;
00131
00132 Q_FOREACH (domain::Project* project, data->projects())
00133 projectsMap[project->description()] = project;
00134
00135 if (!projectsMap.isEmpty())
00136 {
00137 data::StringInputData input = ui->getChoice(projectsMap.keys(),
00138 "Choose a project");
00139
00140 if (input.processData)
00141 {
00142 try
00143 {
00144 task->setProject(projectsMap[input]);
00145 }
00146 catch (tools::StringException& e)
00147 {
00148 ui->showError(e.message());
00149 }
00150 }
00151 }
00152
00153 TaskModel newTasks(data, taskList << task);
00154 ui->showModel(&newTasks, QString("Tasks of user %1").arg(user()->name()));
00155 }
00156
00157 QString TaskCreator::name() const
00158 {
00159 return "TaskCreator";
00160 }
00161
00162 Q_EXPORT_PLUGIN2(controller_taskcreator, TaskCreator)