Beispiel #1
0
        public async Task <OrphanageDataModel.Persons.Guarantor> Add(OrphanageDataModel.Persons.Guarantor guarantor)
        {
            try
            {
                guarantor.UserId = Program.CurrentUser.Id;
                var retBail = (OrphanageDataModel.Persons.Guarantor) await _apiClient.Guarantors_PostAsync(guarantor);

                return(retBail);
            }
            catch (ApiClientException apiEx)
            {
                var retObject = await _exceptionHandler.HandleApiPostFunctionsAndShowErrors(getGuarantor, apiEx);

                if (retObject == null)
                {
                    retObject = await _exceptionHandler.HandleApiPostFunctions(getGuarantor, apiEx);

                    if (retObject != null)
                    {
                        _mainViewModel.ShowGuarantorEditView(retObject.Id);
                    }
                    return(null);
                }
                else
                {
                    return(retObject);
                }
            }
        }
Beispiel #2
0
        public async Task <IEnumerable <OrphanageDataModel.Persons.Guarantor> > GetGuarantors(int Uid, int pageSize, int pageNum)
        {
            IList <OrphanageDataModel.Persons.Guarantor> guarantorsList = new List <OrphanageDataModel.Persons.Guarantor>();

            using (var _orphanageDBC = new OrphanageDbCNoBinary())
            {
                int totalSkiped     = pageSize * pageNum;
                int guarantorsCount = await _orphanageDBC.Guarantors.AsNoTracking().CountAsync();

                if (guarantorsCount < totalSkiped)
                {
                    totalSkiped = guarantorsCount - pageSize;
                }
                if (totalSkiped < 0)
                {
                    totalSkiped = 0;
                }
                var guarantors = await _orphanageDBC.Guarantors.AsNoTracking()
                                 .OrderBy(c => c.Id).Skip(() => totalSkiped).Take(() => pageSize)
                                 .Include(g => g.Address)
                                 .Include(c => c.Name)
                                 .Include(g => g.Account)
                                 .Where(g => g.UserId == Uid)
                                 .ToListAsync();

                foreach (var guarantor in guarantors)
                {
                    OrphanageDataModel.Persons.Guarantor guarantorToFill = guarantor;
                    _selfLoopBlocking.BlockGuarantorSelfLoop(ref guarantorToFill);
                    guarantorsList.Add(guarantorToFill);
                }
            }
            return(guarantorsList);
        }
Beispiel #3
0
        public async Task <OrphanageDataModel.Persons.Guarantor> getGuarantor(int Bid)
        {
            var returnedGuarantor = await _apiClient.Guarantors_GetAsync(Bid);

            _CurrentGuarantor = returnedGuarantor;
            return(returnedGuarantor);
        }
Beispiel #4
0
        public async Task <bool> Save(OrphanageDataModel.Persons.Guarantor guarantor)
        {
            try
            {
                await _apiClient.Guarantors_PutAsync(guarantor);

                return(true);
            }
            catch (ApiClientException apiEx)
            {
                return(_exceptionHandler.HandleApiSaveException(apiEx));
            }
        }
Beispiel #5
0
        private async void LoadGuarantor(int id)
        {
            _Guarantor = await _guarantorEditViewModel.getGuarantor(id);

            if (_Guarantor != null)
            {
                SetData();
                EnableDisableControls(true);
            }
            else
            {
                EnableDisableControls(false);
            }
        }
        public async Task <HttpResponseMessage> Post(object guarantor)
        {
            var guarantorEntity = JsonConvert.DeserializeObject <OrphanageDataModel.Persons.Guarantor>(guarantor.ToString());

            OrphanageDataModel.Persons.Guarantor ret = null;

            ret = await _GuarantorDBService.AddGuarantor(guarantorEntity);

            if (ret != null)
            {
                return(Request.CreateResponse(System.Net.HttpStatusCode.Created, ret));
            }
            else
            {
                return(_httpMessageConfiguerer.NothingChanged());
            }
        }
