00001 #include "reservation.h" 00002 00003 #include "resource.h" 00004 #include "task.h" 00005 #include "clock.h" 00006 00007 #include "tools/invaliddataexception.h" 00008 00009 namespace domain 00010 { 00011 00012 struct ReservationPrivate 00013 { 00014 Task* task; 00015 Resource* resource; 00016 QDateTime time; 00017 Duration duration; 00018 }; 00019 00020 Reservation::Reservation(Task* task, Resource* resource, const QDateTime& time, 00021 const Duration& duration) : d(new ReservationPrivate()) 00022 { 00023 Q_ASSERT(task != 0); 00024 Q_ASSERT(resource != 0); 00025 00026 if (resource->alreadyReserved(time, duration)) 00027 { 00028 throw tools::InvalidDataException("The given resource is already " 00029 "reserved for the specified period."); 00030 } 00031 00032 d->task = task; 00033 d->resource = resource; 00034 d->time = time; 00035 d->duration = duration; 00036 00037 try 00038 { 00039 resource->addReservation(this); 00040 task->addReservation(this); 00041 } 00042 catch (tools::StringException& e) 00043 { 00044 resource->removeReservation(this); 00045 task->removeReservation(this); 00046 throw e; 00047 } 00048 } 00049 00050 Reservation::~Reservation() 00051 { 00052 d->resource->removeReservation(this); 00053 d->task->removeReservation(this); 00054 delete d; 00055 } 00056 00057 const Task* Reservation::task() const 00058 { 00059 return d->task; 00060 } 00061 00062 const Resource* Reservation::resource() const 00063 { 00064 return d->resource; 00065 } 00066 00067 const QDateTime& Reservation::time() const 00068 { 00069 return d->time; 00070 } 00071 00072 const domain::Duration& Reservation::duration() const 00073 { 00074 return d->duration; 00075 } 00076 00077 QDateTime Reservation::endTime() const 00078 { 00079 return d->time + d->duration; 00080 } 00081 00082 00083 bool Reservation::overlapsWith(const domain::Reservation* res, 00084 const Duration& duration) const 00085 { 00086 QDateTime lastStart, firstEnd; 00087 00088 if (time() > res->time()) 00089 lastStart = time(); 00090 else 00091 lastStart = res->time(); 00092 00093 if (endTime() < res->endTime()) 00094 firstEnd = endTime(); 00095 else 00096 firstEnd = res->endTime(); 00097 00098 return lastStart.secsTo(firstEnd) >= duration.totalSeconds(); 00099 } 00100 00101 bool Reservation::isActive() const 00102 { 00103 QDateTime currentTime = Clock::currentTime(); 00104 00105 return currentTime >= time() && currentTime <= endTime(); 00106 } 00107 00108 }