USDADataImport.USDAImport.FoodDesToIngredient C# (CSharp) Méthode

FoodDesToIngredient() public méthode

public FoodDesToIngredient ( USDADataImport foodDes, List food_groups ) : Ingredient
foodDes USDADataImport
food_groups List
Résultat Spontaneous.DataModel.Ingredient
        public Ingredient FoodDesToIngredient(USDADataImport.FOOD_DES foodDes, List<USDADataImport.FD_GROUP> food_groups)
        {
            log.InfoFormat("[FoodDesToIngredient] foodDes.NDB_No={0}, food_groups.count={1}.", foodDes.NDB_No, food_groups.Count);
            try
            {
                Spontaneous.DataModel.Ingredient tempIngredient = new Spontaneous.DataModel.Ingredient()
                {
                    Amount = new IncredientAmount() { Weight = 100 },
                    USDA_NDB_No = int.Parse(foodDes.NDB_No),
                    NutritionFacts = NutDataListToNutritionFacts(foodDes.NUT_DATA.ToList()),
                    Weigts = new List<Spontaneous.DataModel.Foods.Weight>(),
                    Langual = new List<string>()
                };

                if (foodDes.Long_Desc != null) tempIngredient.LongDesc = new Localized(foodDes.Long_Desc);
                if (foodDes.Shrt_Desc != null) tempIngredient.ShrtDesc = new Localized(foodDes.Shrt_Desc);
                if (foodDes.ComName != null) tempIngredient.ComName = new Localized(foodDes.ComName);
                if (foodDes.ManufacName != null) tempIngredient.ManufacName = new Localized(foodDes.ManufacName);
                if (foodDes.Ref_Desc != null) tempIngredient.RefDesc = new Localized(foodDes.Ref_Desc);
                if (foodDes.SciName != null) tempIngredient.SciName = new Localized(foodDes.SciName);
                if (foodDes.Refuse != null && foodDes.Refuse.HasValue) tempIngredient.Refuse = foodDes.Refuse.Value;
                if (foodDes.N_Factor != null && foodDes.N_Factor.HasValue) tempIngredient.NFactor = foodDes.N_Factor.Value;
                if (foodDes.Pro_Factor != null && foodDes.Pro_Factor.HasValue) tempIngredient.ProFactor = foodDes.Pro_Factor.Value;
                if (foodDes.Fat_Factor != null && foodDes.Fat_Factor.HasValue) tempIngredient.FatFactor = foodDes.Fat_Factor.Value;
                if (foodDes.CHO_Factor != null && foodDes.CHO_Factor.HasValue) tempIngredient.CHOFactor = foodDes.CHO_Factor.Value;

                tempIngredient.FoodGrp = (FoodGroupTypeEnum)int.Parse(food_groups.FirstOrDefault(c => c.FdGrp_CD == foodDes.FdGrp_Cd).FdGrp_CD);

                //Add Weigts
                foreach (var weight in foodDes.WEIGHT)
                {
                    Spontaneous.DataModel.Foods.Weight tempWeight = new Spontaneous.DataModel.Foods.Weight()
                    {
                        NDB_No = tempIngredient.USDA_NDB_No,
                        Seq = int.Parse(weight.Seq),
                        Amount = weight.Amount.Value,
                        Msre_Desc = weight.Msre_Desc,
                        Gm_Wgt = weight.Gm_Wgt.Value
                    };

                    if (weight.Num_Data_Pts != null && weight.Num_Data_Pts.HasValue) tempWeight.Num_Data_Pts = weight.Num_Data_Pts.Value;
                    if (weight.Std_Dev != null && weight.Std_Dev.HasValue) tempWeight.Std_Dev = weight.Std_Dev.Value;

                    tempIngredient.Weigts.Add(tempWeight);
                }

                //Add Langual
                foreach (var tempLangDesc in foodDes.LANGDESC)
                {
                    string temp_lang = tempLangDesc != null ? tempLangDesc.ToString() : "";
                    tempIngredient.Langual.Add(temp_lang);
                }

                return tempIngredient;
            }
            catch (Exception e)
            {
                log.ErrorFormat("[FoodDesToIngredient] Exception={0}", e);
                return null;
            }
        }

