#include "gdcmMovePatientRootQuery.h"
#include "gdcmAttribute.h"
#include <algorithm>
namespace gdcm
{
MovePatientRootQuery::MovePatientRootQuery()
{
mRootType = ePatientRootType;
mHelpDescription = "Patient-level root query";
}
void MovePatientRootQuery::InitializeDataSet(const EQueryLevel& inQueryLevel)
{
switch (inQueryLevel)
{
case ePatient:
{
Attribute<0x8,0x52> at1 = { "PATIENT " };
mDataSet.Insert( at1.GetAsDataElement() );
}
break;
case eStudy:
{
Attribute<0x8,0x52> at1 = { "STUDY " };
mDataSet.Insert( at1.GetAsDataElement() );
Attribute<0x10,0x20> PatientLevel = { "" };
mDataSet.Insert( PatientLevel.GetAsDataElement() );
}
break;
case eSeries:
{
Attribute<0x8,0x52> at1 = { "SERIES" };
mDataSet.Insert( at1.GetAsDataElement() );
Attribute<0x10,0x20> PatientLevel = { "" };
mDataSet.Insert( PatientLevel.GetAsDataElement() );
Attribute<0x20, 0xd> Studylevel = { "" };
mDataSet.Insert( Studylevel.GetAsDataElement() );
}
break;
case eImage:
{
Attribute<0x8,0x52> at1 = { "IMAGE " };
mDataSet.Insert( at1.GetAsDataElement() );
Attribute<0x10,0x20> PatientLevel = { "" };
mDataSet.Insert( PatientLevel.GetAsDataElement() );
Attribute<0x20, 0xd> Studylevel = { "" };
mDataSet.Insert( Studylevel.GetAsDataElement() );
Attribute<0x20, 0xe> SeriesLevel = { "" };
mDataSet.Insert( SeriesLevel.GetAsDataElement() );
}
break;
}
}
std::vector<Tag> MovePatientRootQuery::GetTagListByLevel(const EQueryLevel& inQueryLevel)
{
switch (inQueryLevel)
{
case ePatient:
return mPatient.GetUniqueTags(ePatientRootType);
case eStudy:
return mStudy.GetUniqueTags(ePatientRootType);
case eSeries:
return mSeries.GetUniqueTags(ePatientRootType);
case eImage:
return mImage.GetUniqueTags(ePatientRootType);
default: gdcm_assert(0);
{
std::vector<Tag> empty;
return empty;
}
}
}
bool MovePatientRootQuery::ValidateQuery(bool inStrict) const
{
const DataSet &ds = GetQueryDataSet();
if (ds.Size() == 0)
{
if (inStrict) {
gdcmWarningMacro( "Empty DataSet in ValidateQuery" );
}
return false;
}
Attribute<0x0008, 0x0052> level;
level.SetFromDataSet( ds );
const std::string theVal = level.GetValue();
const int ilevel = BaseRootQuery::GetQueryLevelFromString( theVal.c_str() );
if( ilevel == -1 )
{
gdcmWarningMacro( "Invalid Level" );
return false;
}
bool theReturn = true;
std::vector<Tag> tags; std::vector<Tag> hiertags;
if (inStrict)
{
QueryBase* qb = BaseRootQuery::Construct( ePatientRootType, (EQueryLevel)ilevel );
if (qb == nullptr)
{
gdcmWarningMacro( "Invalid Query" );
return false;
}
std::vector<Tag> opttags = qb->GetOptionalTags(ePatientRootType);
tags.insert( tags.begin(), opttags.begin(), opttags.end() );
std::vector<Tag> reqtags = qb->GetRequiredTags(ePatientRootType);
tags.insert( tags.begin(), reqtags.begin(), reqtags.end() );
hiertags = qb->GetHierachicalSearchTags(ePatientRootType);
tags.insert( tags.begin(), hiertags.begin(), hiertags.end() );
delete qb;
}
else {
QueryBase* qb = nullptr;
if (strcmp(theVal.c_str(), "PATIENT ") == 0)
{
std::vector<Tag> tagGroup;
qb = new QueryPatient();
tagGroup = qb->GetUniqueTags(ePatientRootType);
tags.insert(tags.end(), tagGroup.begin(), tagGroup.end());
delete qb;
}
else if (strcmp(theVal.c_str(), "STUDY ") == 0)
{
std::vector<Tag> tagGroup;
qb = new QueryPatient();
tagGroup = qb->GetUniqueTags(ePatientRootType);
tags.insert(tags.end(), tagGroup.begin(), tagGroup.end());
delete qb;
qb = new QueryStudy();
tagGroup = qb->GetUniqueTags(ePatientRootType);
tags.insert(tags.end(), tagGroup.begin(), tagGroup.end());
delete qb;
}
else if (strcmp(theVal.c_str(), "SERIES") == 0)
{
std::vector<Tag> tagGroup;
qb = new QueryPatient();
tagGroup = qb->GetUniqueTags(ePatientRootType);
tags.insert(tags.end(), tagGroup.begin(), tagGroup.end());
delete qb;
qb = new QueryStudy();
tagGroup = qb->GetUniqueTags(eStudyRootType);
tags.insert(tags.end(), tagGroup.begin(), tagGroup.end());
delete qb;
qb = new QuerySeries();
tagGroup = qb->GetUniqueTags(ePatientRootType);
tags.insert(tags.end(), tagGroup.begin(), tagGroup.end());
delete qb;
}
else if (strcmp(theVal.c_str(), "IMAGE ") == 0)
{
std::vector<Tag> tagGroup;
qb = new QueryPatient();
tagGroup = qb->GetUniqueTags(ePatientRootType);
tags.insert(tags.end(), tagGroup.begin(), tagGroup.end());
delete qb;
qb = new QueryStudy();
tagGroup = qb->GetUniqueTags(ePatientRootType);
tags.insert(tags.end(), tagGroup.begin(), tagGroup.end());
delete qb;
qb = new QuerySeries();
tagGroup = qb->GetUniqueTags(ePatientRootType);
tags.insert(tags.end(), tagGroup.begin(), tagGroup.end());
delete qb;
qb = new QueryImage();
tagGroup = qb->GetUniqueTags(ePatientRootType);
tags.insert(tags.end(), tagGroup.begin(), tagGroup.end());
delete qb;
}
if (tags.empty())
{
gdcmWarningMacro( "Invalid Level" );
return false;
}
}
DataSet::ConstIterator itor;
Attribute<0x0008, 0x0005> language;
if( inStrict )
{
unsigned int thePresentTagCount = 0;
for (itor = ds.Begin(); itor != ds.End(); itor++)
{
const Tag &t = itor->GetTag();
if (t == level.GetTag()) continue;
if (t == language.GetTag()) continue;
gdcm_assert( !tags.empty() );
if (std::find(tags.begin(), tags.end(), t) == tags.end())
{
gdcmWarningMacro( "You have an extra tag: " << t );
theReturn = false;
break;
}
else
{
if (std::find(hiertags.begin(), hiertags.end(), t) !=
hiertags.end())
{
gdcmDebugMacro( "Found at least one key: " << t );
thePresentTagCount++;
}
}
}
if( thePresentTagCount != hiertags.size() )
{
gdcmWarningMacro( "Missing Key found (within the hierarchical search ones)" );
theReturn = false;
}
}
return theReturn;
}
UIDs::TSName MovePatientRootQuery::GetAbstractSyntaxUID() const
{
return UIDs::PatientRootQueryRetrieveInformationModelMOVE;
}
}