Exemplo n.º 1
0
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
void RiuWellImportWizard::downloadWellPaths()
{
    for (size_t rIdx = 0; rIdx < m_wellPathImportObject->regions.size(); rIdx++)
    {
        RimOilRegionEntry* oilRegion = m_wellPathImportObject->regions[rIdx];
        if (oilRegion->selected)
        {
            for (size_t fIdx = 0; fIdx < oilRegion->fields.size(); fIdx++)
            {
                RimOilFieldEntry* oilField = oilRegion->fields[fIdx];
                if (oilField->selected)
                {
                    for (size_t wIdx = 0; wIdx < oilField->wells.size(); wIdx++)
                    {
                        RimWellPathEntry* wellPathEntry = oilField->wells[wIdx];
                        
                        if (!wellPathEntry->isWellPathValid())
                            continue;
                        
                        DownloadEntity urlToFile;

                        urlToFile.requestUrl = wellPathEntry->requestUrl;
                        urlToFile.responseFilename = wellPathEntry->wellPathFilePath;

                        m_wellRequestQueue.push_back(urlToFile);
                    }
                }
            }
        }
    }

    m_currentDownloadState = DOWNLOAD_WELL_PATH;

    checkDownloadQueueAndIssueRequests();
}
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
void RiuWellImportWizard::downloadWells()
{
    for (size_t rIdx = 0; rIdx < m_wellPathImportObject->regions.size(); rIdx++)
    {
        RimOilRegionEntry* oilRegion = m_wellPathImportObject->regions[rIdx];
        if (oilRegion->selected)
        {
            for (size_t fIdx = 0; fIdx < oilRegion->fields.size(); fIdx++)
            {
                RimOilFieldEntry* oilField = oilRegion->fields[fIdx];
                if (oilField->selected)
                {
                    DownloadEntity urlToFile;

                    QString wellRequest;
                    if (m_wellPathImportObject->utmFilterMode == RimWellPathImport::UTM_FILTER_OFF)
                    {
                        wellRequest = QString("/resinsight/projects/%1/wells").arg(oilField->edmId);
                    }
                    else
                    {
                        wellRequest = QString("/resinsight/projects/%1/wellsInArea?north=%2&south=%3&east=%4&west=%5&utmzone=32N")
                            .arg(oilField->edmId)
                            .arg(QString::number(m_wellPathImportObject->north, 'g', 10))
                            .arg(QString::number(m_wellPathImportObject->south, 'g', 10))
                            .arg(QString::number(m_wellPathImportObject->east, 'g', 10))
                            .arg(QString::number(m_wellPathImportObject->west, 'g', 10));
                    }

                    urlToFile.requestUrl = m_webServiceAddress + wellRequest;
                    urlToFile.responseFilename = m_destinationFolder + QString("/wells_%1.json").arg(oilField->edmId);

                    oilField->wellsFilePath = urlToFile.responseFilename;

                    m_wellRequestQueue.push_back(urlToFile);
                }
            }
        }
    }

    m_currentDownloadState = DOWNLOAD_WELLS;
    checkDownloadQueueAndIssueRequests();
}
//--------------------------------------------------------------------------------------------------
/// 
//--------------------------------------------------------------------------------------------------
void RiuWellImportWizard::httpFinished()
{
    if (m_httpRequestAborted) {
        if (m_file) {
            m_file->close();
            m_file->remove();
            delete m_file;
            m_file = 0;
        }
        m_reply->deleteLater();
        m_myProgressDialog->hide();
        return;
    }

    m_file->flush();
    m_file->close();

    QVariant redirectionTarget = m_reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
    if (m_reply->error()) {
        m_file->remove();
        QMessageBox::information(this, tr("HTTP"),
            tr("Download failed: %1.")
            .arg(m_reply->errorString()));
    } else if (!redirectionTarget.isNull()) {        
        QUrl newUrl = m_url.resolved(redirectionTarget.toUrl());
        if (QMessageBox::question(this, tr("HTTP"),
            tr("Redirect to %1 ?").arg(newUrl.toString()),
            QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
                m_url = newUrl;
                m_reply->deleteLater();
                m_file->open(QIODevice::WriteOnly);
                m_file->resize(0);
                startRequest(m_url);
                return;
        }
    } else {
        //m_statusLabel->setText(tr("Downloaded data to %1.").arg(m_destinationFolder));
    }

    if (m_currentDownloadState == DOWNLOAD_WELL_PATH)
    {
        QString singleWellPathFilePath = m_file->fileName();

        QFile file(singleWellPathFilePath);
        if (file.open(QFile::ReadOnly))
        {
            QString singleWellPathContent = file.readAll();

            // Strip leading and trailing []

            if (singleWellPathContent.indexOf('{') > 0)
            {
                singleWellPathContent = singleWellPathContent.right(singleWellPathContent.size() - singleWellPathContent.indexOf('{'));
            }

            if (singleWellPathContent[singleWellPathContent.size() - 1] == ']')
            {
                singleWellPathContent = singleWellPathContent.left(singleWellPathContent.size() - 1);
            }

            QString wellPathName = getValue("name", singleWellPathContent);
            if (!singleWellPathContent.isEmpty() && !wellPathName.isEmpty())
            {
                // Write out the content without leading/trailing []
                file.close();
                file.remove(singleWellPathFilePath);

                if (file.open(QFile::WriteOnly))
                {
                    QTextStream out(&file);
                    out << singleWellPathContent;
                }
            }

            m_myProgressDialog->setLabelText(QString("Downloaded well path : %1").arg(wellPathName));
        }

        int newValue = m_myProgressDialog->maximum() - m_wellRequestQueue.size();
        m_myProgressDialog->setValue(newValue);
    }

    m_reply->deleteLater();
    m_reply = 0;
    delete m_file;
    m_file = 0;

    if (m_currentDownloadState == DOWNLOAD_WELLS || m_currentDownloadState == DOWNLOAD_WELL_PATH)
    {
        checkDownloadQueueAndIssueRequests();
    }
    else if (m_currentDownloadState == DOWNLOAD_FIELDS)
    {
        updateFieldsModel();
        m_currentDownloadState = DOWNLOAD_UNDEFINED;
    }
}