00001 #include <QDomElement>
00002 #include <QDebug>
00003 #include <QtAlgorithms>
00004 #include "dataexercise.h"
00005 #include "datahash.h"
00006
00007 bool DataExercise::loadDataNode(const QDomNode data)
00008 {
00009 exerciseName = data.toElement().attribute("name");
00010 exerciseId = data.toElement().attribute("exerciseId").toUInt();
00011 exerciseStatus = NotCompleted;
00012
00013
00014
00015
00016 QDomNode node = data.firstChild();
00017 while(!node.isNull())
00018 {
00019 if(node.nodeName() == "description")
00020 {
00021 m_description = node.firstChild().nodeValue().simplified();
00022 }
00023 else if(node.nodeName() == "records")
00024 {
00025 if(!loadRecords(node, &accountingRecordList))
00026 {
00027 qDebug()<<"loading records failed";
00028 return false;
00029 }
00030
00031 }
00032 else if(node.nodeName() == "solution")
00033 {
00034 if(!loadRecords(node, &solutionAccountingRecordList))
00035 {
00036 qDebug()<<"loading solution failed";
00037 return false;
00038 }
00039 }
00040 else if(node.nodeName() == "accounts")
00041 {
00042 QDomNode nodeAcc = node.firstChild();
00043 while(!nodeAcc.isNull())
00044 {
00045 if(nodeAcc.nodeName() == "account")
00046 {
00047 DataAccount *acc = new DataAccount();
00048 if(!acc->loadDataNode(nodeAcc))
00049 {
00050 qDebug()<<"loading account failed";
00051 delete acc;
00052 return false;
00053 }
00054 connect(acc, SIGNAL(changed()), this, SLOT(dataChanged()));
00055 accountList.append(acc);
00056 }
00057 else if(nodeAcc.nodeName() != "#comment")
00058 {
00059 qDebug() << "Unknown element in accounts tag:" << nodeAcc.nodeName();
00060 }
00061 nodeAcc = nodeAcc.nextSibling();
00062 }
00063 }
00064 else if(node.nodeName() == "exerciseStatus")
00065 {
00066 QString str = node.firstChild().nodeValue().simplified();
00067 if(str == "Correct")
00068 exerciseStatus = Correct;
00069 else if(str == "Wrong")
00070 exerciseStatus = Wrong;
00071 else if(str == "NotCompleted")
00072 exerciseStatus = NotCompleted;
00073 else
00074 {
00075 exerciseStatus = NotCompleted;
00076 qDebug() << "wrong value in exerciseStatus: " << str;
00077 }
00078 }
00079 else if(node.nodeName() != "#comment")
00080 {
00081 qDebug() << "Unknown element in exercise tag:" << node.nodeName();
00082 }
00083 node = node.nextSibling();
00084 }
00085 return true;
00086 }
00087
00088 bool DataExercise::loadRecords(const QDomNode data, QList<DataAccountingRecord *> *targetList)
00089 {
00090 QDomNode node = data.firstChild();
00091 while(!node.isNull())
00092 {
00093 if(node.nodeName() == "record")
00094 {
00095 DataAccountingRecord *dar = new DataAccountingRecord();
00096 if(!dar->loadDataNode(node))
00097 {
00098 qDebug()<<"loading datanode accounting record failed";
00099 delete dar;
00100 return false;
00101 }
00102 if(!dar->isEmpty())
00103 {
00104
00105 targetList->append(dar);
00106 connect(dar, SIGNAL(changed()), this, SLOT(dataChanged()));
00107 }
00108 else
00109 {
00110
00111 delete dar;
00112 }
00113 }
00114 else if(node.nodeName() != "#comment")
00115 {
00116 qDebug() << "Unknown element in records:" << node.nodeName();
00117 }
00118 node = node.nextSibling();
00119 }
00120 return true;
00121 }
00122
00123 QString DataExercise::statusToString(DataExerciseStatus status)
00124 {
00125 switch(status)
00126 {
00127 case Correct: return "Correct";
00128 case Wrong: return "Wrong";
00129 case NotCompleted: return "NotCompleted";
00130 default:
00131 qDebug() << "DataExercise: can't convert DataExerciseStatus to string, please add to the switch statement";
00132 }
00133 return "NotCompleted";
00134 }
00135
00136 QDomElement DataExercise::saveDataNode(QDomDocument *xml)
00137 {
00138 QDomElement exercise = xml->createElement("exercise");
00139 exercise.setAttribute("name", exerciseName);
00140 exercise.setAttribute("exerciseId", QString::number(exerciseId));
00141
00142 QDomElement eDescription = xml->createElement("description");
00143 eDescription.appendChild(xml->createTextNode(m_description));
00144 exercise.appendChild(eDescription);
00145
00146 QDomElement eStatus = xml->createElement("exerciseStatus");
00147 eStatus.appendChild(xml->createTextNode(statusToString(exerciseStatus)));
00148 exercise.appendChild(eStatus);
00149
00150 QDomElement records = xml->createElement("records");
00151 saveRecords(xml, &records, &accountingRecordList);
00152 exercise.appendChild(records);
00153
00154 QDomElement solution = xml->createElement("solution");
00155 saveRecords(xml, &solution, &solutionAccountingRecordList);
00156 exercise.appendChild(solution);
00157
00158 QDomElement eAccounts = xml->createElement("accounts");
00159 for(int i = 0; i < accountList.size(); i++)
00160 {
00161 eAccounts.appendChild(accountList[i]->saveDataNode(xml));
00162 }
00163 exercise.appendChild(eAccounts);
00164
00165 return exercise;
00166 }
00167
00168 void DataExercise::saveRecords(QDomDocument *xml, QDomElement *parent, QList<DataAccountingRecord *> *records)
00169 {
00170 qDebug() << "saveRecords(): list size:" << records->size();
00171 for(int i = 0; i < records->size(); i++)
00172 {
00173 parent->appendChild((*records)[i]->saveDataNode(xml));
00174 }
00175 }
00176
00177 void DataExercise::hash(unsigned int &startValue)
00178 {
00179
00180
00181 DataHash::hash(exerciseName, startValue);
00182 DataHash::hash(&exerciseId, sizeof(exerciseId), startValue);
00183 DataHash::hash(m_description, startValue);
00184 DataHash::hash(&exerciseStatus, sizeof(exerciseStatus), startValue);
00185
00186 for(int i = 0; i < accountingRecordList.size(); i++)
00187 {
00188 accountingRecordList[i]->hash(startValue);
00189 }
00190 for(int i = 0; i < solutionAccountingRecordList.size(); i++)
00191 {
00192 solutionAccountingRecordList[i]->hash(startValue);
00193 }
00194 for(int i = 0; i < accountList.size(); i++)
00195 {
00196 accountList[i]->hash(startValue);
00197 }
00198 }
00199
00200 DataExercise::DataExercise()
00201 {
00202 }
00203
00204 DataExercise::DataExercise(DataExercise &exercise)
00205 {
00206 this->exerciseName = exercise.name();
00207 this->exerciseId = exercise.id();
00208 this->exerciseStatus = exercise.status();
00209 this->m_description = exercise.description();
00210 this->accountingRecordList = QList<DataAccountingRecord *> (*(exercise.accountingRecords()));
00211 this->solutionAccountingRecordList = QList<DataAccountingRecord *> (*(exercise.solutionRecords()));
00212 this->accountList = QList<DataAccount *> (*(exercise.accounts()));
00213 }
00214
00215 DataExercise::~DataExercise()
00216 {
00217 qDeleteAll(accountingRecordList);
00218 accountingRecordList.clear();
00219
00220 qDeleteAll(solutionAccountingRecordList);
00221 solutionAccountingRecordList.clear();
00222
00223 qDeleteAll(accountList);
00224 accountList.clear();
00225 }