Beispiel #1
0
        private static Section GetSection(BsonDocument sectionBsonDocument)
        {
            Section sectObj;

            string sectionType = sectionBsonDocument.GetValue("SectionType")?.ToString();

            Debug.WriteLine("Section Type:" + sectionType);
            switch (sectionType?.ToLower())
            {
            case "image":
                ImageSection imgSectObj = BsonSerializer.Deserialize <ImageSection>(sectionBsonDocument);
                Content      imgContent = Contents.GetFor(imgSectObj);
                if (imgContent != null)
                {
                    imgSectObj.Title   = imgContent.Title;
                    imgSectObj.Caption = imgContent.Caption;
                }
                sectObj = imgSectObj;
                break;

            case "text":
                TextSection textSectObj = BsonSerializer.Deserialize <TextSection>(sectionBsonDocument);
                Content     textContent = Contents.GetFor(textSectObj);
                if (textContent != null)
                {
                    textSectObj.Text = textContent.SectionText;
                    if (textSectObj.DelayInMs == 0)
                    {
                        textSectObj.DelayInMs = Math.Min(Utils.Settings.MaxCapTimeTakenToType, textSectObj.Text.Length * (Utils.Settings.BaseTimeTakenToTypePerChar + rand.Next(0, Utils.Settings.VariableTimeTakenToTypePerChar)));
                    }
                }
                sectObj = textSectObj;
                break;

            case "graph":
            {
                GraphSection gphObj = BsonSerializer.Deserialize <GraphSection>(sectionBsonDocument);

                Content docContent = Contents.GetFor(gphObj);
                if (docContent != null)
                {
                    gphObj.Caption = docContent.Caption;

                    gphObj.X.Label = docContent.XLabel;
                    gphObj.Y.Label = docContent.YLabel;
                }
                gphObj.CoordinatesSet = new List <Coordinates>();
                BsonArray coordinateSetBsonArray = sectionBsonDocument.GetValue("CoordinatesSet").AsBsonArray;


                if (coordinateSetBsonArray != null)
                {
                    foreach (BsonDocument coordinateSetBsonDoc in coordinateSetBsonArray)
                    {
                        var coordinateListId = coordinateSetBsonDoc.GetValue("CoordinateListId")?.ToString();
                        if (!string.IsNullOrWhiteSpace(coordinateListId))
                        {
                            continue;
                        }

                        var coordinatesObj = new Coordinates()
                        {
                            CoordinateListId = coordinateListId
                        };

                        var coordinateContent = Contents.GetFor(coordinatesObj);
                        coordinatesObj.LegendName = coordinateContent?.CoordinateListLegend;

                        if (coordinateSetBsonDoc.TryGetValue("CoordinateList", out BsonValue tempCoordinateList))
                        {
                            BsonArray coordinateListBsonArray = tempCoordinateList?.AsBsonArray;
                            if (coordinateListBsonArray != null)
                            {
                                foreach (BsonDocument coordinateBsonDoc in coordinateListBsonArray)
                                {
                                    string x = coordinateBsonDoc.GetValue("X")?.AsString;
                                    string y = coordinateBsonDoc.GetValue("Y")?.AsString;

                                    string coordinateText = coordinateContent?.CoordinateText;

                                    if (string.IsNullOrWhiteSpace(coordinateText))
                                    {
                                        coordinatesObj.AddXYCoordinates(x, y);
                                    }
                                    else
                                    {
                                        coordinatesObj.AddXYCoordinates(x, y, coordinateText);
                                    }

                                    Debug.WriteLine(coordinatesObj.ToJson());
                                }
                            }
                        }
                        gphObj.CoordinatesSet.Add(coordinatesObj);
                    }
                }
                sectObj = gphObj;
            }

            break;

            case "gif":
                GifSection gifObj     = BsonSerializer.Deserialize <GifSection>(sectionBsonDocument);
                Content    gifContent = Contents.GetFor(gifObj);
                if (gifContent != null)
                {
                    gifObj.Title   = gifContent.Title;
                    gifObj.Caption = gifContent.Caption;
                }
                sectObj = gifObj;
                break;

            case "audio":
                AudioSection audioObj     = BsonSerializer.Deserialize <AudioSection>(sectionBsonDocument);
                Content      audioContent = Contents.GetFor(audioObj);
                if (audioContent != null)
                {
                    audioObj.Title   = audioContent.Title;
                    audioObj.Caption = audioContent.Caption;
                }

                sectObj = audioObj;
                break;

            case "video":
                VideoSection videoObj     = BsonSerializer.Deserialize <VideoSection>(sectionBsonDocument);
                Content      videoContent = Contents.GetFor(videoObj);
                if (videoContent != null)
                {
                    videoObj.Title   = videoContent.Title;
                    videoObj.Caption = videoContent.Caption;
                }
                sectObj = videoObj;
                break;

            case "link":
                UrlSection urlObj = BsonSerializer.Deserialize <UrlSection>(sectionBsonDocument);
                sectObj = urlObj;
                break;

            case "embeddedhtml":
                EmbeddedHtmlSection embeddedHtmlObj     = BsonSerializer.Deserialize <EmbeddedHtmlSection>(sectionBsonDocument);
                Content             embeddedHtmlContent = Contents.GetFor(embeddedHtmlObj);
                if (embeddedHtmlContent != null)
                {
                    embeddedHtmlObj.Title   = embeddedHtmlContent.Title;
                    embeddedHtmlObj.Caption = embeddedHtmlContent.Caption;
                }
                sectObj = embeddedHtmlObj;
                break;

            case "carousel":
                var carouselObj = BsonSerializer.Deserialize <CarouselSection>(sectionBsonDocument);
                var carContent  = Contents.GetFor(carouselObj);
                if (carContent != null)
                {
                    carouselObj.Title   = carContent.Title;
                    carouselObj.Caption = carContent.Caption;
                }
                if (carouselObj.Items != null)
                {
                    foreach (var carItem in carouselObj.Items)
                    {
                        var content = Contents.GetFor(carItem);
                        if (content != null)
                        {
                            carItem.Title   = content.Title;
                            carItem.Caption = content.Caption;
                        }
                        if (carItem.Buttons != null)
                        {
                            foreach (var carBtn in carItem.Buttons)
                            {
                                var carBtnContent = Contents.GetFor(carBtn);
                                carBtn.Text = carBtnContent?.ButtonText;
                            }
                        }
                    }
                }
                sectObj = carouselObj;
                break;

            case "printotp":
                PrintOTPSection printOTPSection = BsonSerializer.Deserialize <PrintOTPSection>(sectionBsonDocument);
                //No content to load for Section Type: PrintOTP
                sectObj = printOTPSection;
                break;

            default:
                sectObj = null;
                break;
            }
            return(sectObj);
        }
