示例#1
0
        /// <summary>
        /// retrieve SNOMED children or parents
        /// </summary>
        /// <param name="isForChildren">otherwise for parents</param>
        /// <param name="searchID">SNOMED ID</param>
        /// <param name="pageSize">e.g. 100</param>
        /// <param name="maxPages">e.g. 100</param>
        /// <param name="snomedConcepts">results</param>
        /// <returns></returns>
        public string RetrieveChildrenOrParents(string searchID,
                                                bool isForChildren,
                                                int pageSize,
                                                int maxPages,
                                                out List <SnomedConcept> snomedConcepts)
        {
            ///content/{version}/source/{source}/{id}/children
            snomedConcepts = new List <SnomedConcept>();
            SnomedConcept c = null;
            bool          isAwaitingName       = false;
            bool          expectMoreOnNextPage = true;
            StringBuilder sbResult             = new StringBuilder();
            string        response             = null; //for each page

            for (int i = 0; i < maxPages; i++)
            {
                //debug:
                //Console.Write(i.ToString() + ";");
                expectMoreOnNextPage = false; //unless we reach max for this page below...
                string        ticket = ObtainSingleUseTicket();
                StringBuilder sbUri  = new StringBuilder(_baseUriRequests);
                sbUri.Append("/content/");
                sbUri.Append("current");//unless you want to specify one
                sbUri.Append("/source/");
                sbUri.Append("SNOMEDCT_US");
                sbUri.Append("/");
                sbUri.Append(System.Uri.EscapeDataString(searchID));
                sbUri.Append(isForChildren ? "/children" : "/parents");
                sbUri.Append("?pageSize=");
                sbUri.Append(pageSize);
                sbUri.Append("&pageNumber=");
                sbUri.Append((i + 1).ToString());
                //if (!string.IsNullOrEmpty(req.SearchType))
                //{
                //    sbUri.Append("&searchType=");
                //    sbUri.Append(req.SearchType);
                //}
                sbUri.Append("&ticket=");
                sbUri.Append(ticket);
                //trap not found error which happens when results
                // count is multiple of pageSize
                try
                {
                    response = SendRequest("GET",
                                           null, null, sbUri.ToString());
                }
                catch (Exception er)
                {
                    if ((i > 0) &&
                        (er is System.Net.WebException) &&
                        (er.ToString().Contains("(404)")))
                    {
                        //ignore the error, just quit
                        break;
                    }
                    else

                    {
                        //rethrow
                        throw er;
                    }
                }

                if (true)
                {
                    sbResult.Append(response);
                    sbResult.Append(Environment.NewLine);
                    if (i == maxPages - 1)
                    {
                        throw new Exception("Truncated at " +
                                            (i + 1).ToString() +
                                            " pages maximum.");
                    }
                    //add to list
                    using (Newtonsoft.Json.JsonTextReader reader =
                               new JsonTextReader(new StringReader(response)))
                    {
                        StringBuilder sb = new StringBuilder();
                        int           numConceptsFound = 0;
                        while (reader.Read())
                        {
                            sb.Append("depth=");
                            sb.Append(reader.Depth);
                            sb.Append("; tokenType=");
                            sb.Append(reader.TokenType);
                            sb.Append("; value=");
                            sb.Append(reader.Value);

                            sb.Append("\r\n");
                            if (reader.Depth == 3)
                            {
                                if (isAwaitingName)
                                {
                                    //then if we get the name before we get end of object
                                    // we'll add it to the list
                                    //if end of object..
                                    if (reader.TokenType == JsonToken.EndObject)
                                    {
                                        //oops got to end before found name
                                        //reset
                                        isAwaitingName = false;
                                        c = null;
                                    }
                                    else if ((reader.TokenType == JsonToken.PropertyName) &&
                                             ((string)reader.Value == "name"))
                                    {
                                        //read value of name and save to list
                                        if (reader.Read())
                                        {
                                            if (reader.TokenType == JsonToken.String)
                                            {
                                                c.Name = (string)reader.Value;
                                                numConceptsFound++;
                                                if (numConceptsFound >= pageSize)
                                                {
                                                    expectMoreOnNextPage = true;
                                                }
                                                snomedConcepts.Add(c);
                                                isAwaitingName = false;
                                                c = null;
                                            }
                                        }
                                    }
                                }//from if awaing name
                                else if ((reader.TokenType == JsonToken.PropertyName) &&
                                         ((string)reader.Value == "ui"))
                                {
                                    //read value of ui
                                    if (reader.Read())
                                    {
                                        if (reader.TokenType == JsonToken.String)
                                        {
                                            c = new SnomedConcept();
                                            //hopefully ui is the Snomed Concept ID
                                            c.ConceptID    = (string)reader.Value;
                                            isAwaitingName = true;
                                        }
                                    }
                                }
                            } //from if depth 3
                        }     //from while reader read
                    }         //from using json reader
                }             //from if not NORESULTS -> from if true
                 //notice they don't tell us when end of list, so the
                 // only way we know is if list is smaller than pageSize
                if (!expectMoreOnNextPage)
                {
                    break;
                }
            }//from for each page
            return(sbResult.ToString());
        }
