00001 #include "taskupdater.h"
00002
00003 #include <controllerfactory.h>
00004 #include <controldata.h>
00005 #include <models/taskmodel.h>
00006 #include <models/resourcemodel.h>
00007 #include <models/userrequirementsmodel.h>
00008 #include <models/resourcerequirementsmodel.h>
00009
00010 #include <domain/task.h>
00011 #include <domain/taskmanagerdata.h>
00012 #include <domain/taskstates.h>
00013 #include <domain/businessrule2exception.h>
00014 #include <domain/statechangedata.h>
00015
00016 #include <tools/invaliddataexception.h>
00017 #include <tools/functions.h>
00018
00019 #include <QDebug>
00020
00021 using namespace control;
00022
00023 QString TaskUpdater::description() const
00024 {
00025 return "Update task";
00026 }
00027
00028 ControllerInterface::ActionType TaskUpdater::actionType() const
00029 {
00030 return UpdateAction;
00031 }
00032
00033 ControllerInterface::DataType TaskUpdater::dataType() const
00034 {
00035 return TaskData;
00036 }
00037
00038 QString TaskUpdater::name() const
00039 {
00040 return "TaskUpdater";
00041 }
00042
00043 bool TaskUpdater::userIsValid() const
00044 {
00045 return userHasLoggedIn() && !userIsAdmin();
00046 }
00047
00048 void TaskUpdater::execute()
00049 {
00050 TaskModel tasks(data, tools::makeConstList(data->tasksForUser(user())));
00051 data::SelectionData selection = ui->getSelection("Select a task", &tasks);
00052
00053 if (!selection.processData)
00054 return;
00055
00056 QModelIndexList list = selection.selection;
00057
00058 if (list.isEmpty())
00059 {
00060 ui->showWarning("No task selected.");
00061 return;
00062 }
00063
00064 Q_ASSERT(list.size() == 1);
00065
00066 const domain::Task* task = tasks.taskForIndex(list.first());
00067
00068 try
00069 {
00070 execute(task);
00071 }
00072 catch (tools::StringException e)
00073 {
00074 ui->showError(e.message());
00075 }
00076 }
00077
00078 void TaskUpdater::execute(const domain::StorableData* data)
00079 {
00080
00081 domain::Task* task = this->data->task(qobject_cast<const domain::Task*>(data));
00082
00083 if (task == 0)
00084 throw tools::InvalidDataException("Given data is not a task.");
00085
00086
00087
00088 control::data::TaskDetails taskDetailsToShow(task);
00089 QList<domain::Task*> subtasksToShow;
00090
00091 Q_FOREACH (const domain::Task* taskPointer, task->subTasks())
00092 subtasksToShow << this->data->task(taskPointer);
00093
00094 TaskModel subtasksModel(this->data, tools::makeConstList(subtasksToShow));
00095 data::TaskRepresentation rep;
00096 rep.subTasks = &subtasksModel;
00097 rep.type = data::TaskTypeDetails(task->taskType());
00098 rep.details = taskDetailsToShow;
00099 UserRequirementsModel userReqs(task);
00100 rep.userReqs = &userReqs;
00101 ResourceRequirementsModel resourceReqs(task);
00102 rep.resourceReqs = &resourceReqs;
00103
00104 if (!ui->showTaskDetails(rep).processData)
00105 return;
00106
00107
00108 QStringList listStates;
00109 Q_FOREACH (domain::TaskState::SettableState state, domain::TaskState::allStates())
00110 {
00111 listStates << domain::TaskState::nameForEnum(state);
00112 }
00113
00114 QString selectedString = ui->getChoice(listStates,
00115 "Select the state to which you want to set the task");
00116
00117 if (selectedString.isEmpty())
00118 {
00119 ui->showError("No state selected.");
00120 return;
00121 }
00122
00123 domain::TaskState::SettableState selectedState;
00124 Q_FOREACH (domain::TaskState::SettableState state, domain::TaskState::allStates())
00125 {
00126 if (domain::TaskState::nameForEnum(state) == selectedString)
00127 selectedState = state;
00128 }
00129
00130
00131 domain::StateChangeData changeData = task->canHaveAsState(selectedState);
00132 QList<const domain::Task*> badTasks = changeData.violatingTasks();
00133 QString message = changeData.message();
00134 TaskModel model(this->data, badTasks);
00135
00136 if (!changeData.isChangePossible())
00137 {
00138 if (badTasks.isEmpty())
00139 ui->showError(message);
00140 else
00141 ui->showModel(&model, message);
00142 }
00143 else
00144 {
00145 if (!changeData.areMoreChangesNeeded() || ui->askQuestion(message, &model,
00146 "Would you like to proceed?"))
00147 {
00148 task->setState(selectedState);
00149 }
00150 }
00151 }
00152
00153 Q_EXPORT_PLUGIN2(controller_taskupdater, TaskUpdater)