* @brief Updates the metadata file containing the categories, subcategories and datasets. * @param fileName the name of the metadata file (path) */
| 334 | * @param fileName the name of the metadata file (path) |
| 335 | */ |
| 336 | void DatasetMetadataManagerWidget::updateDocument(const QString& dirPath) { |
| 337 | if (checkDataValidity()) { |
| 338 | // Check whether the current collection already exists, if yes update it |
| 339 | if (m_datasetModel->collections().contains(ui.cbCollection->currentText())) { |
| 340 | QString fileName = dirPath + ui.cbCollection->currentText() + QStringLiteral(".json"); |
| 341 | qDebug() << "Updating: " << fileName; |
| 342 | QFile file(fileName); |
| 343 | if (file.open(QIODevice::ReadWrite)) { |
| 344 | QJsonDocument document = QJsonDocument::fromJson(file.readAll()); |
| 345 | QJsonObject rootObject = document.object(); |
| 346 | QJsonValueRef categoryArrayRef = rootObject.find(QStringLiteral("categories")).value(); |
| 347 | QJsonArray categoryArray = categoryArrayRef.toArray(); |
| 348 | |
| 349 | // Check whether the current category already exists |
| 350 | bool foundCategory = false; |
| 351 | for (int i = 0; i < categoryArray.size(); ++i) { |
| 352 | QJsonValueRef categoryRef = categoryArray[i]; |
| 353 | QJsonObject currentCategory = categoryRef.toObject(); |
| 354 | QString categoryName = currentCategory.value(QStringLiteral("category_name")).toString(); |
| 355 | |
| 356 | // If we find the category we have to update that QJsonObject |
| 357 | if (categoryName.compare(ui.cbCategory->currentText()) == 0) { |
| 358 | foundCategory = true; |
| 359 | QJsonValueRef subcategoryArrayRef = currentCategory.find(QStringLiteral("subcategories")).value(); |
| 360 | QJsonArray subcategoryArray = subcategoryArrayRef.toArray(); |
| 361 | |
| 362 | // Check whether the current subcategory already exists |
| 363 | bool subcategoryFound = false; |
| 364 | for (int j = 0; j < subcategoryArray.size(); ++j) { |
| 365 | QJsonValueRef subcategoryRef = subcategoryArray[j]; |
| 366 | QJsonObject currentSubcategory = subcategoryRef.toObject(); |
| 367 | QString subcategoryName = currentSubcategory.value(QStringLiteral("subcategory_name")).toString(); |
| 368 | |
| 369 | // If we find the subcategory we have to update that QJsonObject |
| 370 | if (subcategoryName.compare(ui.cbSubcategory->currentText()) == 0) { |
| 371 | subcategoryFound = true; |
| 372 | QJsonValueRef datasetsRef = currentSubcategory.find(QStringLiteral("datasets")).value(); |
| 373 | QJsonArray datasets = datasetsRef.toArray(); |
| 374 | |
| 375 | datasets.append(createDatasetObject()); |
| 376 | datasetsRef = datasets; |
| 377 | |
| 378 | subcategoryRef = currentSubcategory; |
| 379 | subcategoryArrayRef = subcategoryArray; |
| 380 | categoryRef = currentCategory; |
| 381 | categoryArrayRef = categoryArray; |
| 382 | document.setObject(rootObject); |
| 383 | break; |
| 384 | } |
| 385 | } |
| 386 | |
| 387 | // If we didn't find the subcategory, we have to create it |
| 388 | if (!subcategoryFound) { |
| 389 | qDebug() << "Subcategory not found"; |
| 390 | QJsonObject newSubcategory; |
| 391 | newSubcategory.insert(QStringLiteral("subcategory_name"), ui.cbSubcategory->currentText()); |
| 392 | |
| 393 | QJsonArray datasets; |