/// <summary>
        /// Gets URL upon calling which the required study data can be retrieved.
        /// </summary>
        /// <param name="studyUIDs">List of UIDs for the studies to be retrieved</param>
        /// <param name="endPointUrl">URL of the NBIA Data Service to query. This is NOT the Data Transfer URL!</param>
        /// <returns>URL that returns zip file with the studies</returns>
        public string retrieveStudyURL(string[] studyUIDs, string endPointUrl)
        {
            TransferServiceContextService.DataTransferDescriptor dtd;
            using (NCIACoreServicePortTypeClient proxy = new NCIACoreServicePortTypeClient("NCIACoreServicePortTypePort", endPointUrl))
            {
                try
                {
                    TransferServiceContextReference tras     = proxy.retrieveDicomDataByStudyUIDs(studyUIDs); // new TransferServiceContextReference();
                    EndpointReferenceType           endPoint = tras.EndpointReference;                        // new EndpointReferenceType();
                    AddressHeader[] ah = new AddressHeader[endPoint.ReferenceProperties.Any.Length];
                    for (int lcv = 0; lcv < ah.Length; lcv++)
                    {
                        XmlElement refProp = endPoint.ReferenceProperties.Any[lcv];
                        ah[lcv] = AddressHeader.CreateAddressHeader(refProp.LocalName, refProp.NamespaceURI, refProp.InnerText);
                    }
                    EndpointAddress ea = new EndpointAddress(new Uri(endPoint.Address.Value), ah);
                    // create binding by hand so we don't have to associate a config file with a dll
                    BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None);
                    binding.Name = "TransferServiceContextPortTypePort";
                    TransferServiceContextPortTypeClient transProxy = new TransferServiceContextPortTypeClient(binding, ea);
                    dtd = transProxy.getDataTransferDescriptor();
                }
                catch (System.Net.WebException ex)
                {
                    System.Console.WriteLine(ex.Message); //never gets here
                    throw new GridServicerException("Error querying NCIA Grid", ex);
                }
                catch (Exception e)
                {
                    System.Console.WriteLine(e.Message); //never gets here
                    throw new GridServicerException("Error retrieving from NCIA Grid", e);
                }
            }

            if (string.IsNullOrEmpty(dtd.url))
            {
                return(dtd.url);
            }
            else
            {
                return(null);
            }
        }
