00001 #include "resourcereservationmaker.h" 00002 00003 #include <controldata.h> 00004 #include <models/resourcemodel.h> 00005 #include <models/resourcereservationmodel.h> 00006 #include <models/taskmodel.h> 00007 00008 #include <domain/taskmanagerdata.h> 00009 #include <domain/user.h> 00010 #include <domain/resource.h> 00011 #include <domain/reservation.h> 00012 00013 #include <tools/functions.h> 00014 #include <tools/stringexception.h> 00015 00016 using namespace control; 00017 00018 ControllerInterface::ActionType ResourceReservationMaker::actionType() const 00019 { 00020 return ControllerInterface::CreateAction; 00021 } 00022 00023 ControllerInterface::DataType ResourceReservationMaker::dataType() const 00024 { 00025 return ControllerInterface::ReservationData; 00026 } 00027 00028 QString ResourceReservationMaker::description() const 00029 { 00030 return "Make resource reservation"; 00031 } 00032 00033 QString ResourceReservationMaker::name() const 00034 { 00035 return "ResourceReservationMaker"; 00036 } 00037 00038 bool ResourceReservationMaker::userIsValid() const 00039 { 00040 return userHasLoggedIn() && !userIsAdmin(); 00041 } 00042 00043 void ResourceReservationMaker::execute() 00044 { 00045 QList<domain::Task*> tasksToShow; 00046 00047 Q_FOREACH (domain::Task* task, data->tasksForUser(user())) 00048 { 00049 if (task->state() == domain::TaskState::Unfinished) 00050 tasksToShow << task; 00051 } 00052 00053 if (tasksToShow.isEmpty()) 00054 { 00055 ui->showWarning(QString("User %1 has no unfinished tasks").arg(user()->name())); 00056 return; 00057 } 00058 TaskModel tasks(data, tools::makeConstList(tasksToShow)); 00059 00060 data::SelectionData taskInput = ui->getSelection("Select a task tp make a" 00061 "reservation for", 00062 &tasks); 00063 00064 if (!taskInput.processData) 00065 return; 00066 00067 if (taskInput.selection.size() < 1) 00068 { 00069 ui->showWarning("No task selected"); 00070 return; 00071 } 00072 else 00073 Q_ASSERT(taskInput.selection.size() == 1); 00074 00075 ResourceModel resources(data); 00076 data::SelectionData resourceInput = ui->getSelection("Select a resource " 00077 "to reserve", &resources); 00078 00079 if (!resourceInput.processData) 00080 return; 00081 00082 if (resourceInput.selection.size() < 1) 00083 { 00084 ui->showWarning("No resource selected"); 00085 return; 00086 } 00087 else 00088 Q_ASSERT(resourceInput.selection.size() == 1); 00089 00090 domain::Task* task = data->task(tasks.taskForIndex(taskInput.selection.first())); 00091 domain::Resource* resource = data->resource(resources.resourceForIndex( 00092 resourceInput.selection.first())); 00093 00094 ResourceReservationModel reservations(resource); 00095 00096 try 00097 { 00098 data::ReservationDetails input = ui->getReservationDetails(&reservations); 00099 00100 if (!input.processData) 00101 return; 00102 00103 data->createReservation(task, resource, input.time, input.duration); 00104 } 00105 catch (tools::StringException& e) 00106 { 00107 ui->showError(e.message()); 00108 } 00109 } 00110 00111 Q_EXPORT_PLUGIN2(controller_resourcereservationmaker, ResourceReservationMaker)