Exemplo n.º 1
0
Date::Date(const std::string& dicomDate) : _year(1), _month(1), _day(1)
{
	OFDate ofDate = AIMUtil::DateConvert(dicomDate.c_str());
	if (ofDate.isValid())
	{
		_year = ofDate.getYear();
		_month = ofDate.getMonth();
		_day = ofDate.getDay();
	}
}
Exemplo n.º 2
0
bool StoreHandler::AddDICOMFileInfoToDatabase(boost::filesystem::path filename)
{
	// now try to add the file into the database
	if(!boost::filesystem::exists(filename))
		return false;

	DcmFileFormat dfile;
	if(dfile.loadFile(filename.c_str()).bad())
		return false;

	OFDate datebuf;
	OFTime timebuf;
	OFString textbuf;
	long numberbuf;

	std::string sopuid, seriesuid, studyuid;
	dfile.getDataset()->findAndGetOFString(DCM_SOPInstanceUID, textbuf);
	sopuid = textbuf.c_str();
	dfile.getDataset()->findAndGetOFString(DCM_SeriesInstanceUID, textbuf);
	seriesuid = textbuf.c_str();
	dfile.getDataset()->findAndGetOFString(DCM_StudyInstanceUID, textbuf);
	studyuid = textbuf.c_str();

	bool isOk = true;
	try
	{

		Poco::Data::Session dbconnection(config::getConnectionString());				
			
			std::vector<PatientStudy> patientstudies;
			Poco::Data::Statement patientstudiesselect(dbconnection);
			patientstudiesselect << "SELECT id,"
				"StudyInstanceUID,"
				"StudyID,"
				"AccessionNumber,"
				"PatientName,"
				"PatientID,"
				"StudyDate,"
				"ModalitiesInStudy,"
				"StudyDescription,"
				"PatientSex,"
				"PatientBirthDate,"
				"ReferringPhysicianName,"
				"created_at,updated_at"
				" FROM patient_studies WHERE StudyInstanceUID = ?",
				into(patientstudies),
				use(studyuid);

			patientstudiesselect.execute();

			if(patientstudies.size() == 0)
			{
				// insert
				patientstudies.push_back(PatientStudy());
			}
			
			PatientStudy &patientstudy = patientstudies[0];

			patientstudy.StudyInstanceUID = studyuid;

			dfile.getDataset()->findAndGetOFString(DCM_PatientName, textbuf);
			patientstudy.PatientName = textbuf.c_str();
			
			dfile.getDataset()->findAndGetOFString(DCM_StudyID, textbuf);
			patientstudy.StudyID = textbuf.c_str();

			dfile.getDataset()->findAndGetOFString(DCM_AccessionNumber, textbuf);
			patientstudy.AccessionNumber = textbuf.c_str();

			dfile.getDataset()->findAndGetOFString(DCM_PatientID, textbuf);
			patientstudy.PatientID = textbuf.c_str();

			datebuf = getDate(dfile.getDataset(), DCM_StudyDate);
			timebuf = getTime(dfile.getDataset(), DCM_StudyTime);
			// use DCM_TimezoneOffsetFromUTC ?
			if(datebuf.isValid() && timebuf.isValid()) 
			{			
				patientstudy.StudyDate.assign(datebuf.getYear(), datebuf.getMonth(), datebuf.getDay(), timebuf.getHour(), timebuf.getMinute(), timebuf.getSecond());
			}

			// handle modality list...
			std::string modalitiesinstudy = patientstudy.ModalitiesInStudy;
			std::set<std::string> modalityarray;
			if(modalitiesinstudy.length() > 0)
				boost::split(modalityarray, modalitiesinstudy, boost::is_any_of("\\"));
			dfile.getDataset()->findAndGetOFStringArray(DCM_Modality, textbuf);
			modalityarray.insert(textbuf.c_str());
			patientstudy.ModalitiesInStudy = boost::join(modalityarray, "\\");

			dfile.getDataset()->findAndGetOFString(DCM_StudyDescription, textbuf);
			patientstudy.StudyDescription = textbuf.c_str();

			dfile.getDataset()->findAndGetOFString(DCM_PatientSex, textbuf);
			patientstudy.PatientSex = textbuf.c_str();

			datebuf = getDate(dfile.getDataset(), DCM_PatientBirthDate);
			if(datebuf.isValid()) 
				patientstudy.PatientBirthDate.assign(datebuf.getYear(), datebuf.getMonth(), datebuf.getDay());

			dfile.getDataset()->findAndGetOFString(DCM_ReferringPhysicianName, textbuf);
			patientstudy.ReferringPhysicianName = textbuf.c_str();

			patientstudy.updated_at = Poco::DateTime();

			if(patientstudy.id != 0)
			{				
				Poco::Data::Statement update(dbconnection);
				update << "UPDATE patient_studies SET "
					"id = ?,"
					"StudyInstanceUID = ?,"
					"StudyID = ?,"
					"AccessionNumber = ?,"
					"PatientName = ?,"
					"PatientID = ?,"
					"StudyDate = ?,"
					"ModalitiesInStudy = ?,"
					"StudyDescription = ?,"
					"PatientSex = ?,"
					"PatientBirthDate = ?,"
					"ReferringPhysicianName = ?,"
					"created_at = ?, updated_at = ?"
					" WHERE id = ?",
					use(patientstudy),
					use(patientstudy.id);
				update.execute();
			}
			else
			{				
				patientstudy.created_at = Poco::DateTime();

				Poco::Data::Statement insert(dbconnection);
				insert << "INSERT INTO patient_studies VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
					use(patientstudy);
				insert.execute();

				dbconnection << "SELECT LAST_INSERT_ID()", into(patientstudy.id), now;
			}
		
		
			// update the series table			
			std::vector<Series> series_list;
			Poco::Data::Statement seriesselect(dbconnection);
			seriesselect << "SELECT id,"
				"SeriesInstanceUID,"
				"Modality,"
				"SeriesDescription,"
				"SeriesNumber,"
				"SeriesDate,"
				"created_at,updated_at,"
				"patient_study_id"
				" FROM series WHERE SeriesInstanceUID = ?",
				into(series_list),
				use(seriesuid);

			seriesselect.execute();

			if(series_list.size() == 0)
			{
				series_list.push_back(Series(patientstudy.id));
			}

			Series &series = series_list[0];

			series.SeriesInstanceUID = seriesuid;			

			dfile.getDataset()->findAndGetOFString(DCM_Modality, textbuf);
			series.Modality = textbuf.c_str();

			dfile.getDataset()->findAndGetOFString(DCM_SeriesDescription, textbuf);
			series.SeriesDescription = textbuf.c_str();

			dfile.getDataset()->findAndGetSint32(DCM_SeriesNumber, numberbuf);
			series.SeriesNumber = numberbuf;

			datebuf = getDate(dfile.getDataset(), DCM_SeriesDate);
			timebuf = getTime(dfile.getDataset(), DCM_SeriesTime);
			// use DCM_TimezoneOffsetFromUTC ?
			if(datebuf.isValid() && timebuf.isValid()) 
			{
				series.SeriesDate.assign(datebuf.getYear(), datebuf.getMonth(), datebuf.getDay(), timebuf.getHour(), timebuf.getMinute(), timebuf.getSecond());
				
			}
			
			series.updated_at = Poco::DateTime();

			if(series.id != 0)
			{
				Poco::Data::Statement update(dbconnection);
				update << "UPDATE series SET "
					"id = ?,"
					"SeriesInstanceUID = ?,"
					"Modality = ?,"
					"SeriesDescription = ?,"
					"SeriesNumber = ?,"
					"SeriesDate = ?,"
					"created_at = ?, updated_at = ?,"
					"patient_study_id = ?"
					" WHERE id = ?",
					use(series),
					use(series.id);
				update.execute();
			}
			else
			{				
				series.created_at = Poco::DateTime();		

				Poco::Data::Statement insert(dbconnection);
				insert << "INSERT INTO series VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)",
					use(series);
				insert.execute();

				dbconnection << "SELECT LAST_INSERT_ID()", into(series.id), now;
			}
		
		
		// update the images table
		
		std::vector<Instance> instances;
		Poco::Data::Statement instanceselect(dbconnection);
		instanceselect << "SELECT id,"
			"SOPInstanceUID,"			
			"InstanceNumber,"
			"created_at,updated_at,"
			"series_id"
			" FROM instances WHERE SOPInstanceUID = ?",
			into(instances),
			use(sopuid);

		instanceselect.execute();

		if(instances.size() == 0)
		{
			instances.push_back(Instance(series.id));
		}

		Instance &instance = instances[0];

		instance.SOPInstanceUID = sopuid;

		dfile.getDataset()->findAndGetSint32(DCM_InstanceNumber, numberbuf);
		instance.InstanceNumber = numberbuf;

		instance.updated_at = Poco::DateTime();

		if(instance.id != 0)
		{
			Poco::Data::Statement update(dbconnection);
			update << "UPDATE instances SET "
				"id = ?,"
				"SOPInstanceUID = ?,"
				"InstanceNumber = ?,"
				"created_at = ?, updated_at = ?,"
				"series_id = ?"
				" WHERE id = ?",
				use(instance),
				use(instance.id);

			update.execute();
		}
		else
		{			
			instance.created_at = Poco::DateTime();
			
			Poco::Data::Statement insert(dbconnection);
			insert << "INSERT INTO instances VALUES(?, ?, ?, ?, ?, ?)",
				use(instance);
			insert.execute();

			dbconnection << "SELECT LAST_INSERT_ID()", into(series.id), now;
		}		
	}
	catch(Poco::Data::DataException &e)
	{
		std::string what = e.message();
		isOk = false;
		DCMNET_ERROR(what);
	}

	return isOk;
}