Beispiel #2
0
        private static Section GetSection(BsonDocument sectionBsonDocument)
        {
            Random rnd = new Random();

            Section sectObj;

            string sectionType = sectionBsonDocument.GetValue("SectionType")?.ToString();

            Debug.WriteLine("Section Type:" + sectionType);
            switch (sectionType?.ToLower())
            {
            case "image":
                ImageSection imgSectObj = BsonSerializer.Deserialize <ImageSection>(sectionBsonDocument);
                Content      imgContent = Contents.GetFor(imgSectObj);
                if (imgContent != null)
                {
                    imgSectObj.Title   = imgContent.Title;
                    imgSectObj.Caption = imgContent.Caption;
                }

                sectObj = imgSectObj;

                break;

            case "text":
                TextSection textSectObj = BsonSerializer.Deserialize <TextSection>(sectionBsonDocument);
                Content     textContent = Contents.GetFor(textSectObj);
                if (textContent != null)
                {
                    textSectObj.Text = textContent.SectionText;
                }
                sectObj = textSectObj;
                break;

            case "graph":

                GraphSection gphObj = BsonSerializer.Deserialize <GraphSection>(sectionBsonDocument);

                Content docContent = Contents.GetFor(gphObj);
                if (docContent != null)
                {
                    gphObj.Caption = docContent.Caption;

                    gphObj.X.Label = docContent.XLabel;
                    gphObj.Y.Label = docContent.YLabel;
                }
                gphObj.CoordinatesSet = new List <Coordinates>();
                BsonArray coordinateSetBsonArray = sectionBsonDocument.GetValue("CoordinatesSet").AsBsonArray;


                if (coordinateSetBsonArray != null)
                {
                    foreach (BsonDocument coordinateSetBsonDoc in coordinateSetBsonArray)
                    {
                        var coordinateListId = coordinateSetBsonDoc.GetValue("CoordinateListId")?.ToString();
                        if (!string.IsNullOrWhiteSpace(coordinateListId))
                        {
                            continue;
                        }

                        var coordinatesObj = new Coordinates()
                        {
                            CoordinateListId = coordinateListId
                        };

                        var coordinateContent = Contents.GetFor(coordinatesObj);
                        coordinatesObj.LegendName = coordinateContent?.CoordinateListLegend;

                        if (coordinateSetBsonDoc.TryGetValue("CoordinateList", out BsonValue tempCoordinateList))
                        {
                            BsonArray coordinateListBsonArray = tempCoordinateList?.AsBsonArray;
                            if (coordinateListBsonArray != null)
                            {
                                foreach (BsonDocument coordinateBsonDoc in coordinateListBsonArray)
                                {
                                    string x = coordinateBsonDoc.GetValue("X")?.AsString;
                                    string y = coordinateBsonDoc.GetValue("Y")?.AsString;

                                    string coordinateText = coordinateContent?.CoordinateText;

                                    if (string.IsNullOrWhiteSpace(coordinateText))
                                    {
                                        coordinatesObj.AddXYCoordinates(x, y);
                                    }
                                    else
                                    {
                                        coordinatesObj.AddXYCoordinates(x, y, coordinateText);
                                    }

                                    Debug.WriteLine(coordinatesObj.ToJson());
                                }
                            }
                        }
                        gphObj.CoordinatesSet.Add(coordinatesObj);
                    }
                }
                sectObj = gphObj;

                break;

            case "gif":
                GifSection gifObj     = BsonSerializer.Deserialize <GifSection>(sectionBsonDocument);
                Content    gifContent = Contents.GetFor(gifObj);
                if (gifContent != null)
                {
                    gifObj.Title   = gifContent.Title;
                    gifObj.Caption = gifContent.Caption;
                }
                sectObj = gifObj;
                break;

            case "audio":
                AudioSection audioObj     = BsonSerializer.Deserialize <AudioSection>(sectionBsonDocument);
                Content      audioContent = Contents.GetFor(audioObj);
                if (audioContent != null)
                {
                    audioObj.Title   = audioContent.Title;
                    audioObj.Caption = audioContent.Caption;
                }

                sectObj = audioObj;
                break;

            case "video":
                VideoSection videoObj     = BsonSerializer.Deserialize <VideoSection>(sectionBsonDocument);
                Content      videoContent = Contents.GetFor(videoObj);
                if (videoContent != null)
                {
                    videoObj.Title   = videoContent.Title;
                    videoObj.Caption = videoContent.Caption;
                }
                sectObj = videoObj;
                break;

            case "link":
                UrlSection urlObj = BsonSerializer.Deserialize <UrlSection>(sectionBsonDocument);
                sectObj = urlObj;
                break;

            case "embeddedhtml":
                EmbeddedHtmlSection embeddedHtmlObj     = BsonSerializer.Deserialize <EmbeddedHtmlSection>(sectionBsonDocument);
                Content             embeddedHtmlContent = Contents.GetFor(embeddedHtmlObj);
                if (embeddedHtmlContent != null)
                {
                    embeddedHtmlObj.Title   = embeddedHtmlContent.Title;
                    embeddedHtmlObj.Caption = embeddedHtmlContent.Caption;
                }
                sectObj = embeddedHtmlObj;
                break;

            default:
                sectObj = null;
                break;
            }
            return(sectObj);
        }
        /// <summary>
        /// Run the demo.
        /// </summary>
        public static void Run()
        {
            Dictionary <string, string> configDic = new Dictionary <string, string>()
            {
                { "str_setting_1", "str_value_1" },
                { "int_setting_1", "1" },
                { "poco_section:poco_setting_1", "poco_value_1" },
                { "array_section:0:item_setting", "item_value_1" },
                { "array_section:1:item_setting", "item_value_2" },
                { "graph_section:graph_sub_section:graph_setting_1", "graph_value_1" },
            };

            IConfigurationBuilder configBuilder = new ConfigurationBuilder()
                                                  .AddInMemoryCollection(configDic);

            IConfiguration config = configBuilder.Build();

            Action <string, Func <object> > getValueAction =
                (path, getValueFunc) =>
            {
                object value = getValueFunc();
                Console.WriteLine($"path: '{path}', value: '{value}'");
            };

            // get string value demo
            getValueAction(
                "str_setting_1",
                () =>
            {
                string value = config["str_setting_1"];
                return(value);
            });

            // get int value demo, using GetValue<T> method
            getValueAction(
                "int_setting_1",
                () =>
            {
                int value =
                    config.GetValue <int>("int_setting_1", defaultValue: 0);
                return(value);
            });

            // get POCO value demo, using GetSection and Bind methods
            getValueAction(
                "poco_section:poco_setting_1",
                () =>
            {
                PocoSection pocoSection = new PocoSection();

                config.GetSection("poco_section").Bind(pocoSection);
                string value = pocoSection.poco_setting_1;
                return(value);
            });

            // get array item value demo, using GetSection and Bind methods
            getValueAction(
                "array_section:0:item_setting",
                () =>
            {
                List <ArrayItem> array = new List <ArrayItem>();
                config.GetSection("array_section").Bind(array);

                string value = array[0].item_setting;
                return(value);
            });

            // get graph object value demo, using GetSection and Get<T> methods
            getValueAction(
                "graph_section:graph_sub_section:graph_setting_1",
                () =>
            {
                GraphSection graphSection =
                    config.GetSection("graph_section").Get <GraphSection>();
                string value = graphSection.graph_sub_section.graph_setting_1;
                return(value);
            });

            // print config dictionary in memory
            Console.WriteLine();
            Console.WriteLine("[Trace] config dictionary in memory:");
            string configDicContent =
                string.Join(
                    Environment.NewLine,
                    configDic.Select(kvp => $"{kvp.Key} = {kvp.Value}"));

            Console.WriteLine(configDicContent);
        }