Usage Example

        public void FoodDesToIngredientTest_ShouldConvertFOOD_DES_objectToIngreadientObjectWithAllParameters()
        {
            //arrange

            USDADataImport.FOOD_DES food1 = new USDADataImport.FOOD_DES
            {
                NDB_No = "1",
                FdGrp_Cd = "0100", //Dairy and Egg Products
                Long_Desc = "Long_Desc",
                Shrt_Desc = "Shrt_Desc",
                ComName = "ComName",
                ManufacName = "ManufacName",
                Ref_Desc = "Ref_Desc",
                Refuse = 0,
                SciName = "SciName",
                N_Factor = 0,
                Pro_Factor = 0,
                Fat_Factor = 0,
                CHO_Factor = 0,
                NUT_DATA = new List<NUT_DATA>(),
                WEIGHT = new List<WEIGHT>(),
                LANGDESC = new List<LANGDESC>()
            };

            NUT_DATA NutData1 = new NUT_DATA
            {
                NDB_No = "1",
                Nutr_No = "203",
                Nutr_Val = 101,
                Num_Data_Pts = 1,
                Std_Error = 1,
                Src_Cd = "Src_Cd",
                Deriv_Cd = "Deriv_Cd",
                Ref_NDB_No = "Ref_NDB_No",
                Add_Nutr_Mark = "Add_Nutr_Mark",
                Num_Studies = 1,
                Min = 101,
                Max = 201,
                DF = 1,
                Low_EB = 1,
                Up_EB = 1,
                Stat_Cmt = "Stat_Cmt",
                AddMod_Date = "12/2013",
            };
            NUT_DATA NutData2 = new NUT_DATA
            {
                NDB_No = "1",
                Nutr_No = "205",
                Nutr_Val = 102,
                Num_Data_Pts = 2,
                Std_Error = 2,
                Src_Cd = "Src_Cd",
                Deriv_Cd = "Deriv_Cd",
                Ref_NDB_No = "Ref_NDB_No",
                Add_Nutr_Mark = "Add_Nutr_Mark",
                Num_Studies = 2,
                Min = 102,
                Max = 202,
                DF = 2,
                Low_EB = 2,
                Up_EB = 2,
                Stat_Cmt = "Stat_Cmt",
                AddMod_Date = "12/2013",
            };
            NUT_DATA NutData3 = new NUT_DATA
            {
                NDB_No = "1",
                Nutr_No = "210",
                Nutr_Val = 103,
                Num_Data_Pts = 3,
                Std_Error = 3,
                Src_Cd = "Src_Cd",
                Deriv_Cd = "Deriv_Cd",
                Ref_NDB_No = "Ref_NDB_No",
                Add_Nutr_Mark = "Add_Nutr_Mark",
                Num_Studies = 3,
                Min = 103,
                Max = 203,
                DF = 3,
                Low_EB = 3,
                Up_EB = 3,
                Stat_Cmt = "Stat_Cmt",
                AddMod_Date = "12/2013",
            };
            food1.NUT_DATA.Add(NutData1);
            food1.NUT_DATA.Add(NutData2);
            food1.NUT_DATA.Add(NutData3);
            WEIGHT weight1 = new WEIGHT
            {
                NDB_No = "1",
                Seq = "1",
                Amount = 1,
                Msre_Desc = "stick",
                Gm_Wgt = 21
            };
            WEIGHT weight2 = new WEIGHT
            {
                NDB_No = "1",
                Seq = "2",
                Amount = 2,
                Msre_Desc = "serving 5 sticks",
                Gm_Wgt = 107
            };
            food1.WEIGHT.Add(weight1);
            food1.WEIGHT.Add(weight2);

            List<FD_GROUP> food_groups = new List<FD_GROUP>();
            food_groups.Add(new FD_GROUP { FdGrp_CD = "0100", FdGrp_Desc = "Dairy and Egg Products" });
            food_groups.Add(new FD_GROUP { FdGrp_CD = "0200", FdGrp_Desc = "Spices and Herbs" });
            food_groups.Add(new FD_GROUP { FdGrp_CD = "0300", FdGrp_Desc = "Baby Foods" });

            //act

            USDAImport usdaImport = new USDAImport();
            Ingredient ingredient = usdaImport.FoodDesToIngredient(food1, food_groups);

            //assert
            Assert.IsNotNull(ingredient);
            Assert.IsNotNull(ingredient.NutritionFacts);
            Assert.IsNotNull(ingredient.NutritionFacts.FullNutrisionList);
            Assert.AreEqual(ingredient.NutritionFacts.FullNutrisionList.Count, 3);
            Assert.IsNotNull(ingredient.Weigts);
            Assert.AreEqual(ingredient.Weigts.Count, 2);
            Assert.AreEqual((int)ingredient.FoodGrp, 0100);
        }