Example #1
0
        public ActionResult SavePlant(PlantModel plantModel)
        {
            try
            {
                SubjectManager     subjectManager     = new SubjectManager();
                InteractionManager interactionManager = new InteractionManager();

                Plant plant = new Plant();
                if (plantModel.Id > 0)
                {
                    var x = subjectManager.Get(plantModel.Id).Self;
                    plant = x as Plant;
                }

                plant.Name           = plantModel.Name;
                plant.ScientificName = plantModel.ScientificName;

                //Todo Select the type based on the scientific name
                //plant.Rank = Utility.GetTaxonRank(plantModel.ScientificName);
                plant.Rank = plantModel.TaxonRank;

                #region after culture

                foreach (var ac in plantModel.AfterCultures)
                {
                    if (string.IsNullOrEmpty(ac.Name))
                    {
                        break;
                    }

                    Plant afterCultureTmp = null;

                    //add new culture to plant
                    if (ac.Id == 0)
                    {
                        afterCultureTmp = subjectManager.GetAllAsQueryable <Plant>().Where(p => p.Name.Equals(ac.Name)).FirstOrDefault();
                        if (afterCultureTmp != null)
                        {
                            plant.AfterCultures.Add(subjectManager.Get(afterCultureTmp.Id) as Plant);
                        }
                    }
                }

                //delete cultures
                List <string> nameOfDeleteAfterCultures = new List <string>();
                foreach (var ac in plant.AfterCultures)
                {
                    if (!plantModel.AfterCultures.Any(a => a.Name.Equals(ac.Name)))
                    {
                        nameOfDeleteAfterCultures.Add(ac.Name);
                    }
                }

                foreach (var name in nameOfDeleteAfterCultures)
                {
                    var tmp = plant.AfterCultures.Where(a => a.Name.Equals(name)).FirstOrDefault();
                    if (tmp != null)
                    {
                        plant.AfterCultures.Remove(tmp);
                    }
                }

                #endregion after culture

                #region preculture

                foreach (var pc in plantModel.PreCultures)
                {
                    if (string.IsNullOrEmpty(pc.Name))
                    {
                        break;
                    }

                    Plant preCultureTmp = null;;

                    //add new culture to plant
                    if (pc.Id == 0)
                    {
                        preCultureTmp = subjectManager.GetAllAsQueryable <Plant>().Where(p => p.Name.Equals(pc.Name)).FirstOrDefault();
                        if (preCultureTmp != null)
                        {
                            plant.PreCultures.Add(subjectManager.Get(preCultureTmp.Id) as Plant);
                        }
                    }
                }

                //delete cultures
                List <string> nameOfDeletePreCultures = new List <string>();
                foreach (var ac in plant.PreCultures)
                {
                    if (!plantModel.PreCultures.Any(a => a.Name.Equals(ac.Name)))
                    {
                        nameOfDeletePreCultures.Add(ac.Name);
                    }
                }

                foreach (var name in nameOfDeletePreCultures)
                {
                    var tmp = plant.PreCultures.Where(a => a.Name.Equals(name)).FirstOrDefault();
                    if (tmp != null)
                    {
                        plant.PreCultures.Remove(tmp);
                    }
                }

                #endregion preculture

                plant.Description  = plantModel.Description;
                plant.Height       = plantModel.Height;
                plant.LocationType = plantModel.LocationType;

                if (!plantModel.ImagePath.Equals("/Images/Empty.png") && !plant.Medias.Where(m => m.ImagePath.Equals(plantModel.ImagePath)).Any())
                {
                    plant.Medias.Add(new Media()
                    {
                        ImagePath = plantModel.ImagePath,
                        MIMEType  = MimeMapping.GetMimeMapping(plantModel.ImagePath)
                    });
                }

                plant.NutrientClaim = plantModel.NutrientClaim;
                plant.RootDepth     = plantModel.RootDepth;
                plant.SowingDepth   = plantModel.SowingDepth;
                plant.Width         = plantModel.Width;

                plant.TimePeriods = new List <TimePeriod>();

                foreach (var lifeCylce in plantModel.LifeCycles)
                {
                    lifeCylce.Reverse();
                    TimePeriod last = null;
                    foreach (var tpModel in lifeCylce)
                    {
                        TimePeriod tp = Utility.CreateTimePeriodFromModel(tpModel);
                        tp.AssignedTo = plant;

                        if (lifeCylce.Last().Equals(tpModel))
                        {
                            tp.Start = true;
                        }
                        if (last != null)
                        {
                            tp.Next = last;
                        }

                        plant.TimePeriods.Add(tp);

                        last = tp;
                    }

                    plant.TimePeriods.Reverse();
                }

                if (plantModel.Id == 0)
                {
                    plant = subjectManager.CreatePlant(plant);
                }
                else
                {
                    plant = subjectManager.UpdatePlant(plant);
                }

                //set parent
                if (plantModel.Parent != null && plantModel.Parent.Id > 0)
                {
                    var parent = subjectManager.GetAll <Node>().Where(n => n.Id.Equals(plantModel.Parent.Id)).FirstOrDefault();
                    plant.Parent = parent;
                }
                else // delete parent from plant
                {
                    plant.Parent = null;
                }

                subjectManager.Update(plant);

                return(Json(plant.Id, JsonRequestBehavior.AllowGet));
            }
            catch (Exception ex)
            {
                return(Json(ex.Message, JsonRequestBehavior.AllowGet));
            }
        }