Exemple #1
0
        public ActionResult SaveAnimal(AnimalModel animalModel)
        {
            try
            {
                SubjectManager     subjectManager     = new SubjectManager();
                InteractionManager interactionManager = new InteractionManager();

                Animal animal = new Animal();
                if (animalModel.Id > 0)
                {
                    var x = subjectManager.Get(animalModel.Id).Self;
                    animal = x as Animal;
                }

                animal.Name           = animalModel.Name;
                animal.ScientificName = animalModel.ScientificName;
                animal.Rank           = animalModel.TaxonRank;
                animal.Description    = animalModel.Description;
                //TODO Generate the Parent based on the ScientificName
                // a a a = SubSpecies, a a = Species, a = Genus
                // a a var. a is also a species

                /* - based on the scientficname create or set the parents
                 * - use maybe some webservices to create missing one
                 *
                 */

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

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

                //lifecycles
                animal.TimePeriods = new List <TimePeriod>();
                foreach (var lifeCylce in animalModel.LifeCycles)
                {
                    lifeCylce.Reverse();
                    TimePeriod last = null;
                    foreach (var tpModel in lifeCylce)
                    {
                        TimePeriod tp = Utility.CreateTimePeriodFromModel(tpModel);
                        tp.AssignedTo = animal;

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

                        animal.TimePeriods.Add(tp);

                        last = tp;
                    }

                    animal.TimePeriods.Reverse();
                }

                if (animal.Id == 0)
                {
                    animal = subjectManager.CreateAnimal(animal);
                }
                else
                {
                    subjectManager.Update(animal);
                    //animal.Parent = Utility.CreateOrSetParents(animal.ScientificName, typeof(Animal), subjectManager);
                    //subjectManager.Update(animal);
                }

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

                subjectManager.Update(animal);

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