/// <summary>
        /// Patient merge request - update modality worklist information model.
        /// </summary>
        /// <param name="dataset">Dataset containing patient merge attributes.</param>
        public override void PatientMerge(DvtkData.Dimse.DataSet dataset)
        {
            // Get the merge patient - need to use this to search for the corresponding patient IE
            DvtkData.Dimse.Attribute mergePatientId = dataset.GetAttribute(Tag.OTHER_PATIENT_IDS);
            if (mergePatientId.Length != 0)
            {
                LongString    longString     = (LongString)mergePatientId.DicomValue;
                System.String attributeValue = longString.Values[0];

                DataSet queryDataset = new DataSet("Transient");
                queryDataset.AddAttribute(Tag.PATIENT_ID.GroupNumber, Tag.PATIENT_ID.ElementNumber, VR.LO, attributeValue);

                // check each patient IE in the patientRootList
                foreach (PatientInformationEntity lPatientInformationEntity in Root)
                {
                    // if IE matches the unique (merge) patient id field in the query dataset
                    if (lPatientInformationEntity.IsUniqueTagFoundIn(queryDataset))
                    {
                        // update the patient demographics - including the patient id
                        lPatientInformationEntity.CopyFrom(dataset);
                        break;
                    }
                }
            }
        }
Пример #2
0
        private bool LoadTemplate(DvtkData.Dimse.DataSet dataset)
        {
            if (dataset == null)
            {
                return(false);
            }

            // try to find the template tag in the dataset
            foreach (DicomComparisonTag comparisonTag in this._template.ComparisonTags)
            {
                DvtkData.Dimse.Tag tag = comparisonTag.Tag;
                DvtkData.Dimse.Tag parentSequenceTag = comparisonTag.ParentSequenceTag;
                System.String      attributeValue    = System.String.Empty;

                if (parentSequenceTag != Tag.UNDEFINED)
                {
                    DvtkData.Dimse.Attribute sequenceAttribute = dataset.GetAttribute(parentSequenceTag);
                    if ((sequenceAttribute != null) &&
                        (sequenceAttribute.ValueRepresentation == DvtkData.Dimse.VR.SQ))
                    {
                        SequenceOfItems sequenceOfItems = (SequenceOfItems)sequenceAttribute.DicomValue;
                        if (sequenceOfItems.Sequence.Count == 1)
                        {
                            SequenceItem item = sequenceOfItems.Sequence[0];

                            if (item != null)
                            {
                                DvtkData.Dimse.Attribute attribute = item.GetAttribute(tag);
                                attributeValue = GetAttributeValue(attribute);
                            }
                        }
                    }
                }
                else
                {
                    DvtkData.Dimse.Attribute attribute = dataset.GetAttribute(tag);
                    attributeValue = GetAttributeValue(attribute);
                }

                if (attributeValue != System.String.Empty)
                {
                    comparisonTag.DataFormat.FromDicomFormat(attributeValue);
                }
            }

            return(true);
        }
Пример #3
0
 protected override void LoadData(DvtkData.Dimse.DataSet _dataSet)
 {
     base.LoadData(_dataSet);
     patientName = BaseCompositeInformationEntity.GetDicomValue(_dataSet.GetAttribute(DvtkData.Dimse.Tag.PATIENTS_NAME));
     patientId   = BaseCompositeInformationEntity.GetDicomValue(_dataSet.GetAttribute(DvtkData.Dimse.Tag.PATIENT_ID));
 }