示例#2
0
        /// <summary>
        /// returns the raw text of the search and also
        /// assigns result to a list of SnomedConcepts
        /// </summary>
        /// <param name="maxPages">maximum number of pages to return e.g. 100</param>
        /// <param name="pageSize">maximum results per page e.g.100</param>
        /// <param name="result">a list of snomed concepts</param>
        /// <returns></returns>
        public string SearchSnomed(string searchString,
                                   int maxPages,
                                   int pageSize,
                                   out List <SnomedConcept> result)
        {
            List <SnomedConcept> snomedConcepts = new List <SnomedConcept>();
            SnomedConcept        c       = null;
            bool          isAwaitingName = false;
            StringBuilder sbResult       = new StringBuilder();
            string        response; //for each page
            int           pageCount = 0;

            for (int i = 0; i < maxPages; i++)
            {
                //debug:
                //Console.Write(i.ToString() + ";");

                string        ticket = ObtainSingleUseTicket();
                StringBuilder sbUri  = new StringBuilder(_baseUriRequests);
                sbUri.Append("/search/current?string=");
                sbUri.Append(System.Uri.EscapeDataString(searchString));

                sbUri.Append("&sabs=");
                sbUri.Append("SNOMEDCT_US");
                //sbUri.Append("&returnIdType=sourceDescriptor");// don't use - returns nothing
                //sbUri.Append("&returnIdType=sourceConcept"); //seems to give the same info
                sbUri.Append("&returnIdType=code"); //code returns UMLS CUI or source Ui depending on &sabs
                                                    //sbUri.Append("&returnIdType=sourceUi"); //sourceUi returns ui source uses

                sbUri.Append("&pageSize=");
                sbUri.Append(pageSize);
                sbUri.Append("&pageNumber=");
                sbUri.Append((i + 1).ToString());

                sbUri.Append("&ticket=");
                sbUri.Append(ticket);
                response = SendRequest("GET",
                                       null, null, sbUri.ToString());
                if (JsonPageIsNORESULTS(response))
                {
                    pageCount = i;
                    break;
                }

                else
                {
                    sbResult.Append(response);
                    sbResult.Append(Environment.NewLine);
                    if (i == maxPages - 1)
                    {
                        throw new Exception("Truncated at " +
                                            (i + 1).ToString() +
                                            " pages maximum.");
                    }
                    //add to list
                    using (Newtonsoft.Json.JsonTextReader reader =
                               new JsonTextReader(new StringReader(response)))
                    {
                        while (reader.Read())
                        {
                            if (reader.Depth == 4)
                            {
                                if (isAwaitingName)
                                {
                                    //then if we get the name before we get end of object
                                    // we'll add it to the list
                                    //if end of object..
                                    if (reader.TokenType == JsonToken.EndObject)
                                    {
                                        //oops got to end before found name
                                        //reset
                                        isAwaitingName = false;
                                        c = null;
                                    }
                                    else if ((reader.TokenType == JsonToken.PropertyName) &&
                                             ((string)reader.Value == "name"))
                                    {
                                        //read value of name and save to list
                                        if (reader.Read())
                                        {
                                            if (reader.TokenType == JsonToken.String)
                                            {
                                                c.Name = (string)reader.Value;
                                                snomedConcepts.Add(c);
                                                isAwaitingName = false;
                                                c = null;
                                            }
                                        }
                                    }
                                }//from if awaing name
                                else if ((reader.TokenType == JsonToken.PropertyName) &&
                                         ((string)reader.Value == "ui"))
                                {
                                    //read value of ui
                                    if (reader.Read())
                                    {
                                        if (reader.TokenType == JsonToken.String)
                                        {
                                            c = new SnomedConcept();
                                            // the ui value can be umls Concept Unique Id, or the snomed CID depending on query returnIdType
                                            c.ConceptID = (string)reader.Value;
                                            //c.CUI = (string)reader.Value;
                                            isAwaitingName = true;
                                        }
                                    }
                                }
                            } //from if depth 4
                        }     //from while reader read
                    }         //from using json reader
                }             //from if not NORESULTS
            }                 //from for each page
            result = snomedConcepts;
            return(sbResult.ToString());
        }