public IHttpActionResult GetClientConfig(bool isRef = false)
        {
            var clientConfig = new ClientConfigModel();

            foreach (IGTypeFhirElement fit in IGTypeSection.GetSection().FhirIgTypes)
            {
                ImplementationGuideType igType = this.tdb.ImplementationGuideTypes.SingleOrDefault(y => y.Name.ToLower() == fit.ImplementationGuideTypeName.ToLower());

                if (igType == null)
                {
                    Log.For(this).Warn("Configured FHIR IG Type could not be found in the database: " + fit.ImplementationGuideTypeName);
                    continue;
                }

                var fhirIgType = new ClientConfigModel.FhirIgType()
                {
                    Id      = igType.Id,
                    Name    = igType.Name,
                    Version = fit.Version,
                    BaseUrl = ""
                };

                switch (fhirIgType.Version)
                {
                case "DSTU1":
                    fhirIgType.BaseUrl = "/api/FHIR1/";
                    break;

                case "DSTU2":
                    fhirIgType.BaseUrl = "/api/FHIR2/";
                    break;

                case "STU3":
                    fhirIgType.BaseUrl = "/api/FHIR3/";
                    break;
                }

                clientConfig.FhirIgTypes.Add(fhirIgType);
            }

            if (isRef)
            {
                var clientConfigJson = Newtonsoft.Json.JsonConvert.SerializeObject(clientConfig);
                return(Content <string>(HttpStatusCode.OK, "var trifoliaConfig = " + clientConfigJson + ";", new Formatters.JavaScriptFormatter(), "text/javascript"));
            }

            return(Content <ClientConfigModel>(HttpStatusCode.OK, clientConfig));
        }
Example #2
0
        public IHttpActionResult GetClientConfig(bool isRef = false)
        {
            var clientConfig = new ClientConfigModel();

            //Collect all methods in script
            Assembly assembly = Assembly.GetExecutingAssembly();

            //Find all FHIR API IGControllers
            var FHIRClasses = assembly.GetTypes()
                              .Where(t => t.IsClass && t.GetCustomAttributes(typeof(FHIRInfo)).Any())
                              .ToArray();

            foreach (var FHIRClass in FHIRClasses)
            {
                //Get the attributes of the FHIR IGController being examined
                var attributes = FHIRClass.GetCustomAttributes();

                //Collect the version attribute of that IGController
                var versionAttribute = (FHIRInfo)attributes.Single(a => a.GetType() == typeof(FHIRInfo));

                //Find the igType in the database
                ImplementationGuideType igType = this.tdb.ImplementationGuideTypes.SingleOrDefault(y => y.Name.ToLower() == versionAttribute.IGType.ToLower());

                //If doesn't exist, throw an error but continue
                if (igType == null)
                {
                    Log.For(this).Error("Implementation guide type defined in web.config not found in database: " + versionAttribute.IGType);
                    continue;
                }

                //Get the route attribute
                var routeCheck = attributes.Where(a => a.GetType() == typeof(RoutePrefixAttribute));

                //Make sure there's exactly one route attribute (shouldn't be possible but doesn't hurt to check)
                if (routeCheck.Count() > 1 || routeCheck.Count() == 0)
                {
                    throw new Exception("There are " + routeCheck.Count().ToString() + " route prefixes when there should be 1 for FHIR " + versionAttribute.Version);
                }

                //Get the route attribute of the IGController
                var routeAttribute = (RoutePrefixAttribute)attributes.Single(a => a.GetType() == typeof(RoutePrefixAttribute));

                var fhirIgType = new ClientConfigModel.FhirIgType()
                {
                    Id      = igType.Id,
                    Name    = igType.Name,
                    Version = versionAttribute.Version,
                    BaseUrl = "/" + routeAttribute.Prefix + "/"
                };

                clientConfig.FhirIgTypes.Add(fhirIgType);
            }

            if (isRef)
            {
                var clientConfigJson = Newtonsoft.Json.JsonConvert.SerializeObject(clientConfig);
                return(Content <string>(HttpStatusCode.OK, "var trifoliaConfig = " + clientConfigJson + ";", new Formatters.JavaScriptFormatter(), "text/javascript"));
            }

            return(Content <ClientConfigModel>(HttpStatusCode.OK, clientConfig));
        }