Пример #4
0
        private static void AddTagsToDataset(TagValueCollection tags, DvtkData.Dimse.DataSet dataset)
        {
            // iterate over the tags
            foreach (DicomTagValue tag in tags)
            {
                if (tag.ParentSequenceTag != Tag.UNDEFINED)
                {
                    // try to get the sequence tag in the dataset
                    DvtkData.Dimse.Attribute sequenceAttribute = dataset.GetAttribute(tag.ParentSequenceTag);
                    if ((sequenceAttribute != null) &&
                        (sequenceAttribute.ValueRepresentation == DvtkData.Dimse.VR.SQ))
                    {
                        SequenceOfItems sequenceOfItems = (SequenceOfItems)sequenceAttribute.DicomValue;
                        if (sequenceOfItems.Sequence.Count == 1)
                        {
                            SequenceItem item = sequenceOfItems.Sequence[0];

                            if (item != null)
                            {
                                VR vr = VR.UN;

                                // try to get the attribute in the item
                                DvtkData.Dimse.Attribute attribute = item.GetAttribute(tag.Tag);
                                if (attribute != null)
                                {
                                    vr = attribute.ValueRepresentation;
                                    item.Remove(attribute);
                                }

                                // add the query value
                                item.AddAttribute(tag.Tag.GroupNumber,
                                                  tag.Tag.ElementNumber,
                                                  vr,
                                                  tag.Value);
                            }
                        }
                    }
                }
                else
                {
                    VR vr = VR.UN;

                    // try to get the attribute in the dataset
                    DvtkData.Dimse.Attribute attribute = dataset.GetAttribute(tag.Tag);
                    if (attribute != null)
                    {
                        vr = attribute.ValueRepresentation;
                        dataset.Remove(attribute);
                    }

                    // special check for the SPECIFIC CHARACTER SET attribute
                    if (tag.Tag == Tag.SPECIFIC_CHARACTER_SET)
                    {
                        vr = VR.CS;
                    }

                    // add the query value
                    dataset.AddAttribute(tag.Tag.GroupNumber,
                                         tag.Tag.ElementNumber,
                                         vr,
                                         tag.Value);
                }
            }
        }
