private MapComponent GetMapComponentFromViewModel(MapComponentViewModel mapcomponentvm)
        {
            MapComponent mapcomponent = null;

            if (mapcomponentvm.Id > 0)
            {
                ValidateMapComponentGenotype(mapcomponentvm.Id, mapcomponentvm.GenotypeId);
                mapcomponent = u_repo.GetMapComponent(mapcomponentvm.Id);
                mapcomponentvm.ToMapComponent(mapcomponent);
            }
            else
            {
                mapcomponent           = mapcomponentvm.ToMapComponent();
                mapcomponent.isRemoved = false;
            }


            if (mapcomponent.GenotypeId.HasValue)
            {
                mapcomponent.Genotype = u_repo.GetGenotype(mapcomponent.GenotypeId.Value);
            }

            if (mapcomponent.Map == null)
            {
                mapcomponent.Map = u_repo.GetMap(mapcomponent.MapId);
            }


            return(mapcomponent);
        }
        public void Save(MapComponentViewModel mapcomponentvm)
        {
            Years year = u_repo.GetYear(mapcomponentvm.YearsId);

            if (year == null || mapcomponentvm.YearsId <= 0)
            {
                throw new MapException(Properties.Settings.Default.ExceptionMapComponentInvalid);
            }


            MapComponent mapcomponent = GetMapComponentFromViewModel(mapcomponentvm);


            if (mapcomponent.Id > 0)
            {
                UpdateMapComponent(mapcomponent, mapcomponentvm.YearsId);
            }
            else
            {
                CreateMapComponent(mapcomponent, mapcomponentvm.YearsId);
            }


            MapComponentViewModel newMapComponent = u_repo.GetMapComponent(mapcomponent.Id).ToMapComponentViewModel();

            newMapComponent.CopyTo(mapcomponentvm);
        }
        public ActionResult AddMapComponent(MapComponentViewModel mapcomp)
        {
            ActionResult result;
            Map          map = m_repo.GetMap(mapcomp.MapId);

            ViewBag.GenusId = map.GenusId;
            Genus genus = map.Genus;

            ViewBag.GenusId = genus.Id;
            ViewBag.Virus1  = genus.VirusLabel1;
            ViewBag.Virus2  = genus.VirusLabel2;
            ViewBag.Virus3  = genus.VirusLabel3;
            ViewBag.Virus4  = genus.VirusLabel4;

            if (ModelState.IsValid)
            {
                map_repo.Save(mapcomp);
                result = PartialView("_MapComponentRow", mapcomp);
            }
            else
            {
                result = PartialView("_MapComponent", mapcomp);
            }

            return(result);
        }
        /// <summary>
        /// Returns a mapcomponent from a mapcomponentviewmodel
        /// </summary>
        /// <param name="mapcomponent"></param>
        /// <returns></returns>
        public static MapComponent ToMapComponent(this MapComponentViewModel mapcomponentvm)
        {
            MapComponent mapcomponent = new MapComponent();

            mapcomponentvm.CopyTo(mapcomponent);
            return(mapcomponent);
        }
        /// <summary>
        /// Returns a mapcomponent from a mapcomponent viewmodel and copied from mapcomponent
        /// </summary>
        /// <param name="mapcomponent"></param>
        /// <param name="old"></param>
        /// <returns></returns>
        public static MapComponent ToMapComponent(this MapComponentViewModel mapcomponentvm, MapComponent old)
        {
            mapcomponentvm.isSeedling    = old.isSeedling;
            mapcomponentvm.ExternalId    = old.ExternalId;
            mapcomponentvm.PickingNumber = old.PickingNumber;

            mapcomponentvm.CopyTo(old);

            return(old);
        }
        //public async Task<JsonResult> UpdateMapComponent(MapComponentViewModel mapcomp)
        //{
        //    var result = Json(new { Error = false, Message = "" });
        //    if (!ModelState.IsValid)
        //    {
        //        result = Json(new { Error = true, Message = "Invalid" });
        //    }
        //    else
        //    {
        //        try
        //        {
        //            await Task.Run(() =>  map_repo.Save(mapcomp));
        //        }
        //        catch (MapException e)
        //        {
        //            result = Json(new { Error = true, Message = e.Message });
        //        }

        //    }
        //    return result;

        //}

        public JsonResult UpdateMapComponent(MapComponentViewModel mapcomp)
        {
            var result = Json(new { Error = false, Message = "" });

            if (!ModelState.IsValid)
            {
                result = Json(new { Error = true, Message = "Invalid" });
            }
            else
            {
                try
                {
                    map_repo.Save(mapcomp);
                }
                catch (MapException e)
                {
                    result = Json(new { Error = true, Message = e.Message });
                }
            }
            return(result);
        }
        public JsonResult UpdateMapComponentGenotype(MapComponentViewModel mapcomp)
        {
            var result = Json(new { Error = false, Message = "" });

            if (mapcomp.Id <= 0)
            {
                result = Json(new { Error = true, Message = "Invalid" });
            }
            else
            {
                try
                {
                    map_repo.UpdateMapComponentGenotype(mapcomp.Id, mapcomp.GenotypeId);
                }
                catch (MapException e)
                {
                    result = Json(new { Error = true, Message = e.Message });
                }
            }

            return(result);
        }