<commit_before>/* medDatabaseReader.cpp ---
 *
 * Author: <NAME>
 * Copyright (C) 2008 - <NAME>, Inria.
 * Created: Tue Jun 29 15:27:20 2010 (+0200)
 * Version: $Id$
 * Last-Updated: Tue Jun 29 15:46:20 2010 (+0200)
 *           By: <NAME>
 *     Update #: 19
 */

/* Commentary:
 *
 */

/* Change log:
 *
 */

#include "medDatabaseController.h"
#include "medDatabaseReader.h"
#include <medStorage.h>
#include <medAbstractDataImage.h>

#include <medCore/medMetaDataKeys.h>
#include <dtkCore/dtkAbstractDataFactory.h>
#include <dtkCore/dtkAbstractDataReader.h>
#include <dtkCore/dtkAbstractDataWriter.h>
#include <dtkCore/dtkAbstractData.h>
#include <dtkCore/dtkGlobal.h>
#include <dtkCore/dtkLog.h>

class medDatabaseReaderPrivate
{
public:
    medDataIndex index;
};

medDatabaseReader::medDatabaseReader ( const medDataIndex& index ) : QObject(), d ( new medDatabaseReaderPrivate )
{
    d->index = index;
}

medDatabaseReader::~medDatabaseReader ( void )
{
    delete d;

    d = NULL;
}

dtkSmartPointer<dtkAbstractData> medDatabaseReader::run ( void )
{
    QVariant patientId = d->index.patientId();
    QVariant   studyId = d->index.studyId();
    QVariant  seriesId = d->index.seriesId();
    QVariant   imageId = d->index.imageId();

    QSqlQuery query ( ( * ( medDatabaseController::instance()->database() ) ) );

    QString patientName;
    QString birthdate;
    QString studyName;
    QString studyUid;
    QString seriesName;
    QString seriesUid;

    query.prepare ( "SELECT name, birthdate FROM patient WHERE id = :id" );
    query.bindValue ( ":id", patientId );
    if ( !query.exec() )
        qDebug() << DTK_COLOR_FG_RED << query.lastError() << DTK_NO_COLOR;
    if ( query.first() )
    {
        patientName = query.value ( 0 ).toString();
        birthdate = query.value ( 1 ).toString();
    }

    query.prepare ( "SELECT name, uid FROM study WHERE id = :id" );
    query.bindValue ( ":id", studyId );
    if ( !query.exec() )
        qDebug() << DTK_COLOR_FG_RED << query.lastError() << DTK_NO_COLOR;
    if ( query.first() )
    {
        studyName = query.value ( 0 ).toString();
        studyUid = query.value ( 1 ).toString();
    }

    query.prepare ( "SELECT name, uid FROM series WHERE id = :id" );
    query.bindValue ( ":id", seriesId );
    if ( !query.exec() )
        qDebug() << DTK_COLOR_FG_RED << query.lastError() << DTK_NO_COLOR;
    if ( query.first() )
    {
        seriesName = query.value ( 0 ).toString();
        seriesUid = query.value ( 1 ).toString();
    }

    query.prepare ( "SELECT name, id, path, instance_path, isIndexed FROM image WHERE series = :series" );
    query.bindValue ( ":series", seriesId );
    if ( !query.exec() )
        qDebug() << DTK_COLOR_FG_RED << query.lastError() << DTK_NO_COLOR;

    // now we might have both indexed and imported images in the same series
    // so we will get the full path for indexed images and, for the imported
    // (and possibly aggregated) images we will get the path of the
    // aggregated file (which is in instance_path)

    QStringList filenames;
    while ( query.next() )
    {
        bool isIndexed = query.value ( 4 ).toBool();

        if ( isIndexed )
        {
            QString filename = query.value ( 2 ).toString();

            // if the file is indexed the chanced that is not there anymore are higher
            // so we check for existence and return null if they are not there anymore
            QFileInfo fileinfo ( filename );

            if ( !fileinfo.exists() )
            {
                emit failure ( this );
                return NULL;
            }

            filenames << filename;
        }
        else
        {
            filenames << medStorage::dataLocation() + query.value ( 3 ).toString();
        }
    }
    // we remove all the duplicate entries, as imported files
    // might have introduced duplicates
    filenames.removeDuplicates();

    dtkSmartPointer <dtkAbstractData> dtkdata =  this->readFile ( filenames );


    if ( ( !dtkdata.isNull() ) && dtkdata.data() )
    {

        QSqlQuery seriesQuery ( * ( medDatabaseController::instance()->database() ) );
        QVariant seriesThumbnail;

        seriesQuery.prepare ( "SELECT thumbnail FROM series WHERE id = :seriesId" );
        seriesQuery.bindValue ( ":seriesId", seriesId );
        if ( !seriesQuery.exec() )
            qDebug() << DTK_COLOR_FG_RED << seriesQuery.lastError() << DTK_NO_COLOR;

        if ( seriesQuery.first() )
        {
            seriesThumbnail = seriesQuery.value ( 0 );

            QString thumbPath = medStorage::dataLocation() + seriesThumbnail.toString();
            medMetaDataKeys::SeriesThumbnail.add ( dtkdata, thumbPath );

        }
        else
        {
            qWarning() << "Thumbnailpath not found";
        }

        medMetaDataKeys::PatientName.add ( dtkdata, patientName );
        medMetaDataKeys::BirthDate.add ( dtkdata, birthdate );
        medMetaDataKeys::StudyDescription.add ( dtkdata, studyName );
        medMetaDataKeys::SeriesDescription.add ( dtkdata, seriesName );
        medMetaDataKeys::PatientID.add ( dtkdata, patientId.toString() );
        medMetaDataKeys::StudyID.add ( dtkdata, studyUid );
        medMetaDataKeys::SeriesID.add ( dtkdata, seriesUid );
        //medMetaDataKeys::ImageID.add(data, imageId.toString());

        emit success ( this );
    }
    else
    {
        emit failure ( this );
    }
    if ( dtkdata.refCount() != 1 )
        qWarning() << "(Run:Exit) RefCount should be 1 here: " << dtkdata.refCount();
    return dtkdata;

}