Пример #5
0
        /// <summary>
        /// Send all the images found in the given storage directory.
        /// </summary>
        /// <param name="storageDirectory">Given storage directory - containing storage DCM files.</param>
        /// <param name="modalityWorklistItem">Worklist Item used to provide overruling values for
        /// the Image headers.</param>
        /// <param name="withSingleAssociation">Boolean indicating whether the images should be sent in a single
        /// association or not.</param>
        /// <returns>Boolean indicating success or failure.</returns>
        public bool SendModalityImagesStored(System.String storageDirectory, DicomQueryItem modalityWorklistItem, bool withSingleAssociation)
        {
            if ((storageDirectory.Length == 0) ||
                (modalityWorklistItem == null))
            {
                return(false);
            }

            // Use a hash table to store the instance uid mappings
            // - the instance uids for the study, series and sop are going to be updated for all the datasets
            // read from the storageDirectory
            Hashtable instanceMapper = new Hashtable();

            // Get the directory info for the storage directory - and make sure that it exists
            DirectoryInfo directoryInfo = new DirectoryInfo(storageDirectory);

            if (directoryInfo.Exists == false)
            {
                System.String message = System.String.Format("storageDirectory:\"{0}\" - does not exist.", storageDirectory);
                throw new System.Exception(message);
            }

            // Get a trigger
            DicomTrigger trigger = new DicomTrigger(TransactionNameEnum.RAD_8);

            trigger.HandleInSingleAssociation = withSingleAssociation;

            // Interate over all the DCM files found in the storage directory
            // - update the instances found with the worklist item contents and
            // set up the triggers for the Image Archive
            foreach (FileInfo fileInfo in directoryInfo.GetFiles())
            {
                if ((fileInfo.Extension.ToLower().Equals(".dcm")) ||
                    (fileInfo.Extension == System.String.Empty))
                {
                    // Read the file meta information - it must be present for this actor
                    DvtkData.Media.FileMetaInformation fileMetaInformation = Dvtk.DvtkDataHelper.ReadFMIFromFile(fileInfo.FullName);
                    if (fileMetaInformation == null)
                    {
                        continue;
                    }

                    // Try to get the transfer syntax uid
                    // - start with Implicit VR Little Endian
                    System.String            transferSyntaxUid = "1.2.840.10008.1.2";
                    DvtkData.Dimse.Attribute attribute         = fileMetaInformation.GetAttribute(DvtkData.Dimse.Tag.TRANSFER_SYNTAX_UID);
                    if (attribute != null)
                    {
                        UniqueIdentifier uniqueIdentifier = (UniqueIdentifier)attribute.DicomValue;
                        if (uniqueIdentifier.Values.Count > 0)
                        {
                            transferSyntaxUid = uniqueIdentifier.Values[0];
                        }
                    }

                    // Read the dataset from the DCM file
                    DvtkData.Dimse.DataSet dataset = Dvtk.DvtkDataHelper.ReadDataSetFromFile(fileInfo.FullName);
                    if (dataset == null)
                    {
                        continue;
                    }

                    // Remove any Group Lengths from the dataset
                    dataset.RemoveGroupLengthAttributes();

                    // Try to get the series instance uid
                    System.String            oldSeriesInstanceUid       = System.String.Empty;
                    DvtkData.Dimse.Attribute seriesInstanceUidAttribute = dataset.GetAttribute(Tag.SERIES_INSTANCE_UID);
                    if (seriesInstanceUidAttribute != null)
                    {
                        UniqueIdentifier uniqueIdentifier = (UniqueIdentifier)seriesInstanceUidAttribute.DicomValue;
                        if (uniqueIdentifier.Values.Count > 0)
                        {
                            oldSeriesInstanceUid = uniqueIdentifier.Values[0];
                        }

                        // Remove the old series instance from the dataset
                        dataset.Remove(seriesInstanceUidAttribute);
                    }

                    // See if a mapping exists
                    System.String newSeriesInstanceUid = (System.String)instanceMapper[oldSeriesInstanceUid];
                    if (newSeriesInstanceUid == null)
                    {
                        // Add a mapping
                        DefaultValueManager.UpdateInstantiatedDefaultTagValues(AffectedEntityEnum.SeriesEntity);
                        newSeriesInstanceUid = DefaultValueManager.GetInstantiatedValue(Tag.SERIES_INSTANCE_UID);
                        instanceMapper.Add(oldSeriesInstanceUid, newSeriesInstanceUid);
                    }

                    // Add the new series instance uid to the dataset
                    dataset.AddAttribute(Tag.SERIES_INSTANCE_UID.GroupNumber, Tag.SERIES_INSTANCE_UID.ElementNumber, VR.UI, newSeriesInstanceUid);

                    // Try to get the sop instance uid
                    System.String            oldSopInstanceUid       = System.String.Empty;
                    DvtkData.Dimse.Attribute sopInstanceUidAttribute = dataset.GetAttribute(Tag.SOP_INSTANCE_UID);
                    if (sopInstanceUidAttribute != null)
                    {
                        UniqueIdentifier uniqueIdentifier = (UniqueIdentifier)sopInstanceUidAttribute.DicomValue;
                        if (uniqueIdentifier.Values.Count > 0)
                        {
                            oldSopInstanceUid = uniqueIdentifier.Values[0];
                        }

                        // Remove the old sop instance from the dataset
                        dataset.Remove(sopInstanceUidAttribute);
                    }

                    // See if a mapping exists
                    System.String newSopInstanceUid = (System.String)instanceMapper[oldSopInstanceUid];
                    if (newSopInstanceUid == null)
                    {
                        // Add a mapping
                        DefaultValueManager.UpdateInstantiatedDefaultTagValues(AffectedEntityEnum.InstanceEntity);
                        newSopInstanceUid = DefaultValueManager.GetInstantiatedValue(Tag.SOP_INSTANCE_UID);
                        instanceMapper.Add(oldSopInstanceUid, newSopInstanceUid);
                    }

                    // Add the new sop instance uid to the dataset
                    dataset.AddAttribute(Tag.SOP_INSTANCE_UID.GroupNumber, Tag.SOP_INSTANCE_UID.ElementNumber, VR.UI, newSopInstanceUid);

                    // Use modality Worklist Item to help construct the Storage Message
                    // Get the attribute values to copy
                    DvtkHighLevelInterface.Comparator.Comparator worklistItemComparator = new DvtkHighLevelInterface.Comparator.Comparator("WorklistItemComparator");
                    DicomComparator dicomWorklistItemComparator = worklistItemComparator.InitializeDicomComparator(modalityWorklistItem.DicomMessage);

                    // Also try to use the MPPS InProgress Message to help construct the Storage Message
                    DvtkHighLevelInterface.Comparator.Comparator mppsInProgressComparator = new DvtkHighLevelInterface.Comparator.Comparator("MppsInProgressComparator");
                    DicomComparator dicomMppsInProgressComparator = null;
                    if (_nCreateSetMppsInProgress != null)
                    {
                        dicomMppsInProgressComparator = mppsInProgressComparator.InitializeDicomComparator(_nCreateSetMppsInProgress);
                    }

                    // Create the Storage message
                    DvtkHighLevelInterface.Comparator.Comparator       storageComparator = new DvtkHighLevelInterface.Comparator.Comparator("StorageComparator");
                    DvtkHighLevelInterface.Dicom.Messages.DicomMessage cStoreInstance    = new DvtkHighLevelInterface.Dicom.Messages.DicomMessage(DvtkData.Dimse.DimseCommand.CSTORERQ);
                    GenerateTriggers.MakeCStoreInstance(DefaultValueManager, cStoreInstance, dataset);
                    storageComparator.PopulateDicomMessage(cStoreInstance, dicomWorklistItemComparator);
                    if (dicomMppsInProgressComparator != null)
                    {
                        storageComparator.PopulateDicomMessage(cStoreInstance, dicomMppsInProgressComparator);
                    }

                    // Try to get the sop class uid
                    System.String            sopClassUid          = System.String.Empty;
                    DvtkData.Dimse.Attribute sopClassUidAttribute = dataset.GetAttribute(Tag.SOP_CLASS_UID);
                    if (sopClassUidAttribute != null)
                    {
                        UniqueIdentifier uniqueIdentifier = (UniqueIdentifier)sopClassUidAttribute.DicomValue;
                        if (uniqueIdentifier.Values.Count > 0)
                        {
                            sopClassUid = uniqueIdentifier.Values[0];
                        }
                    }

                    // Add trigger item to the trigger
                    trigger.AddItem(cStoreInstance,
                                    sopClassUid,
                                    transferSyntaxUid);
                }
            }

            // RAD-8 - trigger the ImageArchive
            return(TriggerActorInstances(ActorTypeEnum.ImageArchive, trigger, true));
        }
