Exemple #1
0
        /// <summary>
        /// Check if the given instance is present in the Information Model. The instance will be at the leaf nodes of the Information Model.
        /// </summary>
        /// <param name="sopClassUid">SOP Class UID to search for.</param>
        /// <param name="sopInstanceUid">SOP Instance UID to search for.</param>
        /// <returns>Boolean - true if instance found in the Information Model, otherwise false.</returns>
        public bool IsInstanceInInformationModel(System.String sopClassUid, System.String sopInstanceUid)
        {
            bool isInstanceInInformationModel = false;

            // set up the commit tag list for comparing with the leaf
            TagTypeList commitTagTypeList = new TagTypeList();

            commitTagTypeList.Add(new TagType(Tag.SOP_INSTANCE_UID, TagTypeEnum.TagRequired));
            commitTagTypeList.Add(new TagType(Tag.SOP_CLASS_UID, TagTypeEnum.TagRequired));

            // set up the commit dataset
            DvtkData.Dimse.DataSet   commitDataset = new DvtkData.Dimse.DataSet();
            DvtkData.Dimse.Attribute attribute     = new DvtkData.Dimse.Attribute(0x00080016, DvtkData.Dimse.VR.UI, sopClassUid);
            commitDataset.Add(attribute);
            attribute = new DvtkData.Dimse.Attribute(0x00080018, DvtkData.Dimse.VR.UI, sopInstanceUid);
            commitDataset.Add(attribute);

            // iterate over the whole information model - we are interested in the leaf nodes
            foreach (PatientInformationEntity patientInformationEntity in Root)
            {
                foreach (StudyInformationEntity studyInformationEntity in patientInformationEntity.Children)
                {
                    foreach (SeriesInformationEntity seriesInformationEntity in studyInformationEntity.Children)
                    {
                        foreach (InstanceInformationEntity instanceInformationEntity in seriesInformationEntity.Children)
                        {
                            if (instanceInformationEntity.IsFoundIn(commitTagTypeList, commitDataset))
                            {
                                // an instance has been found with the matching commit uids
                                isInstanceInInformationModel = true;
                                break;
                            }
                        }
                    }
                }
            }

            return(isInstanceInInformationModel);
        }
        /// <summary>
        /// Add the given Dataset to the Information Model. The data is normalised into the Information Model.
        /// </summary>
        /// <param name="dataSet">Dataset to add to the informartion model</param>
        /// <param name="transferSyntax">The transfer syntax specified in the dcm file</param>
        /// <param name="fMI">The File Meta Information of the dcm file.</param>
        /// <param name="storeFile">Boolean indicating whether the or not the data set should be stored.</param>
        //public override void AddToInformationModel(DvtkData.Media.DicomFile dicomFile, bool storeFile)
        //{
        //    // PATIENT level
        //    PatientInformationEntity patientInformationEntity = null;
        //    // check if the patient IE is already in the patientRootList
        //    foreach (PatientInformationEntity lPatientInformationEntity in Root)
        //    {
        //        if (lPatientInformationEntity.IsFoundIn(dicomFile.DataSet))
        //        {
        //            patientInformationEntity = lPatientInformationEntity;
        //            break;
        //        }
        //    }
        //    // patient IE is not already in the patientRootList
        //    if (patientInformationEntity == null)
        //    {
        //        // create a new patient IE from the dataset and add to the patientRootList
        //        patientInformationEntity = new PatientInformationEntity();
        //        patientInformationEntity.CopyFrom(dicomFile.DataSet);
        //        //Root.Add(patientInformationEntity);
        //        // Modified by RB 20090128 - when handling an order scheduled event from an actor
        //        // we want to insert the order as the first entry in the information model so that
        //        // it is returned as the first entry in the worklist query
        //        Root.Insert(0, patientInformationEntity);
        //    }
        //    // VISIT level
        //    VisitInformationEntity visitInformationEntity = null;
        //    // check if the visit IE is already in the patient IE children
        //    foreach (VisitInformationEntity lVisitInformationEntity in patientInformationEntity.Children)
        //    {
        //        if (lVisitInformationEntity.IsFoundIn(dicomFile.DataSet))
        //        {
        //            visitInformationEntity = lVisitInformationEntity;
        //            break;
        //        }
        //    }
        //    // visit IE is not already in the patient IE children
        //    if (visitInformationEntity == null)
        //    {
        //        // create a new visit IE from the dataset and add to the patient IE children
        //        visitInformationEntity = new VisitInformationEntity();
        //        visitInformationEntity.CopyFrom(dicomFile.DataSet);
        //        patientInformationEntity.AddChild(visitInformationEntity);
        //    }
        //    // IMAGING SERVICE REQUEST level
        //    ImagingServiceRequestInformationEntity imagingServiceRequestInformationEntity = null;
        //    // check if the imaging service request IE is already in the visit IE children
        //    foreach (ImagingServiceRequestInformationEntity lImagingServiceRequestInformationEntity in visitInformationEntity.Children)
        //    {
        //        if (lImagingServiceRequestInformationEntity.IsFoundIn(dicomFile.DataSet))
        //        {
        //            imagingServiceRequestInformationEntity = lImagingServiceRequestInformationEntity;
        //            break;
        //        }
        //    }
        //    // imaging service request IE is not already in the visit IE children
        //    if (imagingServiceRequestInformationEntity == null)
        //    {
        //        // create a new imaging service request IE from the dataset and add to the visit IE children
        //        imagingServiceRequestInformationEntity = new ImagingServiceRequestInformationEntity();
        //        imagingServiceRequestInformationEntity.CopyFrom(dicomFile.DataSet);
        //        visitInformationEntity.AddChild(imagingServiceRequestInformationEntity);
        //    }
        //    // REQUESTED PROCEDURE level
        //    RequestedProcedureInformationEntity requestedProcedureInformationEntity = null;
        //    // check if the requested procedure IE is already in the imaging service request IE children
        //    foreach (RequestedProcedureInformationEntity lRequestedProcedureInformationEntity in imagingServiceRequestInformationEntity.Children)
        //    {
        //        if (lRequestedProcedureInformationEntity.IsFoundIn(dicomFile.DataSet))
        //        {
        //            requestedProcedureInformationEntity = lRequestedProcedureInformationEntity;
        //            break;
        //        }
        //    }
        //    // requested procedure IE is not already in the imaging service request IE children
        //    if (requestedProcedureInformationEntity == null)
        //    {
        //        // create a new requested procedure IE from the dataset and add to the imaging service request IE children
        //        requestedProcedureInformationEntity = new RequestedProcedureInformationEntity();
        //        requestedProcedureInformationEntity.CopyFrom(dicomFile.DataSet);
        //        imagingServiceRequestInformationEntity.AddChild(requestedProcedureInformationEntity);
        //    }
        //    // SCHEDULED PROCEDURE STEP level
        //    ScheduledProcedureStepInformationEntity scheduledProcedureStepInformationEntity = null;
        //    // check if the scheduled procedure step IE is already in the requested procedure IE children
        //    foreach (ScheduledProcedureStepInformationEntity lScheduledProcedureStepInformationEntity in requestedProcedureInformationEntity.Children)
        //    {
        //        if (lScheduledProcedureStepInformationEntity.IsFoundIn(dicomFile.DataSet))
        //        {
        //            scheduledProcedureStepInformationEntity = lScheduledProcedureStepInformationEntity;
        //            break;
        //        }
        //    }
        //    // scheduled procedure step IE is not already in the requested procedure IE children
        //    if (scheduledProcedureStepInformationEntity == null)
        //    {
        //        // create a new scheduled procedure step IE from the dataset and add to the requested procedure IE children
        //        scheduledProcedureStepInformationEntity = new ScheduledProcedureStepInformationEntity();
        //        scheduledProcedureStepInformationEntity.CopyFrom(dicomFile.DataSet);
        //        requestedProcedureInformationEntity.AddChild(scheduledProcedureStepInformationEntity);
        //    }
        //}
        /// <summary>
        /// Query the Information Model using the given Query Dataset.
        /// </summary>
        /// <param name="queryDataset">Query Dataset.</param>
        /// <returns>A collection of zero or more query reponse datasets.</returns>
        public override DataSetCollection QueryInformationModel(DataSet queryDataset)
        {
            DataSetCollection queryResponses = new DataSetCollection();

            BaseInformationEntityList matchingPatients = new BaseInformationEntityList();
            BaseInformationEntityList matchingVisits = new BaseInformationEntityList();
            BaseInformationEntityList matchingImagingServiceRequests = new BaseInformationEntityList();
            BaseInformationEntityList matchingRequestedProcedures = new BaseInformationEntityList();
            BaseInformationEntityList matchingScheduledProcedureSteps = new BaseInformationEntityList();

            SequenceItem queryItem = null;
            TagTypeList queryTagTypeList = new TagTypeList();
            TagTypeList returnTagTypeList = new TagTypeList();
            foreach (DvtkData.Dimse.Attribute attribute in queryDataset)
            {
                // special check for the Scheduled Procedure Step Sequence
                if (attribute.ValueRepresentation == VR.SQ)
                {
                    foreach (SequenceItem s in ((SequenceOfItems)attribute.DicomValue).Sequence)
                    {
                        if (IsSequenceHavingValue(s))
                        {
                            queryTagTypeList.Add(new TagType(attribute.Tag, TagTypeEnum.TagRequired));
                        }
                    }
                }
                // Query attribute must be present with an attribute value
                // - Do not include the Specific Character Set attribute and group length as a query attribute
                else if ((attribute.Length != 0) &&
                        (attribute.Tag != Tag.SPECIFIC_CHARACTER_SET) &&
                        (attribute.Tag.ElementNumber != 0x0000) &&
                        (attribute.ValueRepresentation != VR.SQ))
                {
                    queryTagTypeList.Add(new TagType(attribute.Tag, TagTypeEnum.TagRequired));
                }

                // Add all attributes as return attributes
                returnTagTypeList.Add(new TagType(attribute.Tag, TagTypeEnum.TagOptional));

            }

            // iterate over the Modality Worklist Information Model and save all the matching
            // Scheduled Procedure Steps
            // iterate of all Information Entities
            foreach (PatientInformationEntity patientInformationEntity in Root)
            {
                if (patientInformationEntity.IsFoundIn(queryTagTypeList, queryDataset))

                {
                    Console.WriteLine("-----PatientInformationEntity------");
                    Console.WriteLine(patientInformationEntity.DataSet.Dump("-"));
                    matchingPatients.Add(patientInformationEntity);

                    foreach (VisitInformationEntity visitInformationEntity in patientInformationEntity.Children)
                    {
                        Console.WriteLine("-----VisitInformationEntity------");
                        Console.WriteLine(visitInformationEntity.DataSet.Dump("--"));
                        if (visitInformationEntity.IsFoundIn(queryTagTypeList, queryDataset))
                        {
                            matchingVisits.Add(visitInformationEntity);

                            foreach (ImagingServiceRequestInformationEntity imagingServiceRequestInformationEntity in visitInformationEntity.Children)
                            {
                                Console.WriteLine("-----ImagingServiceRequestInformationEntity------");
                                Console.WriteLine(imagingServiceRequestInformationEntity.DataSet.Dump("---"));
                                if (imagingServiceRequestInformationEntity.IsFoundIn(queryTagTypeList, queryDataset))
                                {
                                    matchingImagingServiceRequests.Add(imagingServiceRequestInformationEntity);

                                    foreach (RequestedProcedureInformationEntity requestedProcedureInformationEntity in imagingServiceRequestInformationEntity.Children)
                                    {
                                        Console.WriteLine("-----RequestedProcedureInformationEntity------");
                                        Console.WriteLine(requestedProcedureInformationEntity.DataSet.Dump("----"));
                                        if (requestedProcedureInformationEntity.IsFoundIn(queryTagTypeList, queryDataset))
                                        {
                                            matchingRequestedProcedures.Add(requestedProcedureInformationEntity);

                                           // if (queryItem != null)
                                            {

                                                foreach (ScheduledProcedureStepInformationEntity scheduledProcedureStepInformationEntity in requestedProcedureInformationEntity.Children)
                                                {
                                                    Console.WriteLine("-----ScheduledProcedureStepInformationEntity------");
                                                    Console.WriteLine(scheduledProcedureStepInformationEntity.DataSet.Dump("------"));
                                                    if (scheduledProcedureStepInformationEntity.IsFoundIn(queryTagTypeList, queryDataset))
                                                    {
                                                        // add the scheduled procedure step to the matched list
                                                        matchingScheduledProcedureSteps.Add(scheduledProcedureStepInformationEntity);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if (matchingScheduledProcedureSteps.Count > 0)
            {
                // we now have a list of all the matching scheduled procedure steps
                foreach (ScheduledProcedureStepInformationEntity matchingScheduledProcedureStepInformationEntity in matchingScheduledProcedureSteps)
                {
                    //SequenceItem responseItem = new SequenceItem();
                    //matchingScheduledProcedureStepInformationEntity.CopyTo(returnTagTypeList, responseItem);

                    //// remove the specific character set from the responseItem - it is only present in the scheduled procedure step as a helper...
                    //DvtkData.Dimse.Attribute specificChararcterSet = responseItem.GetAttribute(Tag.SPECIFIC_CHARACTER_SET);
                    //if (specificChararcterSet != null)
                    //{
                    //    responseItem.Remove(specificChararcterSet);
                    //}

                    //DvtkData.Dimse.Attribute attribute = new DvtkData.Dimse.Attribute(0x00400100, VR.SQ, responseItem);

                    DataSet queryResponse = new DataSet();
                   // queryResponse.Add(attribute);

                    // if the specific character set attribute has been stored in the sps IE - return it in the query response
                    DvtkData.Dimse.Attribute specificCharacterSetAttribute = matchingScheduledProcedureStepInformationEntity.GetSpecificCharacterSet();
                    if (specificCharacterSetAttribute != null)
                    {
                        queryResponse.Add(specificCharacterSetAttribute);
                    }

                    RequestedProcedureInformationEntity matchingRequestedProcedureInformationEntity
                        = (RequestedProcedureInformationEntity)matchingScheduledProcedureStepInformationEntity.Parent;
                    matchingRequestedProcedureInformationEntity.CopyTo(returnTagTypeList, queryResponse);

                    ImagingServiceRequestInformationEntity matchingImagingServiceRequestInformationEntity
                        = (ImagingServiceRequestInformationEntity)matchingRequestedProcedureInformationEntity.Parent;
                    matchingImagingServiceRequestInformationEntity.CopyTo(returnTagTypeList, queryResponse);

                    VisitInformationEntity matchingVisitInformationEntity
                        = (VisitInformationEntity)matchingImagingServiceRequestInformationEntity.Parent;
                    matchingVisitInformationEntity.CopyTo(returnTagTypeList, queryResponse);

                    PatientInformationEntity matchingPatientInformationEntity
                        = (PatientInformationEntity)matchingVisitInformationEntity.Parent;
                    matchingPatientInformationEntity.CopyTo(returnTagTypeList, queryResponse);
                    matchingScheduledProcedureStepInformationEntity.CopyTo(returnTagTypeList, queryResponse);
                    queryResponses.Add(queryResponse);
                }
            }
            //else if (matchingRequestedProcedures.Count > 0)
            //{
            //    // we now have a list of all the matching requested procedures
            //    foreach (RequestedProcedureInformationEntity matchingRequestedProcedureInformationEntity in matchingRequestedProcedures)
            //    {
            //        DataSet queryResponse = new DataSet();

            //        matchingRequestedProcedureInformationEntity.CopyTo(returnTagTypeList, queryResponse);

            //        ImagingServiceRequestInformationEntity matchingImagingServiceRequestInformationEntity
            //            = (ImagingServiceRequestInformationEntity)matchingRequestedProcedureInformationEntity.Parent;
            //        matchingImagingServiceRequestInformationEntity.CopyTo(returnTagTypeList, queryResponse);

            //        VisitInformationEntity matchingVisitInformationEntity
            //            = (VisitInformationEntity)matchingImagingServiceRequestInformationEntity.Parent;
            //        matchingVisitInformationEntity.CopyTo(returnTagTypeList, queryResponse);

            //        PatientInformationEntity matchingPatientInformationEntity
            //            = (PatientInformationEntity)matchingVisitInformationEntity.Parent;
            //        matchingPatientInformationEntity.CopyTo(returnTagTypeList, queryResponse);

            //        queryResponses.Add(queryResponse);
            //    }
            //}
            //else if (matchingImagingServiceRequests.Count > 0)
            //{
            //    // we now have a list of all the matching image service requests
            //    foreach (ImagingServiceRequestInformationEntity matchingImagingServiceRequestInformationEntity in matchingImagingServiceRequests)
            //    {
            //        DataSet queryResponse = new DataSet();

            //        matchingImagingServiceRequestInformationEntity.CopyTo(returnTagTypeList, queryResponse);

            //        VisitInformationEntity matchingVisitInformationEntity
            //            = (VisitInformationEntity)matchingImagingServiceRequestInformationEntity.Parent;
            //        matchingVisitInformationEntity.CopyTo(returnTagTypeList, queryResponse);

            //        PatientInformationEntity matchingPatientInformationEntity
            //            = (PatientInformationEntity)matchingVisitInformationEntity.Parent;
            //        matchingPatientInformationEntity.CopyTo(returnTagTypeList, queryResponse);

            //        queryResponses.Add(queryResponse);
            //    }
            //}
            //else if (matchingVisits.Count > 0)
            //{
            //    // we now have a list of all the matching visits
            //    foreach (VisitInformationEntity matchingVisitInformationEntity in matchingVisits)
            //    {
            //        DataSet queryResponse = new DataSet();

            //        matchingVisitInformationEntity.CopyTo(returnTagTypeList, queryResponse);

            //        PatientInformationEntity matchingPatientInformationEntity
            //            = (PatientInformationEntity)matchingVisitInformationEntity.Parent;
            //        matchingPatientInformationEntity.CopyTo(returnTagTypeList, queryResponse);

            //        queryResponses.Add(queryResponse);
            //    }
            //}
            //else if (matchingPatients.Count > 0)
            //{
            //    // we now have a list of all the matching patients
            //    foreach (PatientInformationEntity matchingPatientInformationEntity in matchingPatients)
            //    {
            //        DataSet queryResponse = new DataSet();

            //        matchingPatientInformationEntity.CopyTo(returnTagTypeList, queryResponse);

            //        queryResponses.Add(queryResponse);
            //    }
            //}

            return queryResponses;
        }
        /// <summary>
        /// Query the Information Model using the given Query Dataset.
        /// </summary>
        /// <param name="queryDataset">Query Dataset.</param>
        /// <returns>A collection of zero or more query reponse datasets.</returns>
        public override DataSetCollection QueryInformationModel(DataSet queryDataset)
        {
            DataSetCollection queryResponses = new DataSetCollection();

            // get the query/retrieve level
            String queryRetrieveLevel = "UNKNOWN";
            DvtkData.Dimse.Attribute queryRetrieveLevelAttribute = queryDataset.GetAttribute(Tag.QUERY_RETRIEVE_LEVEL);
            if (queryRetrieveLevelAttribute != null)
            {
                CodeString codeString = (CodeString)queryRetrieveLevelAttribute.DicomValue;
                if (codeString.Values.Count == 1)
                {
                    queryRetrieveLevel = codeString.Values[0].Trim();
                }
            }

            // query at the PATIENT level
            if (queryRetrieveLevel == "PATIENT")
            {
                TagTypeList queryTagTypeList = new TagTypeList();
                TagTypeList returnTagTypeList = new TagTypeList();
                foreach (DvtkData.Dimse.Attribute attribute in queryDataset)
                {
                    if (attribute.ValueRepresentation == VR.SQ)
                    {
                        foreach (SequenceItem s in ((SequenceOfItems)attribute.DicomValue).Sequence)
                        {
                            if (IsSequenceHavingValue(s))
                            {
                                queryTagTypeList.Add(new TagType(attribute.Tag, TagTypeEnum.TagRequired));
                            }
                        }
                    }
                    else if ((attribute.Length != 0) &&
                        (attribute.Tag.ElementNumber != 0x0000))
                    {
                        queryTagTypeList.Add(new TagType(attribute.Tag, TagTypeEnum.TagRequired));
                    }
                    if (attribute.Tag != Tag.SPECIFIC_CHARACTER_SET)
                        returnTagTypeList.Add(new TagType(attribute.Tag, TagTypeEnum.TagOptional));
                }

                foreach (PatientInformationEntity patientInformationEntity in Root)
                {
                    if (patientInformationEntity.IsFoundIn(queryTagTypeList, queryDataset))
                    {
                        // PATIENT level matches
                        DataSet queryResponse = new DataSet();

                        // if the specific character set attribute has been stored in the patient IE - return it in the query response
                        DvtkData.Dimse.Attribute specificCharacterSetAttribute = patientInformationEntity.GetSpecificCharacterSet();
                        if (specificCharacterSetAttribute != null)
                        {
                            queryResponse.Add(specificCharacterSetAttribute);
                        }

                        patientInformationEntity.CopyTo(returnTagTypeList, queryResponse);
                        patientInformationEntity.CopyAdditionalAttributes(queryResponse);
                        queryResponses.Add(queryResponse);
                    }
                }
            }
            else
            {
                // find the matching PATIENT
                PatientInformationEntity patientInformationEntity = null;
                foreach (PatientInformationEntity lPatientInformationEntity in Root)
                {
                    if (lPatientInformationEntity.IsUniqueTagFoundIn(queryDataset))
                    {
                        patientInformationEntity = lPatientInformationEntity;
                        break;
                    }
                }
                if (patientInformationEntity != null)
                {
                    // query at the STUDY level
                    if (queryRetrieveLevel == "STUDY")
                    {
                        TagTypeList queryTagTypeList = new TagTypeList();
                        TagTypeList returnTagTypeList = new TagTypeList();
                        foreach (DvtkData.Dimse.Attribute attribute in queryDataset)
                        {
                            // do not add higher level tag
                            if (attribute.Tag == Tag.PATIENT_ID) continue;

                            if (attribute.ValueRepresentation == VR.SQ)
                            {
                                foreach (SequenceItem s in ((SequenceOfItems)attribute.DicomValue).Sequence)
                                {
                                    if (IsSequenceHavingValue(s))
                                    {
                                        queryTagTypeList.Add(new TagType(attribute.Tag, TagTypeEnum.TagRequired));
                                    }
                                }
                            }
                            else if ((attribute.Length != 0) &&
                                (attribute.Tag.ElementNumber != 0x0000))
                            {
                                queryTagTypeList.Add(new TagType(attribute.Tag, TagTypeEnum.TagRequired));
                            }
                            returnTagTypeList.Add(new TagType(attribute.Tag, TagTypeEnum.TagOptional));
                        }

                        foreach (StudyInformationEntity studyInformationEntity in patientInformationEntity.Children)
                        {
                            if (studyInformationEntity.IsFoundIn(queryTagTypeList, queryDataset))
                            {
                                // STUDY level matches
                                DataSet queryResponse = new DataSet();

                                // if the specific character set attribute has been stored in the study IE - return it in the query response
                                DvtkData.Dimse.Attribute specificCharacterSetAttribute = studyInformationEntity.GetSpecificCharacterSet();
                                if (specificCharacterSetAttribute != null)
                                {
                                    queryResponse.Add(specificCharacterSetAttribute);
                                }

                                patientInformationEntity.CopyUniqueTagTo(queryResponse);
                                studyInformationEntity.CopyTo(returnTagTypeList, queryResponse);
                                studyInformationEntity.CopyAdditionalAttributes(queryResponse);
                                queryResponses.Add(queryResponse);
                            }
                        }
                    }
                    else
                    {
                        // find the matching STUDY
                        StudyInformationEntity studyInformationEntity = null;
                        foreach (StudyInformationEntity lStudyInformationEntity in patientInformationEntity.Children)
                        {
                            if (lStudyInformationEntity.IsUniqueTagFoundIn(queryDataset))
                            {
                                studyInformationEntity = lStudyInformationEntity;
                                break;
                            }
                        }

                        if (studyInformationEntity != null)
                        {
                            // query at the SERIES level
                            if (queryRetrieveLevel == "SERIES")
                            {
                                TagTypeList queryTagTypeList = new TagTypeList();
                                TagTypeList returnTagTypeList = new TagTypeList();
                                foreach (DvtkData.Dimse.Attribute attribute in queryDataset)
                                {
                                    // do not add higher level tags
                                    if ((attribute.Tag == Tag.PATIENT_ID) ||
                                        (attribute.Tag == Tag.STUDY_INSTANCE_UID)) continue;

                                    if (attribute.ValueRepresentation == VR.SQ)
                                    {
                                        foreach (SequenceItem s in ((SequenceOfItems)attribute.DicomValue).Sequence)
                                        {
                                            if (IsSequenceHavingValue(s))
                                            {
                                                queryTagTypeList.Add(new TagType(attribute.Tag, TagTypeEnum.TagRequired));
                                            }
                                        }
                                    }
                                    else if ((attribute.Length != 0) &&
                                        (attribute.Tag.ElementNumber != 0x0000))
                                    {
                                        queryTagTypeList.Add(new TagType(attribute.Tag, TagTypeEnum.TagRequired));
                                    }
                                    returnTagTypeList.Add(new TagType(attribute.Tag, TagTypeEnum.TagOptional));
                                }

                                foreach (SeriesInformationEntity seriesInformationEntity in studyInformationEntity.Children)
                                {
                                    if (seriesInformationEntity.IsFoundIn(queryTagTypeList, queryDataset))
                                    {
                                        // SERIES level matches
                                        DataSet queryResponse = new DataSet();

                                        // if the specific character set attribute has been stored in the series IE - return it in the query response
                                        DvtkData.Dimse.Attribute specificCharacterSetAttribute = seriesInformationEntity.GetSpecificCharacterSet();
                                        if (specificCharacterSetAttribute != null)
                                        {
                                            queryResponse.Add(specificCharacterSetAttribute);
                                        }

                                        patientInformationEntity.CopyUniqueTagTo(queryResponse);
                                        studyInformationEntity.CopyUniqueTagTo(queryResponse);
                                        seriesInformationEntity.CopyTo(returnTagTypeList, queryResponse);
                                        seriesInformationEntity.CopyAdditionalAttributes(queryResponse);
                                        queryResponses.Add(queryResponse);
                                    }
                                }
                            }
                            else
                            {
                                // find the matching SERIES
                                SeriesInformationEntity seriesInformationEntity = null;
                                foreach (SeriesInformationEntity lSeriesInformationEntity in studyInformationEntity.Children)
                                {
                                    if (lSeriesInformationEntity.IsUniqueTagFoundIn(queryDataset))
                                    {
                                        seriesInformationEntity = lSeriesInformationEntity;
                                        break;
                                    }
                                }

                                if (seriesInformationEntity != null)
                                {
                                    // query at the IMAGE level
                                    if (queryRetrieveLevel == "IMAGE")
                                    {
                                        TagTypeList queryTagTypeList = new TagTypeList();
                                        TagTypeList returnTagTypeList = new TagTypeList();
                                        foreach (DvtkData.Dimse.Attribute attribute in queryDataset)
                                        {
                                            // do not add higher level tags
                                            if ((attribute.Tag == Tag.PATIENT_ID) ||
                                                (attribute.Tag == Tag.STUDY_INSTANCE_UID) ||
                                                (attribute.Tag == Tag.SERIES_INSTANCE_UID)) continue;

                                            if (attribute.ValueRepresentation == VR.SQ)
                                            {
                                                foreach (SequenceItem s in ((SequenceOfItems)attribute.DicomValue).Sequence)
                                                {
                                                    if (IsSequenceHavingValue(s))
                                                    {
                                                        queryTagTypeList.Add(new TagType(attribute.Tag, TagTypeEnum.TagRequired));
                                                    }
                                                }
                                            }
                                            else if ((attribute.Length != 0) &&
                                                (attribute.Tag.ElementNumber != 0x0000))
                                            {
                                                queryTagTypeList.Add(new TagType(attribute.Tag, TagTypeEnum.TagRequired));
                                            }
                                            returnTagTypeList.Add(new TagType(attribute.Tag, TagTypeEnum.TagOptional));
                                        }

                                        foreach (InstanceInformationEntity instanceInformationEntity in seriesInformationEntity.Children)
                                        {
                                            if (instanceInformationEntity.IsFoundIn(queryTagTypeList, queryDataset))
                                            {
                                                // IMAGE level matches
                                                DataSet queryResponse = new DataSet();

                                                // if the specific character set attribute has been stored in the instance IE - return it in the query response
                                                DvtkData.Dimse.Attribute specificCharacterSetAttribute = instanceInformationEntity.GetSpecificCharacterSet();
                                                if (specificCharacterSetAttribute != null)
                                                {
                                                    queryResponse.Add(specificCharacterSetAttribute);
                                                }

                                                patientInformationEntity.CopyUniqueTagTo(queryResponse);
                                                studyInformationEntity.CopyUniqueTagTo(queryResponse);
                                                seriesInformationEntity.CopyUniqueTagTo(queryResponse);
                                                instanceInformationEntity.CopyTo(returnTagTypeList, queryResponse);
                                                instanceInformationEntity.CopyAdditionalAttributes(queryResponse);
                                                queryResponses.Add(queryResponse);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return queryResponses;
        }
        /// <summary>
        /// Check if the given instance is present in the Information Model. The instance will be at the leaf nodes of the Information Model.
        /// </summary>
        /// <param name="sopClassUid">SOP Class UID to search for.</param>
        /// <param name="sopInstanceUid">SOP Instance UID to search for.</param>
        /// <returns>Boolean - true if instance found in the Information Model, otherwise false.</returns>
        public bool IsInstanceInInformationModel(System.String sopClassUid, System.String sopInstanceUid)
        {
            bool isInstanceInInformationModel = false;

            // set up the commit tag list for comparing with the leaf
            TagTypeList commitTagTypeList = new TagTypeList();
            commitTagTypeList.Add(new TagType(Tag.SOP_INSTANCE_UID, TagTypeEnum.TagRequired));
            commitTagTypeList.Add(new TagType(Tag.SOP_CLASS_UID, TagTypeEnum.TagRequired));

            // set up the commit dataset
            DvtkData.Dimse.DataSet commitDataset = new DvtkData.Dimse.DataSet();
            DvtkData.Dimse.Attribute attribute = new DvtkData.Dimse.Attribute(0x00080016, DvtkData.Dimse.VR.UI, sopClassUid);
            commitDataset.Add(attribute);
            attribute = new DvtkData.Dimse.Attribute(0x00080018, DvtkData.Dimse.VR.UI, sopInstanceUid);
            commitDataset.Add(attribute);

            // iterate over the whole information model - we are interested in the leaf nodes
            foreach (PatientInformationEntity patientInformationEntity in Root)
            {
                foreach (StudyInformationEntity studyInformationEntity in patientInformationEntity.Children)
                {
                    foreach (SeriesInformationEntity seriesInformationEntity in studyInformationEntity.Children)
                    {
                        foreach (InstanceInformationEntity instanceInformationEntity in seriesInformationEntity.Children)
                        {
                            if (instanceInformationEntity.IsFoundIn(commitTagTypeList, commitDataset))
                            {
                                // an instance has been found with the matching commit uids
                                isInstanceInInformationModel = true;
                                break;
                            }
                        }
                    }
                }
            }

            return isInstanceInInformationModel;
        }
Exemple #5
0
        /// <summary>
        /// Cretae a storage commitment event.
        /// </summary>
        /// <param name="informationModels">The information models.</param>
        /// <param name="actionMessage">The action message.</param>
        /// <returns>The created event.</returns>
        public static DvtkHighLevelInterface.Dicom.Messages.DicomMessage MakeStorageCommitEvent(QueryRetrieveInformationModels informationModels, DvtkHighLevelInterface.Dicom.Messages.DicomMessage actionMessage)
        {
            // refresh the information models
            informationModels.Refresh();

            DvtkHighLevelInterface.Dicom.Messages.DicomMessage eventMessage = new DvtkHighLevelInterface.Dicom.Messages.DicomMessage(DvtkData.Dimse.DimseCommand.NEVENTREPORTRQ);
            eventMessage.Set("0x00000002", VR.UI, "1.2.840.10008.1.20.1");
            eventMessage.Set("0x00001000", VR.UI, "1.2.840.10008.1.20.1.1");

            DvtkData.Dimse.DataSet actionDataset = actionMessage.DataSet.DvtkDataDataSet;

            DvtkData.Dimse.DataSet eventDataset = new DvtkData.Dimse.DataSet();

            DvtkData.Dimse.Attribute eventReferenceSopSequence = new DvtkData.Dimse.Attribute(0x00081199, DvtkData.Dimse.VR.SQ);
            SequenceOfItems eventReferenceSopSequenceOfItems = new SequenceOfItems();
            eventReferenceSopSequence.DicomValue = eventReferenceSopSequenceOfItems;

            DvtkData.Dimse.Attribute eventFailedSopSequence = new DvtkData.Dimse.Attribute(0x00081198, DvtkData.Dimse.VR.SQ);
            SequenceOfItems eventFailedSopSequenceOfItems = new SequenceOfItems();
            eventFailedSopSequence.DicomValue = eventFailedSopSequenceOfItems;

            if (actionDataset != null)
            {
                DvtkData.Dimse.Attribute transactionUid = actionDataset.GetAttribute(DvtkData.Dimse.Tag.TRANSACTION_UID);
                if (transactionUid != null)
                {
                    eventDataset.Add(transactionUid);
                }

                DvtkData.Dimse.Attribute referencedSopSequence = actionDataset.GetAttribute(DvtkData.Dimse.Tag.REFERENCED_SOP_SEQUENCE);
                if (referencedSopSequence != null)
                {
                    SequenceOfItems sequenceOfItems = (SequenceOfItems)referencedSopSequence.DicomValue;
                    foreach(DvtkData.Dimse.SequenceItem item in sequenceOfItems.Sequence)
                    {
                        System.String sopClassUid = "";
                        System.String sopInstanceUid = "";

                        DvtkData.Dimse.Attribute attribute = item.GetAttribute(DvtkData.Dimse.Tag.REFERENCED_SOP_CLASS_UID);
                        if (attribute != null)
                        {
                            UniqueIdentifier uniqueIdentifier = (UniqueIdentifier)attribute.DicomValue;
                            if (uniqueIdentifier.Values.Count > 0)
                            {
                                sopClassUid = uniqueIdentifier.Values[0];
                            }
                        }

                        attribute = item.GetAttribute(DvtkData.Dimse.Tag.REFERENCED_SOP_INSTANCE_UID);
                        if (attribute != null)
                        {
                            UniqueIdentifier uniqueIdentifier = (UniqueIdentifier)attribute.DicomValue;
                            if (uniqueIdentifier.Values.Count > 0)
                            {
                                sopInstanceUid = uniqueIdentifier.Values[0];
                            }
                        }

                        if (informationModels.PatientRoot.IsInstanceInInformationModel(sopClassUid, sopInstanceUid))
                        {
                            DvtkData.Dimse.SequenceItem itemOk = new DvtkData.Dimse.SequenceItem();
                            itemOk.AddAttribute(0x00081150, DvtkData.Dimse.VR.UI, sopClassUid);
                            itemOk.AddAttribute(0x00081155, DvtkData.Dimse.VR.UI, sopInstanceUid);

                            // add instance to committed list
                            eventReferenceSopSequenceOfItems.Sequence.Add(itemOk);
                        }
                        else
                        {
                            DvtkData.Dimse.SequenceItem itemNotOk = new DvtkData.Dimse.SequenceItem();
                            itemNotOk.AddAttribute(0x00081150, DvtkData.Dimse.VR.UI, sopClassUid);
                            itemNotOk.AddAttribute(0x00081155, DvtkData.Dimse.VR.UI, sopInstanceUid);
                            itemNotOk.AddAttribute(0x00081197, DvtkData.Dimse.VR.US, 0x0110);

                            // add instance to failed list
                            eventFailedSopSequenceOfItems.Sequence.Add(itemNotOk);
                        }
                    }
                }

                if (eventReferenceSopSequenceOfItems.Sequence.Count > 0)
                {
                    eventMessage.Set("0x00001002", VR.US, 1);
                    eventDataset.Add(eventReferenceSopSequence);
                }

                if (eventFailedSopSequenceOfItems.Sequence.Count > 0)
                {
                    eventMessage.Set("0x00001002", VR.US, 2);
                    eventDataset.Add(eventFailedSopSequence);
                }
            }

            eventMessage.DataSet.DvtkDataDataSet = eventDataset;

            return eventMessage;
        }
        /// <summary>
        /// Query the Information Model using the given Query Dataset.
        /// </summary>
        /// <param name="queryDataset">Query Dataset.</param>
        /// <returns>A collection of zero or more query reponse datasets.</returns>
        public override DataSetCollection QueryInformationModel(DataSet queryDataset)
        {
            DataSetCollection queryResponses = new DataSetCollection();

            BaseInformationEntityList matchingScheduledProcedureSteps = new BaseInformationEntityList();

            SequenceItem queryItem = null;
            TagTypeList queryTagTypeList = new TagTypeList();
            TagTypeList returnTagTypeList = new TagTypeList();
            foreach (DvtkData.Dimse.Attribute attribute in queryDataset)
            {
                // special check for the Scheduled Procedure Step Sequence
                if (attribute.Tag == Tag.SCHEDULED_PROCEDURE_STEP_SEQUENCE)
                {
                    SequenceOfItems sequenceOfItems = (SequenceOfItems)attribute.DicomValue;
                    if (sequenceOfItems.Sequence.Count == 1)
                    {
                        queryItem = sequenceOfItems.Sequence[0];

                        foreach (DvtkData.Dimse.Attribute itemAttribute in queryItem)
                        {
                            if (itemAttribute.Length != 0)
                            {
                                queryTagTypeList.Add(new TagType(itemAttribute.Tag, TagTypeEnum.TagRequired));
                            }
                            returnTagTypeList.Add(new TagType(itemAttribute.Tag, TagTypeEnum.TagOptional));
                        }
                    }
                }
                else
                {
                    if (attribute.Length != 0)
                    {
                        queryTagTypeList.Add(new TagType(attribute.Tag, TagTypeEnum.TagRequired));
                    }
                    returnTagTypeList.Add(new TagType(attribute.Tag, TagTypeEnum.TagOptional));
                }
            }

            // iterate over the Modality Worklist Information Model and save all the matching
            // Scheduled Procedure Steps
            // iterate of all Information Entities
            foreach (PatientInformationEntity patientInformationEntity in Root)
            {
                if ((patientInformationEntity.IsFoundIn(queryTagTypeList, queryDataset)) ||
                    (patientInformationEntity.UniversalMatch(queryTagTypeList)))
                {
                    foreach (VisitInformationEntity visitInformationEntity in patientInformationEntity.Children)
                    {
                        if ((visitInformationEntity.IsFoundIn(queryTagTypeList, queryDataset)) ||
                            (visitInformationEntity.UniversalMatch(queryTagTypeList)))
                        {
                            foreach (ImagingServiceRequestInformationEntity imagingServiceRequestInformationEntity in visitInformationEntity.Children)
                            {
                                if ((imagingServiceRequestInformationEntity.IsFoundIn(queryTagTypeList, queryDataset)) ||
                                    (imagingServiceRequestInformationEntity.UniversalMatch(queryTagTypeList)))
                                {
                                    foreach (RequestedProcedureInformationEntity requestedProcedureInformationEntity in imagingServiceRequestInformationEntity.Children)
                                    {
                                        if ((requestedProcedureInformationEntity.IsFoundIn(queryTagTypeList, queryDataset)) ||
                                            (requestedProcedureInformationEntity.UniversalMatch(queryTagTypeList)))
                                        {
                                            foreach (ScheduledProcedureStepInformationEntity scheduledProcedureStepInformationEntity in requestedProcedureInformationEntity.Children)
                                            {
                                                if (scheduledProcedureStepInformationEntity.IsFoundIn(queryTagTypeList, queryItem))
                                                {
                                                    // add the scheduled procedure step to the matched list
                                                    matchingScheduledProcedureSteps.Add(scheduledProcedureStepInformationEntity);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            // we now have a list of all the matching scheduled procedure steps
            foreach (ScheduledProcedureStepInformationEntity matchingScheduledProcedureStepInformationEntity in matchingScheduledProcedureSteps)
            {
                SequenceItem responseItem = new SequenceItem();
                matchingScheduledProcedureStepInformationEntity.CopyTo(returnTagTypeList, responseItem);
                DvtkData.Dimse.Attribute attribute = new DvtkData.Dimse.Attribute(0x00400100, VR.SQ, responseItem);

                DataSet queryResponse = new DataSet();
                queryResponse.Add(attribute);

                RequestedProcedureInformationEntity matchingRequestedProcedureInformationEntity
                    = (RequestedProcedureInformationEntity)matchingScheduledProcedureStepInformationEntity.Parent;
                matchingRequestedProcedureInformationEntity.CopyTo(returnTagTypeList, queryResponse);

                ImagingServiceRequestInformationEntity matchingImagingServiceRequestInformationEntity
                    = (ImagingServiceRequestInformationEntity)matchingRequestedProcedureInformationEntity.Parent;
                matchingImagingServiceRequestInformationEntity.CopyTo(returnTagTypeList, queryResponse);

                VisitInformationEntity matchingVisitInformationEntity
                    = (VisitInformationEntity)matchingImagingServiceRequestInformationEntity.Parent;
                matchingVisitInformationEntity.CopyTo(returnTagTypeList, queryResponse);

                PatientInformationEntity matchingPatientInformationEntity
                    = (PatientInformationEntity)matchingVisitInformationEntity.Parent;
                matchingPatientInformationEntity.CopyTo(returnTagTypeList, queryResponse);

                queryResponses.Add(queryResponse);
            }

            return queryResponses;
        }
        public static DvtkHighLevelInterface.Messages.DicomMessage MakeStorageCommitEvent(QueryRetrieveInformationModels informationModels, DvtkHighLevelInterface.Messages.DicomMessage actionMessage)
        {
            // refresh the information models
            informationModels.Refresh();

            DvtkHighLevelInterface.Messages.DicomMessage eventMessage = new DvtkHighLevelInterface.Messages.DicomMessage(DvtkData.Dimse.DimseCommand.NEVENTREPORTRQ);
            eventMessage.Set("0x00000002", DvtkData.Dimse.VR.UI, "1.2.840.10008.1.20.1");
            eventMessage.Set("0x00001000", DvtkData.Dimse.VR.UI, "1.2.840.10008.1.20.1.1");
            eventMessage.Set("0x00001002", DvtkData.Dimse.VR.US, 1);

            DvtkData.Dimse.DataSet actionDataset = actionMessage.DataSet.DvtkDataDataSet;

            DvtkData.Dimse.DataSet eventDataset = new DvtkData.Dimse.DataSet();

            DvtkData.Dimse.Attribute eventReferenceSopSequence        = new DvtkData.Dimse.Attribute(0x00081199, DvtkData.Dimse.VR.SQ);
            SequenceOfItems          eventReferenceSopSequenceOfItems = new SequenceOfItems();

            eventReferenceSopSequence.DicomValue = eventReferenceSopSequenceOfItems;

            DvtkData.Dimse.Attribute eventFailedSopSequence        = new DvtkData.Dimse.Attribute(0x00081198, DvtkData.Dimse.VR.SQ);
            SequenceOfItems          eventFailedSopSequenceOfItems = new SequenceOfItems();

            eventFailedSopSequence.DicomValue = eventFailedSopSequenceOfItems;

            if (actionDataset != null)
            {
                DvtkData.Dimse.Attribute transactionUid = actionDataset.GetAttribute(DvtkData.Dimse.Tag.TRANSACTION_UID);
                if (transactionUid != null)
                {
                    eventDataset.Add(transactionUid);
                }

                DvtkData.Dimse.Attribute referencedSopSequence = actionDataset.GetAttribute(DvtkData.Dimse.Tag.REFERENCED_SOP_SEQUENCE);
                if (referencedSopSequence != null)
                {
                    SequenceOfItems sequenceOfItems = (SequenceOfItems)referencedSopSequence.DicomValue;
                    foreach (DvtkData.Dimse.SequenceItem item in sequenceOfItems.Sequence)
                    {
                        System.String sopClassUid    = "";
                        System.String sopInstanceUid = "";

                        DvtkData.Dimse.Attribute attribute = item.GetAttribute(DvtkData.Dimse.Tag.REFERENCED_SOP_CLASS_UID);
                        if (attribute != null)
                        {
                            UniqueIdentifier uniqueIdentifier = (UniqueIdentifier)attribute.DicomValue;
                            sopClassUid = uniqueIdentifier.Values[0];
                        }

                        attribute = item.GetAttribute(DvtkData.Dimse.Tag.REFERENCED_SOP_INSTANCE_UID);
                        if (attribute != null)
                        {
                            UniqueIdentifier uniqueIdentifier = (UniqueIdentifier)attribute.DicomValue;
                            sopInstanceUid = uniqueIdentifier.Values[0];
                        }

                        if (informationModels.PatientRoot.IsInstanceInInformationModel(sopClassUid, sopInstanceUid))
                        {
                            DvtkData.Dimse.SequenceItem itemOk = new DvtkData.Dimse.SequenceItem();
                            itemOk.AddAttribute(0x00081150, DvtkData.Dimse.VR.UI, sopClassUid);
                            itemOk.AddAttribute(0x00081155, DvtkData.Dimse.VR.UI, sopInstanceUid);

                            // add instance to committed list
                            eventReferenceSopSequenceOfItems.Sequence.Add(itemOk);
                        }
                        else
                        {
                            DvtkData.Dimse.SequenceItem itemNotOk = new DvtkData.Dimse.SequenceItem();
                            itemNotOk.AddAttribute(0x00081150, DvtkData.Dimse.VR.UI, sopClassUid);
                            itemNotOk.AddAttribute(0x00081155, DvtkData.Dimse.VR.UI, sopInstanceUid);
                            itemNotOk.AddAttribute(0x00081197, DvtkData.Dimse.VR.US, 0x0110);

                            // add instance to failed list
                            eventFailedSopSequenceOfItems.Sequence.Add(itemNotOk);
                        }
                    }
                }

                if (eventReferenceSopSequenceOfItems.Sequence.Count > 0)
                {
                    eventDataset.Add(eventReferenceSopSequence);
                }

                if (eventFailedSopSequenceOfItems.Sequence.Count > 0)
                {
                    eventDataset.Add(eventFailedSopSequence);
                }
            }

            eventMessage.DataSet.DvtkDataDataSet = eventDataset;

            return(eventMessage);
        }