/** * @RDE_HEADER@ * Importieren von MSDL-Dateien * $Id$ * $HeadURL$ * $Author$ */ // at first to avoid compiler errors on windows (CONST as enum value) #include #include #include "msdl_import.h" #include "ui_msdl_import.h" #include "msdl_logging.h" #include "msdl_import_show.h" #include "msdl_mapping_dialog.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace msdl { static QString gStrMSDLWorkingFolderPath() { return QDir::tempPath() + QLatin1String("/excon_msdl/"); } static QString gStrMSDLWorkingFile() { return gStrMSDLWorkingFolderPath() + QStringLiteral("msdl_user_input.xml"); } using descriptionType = schemas::modelID::modelIdentificationType::description_type; using organizationsType = msdl_1::MilitaryScenarioType::Organizations_type; using equipType = organizationsType::Equipment_type; using equipItemType = equipType::EquipmentItem_type; using equipOptional = organizationsType::Equipment_optional; using equipRelationsType = equipItemType::Relations_type; using unitsType = organizationsType::Units_type; using unitType = unitsType::Unit_type; using relationsType = unitType::Relations_type; using forceRelationDataType = relationsType::ForceRelation_type::ForceRelationData_type; using forceSidesType = msdl_1::MilitaryScenarioType::ForceSides_type; using forceSideType = forceSidesType::ForceSide_type; using ownerChoiceType = equipRelationsType::HoldingOrganization_type::OwnerChoice_type; using ownerDataType = equipRelationsType::HoldingOrganization_type::OwnerData_type; using purposeOptional = schemas::modelID::modelIdentificationType::purpose_optional; ImportMsdl::ImportMsdl(ParticipantManagement::ParticipantEditModel *pModel, const QModelIndex &index, QWidget *parent, LtsSession::LtsSession *pSession) : NGLDialog(parent) , m_indexRoot(index) , m_pModel(pModel) , m_requestHelper(new LtsUtils::RequestHelper(this)) , m_pMsdlImportShow(nullptr) , m_pUi(new Ui_MSDLImport) , mLogger(msdl::getLogger("ImportMsdl")) { m_pUi->setupUi(this); setWindowTitle(tr("Import Exercise Scenario")); if (!pSession->getDtc(m_ppmDtc) || !pSession->getFtc(m_ppmFtc)) { ERROR_LOG(mLogger) << "Fetching m_ppmDtc or m_ppmFtc failed!"; setEnabled(false); } else { m_mapTypes = m_ppmDtc->types(); /* * Store all DIS ids of each type in 'm_mapDisStr2TypeId' to allow a mapping between * DIS ids and types when importing units. * * Note: A type may have more than one DIS id. * Note: Each type should have an assigned DIS id! Otherwise it will not be available for mapping! */ for (const ParticipantManagement::Type &type : qAsConst(m_mapTypes)) { for (const auto &disId : type.disEntities()) { QString strMSDL_DISId = ParticipantManagement::DIS::convertDISTypeInteger2MSDLString(disId); if (m_mapMSDL_DIS_2TypeId.find(strMSDL_DISId) != m_mapMSDL_DIS_2TypeId.end()) { WARNING_LOG(mLogger) << "DIS id \"" << strMSDL_DISId << "\" which is assigned to type id \"" << type.id() << "\" is already assigned to type id \"" << m_mapMSDL_DIS_2TypeId[strMSDL_DISId] << "\""; continue; } m_mapMSDL_DIS_2TypeId[strMSDL_DISId] = type.id(); } } } QDir dir(gStrMSDLWorkingFolderPath()); dir.removeRecursively(); if (!dir.mkpath(QStringLiteral("."))) { ERROR_LOG(mLogger) << "Could not create path \"" << gStrMSDLWorkingFolderPath() << "\" which is required to parse MSDL data"; setEnabled(false); } if (!NGL::copyDirContentsToDir(QStringLiteral("msdl"), gStrMSDLWorkingFolderPath(), true)) { LtsWidgets::LtsMessageDialog::showMessage(this, tr("Not possible to start MSDL import. Could not copy validation files.")); ERROR_LOG(mLogger) << "Could not copy xsd files to " << gStrMSDLWorkingFolderPath(); return; } connect(m_pUi->m_pushbutton_SelectFile, &QPushButton::clicked, this, &ImportMsdl::slotOpenImportDialog); connect(m_pUi->m_pushButton_StartImport, &QPushButton::clicked, this, &ImportMsdl::slotStartImport); QString strImportFile = LtsUtils::OperatorSettings::getInstance()->value(QStringLiteral("MSDL"), QStringLiteral("pathSelectedImportFile"), QString()).toString(); m_pUi->m_lineedit_ImportFile->setText(strImportFile); m_pUi->m_pushButton_StartImport->setEnabled(strImportFile.size()); } ImportMsdl::~ImportMsdl() { delete m_pUi; } void ImportMsdl::slotOpenImportDialog() { QString strPathSelectedImportFolder = LtsUtils::OperatorSettings::getInstance()->value(QStringLiteral("MSDL"), QStringLiteral("pathSelectedImportFolder"), QStringLiteral("/shd/CTC/TOOLS/TDS/")).toString(); QString strPathSelectedImportFile = LtsUtils::OperatorSettings::getInstance()->value(QStringLiteral("MSDL"), QStringLiteral("pathSelectedImportFile"), QStringLiteral("/shd/CTC/TOOLS/TDS/")).toString(); QList urls; urls << QUrl::fromLocalFile(strPathSelectedImportFile); QFileDialog fileDialog(this, tr("Select File"), strPathSelectedImportFolder, tr("XML files (*.xml)")); fileDialog.setSidebarUrls(urls); fileDialog.setOptions(QFileDialog::ReadOnly); fileDialog.setFileMode(QFileDialog::ExistingFile); if (fileDialog.exec()) // TODO: use show(), use signals & slots ! { strPathSelectedImportFile = fileDialog.selectedFiles().constFirst(); LtsUtils::OperatorSettings::getInstance()->setValue(QStringLiteral("MSDL"), QStringLiteral("pathSelectedImportFolder"), strPathSelectedImportFolder); if (!strPathSelectedImportFile.size()) return; LtsUtils::OperatorSettings::getInstance()->setValue(QStringLiteral("MSDL"), QStringLiteral("pathSelectedImportFile"), strPathSelectedImportFile); m_pUi->m_lineedit_ImportFile->setText(strPathSelectedImportFile); m_pUi->m_pushButton_StartImport->setEnabled(true); } } void ImportMsdl::slotStartImport() { QString strPathSelectedImportFile = m_pUi->m_lineedit_ImportFile->text(); createWorkingCopy(strPathSelectedImportFile); if (!schemaValidation()) return; #ifdef LTS_DEBUG delete m_pMsdlImportShow; m_pMsdlImportShow = new msdl::ImportMsdlShow(nullptr, gStrMSDLWorkingFile()); m_pMsdlImportShow->show(); #endif QFile file(gStrMSDLWorkingFile()); if (!file.open(QIODevice::ReadOnly)) { ERROR_LOG(mLogger) << "Could not open msdl working copy: " << gStrMSDLWorkingFile(); return; } QDomDocument doc(QStringLiteral("mydocument")); if (!doc.setContent(&file)) { file.close(); return; } try { importExerciseScenario(); } catch (std::exception &e) { // There is so much that could be wrong when importing or exporting MSDL files (missing tags, optional tags, ...) LtsWidgets::LtsMessageDialog::showMessage(this, tr("MSDL import failed!")); ERROR_LOG(mLogger) << "MSDL import failed with an exception, e.what(): " << e.what(); } close(); } void ImportMsdl::createWorkingCopy(const QString &strImportFile) { // Input File QFile file(strImportFile); if (!file.open(QIODevice::ReadOnly)) { LtsWidgets::LtsMessageDialog::showMessage(this, tr("Selected input file doesn't exist on filesystem. Has it been deleted?"), tr("Error")); return; } // Output File QFile fileWithNamespace(gStrMSDLWorkingFile()); if (!fileWithNamespace.open(QIODevice::WriteOnly | QIODevice::Text)) { LtsWidgets::LtsMessageDialog::showMessage(this, tr("Cannot create working copy of input file on filesystem:") + QLatin1Char('\n') + gStrMSDLWorkingFile(), tr("Error")); return; } QTextStream inputTextStream(&file); QTextStream outputTextStream(&fileWithNamespace); QString line; bool kommentar = false; do { line = inputTextStream.readLine(); if (kommentar && !line.contains(QStringLiteral("-->"))) continue; if (line.contains(QStringLiteral("-->"))) { kommentar = false; continue; } if (line.contains(QStringLiteral("