private void buttonOK_Click(object sender, EventArgs e)
        {
            if (!ValidateCreatureIdentity())
            {
                this.DialogResult = System.Windows.Forms.DialogResult.None;
            }
            else
            {
                try
                {
                    if (OpMode == CreatureViewEditOpType.New)
                    {
                        var newEntity = new CreatureEntity()
                        {
                            Id = CreatureEntity.GenerateNewCreatureId(context)
                        };
                        creature = new Creature(mainForm, newEntity, context, creatureColorDefinitions);
                    }

                    List <CreatureTrait> traitlist = new List <CreatureTrait>();
                    foreach (var item in checkedListBoxTraits.CheckedItems)
                    {
                        try
                        {
                            traitlist.Add(CreatureTrait.FromWurmTextRepr(item.ToString()));
                        }
                        catch (Exception _e)
                        {
                            logger.Error(_e, "failed to create creature trait from text:" + item.ToString());
                        }
                    }
                    var traitlistArray = traitlist.ToArray();

                    creature.Name          = textBoxName.Text.Trim();
                    creature.Father        = comboBoxFather.Text.Trim();
                    creature.Mother        = comboBoxMother.Text.Trim();
                    creature.TakenCareOfBy = textBoxCaredForBy.Text.Trim();
                    creature.BrandedFor    = textBoxBrandedFor.Text.Trim();

                    creature.Traits = traitlistArray;

                    creature.TraitsInspectSkill = (float)numericUpDownAHskill.Value;
                    float traitSkill = CreatureTrait.GetMinSkillForTraits(traitlistArray, checkBoxEpic.Checked);
                    if (creature.TraitsInspectSkill < traitSkill)
                    {
                        creature.TraitsInspectSkill = traitSkill;
                    }
                    creature.EpicCurve = checkBoxEpic.Checked;

                    creature.Comments = textBoxComment.Text;
                    creature.IsMale   = radioButtonMale.Checked;
                    creature.Color    = creatureColorDefinitions.GetForId(comboBoxColor.Text);
                    creature.Age      = CreatureAge.CreateAgeFromEnumString(comboBoxAge.Text);

                    creature.NotInMoodUntil = dateTimePickerBred.Value;
                    creature.GroomedOn      = dateTimePickerGroomed.Value;
                    creature.PregnantUntil  = dateTimePickerPregnant.Value;
                    creature.BirthDate      = dateTimePickerBirthDate.Value;

                    creature.SetTag("diseased", checkBoxDiseased.Checked);
                    creature.SetTag("dead", checkBoxDead.Checked);
                    creature.SetTag("sold", checkBoxSold.Checked);

                    creature.ServerName = comboBoxServerName.Text.Trim();

                    if (OpMode == CreatureViewEditOpType.New)
                    {
                        creature.Herd = herdId;
                        context.InsertCreature(creature.Entity);
                    }
                    else
                    {
                        context.SubmitChanges();
                    }
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                }
                catch (Exception _e)
                {
                    logger.Error(_e, "problem while updating database, op: " + OpMode);
                    MessageBox.Show("There was a problem on submitting to database.\r\n" + _e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.DialogResult = System.Windows.Forms.DialogResult.None;
                }
            }
        }
Пример #2
0
        public void ImportHerd(GrangerContext context, string newHerdName, string xmlFilePath)
        {
            if (newHerdName == null || newHerdName.Trim() == string.Empty)
            {
                throw new GrangerException("new herd name cannot be empty");
            }

            // check if this herd already exists in database
            if (context.Herds.Any(x => x.HerdId == newHerdName))
            {
                throw new GrangerException($"there is already a herd with named {newHerdName} in database");
            }

            XDocument doc = XDocument.Load(xmlFilePath);
            var       creatureEntities = new List <CreatureEntity>();
            var       elements         = doc.Root.Elements("Horse").Union(doc.Root.Elements("Creature"));

            foreach (var x in elements)
            {
                var entity = new CreatureEntity();
                entity.Herd = newHerdName;
                entity.Name = x.Element("Name").Value;

                if (creatureEntities.Any(y => y.Name.Equals(entity.Name, StringComparison.InvariantCultureIgnoreCase)))
                {
                    throw new GrangerException(
                              $"Creature named {entity.Name} was already added from this XML file. Review the file for any errors.");
                }

                entity.FatherName = x.Element("Father").Value;
                entity.MotherName = x.Element("Mother").Value;
                entity.Traits     = GetTraitsFromXML(x.Element("Traits"));

                var notInMood = x.Element("NotInMoodUntil").Value;
                if (string.IsNullOrEmpty(notInMood))
                {
                    entity.NotInMood = null;
                }
                else
                {
                    entity.NotInMood = DateTime.Parse(notInMood, CultureInfo.InvariantCulture);
                }

                var pregnantUntil = x.Element("PregnantUntil").Value;
                if (string.IsNullOrEmpty(pregnantUntil))
                {
                    entity.PregnantUntil = null;
                }
                else
                {
                    entity.PregnantUntil = DateTime.Parse(pregnantUntil, CultureInfo.InvariantCulture);
                }

                var groomedOn = x.Element("GroomedOn").Value;
                if (string.IsNullOrEmpty(groomedOn))
                {
                    entity.GroomedOn = null;
                }
                else
                {
                    entity.GroomedOn = DateTime.Parse(groomedOn, CultureInfo.InvariantCulture);
                }

                var gender = x.Element("Gender").Value;
                if (string.IsNullOrEmpty(gender))
                {
                    entity.IsMale = null;
                }
                else
                {
                    entity.IsMale = gender.Equals("male", StringComparison.InvariantCultureIgnoreCase);
                }

                entity.TakenCareOfBy = x.Element("CaredBy").Value;

                var xInspect = x.Element("InspectSkill");
                if (string.IsNullOrEmpty(xInspect.Value))
                {
                    entity.TraitsInspectedAtSkill = null;
                }
                else
                {
                    entity.TraitsInspectedAtSkill = float.Parse(xInspect.Value, CultureInfo.InvariantCulture);
                }
                var xInspectAttr = xInspect.Attribute("IsEpic");
                if (string.IsNullOrEmpty(xInspectAttr.Value))
                {
                    entity.EpicCurve = null;
                }
                else
                {
                    entity.EpicCurve = bool.Parse(xInspectAttr.Value);
                }

                entity.Age             = CreatureAge.CreateAgeFromEnumString(x.Element("Age").Value);
                entity.CreatureColorId = x.Element("CreatureColorId")?.Value ?? CreatureColorEntity.Unknown.Id;
                entity.Comments        = x.Element("Comments").Value;
                entity.SpecialTagsRaw  = x.Element("Tags").Value;
                entity.BrandedFor      = x.Element("BrandedFor").Value;
                var smilexaminedElement = x.Element("SmilexamineLastDate");
                if (smilexaminedElement != null && !string.IsNullOrWhiteSpace(smilexaminedElement.Value))
                {
                    entity.SmilexamineLastDate = DateTime.Parse(smilexaminedElement.Value, CultureInfo.InvariantCulture);
                }

                var serverNameElem = x.Element("ServerName");
                entity.ServerName = serverNameElem?.Value;

                creatureEntities.Add(entity);
            }

            context.InsertHerd(newHerdName);
            foreach (var creatureEntity in creatureEntities)
            {
                creatureEntity.Id = CreatureEntity.GenerateNewCreatureId(context);
                context.InsertCreature(creatureEntity);
            }
        }