public BusinessCompound InsertCompound(BusinessCompound compound)
        {
            var inserted = this.CompoundRepository.InsertCompound(compound.Compound);

            SaveChangesInCompound(compound, inserted);

            return(GetCompoundWithId(inserted.Id));
        }
 public void RemovePercentToCompoundAt(int percentId, BusinessCompound compound)
 {
     foreach (var item in compound.PercentageToCompound)
     {
         if (item.PercentId == percentId)
         {
             this._sqlPercentToCompoundRepository.DeletePercentToCompoundWithId(item.Id);
             compound.PercentageToCompound.Remove(item);
         }
     }
 }
        public BusinessCompound UpdateCompoundWithId(int id, BusinessCompound compound)
        {
            var inserted = compound.Compound;

            if (this.CompoundRepository.GetCompoundById(inserted.Id) == null)
            {
                compound.Compound = this.CompoundRepository.InsertCompound(inserted);
            }
            else if (inserted.WasModified)
            {
                this._sqlCompoundRepository.UpdateCompound(inserted.Id, inserted);
            }

            SaveChangesInCompound(compound, inserted);

            return(GetCompoundWithId(compound.Compound.Id));
        }
        public BusinessCompound GetCompoundWithId(int id)
        {
            var compound = this.CompoundRepository.GetCompoundById(id);

            if (compound != null)
            {
                var resultCompound = new BusinessCompound();
                resultCompound.Compound = compound;
                var connections = this.PercentToCompoundRepository.GetAllWithCompountId(id);

                if (connections.Count() > 0)
                {
                    resultCompound.PercentageToCompound = connections as List <PercentToCompound>;

                    foreach (var item in connections)
                    {
                        var percentage = this.PercentageRepository.GetPersentageById(item.PercentId);
                        if (percentage != null)
                        {
                            if (percentage.MatrixId != null)
                            {
                                var matrix = this.MatrixRepository.GetMatrixById((int)percentage.MatrixId);
                                resultCompound.Matrixes.Add(percentage, matrix);
                            }
                            else if (percentage.DopantId != null)
                            {
                                var dopant = this.DopantRepository.GetDopantById((int)percentage.DopantId);
                                resultCompound.Dopants.Add(percentage, dopant);
                            }
                        }
                    }
                }
                return(resultCompound);
            }

            return(null);
        }
 public CreateOrEditCompound(BusinessCompound compound)
     : this()
 {
     this._currentCompound = compound;
 }
 public CreateOrEditCompound()
 {
     InitializeComponent();
     this._currentCompound = new BusinessCompound();
     this._compoundServise = new CompoundService(this._connectionString);
 }
 private void SetName(BusinessCompound sender, EventArgs e)
 {
     sender.ResetDefaultName();
     this.Text         = sender.Name;
     this.lblName.Text = string.Format("Mame: {0}", sender.Name);
 }
        private void SaveChangesInCompound(BusinessCompound compound, Compound inserted)
        {
            Matrix     matrix;
            Dopant     dopant;
            Percentage percentage;

            for (int i = 0; i < compound.Matrixes.Keys.Count; i++)
            {
                percentage = compound.Matrixes.Keys.OrderBy(p => p.Number).ToArray()[i];
                matrix     = compound.Matrixes[percentage];

                compound.Matrixes.Remove(percentage);

                if (this.MatrixRepository.GetMatrixById(matrix.Id) != null)
                {
                    percentage.MatrixId = matrix.Id;
                }
                else
                {
                    throw new Exception(string.Format("There is no matrix {0} in database!", matrix.Id));
                }

                if (this.PercentageRepository.GetPercentageWithValues
                        (percentage.Number, percentage.MatrixId, percentage.DopantId) ==
                    null)
                {
                    percentage = this.PercentageRepository.InsertPersentage(percentage);
                }

                compound.Matrixes.Add(percentage, matrix);
            }

            for (int i = 0; i < compound.Dopants.Keys.Count; i++)
            {
                percentage = compound.Dopants.Keys.OrderBy(p => p.Number).ToArray()[i];
                dopant     = compound.Dopants[percentage];

                compound.Dopants.Remove(percentage);

                if (this.DopantRepository.GetDopantById(dopant.Id) != null)
                {
                    percentage.DopantId = dopant.Id;
                }
                else
                {
                    throw new Exception(string.Format("There is no dopant {0} in database!", dopant.Id));
                }

                if (this.PercentageRepository.GetPercentageWithValues
                        (percentage.Number, percentage.MatrixId, percentage.DopantId) ==
                    null)
                {
                    percentage = this.PercentageRepository.InsertPersentage(percentage);
                }

                compound.Dopants.Add(percentage, dopant);
            }

            foreach (var item in compound.PercentageToCompound)
            {
                this.PercentToCompoundRepository.DeletePercentToCompoundWithId(item.Id);
            }

            var percentages = new List <Percentage>();
            int id          = compound.Compound.Id;

            percentages.AddRange(compound.Matrixes.Keys);
            percentages.AddRange(compound.Dopants.Keys);

            foreach (var item in percentages)
            {
                this.PercentToCompoundRepository.InsertPercentToCompound(new PercentToCompound(item.Id, id));
            }
        }