Пример #6
0
        /// <summary>
        /// Add the default attribute values to the local dataset if the attribute values
        /// are not already present in the local dataset. A zero-length attribute in the local
        /// dataset will be replaced by a value of the same attribute (tag) in the defaultDataset.
        /// All child Entities are also processed.
        /// </summary>
        /// <param name="defaultDataset"></param>
        public void AddDefaultAttributes(DataSet defaultDataset)
        {
            // use tag list to determine which attributes should take default values
            foreach (TagType tagType in _tagTypeList)
            {
                // only add a default value it the attribute is not already in the dataset.
                DvtkData.Dimse.Attribute attribute = _dataset.GetAttribute(tagType.Tag);
                if ((attribute == null) ||
                    (attribute.Length == 0))
                {
                    // check if a default value is available for this tag
                    DvtkData.Dimse.Attribute defaultAttribute = defaultDataset.GetAttribute(tagType.Tag);
                    if (defaultAttribute != null)
                    {
                        // need to remove any zero-length attribute
                        if ((attribute != null) &&
                            (attribute.Length == 0))
                        {
                            _dataset.Remove(attribute);
                        }

                        // add default value to the dataset
                        _dataset.Add(defaultAttribute);
                    }
                }
            }

            // include all children
            foreach (BaseInformationEntity informationEntity in _children)
            {
                informationEntity.AddDefaultAttributes(defaultDataset);
            }
        }
Пример #7
0
        /// <summary>
        /// Use the information entity tagTypeList to add default values from the defaultDataset
        /// into this _dataset.
        /// A default value will be added if:
        /// overWriteExistingValue is true and a default value exists.
        /// or:
        /// overWriteExistingValue is false and the default value exists and this _dataset either does
        /// not contain the default tag or the default tag has a zero-length value.
        /// All child Entities are also processed.
        /// </summary>
        /// <param name="overWriteExistingValue"></param>
        /// <param name="defaultDataset"></param>
        public void AddDefaultAttributes(bool overWriteExistingValue, DataSet defaultDataset)
        {
            // use tag list to determine which attributes should take default values

            foreach (TagType tagType in _tagTypeList)
            {
                // only add a default value it the attribute is not already in the dataset.
                DvtkData.Dimse.Attribute attribute = _dataset.GetAttribute(tagType.Tag);

                // check if a default value is available for this tag
                DvtkData.Dimse.Attribute defaultAttribute = defaultDataset.GetAttribute(tagType.Tag);

                // Only make changes if a default attribute is found
                if (defaultAttribute != null)
                {
                    // overwrite any existing value
                    if (overWriteExistingValue == true)
                    {
                        if (attribute != null && attribute.ValueRepresentation == VR.SQ&&defaultAttribute.ValueRepresentation==VR.SQ)
                        {
                            DvtkData.Dimse.Attribute actualAttrib = attribute;
                            foreach( SequenceItem sourceItem in ((SequenceOfItems)defaultAttribute.DicomValue).Sequence)
                            {
                                for (int i = 0; i < sourceItem.Count; i++)
                                {
                                    DvtkData.Dimse.Attribute sourceAttrib = sourceItem[i];
                                    foreach (SequenceItem destItem in ((SequenceOfItems)attribute.DicomValue).Sequence)
                                    {
                                        DvtkData.Dimse.Attribute desattrib = destItem.GetAttribute(sourceAttrib.Tag);
                                        if (desattrib != null)
                                        {
                                            destItem.Remove(desattrib);
                                        }
                                        destItem.Add(sourceAttrib);
                                    }
                                }
                            }
                            _dataset.Remove(actualAttrib);
                            _dataset.Add(attribute);
                        }
                        else
                        {
                            if (attribute != null)
                            {
                                // remove the old attribute value
                                _dataset.Remove(attribute);
                            }

                            // add default value to the dataset
                            _dataset.Add(defaultAttribute);
                        }
                    }
                    else
                    {
                        // need to remove any zero-length attribute
                        if ((attribute != null) &&
                            (attribute.Length == 0))
                        {
                            _dataset.Remove(attribute);

                            // add default value to the dataset
                            _dataset.Add(defaultAttribute);
                        }
                        else if (attribute == null)
                        {
                            // add default value to the dataset
                            _dataset.Add(defaultAttribute);
                        }
                    }
                }
            }

            // include all children
            foreach (BaseInformationEntity informationEntity in _children)
            {
                informationEntity.AddDefaultAttributes(overWriteExistingValue, defaultDataset);
            }
        }