Beispiel #2
0
        public string retrieveStudyURL(string[] studyUIDs, string endPointUrl)
        {
            TransferServiceContextService.DataTransferDescriptor dtd;
            try
            {
                var proxy    = new NCIACoreServicePortTypeClient();
                var tras     = proxy.retrieveDicomDataByStudyUIDs(studyUIDs);
                var endPoint = tras.EndpointReference;
                var ah       = new AddressHeader[endPoint.ReferenceProperties.Any.Length];
                proxy.Endpoint.Address = new EndpointAddress(endPointUrl);
                for (var lcv = 0; lcv < ah.Length; lcv++)
                {
                    var refProp = endPoint.ReferenceProperties.Any[lcv];
                    ah[lcv] = AddressHeader.CreateAddressHeader(refProp.LocalName, refProp.NamespaceURI, refProp.InnerText);
                }
                var ea      = new EndpointAddress(new Uri(endPoint.Address.Value), ah);
                var binding = new BasicHttpBinding(BasicHttpSecurityMode.None);
                binding.Name = "TransferServiceContextPortTypePort";
                var transProxy = new TransferServiceContextPortTypeClient(binding, ea);
                dtd = transProxy.getDataTransferDescriptor();
            }
            catch (WebException ex)
            {
                Console.WriteLine(ex.Message);
                throw new GridServicerException("Error querying NCIA Grid", ex);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw new GridServicerException("Error retrieving from NCIA Grid", e);
            }

            if (dtd.url != null || dtd.url.Equals(""))
            {
                return(dtd.url);
            }
            else
            {
                return(null);
            }
        }
        /// <summary>
        /// Gets URL upon calling which the required study data can be retrieved.
        /// </summary>
        /// <param name="studyUIDs">List of UIDs for the studies to be retrieved</param>
        /// <param name="endPointUrl">URL of the NBIA Data Service to query. This is NOT the Data Transfer URL!</param>
        /// <returns>URL that returns zip file with the studies</returns>
        public string retrieveStudyURL(string[] studyUIDs, string endPointUrl)
        {
            TransferServiceContextService.DataTransferDescriptor dtd;
            using (NCIACoreServicePortTypeClient proxy = new NCIACoreServicePortTypeClient("NCIACoreServicePortTypePort", endPointUrl))
            {
                try
                {
                    TransferServiceContextReference tras = proxy.retrieveDicomDataByStudyUIDs(studyUIDs); // new TransferServiceContextReference();
                    EndpointReferenceType endPoint = tras.EndpointReference; // new EndpointReferenceType();
                    AddressHeader[] ah = new AddressHeader[endPoint.ReferenceProperties.Any.Length];
                    for (int lcv = 0; lcv < ah.Length; lcv++)
                    {
                        XmlElement refProp = endPoint.ReferenceProperties.Any[lcv];
                        ah[lcv] = AddressHeader.CreateAddressHeader(refProp.LocalName, refProp.NamespaceURI, refProp.InnerText);
                    }
                    EndpointAddress ea = new EndpointAddress(new Uri(endPoint.Address.Value), ah);
                    // create binding by hand so we don't have to associate a config file with a dll
                    BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None);
                    binding.Name = "TransferServiceContextPortTypePort";
                    TransferServiceContextPortTypeClient transProxy = new TransferServiceContextPortTypeClient(binding, ea);
                    dtd = transProxy.getDataTransferDescriptor();
                }
                catch (System.Net.WebException ex)
                {
                    System.Console.WriteLine(ex.Message); //never gets here
                    throw new GridServicerException("Error querying NCIA Grid", ex);

                }
                catch (Exception e)
                {
                    System.Console.WriteLine(e.Message); //never gets here
                    throw new GridServicerException("Error retrieving from NCIA Grid", e);
                }
            }

            if (string.IsNullOrEmpty(dtd.url))
                return dtd.url;
            else
                return null;
        }
        public string retrieveStudyURL(string[] studyUIDs, string endPointUrl)
        {
            TransferServiceContextService.DataTransferDescriptor dtd;
            try
            {
                var proxy = new NCIACoreServicePortTypeClient();
                var tras = proxy.retrieveDicomDataByStudyUIDs(studyUIDs);
                var endPoint = tras.EndpointReference;
                var ah = new AddressHeader[endPoint.ReferenceProperties.Any.Length];
                proxy.Endpoint.Address = new EndpointAddress(endPointUrl);
                for (var lcv = 0; lcv < ah.Length; lcv++)
                {
                    var refProp = endPoint.ReferenceProperties.Any[lcv];
                    ah[lcv] = AddressHeader.CreateAddressHeader(refProp.LocalName, refProp.NamespaceURI, refProp.InnerText);
                }
                var ea = new EndpointAddress(new Uri(endPoint.Address.Value), ah);
                var binding = new BasicHttpBinding(BasicHttpSecurityMode.None);
                binding.Name = "TransferServiceContextPortTypePort";
                var transProxy = new TransferServiceContextPortTypeClient(binding, ea);
                dtd = transProxy.getDataTransferDescriptor();
            }
            catch (WebException ex)
            {
                Console.WriteLine(ex.Message);
                throw new GridServicerException("Error querying NCIA Grid", ex);

            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw new GridServicerException("Error retrieving from NCIA Grid", e);
            }

            if (dtd.url != null || dtd.url.Equals(""))
                return dtd.url;
            else
                return null;
        }
        private CQLQueryResults getClinicalTrialSiteCQLInfo(string endPointUrl)
        {
            object[] obj;
            CQLQueryResults result;
            Association assoTrialDataProvenance = null;
            Association assoClinicalTrialProtocol = null;
            Association assoClinicalTrialSubject = null;
            Association assoImage = null;
            Association assoSeries = null;
            Association assoStudy = null;
            Association assoPatient = null;
            var results = new ArrayList();
            var proxy = new NCIACoreServicePortTypeClient();
            proxy.Endpoint.Address = new System.ServiceModel.EndpointAddress(endPointUrl);
            var items = new[] { "siteId", "siteName" };
            var itemsChoiceType1 = new[] {
                ItemsChoiceType.AttributeNames, ItemsChoiceType.AttributeNames, ItemsChoiceType.AttributeNames};
            assoImage = null;
            if (!_queryParameters.SliceThickness.IsEmpty)
            {
                var attrPatient = CreateAttribute("sliceThickness", _queryParameters.SliceThickness);
                var grpPatient = CreateQRAttrAssoGroup.createGroup(attrPatient, LogicalOperator.AND);
                assoImage = CreateQRAttrAssoGroup.createAssociation("gov.nih.nci.ncia.domain.Image", "imageCollection", grpPatient);
            }
            results = new ArrayList();
            assoSeries = null;
            obj = null;
            if (assoImage != null)
                results.Add(assoImage);
            if (!_queryParameters.Modality.IsEmpty)
                results.Add(CreateAttribute("modality", _queryParameters.Modality));
            if (results.Count > 0)
                obj = (object[])results.ToArray(typeof(object));
            if (obj != null)
            {
                var grpSeries = CreateQRAttrAssoGroup.createGroup(obj, LogicalOperator.AND);
                assoSeries = CreateQRAttrAssoGroup.createAssociation("gov.nih.nci.ncia.domain.Series", "seriesCollection", grpSeries);
            }
            obj = null;
            assoStudy = null;
            results = new ArrayList();
            if (assoSeries != null)
                results.Add(assoSeries);
            if (!_queryParameters.StudyInstanceUID.IsEmpty)
                results.Add(CreateAttribute("studyInstanceUID", _queryParameters.StudyInstanceUID));
            assoStudy = null;
            if (results.Count > 0)
                obj = (object[])results.ToArray(typeof(object));
            if (obj != null)
            {
                var groupStudy = CreateQRAttrAssoGroup.createGroup(obj, LogicalOperator.AND);
                assoStudy = CreateQRAttrAssoGroup.createAssociation("gov.nih.nci.ncia.domain.Study", "studyCollection", groupStudy);
            }

            assoTrialDataProvenance = null;
            if (!_queryParameters.ProjectName.IsEmpty)
            {
                var attrTrialDataProvenance = CreateAttribute("project", _queryParameters.ProjectName);
                var grpTrialDataProvenance = CreateQRAttrAssoGroup.createGroup(attrTrialDataProvenance, LogicalOperator.AND);
                assoTrialDataProvenance = CreateQRAttrAssoGroup.createAssociation("gov.nih.nci.ncia.domain.TrialDataProvenance", "dataProvenance", grpTrialDataProvenance);
            }
            obj = null;
            results = new ArrayList();
            if (assoStudy != null)
                results.Add(assoStudy);
            if (assoTrialDataProvenance != null)
                results.Add(assoTrialDataProvenance);
            if (!_queryParameters.PatientBirthDate.IsEmpty)
                results.Add(CreateAttribute("patientBirthDate", _queryParameters.PatientBirthDate));
            if (!_queryParameters.PatientId.IsEmpty)
                results.Add(CreateAttribute("patientId", _queryParameters.PatientId));
            if (!_queryParameters.PatientName.IsEmpty)
                results.Add(CreateAttribute("patientName", _queryParameters.PatientName));
            if (!_queryParameters.PatientSex.IsEmpty)
                results.Add(CreateAttribute("patientSex", _queryParameters.PatientSex));
            if (results.Count > 0)
                obj = (object[])results.ToArray(typeof(object));
            if (obj != null)
            {
                var patientGroup = CreateQRAttrAssoGroup.createGroup(obj, LogicalOperator.AND);
                assoPatient = CreateQRAttrAssoGroup.createAssociation("gov.nih.nci.ncia.domain.Patient", "patient", patientGroup);
            }

            assoClinicalTrialProtocol = null;
            obj = null;
            results = new ArrayList();
            if (!_queryParameters.ProtocolId.IsEmpty)
                results.Add(CreateAttribute("protocolId", _queryParameters.ProtocolId));
            if (!_queryParameters.ProtocolName.IsEmpty)
                results.Add(CreateAttribute("protocolName", _queryParameters.ProtocolName));
            if (results.Count > 0)
                obj = (object[])results.ToArray(typeof(object));
            if (obj != null)
            {
                var grpClinicalTrialProtocol = CreateQRAttrAssoGroup.createGroup(obj, LogicalOperator.AND);
                assoClinicalTrialProtocol = CreateQRAttrAssoGroup.createAssociation("gov.nih.nci.ncia.domain.ClinicalTrialProtocol",
                                                                                            "protocol", grpClinicalTrialProtocol);
            }

            results = new ArrayList();
            if (assoClinicalTrialProtocol != null)
                results.Add(assoClinicalTrialProtocol);
            if (assoPatient != null)
                results.Add(assoPatient);
            if (results.Count > 0)
            {
                obj = (object[])results.ToArray(typeof(object));
                var grpClinicalTrialSubject = CreateQRAttrAssoGroup.createGroup(obj, LogicalOperator.AND);
                assoClinicalTrialSubject = CreateQRAttrAssoGroup.createAssociation("gov.nih.nci.ncia.domain.ClinicalTrialSubject", "subjectCollection", grpClinicalTrialSubject);
            }

            obj = null;
            results = new ArrayList();
            if (assoClinicalTrialSubject != null)
                results.Add(assoClinicalTrialSubject);
            if (!_queryParameters.SiteId.IsEmpty)
                results.Add(CreateAttribute("siteId", _queryParameters.SiteId));
            if (!_queryParameters.SiteName.IsEmpty)
                results.Add(CreateAttribute("siteName", _queryParameters.SiteName));
            if (results.Count > 0)
                obj = (object[])results.ToArray(typeof(object));
            Group groupClinicalTrialSite = null;
            if (obj != null)
                groupClinicalTrialSite = CreateQRAttrAssoGroup.createGroup(obj, LogicalOperator.AND);
            var arg = CreateQRAttrAssoGroup.createQueryRequestCqlQuery("gov.nih.nci.ncia.domain.ClinicalTrialSite", items, itemsChoiceType1, null, groupClinicalTrialSite);
            var doc = XMLSerializingDeserializing.Serialize(arg);
            Console.WriteLine(doc.InnerXml);
            try
            {
                result = proxy.query(arg);
            }
            catch (System.Net.WebException ex)
            {
                Console.WriteLine(ex.Message);
                result = null;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                result = null;
                throw new GridServicerException("Error querying NCIA Grid", e);
            }
            return result;
        }
        private void retrieveStudyUID(string[] str)
        {
            TransferServiceContextService.DataTransferDescriptor dtd =null;
            TransferServiceContextPortTypeClient transProxy = null;
            NCIACoreServicePortTypeClient proxy = null;

            try
            {
                proxy = new NCIACoreServicePortTypeClient();
                TransferServiceContextReference tras = proxy.retrieveDicomDataByStudyUIDs(str); // new TransferServiceContextReference();
                EndpointReferenceType endPoint = tras.EndpointReference; // new EndpointReferenceType();
                AddressHeader[] ah = new AddressHeader[endPoint.ReferenceProperties.Any.Length];
                for (int lcv = 0; lcv < ah.Length; lcv++)
                {
                    XmlElement refProp = endPoint.ReferenceProperties.Any[lcv];
                    ah[lcv] = AddressHeader.CreateAddressHeader(refProp.LocalName, refProp.NamespaceURI, refProp.InnerText);
                }
                EndpointAddress ea = new EndpointAddress(new Uri(endPoint.Address.Value), ah);
                // create binding by hand so we don't have to associate a config file with a dll
                BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.None);
                binding.Name = "TransferServiceContextPortTypePort";
                transProxy = new TransferServiceContextPortTypeClient(binding, ea);
                dtd = transProxy.getDataTransferDescriptor();
            }
            catch (System.Net.WebException ex)
            {
                System.Console.WriteLine(ex.Message);
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.Message);
                throw new GridServicerException("Error querying NCIA Grid", e);
            }

            if (dtd.url != null && dtd.url.Equals(""))
            {
                WebRequest wr = WebRequest.Create(dtd.url);
                WebResponse resp = wr.GetResponse();
                byte[] buf = new byte[8192];
                int read;
                if (transProxy != null && proxy != null)
                {
                    try
                    {
                        Stream stream = resp.GetResponseStream();
                        FileStream fs = new FileStream(_directory + "/" + str[0] + ".zip", FileMode.Create, FileAccess.Write);
                        while ((read = stream.Read(buf, 0, buf.Length)) > 0)
                        {
                            fs.Write(buf, 0, read);
                        }
                        fs.Close();
                        stream.Close();
                        resp.Close();
                        proxy.Close();
                        transProxy.Close();
                    }
                    catch (IOException ioe)
                    {
                        throw new GridServicerException("Error writting zip file from caGrid", ioe);
                    }
                }
            }
        }
        private CQLQueryResults getClinicalTrialProtocolCQLInfo(string endPointUrl)
        {
            object[]        obj;
            CQLQueryResults result;
            Association     assoTrialDataProvenance  = null;
            Association     assoClinicalTrialSubject = null;
            Association     assoClinicalTrialSite    = null;
            Association     assoImage   = null;
            Association     assoSeries  = null;
            Association     assoStudy   = null;
            Association     assoPatient = null;
            var             results     = new ArrayList();
            var             proxy       = new NCIACoreServicePortTypeClient();

            proxy.Endpoint.Address = new System.ServiceModel.EndpointAddress(endPointUrl);
            var items            = new[] { "protocolId", "protocolName" };
            var itemsChoiceType1 = new[] {
                ItemsChoiceType.AttributeNames, ItemsChoiceType.AttributeNames, ItemsChoiceType.AttributeNames
            };

            assoImage = null;
            if (!_queryParameters.SliceThickness.IsEmpty)
            {
                var attrPatient = CreateAttribute("sliceThickness", _queryParameters.SliceThickness);
                var grpPatient  = CreateQRAttrAssoGroup.createGroup(attrPatient, LogicalOperator.AND);
                assoImage = CreateQRAttrAssoGroup.createAssociation("gov.nih.nci.ncia.domain.Image", "imageCollection", grpPatient);
            }
            results    = new ArrayList();
            assoSeries = null;
            obj        = null;
            if (assoImage != null)
            {
                results.Add(assoImage);
            }
            if (!_queryParameters.Modality.IsEmpty)
            {
                results.Add(CreateAttribute("modality", _queryParameters.Modality));
            }
            if (results.Count > 0)
            {
                obj = (object[])results.ToArray(typeof(object));
            }
            if (obj != null)
            {
                var grpSeries = CreateQRAttrAssoGroup.createGroup(obj, LogicalOperator.AND);
                assoSeries = CreateQRAttrAssoGroup.createAssociation("gov.nih.nci.ncia.domain.Series", "seriesCollection", grpSeries);
            }
            obj       = null;
            assoStudy = null;
            results   = new ArrayList();
            if (assoSeries != null)
            {
                results.Add(assoSeries);
            }
            if (!_queryParameters.StudyInstanceUID.IsEmpty)
            {
                results.Add(CreateAttribute("studyInstanceUID", _queryParameters.StudyInstanceUID));
            }
            assoStudy = null;
            if (results.Count > 0)
            {
                obj = (object[])results.ToArray(typeof(object));
            }
            if (obj != null)
            {
                var groupStudy = CreateQRAttrAssoGroup.createGroup(obj, LogicalOperator.AND);
                assoStudy = CreateQRAttrAssoGroup.createAssociation("gov.nih.nci.ncia.domain.Study", "studyCollection", groupStudy);
            }
            assoTrialDataProvenance = null;
            if (!_queryParameters.ProjectName.IsEmpty)
            {
                var attrTrialDataProvenance = CreateAttribute("project", _queryParameters.ProjectName);
                var grpTrialDataProvenance  = CreateQRAttrAssoGroup.createGroup(attrTrialDataProvenance, LogicalOperator.AND);
                assoTrialDataProvenance = CreateQRAttrAssoGroup.createAssociation("gov.nih.nci.ncia.domain.TrialDataProvenance", "dataProvenance", grpTrialDataProvenance);
            }
            obj     = null;
            results = new ArrayList();
            if (assoStudy != null)
            {
                results.Add(assoStudy);
            }
            if (assoTrialDataProvenance != null)
            {
                results.Add(assoTrialDataProvenance);
            }
            if (!_queryParameters.PatientBirthDate.IsEmpty)
            {
                results.Add(CreateAttribute("patientBirthDate", _queryParameters.PatientBirthDate));
            }
            if (!_queryParameters.PatientId.IsEmpty)
            {
                results.Add(CreateAttribute("patientId", _queryParameters.PatientId));
            }
            if (!_queryParameters.PatientName.IsEmpty)
            {
                results.Add(CreateAttribute("patientName", _queryParameters.PatientName));
            }
            if (!_queryParameters.PatientSex.IsEmpty)
            {
                results.Add(CreateAttribute("patientSex", _queryParameters.PatientSex));
            }

            if (results.Count > 0)
            {
                obj = (object[])results.ToArray(typeof(object));
            }
            if (obj != null)
            {
                var patientGroup = CreateQRAttrAssoGroup.createGroup(obj, LogicalOperator.AND);
                assoPatient = CreateQRAttrAssoGroup.createAssociation("gov.nih.nci.ncia.domain.Patient", "patient", patientGroup);
            }
            obj     = null;
            results = new ArrayList();
            if (!_queryParameters.SiteId.IsEmpty)
            {
                results.Add(CreateAttribute("siteId", _queryParameters.SiteId));
            }
            if (!_queryParameters.SiteName.IsEmpty)
            {
                results.Add(CreateAttribute("siteName", _queryParameters.SiteName));
            }
            if (results.Count > 0)
            {
                obj = (object[])results.ToArray(typeof(object));
            }
            Group grpClinicalTrialSite = null;

            if (obj != null)
            {
                grpClinicalTrialSite  = CreateQRAttrAssoGroup.createGroup(obj, LogicalOperator.AND);
                assoClinicalTrialSite = CreateQRAttrAssoGroup.createAssociation("gov.nih.nci.ncia.domain.ClinicalTrialSite", "site", grpClinicalTrialSite);
            }

            results = new ArrayList();
            if (assoClinicalTrialSite != null)
            {
                results.Add(assoClinicalTrialSite);
            }
            if (assoPatient != null)
            {
                results.Add(assoPatient);
            }
            if (results.Count > 0)
            {
                obj = (object[])results.ToArray(typeof(object));
                var grpClinicalTrialSubject = CreateQRAttrAssoGroup.createGroup(obj, LogicalOperator.AND);
                assoClinicalTrialSubject = CreateQRAttrAssoGroup.createAssociation("gov.nih.nci.ncia.domain.ClinicalTrialSubject", "subjectCollection", grpClinicalTrialSubject);
            }

            obj     = null;
            results = new ArrayList();
            if (assoClinicalTrialSubject != null)
            {
                results.Add(assoClinicalTrialSubject);
            }
            if (!_queryParameters.ProtocolId.IsEmpty)
            {
                results.Add(CreateAttribute("protocolId", _queryParameters.ProtocolId));
            }
            if (!_queryParameters.ProtocolName.IsEmpty)
            {
                results.Add(CreateAttribute("protocolName", _queryParameters.ProtocolName));
            }

            if (results.Count > 0)
            {
                obj = (object[])results.ToArray(typeof(object));
            }
            Group grpClinicalTrialProtocol = null;

            if (obj != null)
            {
                grpClinicalTrialProtocol = CreateQRAttrAssoGroup.createGroup(obj, LogicalOperator.AND);
            }

            var arg = CreateQRAttrAssoGroup.createQueryRequestCqlQuery("gov.nih.nci.ncia.domain.ClinicalTrialProtocol", items, itemsChoiceType1, null, grpClinicalTrialProtocol);
            var doc = XMLSerializingDeserializing.Serialize(arg);

            Console.WriteLine(doc.InnerXml);
            try
            {
                result = proxy.query(arg);
            }
            catch (System.Net.WebException ex)
            {
                Console.WriteLine(ex.Message);
                result = null;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                result = null;
                throw new GridServicerException("Error querying NCIA Grid", e);
            }
            return(result);
        }