Beispiel #7
0
        public GuarantorEditView()
        {
            InitializeComponent();
            _guarantorEditViewModel = Program.Factory.Resolve <GuarantorEditViewModel>();
            _guarantorEditViewModel.AccountsLoaded += _guarantorEditViewModel_AccountsLoaded;

            _GuarantorEntityValidator = Program.Factory.Resolve <IEntityValidator>();
            _GuarantorEntityValidator.controlCollection = Controls;
            _GuarantorEntityValidator.DataEntity        = _Guarantor;

            guarantorBindingSource.DataSource = new OrphanageDataModel.Persons.Guarantor();
            _Guarantor      = new OrphanageDataModel.Persons.Guarantor();
            _isNewGuarantor = true;
            addressForm1.AddressDataSource = new Address();
            nameForm1.NameDataSource       = new Name();
            EnableDisableControls(false);
            TranslateControls();
        }
        private IEnumerable <OrphanageDataModel.Persons.Guarantor> prepareGuarantorsList(IEnumerable <OrphanageDataModel.Persons.Guarantor> guarantorsList)
        {
            IList <OrphanageDataModel.Persons.Guarantor> returnedGuarantorsList = new List <OrphanageDataModel.Persons.Guarantor>();

            if (guarantorsList != null && guarantorsList.Count() > 0)
            {
                foreach (var guarantor in guarantorsList)
                {
                    OrphanageDataModel.Persons.Guarantor guarantorToFill = guarantor;
                    _selfLoopBlocking.BlockGuarantorSelfLoop(ref guarantorToFill);
                    returnedGuarantorsList.Add(guarantorToFill);
                }
            }
            else
            {
                _logger.Warning($"the returned guarantors are null, empty list will be returned");
            }
            _logger.Information($"{returnedGuarantorsList.Count} records of guarantors will be returned");
            return(returnedGuarantorsList);
        }
Beispiel #9
0
 private async void btnSave_Click(object sender, EventArgs e)
 {
     _Guarantor = (OrphanageDataModel.Persons.Guarantor)guarantorBindingSource.DataSource;
     _GuarantorEntityValidator.DataEntity = _Guarantor;
     if (_GuarantorEntityValidator.IsValid())
     {
         _Guarantor.Address = (Address)addressForm1.AddressDataSource;
         _Guarantor.Name    = (Name)nameForm1.NameDataSource;
         if (!nameForm1.IsValid())
         {
             errorProvider1.SetError(txtName, Properties.Resources.ErrorMessageCheckName);
             nameForm1.ShowMe();
             return;
         }
         if (!addressForm1.IsValid())
         {
             errorProvider1.SetError(txtAddress, Properties.Resources.ErrorMessageCheckAddress);
             addressForm1.ShowMe();
             return;
         }
         if (!_isNewGuarantor)
         {
             if (await _guarantorEditViewModel.Save(_Guarantor))
             {
                 this.Close();
             }
         }
         else
         {
             if (await _guarantorEditViewModel.Add(_Guarantor) != null)
             {
                 this.Close();
             }
         }
     }
     else
     {
         ValidateAndShowError();
     }
 }
Beispiel #10
0
        public async Task <IEnumerable <OrphanageDataModel.Persons.Guarantor> > GetGuarantors(int Uid)
        {
            IList <OrphanageDataModel.Persons.Guarantor> guarantorsList = new List <OrphanageDataModel.Persons.Guarantor>();

            using (var _orphanageDBC = new OrphanageDbCNoBinary())
            {
                var guarantors = await _orphanageDBC.Guarantors.AsNoTracking()
                                 .Include(g => g.Address)
                                 .Include(c => c.Name)
                                 .Include(g => g.Account)
                                 .Where(g => g.UserId == Uid)
                                 .ToListAsync();

                foreach (var guarantor in guarantors)
                {
                    OrphanageDataModel.Persons.Guarantor guarantorToFill = guarantor;
                    _selfLoopBlocking.BlockGuarantorSelfLoop(ref guarantorToFill);
                    guarantorsList.Add(guarantorToFill);
                }
            }
            return(guarantorsList);
        }