Пример #8
0
        /// <summary>
        /// Retrieve a list of filenames from the Information Model. The filenames match the
        /// individual instances matching the retrieve dataset attributes.
        /// </summary>
        /// <param name="retrieveDataset">Retrive dataset.</param>
        /// <returns>File list - containing the filenames of all instances matching the retrieve dataset attributes.</returns>
        public DvtkData.Collections.StringCollection RetrieveInformationModel(DataSet retrieveDataset)
        {
            DvtkData.Collections.StringCollection fileList = new DvtkData.Collections.StringCollection();

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

            // Find the matching PATIENT.
            PatientInformationEntity patientInformationEntity = null;
            foreach (PatientInformationEntity lPatientInformationEntity in Root)
            {
                if (lPatientInformationEntity.IsUniqueTagFoundIn(retrieveDataset))
                {
                    patientInformationEntity = lPatientInformationEntity;
                    break;
                }
            }

            if (patientInformationEntity != null)
            {
                // retrieve at the PATIENT level
                if (queryRetrieveLevel == "PATIENT")
                {
                    fileList = patientInformationEntity.FileNames;
                }
                else
                {
                    // Find the matching STUDIES.
                    BaseInformationEntityList studyInformationEntities = patientInformationEntity.ChildrenWithUniqueTagFoundIn(retrieveDataset);

                    if (studyInformationEntities.Count > 0)
                    {
                        // Retrieve at the STUDY level
                        if (queryRetrieveLevel == "STUDY")
                        {
                            foreach (StudyInformationEntity studyInformationEntity in studyInformationEntities)
                            {
                                foreach (String fileName in studyInformationEntity.FileNames)
                                {
                                    fileList.Add(fileName);
                                }
                            }
                        }
                        else
                        {
                            // Find the matching SERIES.
                            BaseInformationEntityList seriesInformationEntities = studyInformationEntities[0].ChildrenWithUniqueTagFoundIn(retrieveDataset);

                            if (seriesInformationEntities.Count > 0)
                            {
                                // retrieve at the SERIES level
                                if (queryRetrieveLevel == "SERIES")
                                {
                                    foreach (SeriesInformationEntity seriesInformationEntity in seriesInformationEntities)
                                    {
                                        foreach (String fileName in seriesInformationEntity.FileNames)
                                        {
                                            fileList.Add(fileName);
                                        }
                                    }
                                }
                                else
                                {
                                    // Find the matching IMAGE
                                    BaseInformationEntityList instanceInformationEntities = seriesInformationEntities[0].ChildrenWithUniqueTagFoundIn(retrieveDataset);

                                    // retrieve at the IMAGE level
                                    if ((instanceInformationEntities.Count > 0) &&
                                        (queryRetrieveLevel == "IMAGE"))
                                    {
                                        foreach (InstanceInformationEntity instanceInformationEntity in instanceInformationEntities)
                                        {
                                            if (instanceInformationEntity.Filename != null)
                                                fileList.Add(instanceInformationEntity.Filename);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return fileList;
        }
Пример #9
0
        /// <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>
        /// 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 = null;

            // 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.Length != 0)
                    {
                        queryTagTypeList.Add(new TagType(attribute.Tag, TagTypeEnum.TagRequired));
                    }
                    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();
                        patientInformationEntity.CopyTo(returnTagTypeList, 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.Length != 0)
                            {
                                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();
                                patientInformationEntity.CopyUniqueTagTo(queryResponse);
                                studyInformationEntity.CopyTo(returnTagTypeList, queryResponse);
                                queryResponses.Add(queryResponse);
                            }
                        }
                    }
                }
            }

            return queryResponses;
        }
Пример #11
0
        private bool CopyToDicomMessage(DvtkData.Dimse.DicomMessage dicomMessage, DicomComparator sourceComparator)
        {
            bool messagePopulated = true;

            // Check if both templates have been initialized correctly
            if ((this.Template == null) ||
                (sourceComparator.Template == null))
            {
                return(false);
            }

            // Iterate over this comparator
            foreach (DicomComparisonTag thisComparisonTag in this.Template.ComparisonTags)
            {
                // try to get the equivalent tag in the sourceComparator
                DicomComparisonTag sourceComparisonTag = sourceComparator.Template.ComparisonTags.Find(thisComparisonTag.Tag);
                if (sourceComparisonTag != null)
                {
                    System.String          stringValue = sourceComparisonTag.DataFormat.ToDicomFormat();
                    DvtkData.Dimse.DataSet dataset     = dicomMessage.DataSet;
                    if (dataset != null)
                    {
                        // we need to see if the parent sequence has been set up in the dataset
                        if (thisComparisonTag.ParentSequenceTag != Tag.UNDEFINED)
                        {
                            // set up the parent sequence and add it to the dataset
                            SequenceOfItems          sequenceOfItems   = null;
                            DvtkData.Dimse.Attribute sequenceAttribute = dataset.GetAttribute(thisComparisonTag.ParentSequenceTag);
                            if (sequenceAttribute == null)
                            {
                                // add in an empty item
                                DvtkData.Dimse.SequenceItem item = new SequenceItem();
                                dataset.AddAttribute(thisComparisonTag.ParentSequenceTag.GroupNumber,
                                                     thisComparisonTag.ParentSequenceTag.ElementNumber,
                                                     VR.SQ,
                                                     item);
                                sequenceAttribute = dataset.GetAttribute(thisComparisonTag.ParentSequenceTag);
                            }

                            // get the sequence item and add in the required attribute
                            sequenceOfItems = (SequenceOfItems)sequenceAttribute.DicomValue;
                            if (sequenceOfItems.Sequence.Count == 1)
                            {
                                DvtkData.Dimse.SequenceItem item = sequenceOfItems.Sequence[0];
                                if (item != null)
                                {
                                    // add the attribute to the item
                                    if (sourceComparisonTag.Vr == VR.SQ)
                                    {
                                        // add in an empty item
                                        // TODO - fix this properly
                                        DvtkData.Dimse.SequenceItem item1 = new SequenceItem();
                                        item.AddAttribute(sourceComparisonTag.Tag.GroupNumber,
                                                          sourceComparisonTag.Tag.ElementNumber,
                                                          VR.SQ,
                                                          item1);
                                    }
                                    else
                                    {
                                        // if the attribute already exists - then we need to remove it
                                        // - it was probably set to the default value
                                        DvtkData.Dimse.Attribute attribute = item.GetAttribute(sourceComparisonTag.Tag);
                                        if (attribute != null)
                                        {
                                            item.Remove(attribute);
                                        }

                                        // add the attribute to the item
                                        item.AddAttribute(sourceComparisonTag.Tag.GroupNumber,
                                                          sourceComparisonTag.Tag.ElementNumber,
                                                          sourceComparisonTag.Vr,
                                                          stringValue);
                                    }
                                }
                            }
                        }
                        else
                        {
                            // if the attribute already exists - then we need to remove it
                            // - it was probably set to the default value
                            DvtkData.Dimse.Attribute attribute = dataset.GetAttribute(sourceComparisonTag.Tag);
                            if (attribute != null)
                            {
                                dataset.Remove(attribute);
                            }

                            // add the attribute at the top level
                            dataset.AddAttribute(sourceComparisonTag.Tag.GroupNumber,
                                                 sourceComparisonTag.Tag.ElementNumber,
                                                 sourceComparisonTag.Vr,
                                                 stringValue);
                        }
                    }
                }
            }

            return(messagePopulated);
        }
Пример #12
0
        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);
        }