Beispiel #8
0
        private void retrieveStudyUID(string[] str)
        {
            TransferServiceContextService.DataTransferDescriptor dtd = null;
            TransferServiceContextPortTypeClient transProxy          = null;
            NCIACoreServicePortTypeClient        proxy = null;

            try
            {
                proxy = new NCIACoreServicePortTypeClient();
                var tras     = proxy.retrieveDicomDataByStudyUIDs(str);
                var endPoint = tras.EndpointReference;
                var ah       = new AddressHeader[endPoint.ReferenceProperties.Any.Length];
                for (var lcv = 0; lcv < ah.Length; lcv++)
                {
                    var refProp = endPoint.ReferenceProperties.Any[lcv];
                    ah[lcv] = AddressHeader.CreateAddressHeader(refProp.LocalName, refProp.NamespaceURI, refProp.InnerText);
                }
                var ea      = new EndpointAddress(new Uri(endPoint.Address.Value), ah);
                var binding = new BasicHttpBinding(BasicHttpSecurityMode.None);
                binding.Name = "TransferServiceContextPortTypePort";
                transProxy   = new TransferServiceContextPortTypeClient(binding, ea);
                dtd          = transProxy.getDataTransferDescriptor();
            }
            catch (WebException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw new GridServicerException("Error querying NCIA Grid", e);
            }

            if (dtd.url != null && dtd.url.Equals(""))
            {
                var wr   = WebRequest.Create(dtd.url);
                var resp = wr.GetResponse();
                var buf  = new byte[8192];
                int read;
                if (transProxy != null && proxy != null)
                {
                    try
                    {
                        var stream = resp.GetResponseStream();
                        var fs     = new FileStream(_directory + "/" + str[0] + ".zip", FileMode.Create, FileAccess.Write);
                        while ((read = stream.Read(buf, 0, buf.Length)) > 0)
                        {
                            fs.Write(buf, 0, read);
                        }
                        fs.Close();
                        stream.Close();
                        resp.Close();
                        proxy.Close();
                        transProxy.Close();
                    }
                    catch (IOException ioe)
                    {
                        throw new GridServicerException("Error writting zip file from caGrid", ioe);
                    }
                }
            }
        }
        private CQLQueryResults getStudyCQLInfo(string endPointUrl)
        {
            object[] obj;
            CQLQueryResults result;
            Association assoImage = null;
            Association assoTrialDataProvenance = null;
            Association assoSeries = null;
            Association assoClinicalTrialSite = null;
            Association assoClinicalTrialProtocol = null;
            Association assoClinicalTrialSubject = null;
            Association assoPatient = null;
            ArrayList results = new ArrayList();
            NCIACoreServicePortTypeClient proxy = new NCIACoreServicePortTypeClient();
            proxy.Endpoint.Address = new System.ServiceModel.EndpointAddress(endPointUrl);
            string[] items = new string[] { "additionalPatientHistory",
            "admittingDiagnosesCodeSequence", "occupation", "patientAge", "patientSize", "patientWeight",
            "studyDate", "studyId", "studyInstanceUID", "studyTime", "timePointDescription" , "timePointId" };
            ItemsChoiceType[] itemsChoiceType1 = new ItemsChoiceType[] {
                ItemsChoiceType.AttributeNames, ItemsChoiceType.AttributeNames, ItemsChoiceType.AttributeNames,
                ItemsChoiceType.AttributeNames, ItemsChoiceType.AttributeNames, ItemsChoiceType.AttributeNames,
                ItemsChoiceType.AttributeNames, ItemsChoiceType.AttributeNames, ItemsChoiceType.AttributeNames,
                ItemsChoiceType.AttributeNames, ItemsChoiceType.AttributeNames, ItemsChoiceType.AttributeNames};

            // Image
            assoImage = null;
            if (!_queryParameters.SliceThickness.IsEmpty)
            {
                Attribute attrPatient = this.CreateAttribute("sliceThickness", _queryParameters.SliceThickness);
                Group grpPatient = CreateQRAttrAssoGroup.createGroup(attrPatient, LogicalOperator.AND);
                assoImage = CreateQRAttrAssoGroup.createAssociation("gov.nih.nci.ncia.domain.Image", "imageCollection", grpPatient);
            }
            // Series
            assoSeries = null;
            obj = null;
            results = new ArrayList();
            if (assoImage != null)
                results.Add(assoImage);
            if (!_queryParameters.Modality.IsEmpty)
                results.Add(this.CreateAttribute("modality", _queryParameters.Modality));
            if (results.Count > 0 || assoImage != null)
                obj = (object[])results.ToArray(typeof(object));
            if (obj != null && obj.Length > 0)
            {
                Group grpSeries = CreateQRAttrAssoGroup.createGroup(obj, LogicalOperator.AND);
                assoSeries = CreateQRAttrAssoGroup.createAssociation("gov.nih.nci.ncia.domain.Series", "seriesCollection", grpSeries);
            }
            // TrialDataProvenance
            assoTrialDataProvenance = null;
            if (!_queryParameters.ProjectName.IsEmpty)
            {
                Attribute attrTrialDataProvenance = this.CreateAttribute("project", _queryParameters.ProjectName);
                Group grpTrialDataProvenance = CreateQRAttrAssoGroup.createGroup(attrTrialDataProvenance, LogicalOperator.AND);
                assoTrialDataProvenance = CreateQRAttrAssoGroup.createAssociation("gov.nih.nci.ncia.domain.TrialDataProvenance", "dataProvenance", grpTrialDataProvenance);
            }
            // ClinicalTrial Site
            assoClinicalTrialSite = null;
            obj = null;
            results = new ArrayList();
            if (!_queryParameters.SiteId.IsEmpty)
                results.Add(this.CreateAttribute("siteId", _queryParameters.SiteId));
            if (!_queryParameters.SiteName.IsEmpty)
                results.Add(this.CreateAttribute("siteName", _queryParameters.SiteName));
            if (results.Count > 0)
                obj =  (object[])results.ToArray(typeof(object)) ;
            if (obj != null && obj.Length > 0)
            {
                Group grpClinicalTrialSite = CreateQRAttrAssoGroup.createGroup(obj, LogicalOperator.AND);
                assoClinicalTrialSite = CreateQRAttrAssoGroup.createAssociation("gov.nih.nci.ncia.domain.ClinicalTrialSite", "site", grpClinicalTrialSite);
            }
            // ClinicalTrial Protocol
            assoClinicalTrialProtocol = null;
            obj = null;
            results = new ArrayList();
            if (!_queryParameters.ProtocolId.IsEmpty)
                results.Add(this.CreateAttribute("protocolId", _queryParameters.ProtocolId));
            if (!_queryParameters.ProtocolName.IsEmpty)
                results.Add(this.CreateAttribute("protocolName", _queryParameters.ProtocolName));
            if (results.Count > 0)
                obj = (object[])results.ToArray(typeof(object));
            if (obj != null && obj.Length > 0)
            {
                Group grpClinicalTrialProtocol = CreateQRAttrAssoGroup.createGroup(obj, LogicalOperator.AND);
                assoClinicalTrialProtocol = CreateQRAttrAssoGroup.createAssociation("gov.nih.nci.ncia.domain.ClinicalTrialProtocol",
                                                                                            "protocol", grpClinicalTrialProtocol);
            }
            // Clinical Trial Subject
            obj = null;
            results = new ArrayList();
            assoClinicalTrialSubject = null;
            if (assoClinicalTrialSite != null)
                results.Add(assoClinicalTrialSite);
            if (assoClinicalTrialProtocol != null)
                results.Add(assoClinicalTrialProtocol);
            if (results.Count > 0)
                obj =  (object[])results.ToArray(typeof(object));
            if (obj != null && obj.Length > 0)
            {
                Group grpClinicalTrialSubject = CreateQRAttrAssoGroup.createGroup(obj, LogicalOperator.AND);
                assoClinicalTrialSubject = CreateQRAttrAssoGroup.createAssociation("gov.nih.nci.ncia.domain.ClinicalTrialSubject",
                                                                                               "subjectCollection", grpClinicalTrialSubject);
            }
            // Patient
            obj = null;
            results = new ArrayList();
            if (assoClinicalTrialSubject != null)
                results.Add(assoClinicalTrialSubject);
            if (assoTrialDataProvenance != null)
                results.Add(assoTrialDataProvenance);
            if (!_queryParameters.PatientBirthDate.IsEmpty)
                results.Add(this.CreateAttribute("patientBirthDate", _queryParameters.PatientBirthDate));
            if (!_queryParameters.PatientId.IsEmpty)
                results.Add(this.CreateAttribute("patientId", _queryParameters.PatientId));
            if (!_queryParameters.PatientName.IsEmpty)
                results.Add(this.CreateAttribute("patientName", _queryParameters.PatientName));
            if (!_queryParameters.PatientSex.IsEmpty)
                results.Add(this.CreateAttribute("patientSex", _queryParameters.PatientSex));
            if ( results.Count > 0)
                obj = (object[])results.ToArray(typeof(object));
            if (obj != null && obj.Length > 0)
            {
                Group patientGroup = CreateQRAttrAssoGroup.createGroup(obj, LogicalOperator.AND);
                assoPatient = CreateQRAttrAssoGroup.createAssociation("gov.nih.nci.ncia.domain.Patient", "patient", patientGroup);
            }

            // Study
            obj = null;
            results = new ArrayList();
            Group groupStudy = null;
            if (assoSeries != null)
                results.Add(assoSeries);
            if (assoPatient != null)
                results.Add(assoPatient);
            if (!_queryParameters.StudyInstanceUID.IsEmpty)
                results.Add(this.CreateAttribute("studyInstanceUID", _queryParameters.StudyInstanceUID));

            if (results.Count > 0)
                obj =  (object[])results.ToArray(typeof(object)) ;
            if (obj != null && obj.Length > 0)
                groupStudy = CreateQRAttrAssoGroup.createGroup(obj, LogicalOperator.AND);
            QueryRequestCqlQuery arg = CreateQRAttrAssoGroup.createQueryRequestCqlQuery("gov.nih.nci.ncia.domain.Study", items, itemsChoiceType1, null, groupStudy);

            XmlDocument doc = XMLSerializingDeserializing.Serialize(arg);
            Console.WriteLine(((System.Xml.XmlDocument)((System.Xml.XmlNode)(doc))).InnerXml);

            try
            {
                result = proxy.query(arg);
            }
            catch (System.Net.WebException ex)
            {
                System.Console.WriteLine(ex.Message);
                result = null;
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.Message);
                result = null;
                throw new GridServicerException("Error querying NCIA Grid", e);
            }
            return result;
        }
        private CQLQueryResults getStudyCQLInfo(string endPointUrl)
        {
            object[]        obj;
            CQLQueryResults result;
            Association     assoImage = null;
            Association     assoTrialDataProvenance = null;
            Association     assoSeries                = null;
            Association     assoClinicalTrialSite     = null;
            Association     assoClinicalTrialProtocol = null;
            Association     assoClinicalTrialSubject  = null;
            Association     assoPatient               = null;
            ArrayList       results             = new ArrayList();
            NCIACoreServicePortTypeClient proxy = new NCIACoreServicePortTypeClient();

            proxy.Endpoint.Address = new System.ServiceModel.EndpointAddress(endPointUrl);
            string[] items = new string[] { "additionalPatientHistory",
                                            "admittingDiagnosesCodeSequence", "occupation", "patientAge", "patientSize", "patientWeight",
                                            "studyDate", "studyId", "studyInstanceUID", "studyTime", "timePointDescription", "timePointId" };
            ItemsChoiceType[] itemsChoiceType1 = new ItemsChoiceType[] {
                ItemsChoiceType.AttributeNames, ItemsChoiceType.AttributeNames, ItemsChoiceType.AttributeNames,
                ItemsChoiceType.AttributeNames, ItemsChoiceType.AttributeNames, ItemsChoiceType.AttributeNames,
                ItemsChoiceType.AttributeNames, ItemsChoiceType.AttributeNames, ItemsChoiceType.AttributeNames,
                ItemsChoiceType.AttributeNames, ItemsChoiceType.AttributeNames, ItemsChoiceType.AttributeNames
            };

            // Image
            assoImage = null;
            if (!_queryParameters.SliceThickness.IsEmpty)
            {
                Attribute attrPatient = this.CreateAttribute("sliceThickness", _queryParameters.SliceThickness);
                Group     grpPatient  = CreateQRAttrAssoGroup.createGroup(attrPatient, LogicalOperator.AND);
                assoImage = CreateQRAttrAssoGroup.createAssociation("gov.nih.nci.ncia.domain.Image", "imageCollection", grpPatient);
            }
            // Series
            assoSeries = null;
            obj        = null;
            results    = new ArrayList();
            if (assoImage != null)
            {
                results.Add(assoImage);
            }
            if (!_queryParameters.Modality.IsEmpty)
            {
                results.Add(this.CreateAttribute("modality", _queryParameters.Modality));
            }
            if (results.Count > 0 || assoImage != null)
            {
                obj = (object[])results.ToArray(typeof(object));
            }
            if (obj != null && obj.Length > 0)
            {
                Group grpSeries = CreateQRAttrAssoGroup.createGroup(obj, LogicalOperator.AND);
                assoSeries = CreateQRAttrAssoGroup.createAssociation("gov.nih.nci.ncia.domain.Series", "seriesCollection", grpSeries);
            }
            // TrialDataProvenance
            assoTrialDataProvenance = null;
            if (!_queryParameters.ProjectName.IsEmpty)
            {
                Attribute attrTrialDataProvenance = this.CreateAttribute("project", _queryParameters.ProjectName);
                Group     grpTrialDataProvenance  = CreateQRAttrAssoGroup.createGroup(attrTrialDataProvenance, LogicalOperator.AND);
                assoTrialDataProvenance = CreateQRAttrAssoGroup.createAssociation("gov.nih.nci.ncia.domain.TrialDataProvenance", "dataProvenance", grpTrialDataProvenance);
            }
            // ClinicalTrial Site
            assoClinicalTrialSite = null;
            obj     = null;
            results = new ArrayList();
            if (!_queryParameters.SiteId.IsEmpty)
            {
                results.Add(this.CreateAttribute("siteId", _queryParameters.SiteId));
            }
            if (!_queryParameters.SiteName.IsEmpty)
            {
                results.Add(this.CreateAttribute("siteName", _queryParameters.SiteName));
            }
            if (results.Count > 0)
            {
                obj = (object[])results.ToArray(typeof(object));
            }
            if (obj != null && obj.Length > 0)
            {
                Group grpClinicalTrialSite = CreateQRAttrAssoGroup.createGroup(obj, LogicalOperator.AND);
                assoClinicalTrialSite = CreateQRAttrAssoGroup.createAssociation("gov.nih.nci.ncia.domain.ClinicalTrialSite", "site", grpClinicalTrialSite);
            }
            // ClinicalTrial Protocol
            assoClinicalTrialProtocol = null;
            obj     = null;
            results = new ArrayList();
            if (!_queryParameters.ProtocolId.IsEmpty)
            {
                results.Add(this.CreateAttribute("protocolId", _queryParameters.ProtocolId));
            }
            if (!_queryParameters.ProtocolName.IsEmpty)
            {
                results.Add(this.CreateAttribute("protocolName", _queryParameters.ProtocolName));
            }
            if (results.Count > 0)
            {
                obj = (object[])results.ToArray(typeof(object));
            }
            if (obj != null && obj.Length > 0)
            {
                Group grpClinicalTrialProtocol = CreateQRAttrAssoGroup.createGroup(obj, LogicalOperator.AND);
                assoClinicalTrialProtocol = CreateQRAttrAssoGroup.createAssociation("gov.nih.nci.ncia.domain.ClinicalTrialProtocol",
                                                                                    "protocol", grpClinicalTrialProtocol);
            }
            // Clinical Trial Subject
            obj     = null;
            results = new ArrayList();
            assoClinicalTrialSubject = null;
            if (assoClinicalTrialSite != null)
            {
                results.Add(assoClinicalTrialSite);
            }
            if (assoClinicalTrialProtocol != null)
            {
                results.Add(assoClinicalTrialProtocol);
            }
            if (results.Count > 0)
            {
                obj = (object[])results.ToArray(typeof(object));
            }
            if (obj != null && obj.Length > 0)
            {
                Group grpClinicalTrialSubject = CreateQRAttrAssoGroup.createGroup(obj, LogicalOperator.AND);
                assoClinicalTrialSubject = CreateQRAttrAssoGroup.createAssociation("gov.nih.nci.ncia.domain.ClinicalTrialSubject",
                                                                                   "subjectCollection", grpClinicalTrialSubject);
            }
            // Patient
            obj     = null;
            results = new ArrayList();
            if (assoClinicalTrialSubject != null)
            {
                results.Add(assoClinicalTrialSubject);
            }
            if (assoTrialDataProvenance != null)
            {
                results.Add(assoTrialDataProvenance);
            }
            if (!_queryParameters.PatientBirthDate.IsEmpty)
            {
                results.Add(this.CreateAttribute("patientBirthDate", _queryParameters.PatientBirthDate));
            }
            if (!_queryParameters.PatientId.IsEmpty)
            {
                results.Add(this.CreateAttribute("patientId", _queryParameters.PatientId));
            }
            if (!_queryParameters.PatientName.IsEmpty)
            {
                results.Add(this.CreateAttribute("patientName", _queryParameters.PatientName));
            }
            if (!_queryParameters.PatientSex.IsEmpty)
            {
                results.Add(this.CreateAttribute("patientSex", _queryParameters.PatientSex));
            }
            if (results.Count > 0)
            {
                obj = (object[])results.ToArray(typeof(object));
            }
            if (obj != null && obj.Length > 0)
            {
                Group patientGroup = CreateQRAttrAssoGroup.createGroup(obj, LogicalOperator.AND);
                assoPatient = CreateQRAttrAssoGroup.createAssociation("gov.nih.nci.ncia.domain.Patient", "patient", patientGroup);
            }

            // Study
            obj     = null;
            results = new ArrayList();
            Group groupStudy = null;

            if (assoSeries != null)
            {
                results.Add(assoSeries);
            }
            if (assoPatient != null)
            {
                results.Add(assoPatient);
            }
            if (!_queryParameters.StudyInstanceUID.IsEmpty)
            {
                results.Add(this.CreateAttribute("studyInstanceUID", _queryParameters.StudyInstanceUID));
            }

            if (results.Count > 0)
            {
                obj = (object[])results.ToArray(typeof(object));
            }
            if (obj != null && obj.Length > 0)
            {
                groupStudy = CreateQRAttrAssoGroup.createGroup(obj, LogicalOperator.AND);
            }
            QueryRequestCqlQuery arg = CreateQRAttrAssoGroup.createQueryRequestCqlQuery("gov.nih.nci.ncia.domain.Study", items, itemsChoiceType1, null, groupStudy);

            XmlDocument doc = XMLSerializingDeserializing.Serialize(arg);

            Console.WriteLine(((System.Xml.XmlDocument)((System.Xml.XmlNode)(doc))).InnerXml);

            try
            {
                result = proxy.query(arg);
            }
            catch (System.Net.WebException ex)
            {
                System.Console.WriteLine(ex.Message);
                result = null;
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e.Message);
                result = null;
                throw new GridServicerException("Error querying NCIA Grid", e);
            }
            return(result);
        }