qint64 medDatabaseReader::getDataSize()
{
    QString filename = getFilePath();
    QFileInfo info ( filename );
    return info.size();
}

QString medDatabaseReader::getFilePath()
{
    QVariant patientId = d->index.patientId();
    QVariant   studyId = d->index.studyId();
    QVariant  seriesId = d->index.seriesId();
    QVariant   imageId = d->index.imageId();

    QSqlQuery query ( ( * ( medDatabaseController::instance()->database() ) ) );<commit_msg>Load all metadata when reading a data from db<commit_after>/* medDatabaseReader.cpp ---
 *
 * Author: <NAME>
 * Copyright (C) 2008 - <NAME>, Inria.
 * Created: Tue Jun 29 15:27:20 2010 (+0200)
 * Version: $Id$
 * Last-Updated: Tue Jun 29 15:46:20 2010 (+0200)
 *           By: <NAME>
 *     Update #: 19
 */

/* Commentary:
 *
 */

/* Change log:
 *
 */

#include "medDatabaseController.h"
#include "medDatabaseReader.h"
#include <medStorage.h>
#include <medAbstractDataImage.h>

#include <medCore/medMetaDataKeys.h>
#include <dtkCore/dtkAbstractDataFactory.h>
#include <dtkCore/dtkAbstractDataReader.h>
#include <dtkCore/dtkAbstractDataWriter.h>
#include <dtkCore/dtkAbstractData.h>
#include <dtkCore/dtkGlobal.h>
#include <dtkCore/dtkLog.h>

class medDatabaseReaderPrivate
{
public:
    medDataIndex index;
};

medDatabaseReader::medDatabaseReader ( const medDataIndex& index ) : QObject(), d ( new medDatabaseReaderPrivate )
{
    d->index = index;
}

medDatabaseReader::~medDatabaseReader ( void )
{
    delete d;

    d = NULL;
}

dtkSmartPointer<dtkAbstractData> medDatabaseReader::run ( void )
{
    QVariant patientId = d->index.patientId();
    QVariant   studyId = d->index.studyId();
    QVariant  seriesId = d->index.seriesId();
    QVariant   imageId = d->index.imageId();

    QSqlQuery query ( ( * ( medDatabaseController::instance()->database() ) ) );

    QString patientName, birthdate, age, gender;
    QString studyName, studyUid;
    QString seriesName, seriesUid, orientation, seriesNumber, sequenceName,
            sliceThickness, rows, columns, refThumbPath, description, protocol,
            comments, status, acquisitiondate, importationdate, referee,
            institution, report, modality;

    query.prepare ( "SELECT name, birthdate, gender FROM patient WHERE id = :id" );
    query.bindValue ( ":id", patientId );
    if ( !query.exec() )
        qDebug() << DTK_COLOR_FG_RED << query.lastError() << DTK_NO_COLOR;
    if ( query.first() )
    {
        patientName = query.value ( 0 ).toString();
        birthdate = query.value ( 1 ).toString();
        gender = query.value ( 2 ).toString();
    }

    query.prepare ( "SELECT name, uid FROM study WHERE id = :id" );
    query.bindValue ( ":id", studyId );
    if ( !query.exec() )
        qDebug() << DTK_COLOR_FG_RED << query.lastError() << DTK_NO_COLOR;
    if ( query.first() )
    {
        studyName = query.value ( 0 ).toString();
        studyUid = query.value ( 1 ).toString();
    }

    query.prepare ( "SELECT name, uid, orientation, seriesNumber, sequenceName, sliceThickness, rows, columns, \
                     description, protocol, comments, status, acquisitiondate, importationdate, referee,       \
                     institution, report, modality \
                     FROM series WHERE id = :id" );
    
    query.bindValue ( ":id", seriesId );
    if ( !query.exec() )
        qDebug() << DTK_COLOR_FG_RED << query.lastError() << DTK_NO_COLOR;
    if ( query.first() )
    {
        seriesName = query.value ( 0 ).toString();
        seriesUid = query.value ( 1 ).toString();
        orientation = query.value ( 2 ).toString();
        seriesNumber = query.value ( 3 ).toString();
        sequenceName = query.value ( 4 ).toString();
        sliceThickness = query.value ( 5 ).toString();
        rows = query.value ( 6 ).toString();
        columns = query.value ( 7 ).toString();
        description = query.value ( 8 ).toString();
        protocol = query.value ( 9 ).toString();
        comments = query.value ( 10 ).toString();
        status = query.value ( 11 ).toString();
        acquisitiondate = query.value ( 12 ).toString();
        importationdate = query.value ( 13 ).toString();
        referee = query.value ( 14 ).toString();
        institution = query.value ( 15 ).toString();
        report = query.value ( 16 ).toString();
        modality = query.value ( 17 ).toString();
    }

    query.prepare ( "SELECT name, id, path, instance_path, isIndexed FROM image WHERE series = :series" );
    query.bindValue ( ":series", seriesId );
    if ( !query.exec() )
        qDebug() << DTK_COLOR_FG_RED << query.lastError() << DTK_NO_COLOR;

    // now we might have both indexed and imported images in the same series
    // so we will get the full path for indexed images and, for the imported
    // (and possibly aggregated) images we will get the path of the
    // aggregated file (which is in instance_path)

    QStringList filenames;
    while ( query.next() )
    {
        bool isIndexed = query.value ( 4 ).toBool();

        if ( isIndexed )
        {
            QString filename = query.value ( 2 ).toString();

            // if the file is indexed the chanced that is not there anymore are higher
            // so we check for existence and return null if they are not there anymore
            QFileInfo fileinfo ( filename );

            if ( !fileinfo.exists() )
            {
                emit failure ( this );
                return NULL;
            }

            filenames << filename;
        }
        else
        {
            filenames << medStorage::dataLocation() + query.value ( 3 ).toString();
        }
    }
    // we remove all the duplicate entries, as imported files
    // might have introduced duplicates
    filenames.removeDuplicates();

    dtkSmartPointer <dtkAbstractData> dtkdata =  this->readFile ( filenames );


    if ( ( !dtkdata.isNull() ) && dtkdata.data() )
    {

        QSqlQuery seriesQuery ( * ( medDatabaseController::instance()->database() ) );
        QVariant seriesThumbnail;

        seriesQuery.prepare ( "SELECT thumbnail FROM series WHERE id = :seriesId" );
        seriesQuery.bindValue ( ":seriesId", seriesId );
        if ( !seriesQuery.exec() )
            qDebug() << DTK_COLOR_FG_RED << seriesQuery.lastError() << DTK_NO_COLOR;

        if ( seriesQuery.first() )
        {
            seriesThumbnail = seriesQuery.value ( 0 );

            QString thumbPath = medStorage::dataLocation() + seriesThumbnail.toString();
            medMetaDataKeys::SeriesThumbnail.add ( dtkdata, thumbPath );
        }
        else
        {
            qWarning() << "Thumbnailpath not found";
        }

        medMetaDataKeys::PatientID.add ( dtkdata, patientId.toString() );
        medMetaDataKeys::PatientName.add ( dtkdata, patientName );
        medMetaDataKeys::BirthDate.add ( dtkdata, birthdate );
        medMetaDataKeys::Gender.add ( dtkdata, gender );
        medMetaDataKeys::StudyDescription.add ( dtkdata, studyName );
        medMetaDataKeys::StudyID.add ( dtkdata, studyUid );
        medMetaDataKeys::SeriesDescription.add ( dtkdata, seriesName );
        medMetaDataKeys::SeriesID.add ( dtkdata, seriesUid );
        medMetaDataKeys::Orientation.add ( dtkdata, orientation );
        medMetaDataKeys::Columns.add ( dtkdata, columns );
        medMetaDataKeys::Rows.add ( dtkdata, rows );
        medMetaDataKeys::AcquisitionDate.add ( dtkdata, acquisitiondate );
        medMetaDataKeys::Comments.add ( dtkdata, comments );
        medMetaDataKeys::Description.add ( dtkdata, description );
        medMetaDataKeys::ImportationDate.add ( dtkdata, importationdate );
        medMetaDataKeys::Modality.add ( dtkdata, modality );
        medMetaDataKeys::Protocol.add ( dtkdata, protocol );
        medMetaDataKeys::Referee.add ( dtkdata, referee );
        medMetaDataKeys::Institution.add ( dtkdata, institution );
        medMetaDataKeys::Report.add ( dtkdata, report );
        medMetaDataKeys::Status.add ( dtkdata, status );
        medMetaDataKeys::SequenceName.add ( dtkdata, sequenceName );
        medMetaDataKeys::SliceThickness.add ( dtkdata, sliceThickness );
        medMetaDataKeys::SeriesNumber.add(dtkdata, seriesNumber);
        //medMetaDataKeys::ImageID.add(data, imageId.toString());

        emit success ( this );
    }
    else
    {
        emit failure ( this );
    }
    if ( dtkdata.refCount() != 1 )
        qWarning() << "(Run:Exit) RefCount should be 1 here: " << dtkdata.refCount();
    return dtkdata;

}

qint64 medDatabaseReader::getDataSize()
{
    QString filename = getFilePath();
    QFileInfo info ( filename );
    return info.size();
}

QString medDatabaseReader::getFilePath()
{
    QVariant patientId = d->index.patientId();
    QVariant   studyId = d->index.studyId();
    QVariant  seriesId = d->index.seriesId();
    QVariant   imageId = d->index.imageId();

    QSqlQuery query ( ( * ( medDatabaseController::instance()->database() ) ) );
{'fraction_non_alphanumeric': 0.1269273127753304, 'fraction_numerical': 0.008879405286343612, 'mean_word_length': 2.680091185410334, 'pattern_counts': {'":': 10, '<': 111, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3082177', 'n_tokens_mistral': 4424, 'n_tokens_neox': 4259, 'n_words': 1053}