00001 #include "datasaver.h"
00002
00003 #include <domain/project.h>
00004 #include <domain/taskmanagerdata.h>
00005 #include <domain/user.h>
00006 #include <domain/schedule.h>
00007 #include <domain/taskstates.h>
00008 #include <domain/reservation.h>
00009
00010 #include <QDomDocument>
00011 #include <QDebug>
00012 #include <QFile>
00013
00014 namespace control
00015 {
00016
00017 struct DataSaverPrivate
00018 {
00019 DataSaverPrivate(domain::TaskManagerData* d) : data(d) {}
00020
00021 void createIds();
00022 QDomElement writeProjects(QDomDocument doc);
00023 QDomElement writeResources(QDomDocument doc);
00024 QDomElement writeUser(domain::User* user, QDomDocument doc);
00025 QDomElement writeTasks(domain::User* user, QDomDocument doc);
00026 QDomElement writeReservations(domain::User* user, QDomDocument doc);
00027
00028 QDomElement createTextElement(const QString& name, const QString& value,
00029 QDomDocument& doc);
00030
00031 domain::TaskManagerData* data;
00032 QMap<const domain::StorableData*, QString> idMap;
00033 };
00034
00035 }
00036
00037 using namespace control;
00038
00039 ControllerInterface::ActionType DataSaver::actionType() const
00040 {
00041 return SaveAction;
00042 }
00043
00044 ControllerInterface::DataType DataSaver::dataType() const
00045 {
00046 return FileData;
00047 }
00048
00049 QString DataSaver::name() const
00050 {
00051 return "DataSaver";
00052 }
00053
00054 QString DataSaver::description() const
00055 {
00056 return "Save data to file";
00057 }
00058
00059 void DataSaver::init(domain::TaskManagerData* data, ui::UiInterface* ui,
00060 ControllerFactory* factory)
00061 {
00062 control::ControllerInterface::init(data, ui, factory);
00063
00064 d = new DataSaverPrivate(data);
00065 }
00066
00067 bool DataSaver::userIsValid() const
00068 {
00069 return true;
00070 }
00071
00072 void DataSaver::execute()
00073 {
00074 QString fileName = ui->getSaveFileName("", "XML files(*.xml)");
00075 QFile file(fileName);
00076
00077 if (!file.open(QFile::WriteOnly))
00078 {
00079 ui->showError("Failed to open file " + fileName);
00080 return;
00081 }
00082
00083 QDomDocument doc("TaskPlanner");
00084 QDomElement root = doc.createElement("mop:taskplanner");
00085 doc.appendChild(root);
00086
00087 d->createIds();
00088 root.appendChild(d->writeProjects(doc));
00089 root.appendChild(d->writeResources(doc));
00090
00091 Q_FOREACH (domain::User* user, data->users())
00092 root.appendChild(d->writeUser(user, doc));
00093
00094 qDebug() << doc.toString(4);
00095
00096 QTextStream stream(&file);
00097 doc.save(stream, 4);
00098 file.close();
00099
00100 ui->showMessage("Data successfully saved to " + fileName);
00101 }
00102
00103 void DataSaverPrivate::createIds()
00104 {
00105 idMap.clear();
00106
00107
00108 int count = 0;
00109 Q_FOREACH (domain::Project* project, data->projects())
00110 idMap[project] = QString("project%1").arg(count++);
00111
00112
00113 count = 0;
00114 Q_FOREACH (domain::Resource* resource, data->resources())
00115 idMap[resource] = QString("resource%1").arg(count++);
00116
00117
00118 count = 0;
00119 Q_FOREACH (domain::Task* task, data->tasks())
00120 idMap[task] = QString("task%1").arg(count++);
00121 }
00122
00123 QDomElement DataSaverPrivate::writeProjects(QDomDocument doc)
00124 {
00125 QDomElement projects = doc.createElement("mop:projects");
00126
00127 Q_FOREACH (domain::Project* project, data->projects())
00128 {
00129 Q_ASSERT(idMap.contains(project));
00130
00131 QDomElement projectElement = doc.createElement("mop:project");
00132 projects.appendChild(projectElement);
00133
00134 projectElement.setAttribute("id", idMap[project]);
00135 projectElement.appendChild(createTextElement("mop:description",
00136 project->description(),
00137 doc));
00138 }
00139
00140 return projects;
00141 }
00142
00143 QDomElement DataSaverPrivate::writeResources(QDomDocument doc)
00144 {
00145 QDomElement resources = doc.createElement("mop:resources");
00146
00147 Q_FOREACH (domain::Resource* resource, data->resources())
00148 {
00149 QDomElement resourceElement = doc.createElement("mop:resource");
00150 resources.appendChild(resourceElement);
00151
00152 resourceElement.setAttribute("id", idMap[resource]);
00153 resourceElement.appendChild(createTextElement("mop:description",
00154 resource->description(),
00155 doc));
00156 resourceElement.appendChild(createTextElement("mop:type",
00157 resource->resourceTypeName(),
00158 doc));
00159 }
00160
00161 return resources;
00162 }
00163
00164 QDomElement DataSaverPrivate::writeUser(domain::User* user, QDomDocument doc)
00165 {
00166 QDomElement userElement = doc.createElement("mop:user");
00167 userElement.appendChild(createTextElement("mop:name", user->name(), doc));
00168 userElement.appendChild(writeTasks(user, doc));
00169 userElement.appendChild(writeReservations(user, doc));
00170 return userElement;
00171 }
00172
00173 QDomElement DataSaverPrivate::writeTasks(domain::User* user, QDomDocument doc)
00174 {
00175 QDomElement tasks = doc.createElement("mop:tasks");
00176
00177 Q_FOREACH (const domain::Task* task, user->tasks())
00178 {
00179 QDomElement taskElement = doc.createElement("mop:task");
00180 tasks.appendChild(taskElement);
00181
00182 taskElement.setAttribute("id", idMap[task]);
00183
00184 taskElement.appendChild(createTextElement(
00185 "mop:startDate",
00186 task->schedule().startDate().toString(Qt::ISODate),
00187 doc));
00188 taskElement.appendChild(createTextElement(
00189 "mop:endDate",
00190 task->schedule().dueDate().toString(Qt::ISODate),
00191 doc));
00192
00193 taskElement.appendChild(createTextElement(
00194 "mop:duration",
00195 QString::number(task->schedule().duration().totalSeconds() / 60),
00196 doc));
00197
00198 taskElement.appendChild(createTextElement("mop:status",
00199 domain::TaskState::nameForEnum(task->state()), doc));
00200
00201 if (task->project() != 0)
00202 {
00203 taskElement.appendChild(createTextElement("mop:refProject",
00204 idMap[task->project()],
00205 doc));
00206 }
00207
00208 QDomElement subTasks = doc.createElement("mop:dependsOn");
00209 taskElement.appendChild(subTasks);
00210
00211 Q_FOREACH (const domain::Task* subTask, task->subTasks())
00212 {
00213 subTasks.appendChild(createTextElement("mop:refTask",
00214 idMap[subTask],
00215 doc));
00216 }
00217 }
00218
00219 return tasks;
00220 }
00221
00222 QDomElement DataSaverPrivate::writeReservations(domain::User*, QDomDocument)
00223 {
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249 return QDomElement();
00250 }
00251
00252 QDomElement DataSaverPrivate::createTextElement(const QString& name,
00253 const QString& value,
00254 QDomDocument& doc)
00255 {
00256 QDomElement text = doc.createElement(name);
00257 text.appendChild(doc.createTextNode(value));
00258 return text;
00259 }
00260
00261 Q_EXPORT_PLUGIN2(controller_datasaver, DataSaver)
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285