00001 #include "resource.h"
00002
00003 #include "reservation.h"
00004 #include "resourcetype.h"
00005
00006 #include <tools/functions.h>
00007 #include <tools/invaliddataexception.h>
00008
00009 #include <QString>
00010 #include <QList>
00011 #include <QDateTime>
00012
00013 namespace domain
00014 {
00015
00016 class Task;
00017 class Reservation;
00018
00019 struct ResourcePrivate
00020 {
00021 QString description;
00022 QList<Reservation*> reservations;
00023 ResourceType* type;
00024 };
00025
00026 Resource::Resource(ResourceType* type, const QString& description) : d(new ResourcePrivate())
00027 {
00028 if (description.isEmpty())
00029 throw tools::InvalidDataException("An empty description is not allowed");
00030
00031 d->type = type;
00032 d->description = description;
00033 }
00034
00035 Resource::Resource(const Resource& other) : d(new ResourcePrivate())
00036 {
00037
00038 d->description = other.description();
00039 d->type = other.resourceType();
00040 }
00041
00042 Resource::~Resource()
00043 {
00044 deleteDependentData();
00045
00046 delete d;
00047 }
00048
00049 const QString& Resource::description() const
00050 {
00051 return d->description;
00052 }
00053
00054 void Resource::addReservation(Reservation* reservation)
00055 {
00056 Q_ASSERT(reservation->resource() == this);
00057
00058 d->reservations << reservation;
00059 emit dataChanged();
00060 }
00061
00062 void Resource::removeReservation(Reservation* reservation)
00063 {
00064 d->reservations.removeAll(reservation);
00065 emit dataChanged();
00066 }
00067
00068 QList<const Reservation*> Resource::reservations() const
00069 {
00070 return tools::makeConstList(d->reservations);
00071 }
00072
00073 QList<StorableData*> Resource::dependentData() const
00074 {
00075 QList<StorableData*> ret;
00076
00077 Q_FOREACH (Reservation* reservation, d->reservations)
00078 ret << reservation;
00079
00080 return ret;
00081 }
00082
00083 ResourceType* Resource::resourceType() const
00084 {
00085 return d->type;
00086 }
00087
00088 QString Resource::resourceTypeName() const
00089 {
00090 return d->type->name();
00091 }
00092
00093 bool Resource::alreadyReserved(QDateTime startTime, Duration duration) const
00094 {
00095 QDateTime endTime = startTime.addSecs(duration.totalSeconds());
00096 QList<const Reservation*> reservationsOfResource = reservations();
00097 Q_FOREACH (const Reservation* reservation, reservationsOfResource)
00098 {
00099 QDateTime reservationStartTime(reservation->time());
00100 QDateTime reservationEndTime = reservation->time().addSecs(
00101 reservation->duration().totalSeconds());
00102 if (startTime >= reservationStartTime &&
00103 startTime < reservationEndTime)
00104 return true;
00105 if (endTime > reservationStartTime &&
00106 endTime <= reservationEndTime)
00107 return true;
00108 if (startTime <= reservationStartTime &&
00109 endTime >= reservationEndTime)
00110 return true;
00111 }
00112 return false;
00113 }
00114
00115
00116
00117 }