Beispiel #11
0
        public void BlockGuarantorSelfLoop(ref OrphanageDataModel.Persons.Guarantor guarantor)
        {
            if (guarantor == null)
            {
                return;
            }

            dynamic guarantorN = guarantor.Name;

            BlockForignKeys(ref guarantorN);
            guarantor.Name = guarantorN;

            dynamic guarantorA = guarantor.Address;

            BlockForignKeys(ref guarantorA);
            guarantor.Address = guarantorA;

            if (guarantor.Account != null)
            {
                if (guarantor.Account.Guarantors != null)
                {
                    guarantor.Account.Guarantors = null;
                }
                if (guarantor.Account.Bails != null)
                {
                    guarantor.Account.Bails = null;
                }
            }
            if (guarantor.Bails != null)
            {
                foreach (var bail in guarantor.Bails)
                {
                    bail.Guarantor = null;
                }
            }
        }
        public async Task <bool> SaveGuarantor(OrphanageDataModel.Persons.Guarantor guarantor)
        {
            _logger.Information($"Trying to save guarantor");
            if (guarantor == null)
            {
                _logger.Error($"the parameter object guarantor is null, NullReferenceException will be thrown");
                throw new NullReferenceException();
            }
            if (guarantor.AccountId <= 0)
            {
                _logger.Error($"the AccountId of the parameter object guarantor equals {guarantor.AccountId}, NullReferenceException will be thrown");
                throw new NullReferenceException();
            }
            if (guarantor.NameId <= 0)
            {
                _logger.Error($"the NameID of the parameter object guarantor equals {guarantor.NameId}, NullReferenceException will be thrown");
                throw new NullReferenceException();
            }
            using (OrphanageDbCNoBinary orphanageDc = new OrphanageDbCNoBinary())
            {
                int ret = 0;
                orphanageDc.Configuration.LazyLoadingEnabled       = true;
                orphanageDc.Configuration.ProxyCreationEnabled     = true;
                orphanageDc.Configuration.AutoDetectChangesEnabled = true;

                var orginalGuarantor = await orphanageDc.Guarantors.
                                       Include(m => m.Address).
                                       Include(c => c.Name).
                                       FirstOrDefaultAsync(m => m.Id == guarantor.Id);

                if (orginalGuarantor == null)
                {
                    _logger.Error($"the original Guarantor object with id {guarantor.Id} object is not founded, ObjectNotFoundException will be thrown");
                    throw new Exceptions.ObjectNotFoundException();
                }

                _logger.Information($"processing the address object of the Guarantor with id({guarantor.Id})");
                if (guarantor.Address != null)
                {
                    if (orginalGuarantor.Address != null)
                    {
                        //edit existing caregiver address
                        ret += await _regularDataService.SaveAddress(guarantor.Address, orphanageDc);
                    }
                    else
                    {
                        //create new address for the caregiver
                        var addressId = await _regularDataService.AddAddress(guarantor.Address, orphanageDc);

                        orginalGuarantor.AddressId = addressId;
                        ret++;
                    }
                }
                else
                if (orginalGuarantor.Address != null)
                {
                    //delete existing caregiver address
                    int alAdd = orginalGuarantor.AddressId.Value;
                    orginalGuarantor.AddressId = null;
                    await orphanageDc.SaveChangesAsync();

                    await _regularDataService.DeleteAddress(alAdd, orphanageDc);
                }
                _logger.Information($"processing the name object of the Guarantor with id({guarantor.Id})");
                ret += await _regularDataService.SaveName(guarantor.Name, orphanageDc);

                orginalGuarantor.AccountId                = guarantor.AccountId;
                orginalGuarantor.ColorMark                = guarantor.ColorMark;
                orginalGuarantor.IsPayingMonthly          = guarantor.IsPayingMonthly;
                orginalGuarantor.IsStillPaying            = guarantor.IsStillPaying;
                orginalGuarantor.IsSupportingOnlyFamilies = guarantor.IsSupportingOnlyFamilies;
                orginalGuarantor.Note = guarantor.Note;
                ret += await orphanageDc.SaveChangesAsync();

                if (ret > 0)
                {
                    _logger.Information($"Guarantor with id({guarantor.Id}) has been successfully saved to the database, {ret} changes have been made");
                    return(true);
                }
                else
                {
                    _logger.Information($"nothing has changed, false will be returned");
                    return(false);
                }
            }
        }
        public async Task <OrphanageDataModel.Persons.Guarantor> AddGuarantor(OrphanageDataModel.Persons.Guarantor guarantor)
        {
            _logger.Information($"Trying to add new Guarantor");
            if (guarantor == null)
            {
                _logger.Error($"the parameter object guarantorToAdd is null, NullReferenceException will be thrown");
                throw new NullReferenceException();
            }
            if (guarantor.Name == null)
            {
                _logger.Error($"the Name object of the parameter object guarantorToAdd is null, NullReferenceException will be thrown");
                throw new NullReferenceException();
            }
            if (guarantor.AccountId <= 0)
            {
                _logger.Error($"the AccountId of the parameter object guarantorToAdd equals {guarantor.AccountId}, NullReferenceException will be thrown");
                throw new NullReferenceException();
            }
            using (var orphanageDBC = new OrphanageDbCNoBinary())
            {
                using (var Dbt = orphanageDBC.Database.BeginTransaction())
                {
                    if (!Properties.Settings.Default.ForceAdd)
                    {
                        _logger.Information($"ForceAdd option is not activated");
                        if (Properties.Settings.Default.CheckName)
                        {
                            _logger.Information($"CheckName option is activated, trying to get the equal names from database");
                            var retguarantors = GetGuarantorByName(guarantor.Name, orphanageDBC).FirstOrDefault();
                            if (retguarantors != null)
                            {
                                _logger.Error($"guarantor with id({retguarantors.Id}) has the same name, DuplicatedObjectException will be thrown");
                                throw new DuplicatedObjectException(guarantor.GetType(), retguarantors.GetType(), retguarantors.Id);
                            }
                            else
                            {
                                _logger.Information($"didn't found any similar names to ({guarantor.Name.FullName()}) in the database");
                            }
                        }
                        if (Properties.Settings.Default.CheckContactData)
                        {
                            _logger.Information($"CheckContactData option is activated, trying to get the equal contact data for the guarantor address from database");
                            var retguarantors = GetGuarantorByAddress(guarantor.Address, orphanageDBC).FirstOrDefault();
                            if (retguarantors != null)
                            {
                                _logger.Warning($"guarantor with id({retguarantors.Id}) has the same address, no DuplicatedObjectException will be thrown");
                                //    throw new DuplicatedObjectException(guarantor.GetType(), retguarantors.GetType(), retguarantors.Id);
                            }
                            else
                            {
                                _logger.Information($"didn't found any similar contact data to guarantor address in the database");
                            }
                        }
                    }
                    var nameId = await _regularDataService.AddName(guarantor.Name, orphanageDBC);

                    if (nameId == -1)
                    {
                        Dbt.Rollback();
                        _logger.Warning($"Name object has not been added, nothing will be added, null will be returned");
                        return(null);
                    }
                    guarantor.NameId = nameId;
                    if (guarantor.Address != null)
                    {
                        var addressId = await _regularDataService.AddAddress(guarantor.Address, orphanageDBC);

                        if (addressId == -1)
                        {
                            Dbt.Rollback();
                            _logger.Warning($"Address object has not been added, nothing will be added, null will be returned");
                            return(null);
                        }
                        guarantor.AddressId = addressId;
                    }
                    if (guarantor.Orphans != null)
                    {
                        guarantor.Orphans = null;
                    }
                    if (guarantor.Account != null)
                    {
                        guarantor.Account = null;
                    }
                    if (guarantor.Address != null)
                    {
                        guarantor.Address = null;
                    }
                    if (guarantor.Name != null)
                    {
                        guarantor.Name = null;
                    }
                    if (guarantor.Bails != null)
                    {
                        guarantor.Bails = null;
                    }
                    if (guarantor.ActingUser != null)
                    {
                        guarantor.ActingUser = null;
                    }
                    orphanageDBC.Guarantors.Add(guarantor);
                    var ret = await orphanageDBC.SaveChangesAsync();

                    if (ret >= 1)
                    {
                        Dbt.Commit();
                        _logger.Information($"new guarantor object with id {guarantor.Id} has been added");
                        _selfLoopBlocking.BlockGuarantorSelfLoop(ref guarantor);
                        _logger.Information($"the guarantor object with id {guarantor.Id}  will be returned");
                        return(guarantor);
                    }
                    else
                    {
                        Dbt.Rollback();
                        _logger.Warning($"something went wrong, nothing was added, null will be returned");
                        return(null);
                    }
                }
            }
        }