00001 #include "schedule.h"
00002 
00003 #include "tools/invaliddataexception.h"
00004 
00005 namespace domain
00006 {
00007 
00008 struct SchedulePrivate : public QSharedData
00009 {
00010     QDateTime startDate;
00011     QDateTime dueDate;
00012     Duration duration;
00013 };
00014 
00015 Schedule::Schedule(const Duration& duration, const QDateTime& startDate,
00016                    const QDateTime& dueDate) : d(new SchedulePrivate())
00017 {
00018     if (isValid(duration, startDate, dueDate))
00019     {
00020         d->duration = duration;
00021         d->startDate = startDate;
00022         d->dueDate = dueDate;
00023     }
00024     else
00025         throw tools::InvalidDataException("Invalid schedule given");
00026 }
00027 
00028 Schedule::Schedule(const Schedule& other) : d(other.d)
00029 {
00030 }
00031 
00032 Schedule::~Schedule()
00033 {
00034 }
00035 
00036 Schedule& Schedule::operator=(const Schedule& other)
00037 {
00038     if (this != &other)
00039         d = other.d;
00040 
00041     return *this;
00042 }
00043 
00044 bool Schedule::operator==(const Schedule& other) const
00045 {
00046     return startDate() == other.startDate() &&
00047            dueDate() == other.dueDate() &&
00048            duration() == other.duration();
00049 }
00050 
00051 const QDateTime& Schedule::dueDate() const
00052 {
00053     return d->dueDate;
00054 }
00055 
00056 void Schedule::setDueDate(const QDateTime& dueDate)
00057 {
00058     if (isValid(duration(), startDate(), dueDate))
00059     {
00060         d->dueDate = dueDate;
00061     }
00062     else
00063         throw tools::InvalidDataException("Invalid dueDate");
00064 }
00065 
00066 const domain::Duration& Schedule::duration() const
00067 {
00068     return d->duration;
00069 }
00070 
00071 void Schedule::setDuration(const domain::Duration& duration)
00072 {
00073     if (isValid(duration, startDate(), dueDate()))
00074     {
00075         d->duration = duration;
00076     }
00077     else
00078         throw tools::InvalidDataException("Invalid duration");
00079 }
00080 
00081 const QDateTime& Schedule::startDate() const
00082 {
00083     return d->startDate;
00084 }
00085 
00086 void Schedule::setStartDate(const QDateTime& startDate)
00087 {
00088     if (isValid(duration(), startDate, dueDate()))
00089     {
00090         d->startDate = startDate;
00091     }
00092     else
00093         throw tools::InvalidDataException("Invalid startDate");
00094 }
00095 
00096 bool Schedule::isValid(const Duration& duration, const QDateTime& startDate,
00097                        const QDateTime& dueDate)
00098 {
00099     if (startDate == dueDate)
00100         return false;
00101 
00102     return startDate + duration <= dueDate;
00103 }
00104 
00105 }