Esempio n. 1
0
        /// <summary>
        /// Modificar una funcionalidad
        /// </summary>
        /// <param name="modFunctionality"></param>
        /// <returns>Objeto Functionality con la funcioliadad modificada</returns>
        /// <remarks>
        /// Modifica los campos:
        /// - Category
        /// - FormName
        /// </remarks>
        public Functionality ModifyFunctionality(Functionality modFunctionality)
        {
            try
            {
                if (modFunctionality == null)
                {
                    throw new ArgumentNullException();
                }

                using (var db = new HKSupplyContext())
                {
                    var functionality = db.Functionalities.FirstOrDefault(f => f.FunctionalityId.Equals(modFunctionality.FunctionalityId));

                    if (functionality == null)
                    {
                        throw new NonexistentFunctionalityException(GlobalSetting.ResManager.GetString("NoFunctionalityExist"));
                    }

                    functionality.Category = modFunctionality.Category;
                    functionality.FormName = modFunctionality.FormName;
                    db.SaveChanges();
                    return(functionality);
                }
            }
            catch (ArgumentNullException anex)
            {
                _log.Error(anex.Message, anex);
                throw anex;
            }
            catch (NonexistentFunctionalityException nefex)
            {
                _log.Error(nefex.Message, nefex);
                throw nefex;
            }
            catch (SqlException sqlex)
            {
                for (int i = 0; i < sqlex.Errors.Count; i++)
                {
                    _log.Error("Index #" + i + "\n" +
                               "Message: " + sqlex.Errors[i].Message + "\n" +
                               "Error Number: " + sqlex.Errors[i].Number + "\n" +
                               "LineNumber: " + sqlex.Errors[i].LineNumber + "\n" +
                               "Source: " + sqlex.Errors[i].Source + "\n" +
                               "Procedure: " + sqlex.Errors[i].Procedure + "\n");

                    switch (sqlex.Errors[i].Number)
                    {
                    case -1:     //connection broken
                    case -2:     //timeout
                        throw new DBServerConnectionException(GlobalSetting.ResManager.GetString("DBServerConnectionError"));
                    }
                }
                throw sqlex;
            }
            catch (Exception ex)
            {
                _log.Error(ex.Message, ex);
                throw ex;
            }
        }
Esempio n. 2
0
        public bool UpdateItems(IEnumerable <ItemMt> itemsToUpdate)
        {
            try
            {
                if (itemsToUpdate == null)
                {
                    throw new ArgumentNullException(nameof(itemsToUpdate));
                }

                using (var db = new HKSupplyContext())
                {
                    using (var dbTrans = db.Database.BeginTransaction())
                    {
                        try
                        {
                            foreach (var item in itemsToUpdate)
                            {
                                ItemMt itemToUpdate = db.ItemsMt.Where(a => a.IdItemBcn.Equals(item.IdItemBcn)).FirstOrDefault();

                                itemToUpdate.IdSubVer += 1;
                                itemToUpdate.Timestamp = DateTime.Now;
                                //no hacemos  un "entry" en el contexto de db y lo marcamos como modificado, sino que updatamos sólo los pocos campos
                                //que se pueden modificar de un item desde la aplicación para que EF genere el update sólo de esos campos y no de todos
                                itemToUpdate.IdDefaultSupplier = item.IdDefaultSupplier;
                                itemToUpdate.IdFamilyHK        = item.IdFamilyHK;
                                itemToUpdate.IdStatusProd      = item.IdStatusProd;
                                itemToUpdate.IdUserAttri1      = item.IdUserAttri1;
                                itemToUpdate.IdUserAttri2      = item.IdUserAttri2;
                                itemToUpdate.IdUserAttri3      = item.IdUserAttri3;

                                ItemMtHistory itemHistory = (ItemMtHistory)itemToUpdate;
                                itemHistory.User = GlobalSetting.LoggedUser.UserLogin;

                                db.ItemsMtHistory.Add(itemHistory);
                            }

                            db.SaveChanges();
                            dbTrans.Commit();
                            return(true);
                        }
                        catch (SqlException sqlex)
                        {
                            dbTrans.Rollback();

                            for (int i = 0; i < sqlex.Errors.Count; i++)
                            {
                                _log.Error("Index #" + i + "\n" +
                                           "Message: " + sqlex.Errors[i].Message + "\n" +
                                           "Error Number: " + sqlex.Errors[i].Number + "\n" +
                                           "LineNumber: " + sqlex.Errors[i].LineNumber + "\n" +
                                           "Source: " + sqlex.Errors[i].Source + "\n" +
                                           "Procedure: " + sqlex.Errors[i].Procedure + "\n");

                                switch (sqlex.Errors[i].Number)
                                {
                                case -1:     //connection broken
                                case -2:     //timeout
                                    throw new DBServerConnectionException(GlobalSetting.ResManager.GetString("DBServerConnectionError"));
                                }
                            }
                            throw sqlex;
                        }
                        catch (DbEntityValidationException e)
                        {
                            dbTrans.Rollback();
                            _log.Error(e.Message, e);
                            throw e;
                        }
                        catch (Exception ex)
                        {
                            dbTrans.Rollback();
                            _log.Error(ex.Message, ex);
                            throw ex;
                        }
                    }
                }
            }
            catch (ArgumentNullException nrex)
            {
                _log.Error(nrex.Message, nrex);
                throw nrex;
            }
        }
Esempio n. 3
0
        public bool UpdateSupplierPriceList(SupplierPriceList updateSupplierPriceList, bool newVer = false)
        {
            try
            {
                if (updateSupplierPriceList == null)
                {
                    throw new ArgumentNullException("updateSupplierPriceList");
                }

                using (var db = new HKSupplyContext())
                {
                    using (var dbTrans = db.Database.BeginTransaction())
                    {
                        try
                        {
                            updateSupplierPriceList.IdSubVer += 1;
                            if (newVer == true)
                            {
                                updateSupplierPriceList.IdVer   += 1;
                                updateSupplierPriceList.IdSubVer = 0;
                            }
                            updateSupplierPriceList.Timestamp = DateTime.Now;

                            SupplierPriceListHistory supplierPriceListHistory = (SupplierPriceListHistory)updateSupplierPriceList;
                            supplierPriceListHistory.User = GlobalSetting.LoggedUser.UserLogin;

                            //Con esto marcaremos todo el objeto como modificado y actualizará todos los campos.
                            //En este caso nos interesa porque la mayoría de los campos de supplier se pueden modificar
                            db.Entry(updateSupplierPriceList).State = EntityState.Modified;

                            db.SuppliersPriceListHistory.Add(supplierPriceListHistory);
                            db.SaveChanges();

                            dbTrans.Commit();
                            return(true);
                        }
                        catch (SqlException sqlex)
                        {
                            dbTrans.Rollback();

                            for (int i = 0; i < sqlex.Errors.Count; i++)
                            {
                                _log.Error("Index #" + i + "\n" +
                                           "Message: " + sqlex.Errors[i].Message + "\n" +
                                           "Error Number: " + sqlex.Errors[i].Number + "\n" +
                                           "LineNumber: " + sqlex.Errors[i].LineNumber + "\n" +
                                           "Source: " + sqlex.Errors[i].Source + "\n" +
                                           "Procedure: " + sqlex.Errors[i].Procedure + "\n");

                                switch (sqlex.Errors[i].Number)
                                {
                                case -1:     //connection broken
                                case -2:     //timeout
                                    throw new DBServerConnectionException(GlobalSetting.ResManager.GetString("DBServerConnectionError"));
                                }
                            }
                            throw sqlex;
                        }
                        catch (Exception ex)
                        {
                            dbTrans.Rollback();
                            _log.Error(ex.Message, ex);
                            throw ex;
                        }
                    }
                }
            }
            catch (ArgumentNullException anex)
            {
                _log.Error(anex.Message, anex);
                throw anex;
            }
        }
Esempio n. 4
0
        public bool NewSupplierPriceList(SupplierPriceList newSupplierPriceList)
        {
            try
            {
                if (newSupplierPriceList == null)
                {
                    throw new ArgumentNullException("newSupplierPriceList");
                }

                using (var db = new HKSupplyContext())
                {
                    using (var dbTrans = db.Database.BeginTransaction())
                    {
                        try
                        {
                            var tmpSupplierPriceList = GetSupplierPriceList(newSupplierPriceList.IdItemBcn, newSupplierPriceList.IdSupplier);
                            if (tmpSupplierPriceList != null)
                            {
                                throw new Exception("Supplier Price List already exist");
                            }

                            newSupplierPriceList.IdVer     = 1;
                            newSupplierPriceList.IdSubVer  = 0;
                            newSupplierPriceList.Timestamp = DateTime.Now;

                            SupplierPriceListHistory supplierPriceListHistory = (SupplierPriceListHistory)newSupplierPriceList;
                            supplierPriceListHistory.User = GlobalSetting.LoggedUser.UserLogin;

                            db.SuppliersPriceList.Add(newSupplierPriceList);
                            db.SuppliersPriceListHistory.Add(supplierPriceListHistory);
                            db.SaveChanges();

                            dbTrans.Commit();
                            return(true);
                        }
                        catch (SqlException sqlex)
                        {
                            dbTrans.Rollback();

                            for (int i = 0; i < sqlex.Errors.Count; i++)
                            {
                                _log.Error("Index #" + i + "\n" +
                                           "Message: " + sqlex.Errors[i].Message + "\n" +
                                           "Error Number: " + sqlex.Errors[i].Number + "\n" +
                                           "LineNumber: " + sqlex.Errors[i].LineNumber + "\n" +
                                           "Source: " + sqlex.Errors[i].Source + "\n" +
                                           "Procedure: " + sqlex.Errors[i].Procedure + "\n");

                                switch (sqlex.Errors[i].Number)
                                {
                                case -1:     //connection broken
                                case -2:     //timeout
                                    throw new DBServerConnectionException(GlobalSetting.ResManager.GetString("DBServerConnectionError"));
                                }
                            }
                            throw sqlex;
                        }
                        catch (Exception ex)
                        {
                            dbTrans.Rollback();
                            _log.Error(ex.Message, ex);
                            throw ex;
                        }
                    }
                }
            }
            catch (ArgumentNullException anex)
            {
                _log.Error(anex.Message, anex);
                throw anex;
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Modificar una colección de usuarios.
        /// </summary>
        /// <param name="usersToUpdate"></param>
        /// <returns></returns>
        /// <remarks>
        /// Los campos que se modifican son los siguientes:
        /// - Name
        /// - RoleId
        /// - Enabled
        /// - Remarks
        /// </remarks>
        public bool UpdateUsers(IEnumerable <User> usersToUpdate)
        {
            try
            {
                if (usersToUpdate == null)
                {
                    throw new ArgumentNullException("usersToUpdate");
                }

                using (var db = new HKSupplyContext())
                {
                    using (var dbTrans = db.Database.BeginTransaction())
                    {
                        try
                        {
                            foreach (var user in usersToUpdate)
                            {
                                var userToUpdate = db.Users.FirstOrDefault(u => u.Id.Equals(user.Id));
                                if (userToUpdate != null)
                                {
                                    userToUpdate.Name    = user.Name;
                                    userToUpdate.RoleId  = user.RoleId;
                                    userToUpdate.Enabled = user.Enabled;
                                    userToUpdate.Remarks = user.Remarks;
                                }
                            }

                            db.SaveChanges();
                            dbTrans.Commit();
                            return(true);
                        }
                        catch (SqlException sqlex)
                        {
                            dbTrans.Rollback();

                            for (int i = 0; i < sqlex.Errors.Count; i++)
                            {
                                _log.Error("Index #" + i + "\n" +
                                           "Message: " + sqlex.Errors[i].Message + "\n" +
                                           "Error Number: " + sqlex.Errors[i].Number + "\n" +
                                           "LineNumber: " + sqlex.Errors[i].LineNumber + "\n" +
                                           "Source: " + sqlex.Errors[i].Source + "\n" +
                                           "Procedure: " + sqlex.Errors[i].Procedure + "\n");

                                switch (sqlex.Errors[i].Number)
                                {
                                case -1:     //connection broken
                                case -2:     //timeout
                                    throw new DBServerConnectionException(GlobalSetting.ResManager.GetString("DBServerConnectionError"));
                                }
                            }
                            throw sqlex;
                        }
                        catch (DbEntityValidationException e)
                        {
                            dbTrans.Rollback();
                            _log.Error(e.Message, e);
                            throw e;
                        }
                        catch (Exception ex)
                        {
                            dbTrans.Rollback();
                            _log.Error(ex.Message, ex);
                            throw ex;
                        }
                    }
                }
            }
            catch (ArgumentNullException nrex)
            {
                _log.Error(nrex.Message, nrex);
                throw nrex;
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Modificar un FunctionalityRole
        /// </summary>
        /// <param name="modFunctionalityRole"></param>
        /// <returns></returns>
        /// <remarks>
        /// Los campos que se actualizan son los siguientes:
        /// - Read
        /// - New
        /// - Modify
        /// </remarks>
        public FunctionalityRole ModifyFunctionalityRole(FunctionalityRole modFunctionalityRole)
        {
            try
            {
                if (modFunctionalityRole == null)
                {
                    throw new ArgumentNullException("modFunctionalityRole");
                }

                using (var db = new HKSupplyContext())
                {
                    var functionalityRole = db.FunctionalitiesRole
                                            .Where(fr => fr.FunctionalityId.Equals(modFunctionalityRole.FunctionalityId) && fr.RoleId.Equals(modFunctionalityRole.RoleId))
                                            .FirstOrDefault();

                    if (functionalityRole == null)
                    {
                        throw new NonexistentFunctionalityRoleException(GlobalSetting.ResManager.GetString("NoFunctionalityRoleExist"));
                    }


                    functionalityRole.Read   = modFunctionalityRole.Read;
                    functionalityRole.New    = modFunctionalityRole.New;
                    functionalityRole.Modify = modFunctionalityRole.Modify;
                    db.SaveChanges();

                    return(GetFunctionalityRole(modFunctionalityRole.FunctionalityId, modFunctionalityRole.RoleId));

                    //return db.FunctionalitiesRole
                    //    .Where(fr => fr.FunctionalityId.Equals(modFunctionalityRole.FunctionalityId) && fr.RoleId.Equals(modFunctionalityRole.RoleId))
                    //    .FirstOrDefault();
                }
            }
            catch (ArgumentNullException anex)
            {
                _log.Error(anex.Message, anex);
                throw anex;
            }
            catch (NonexistentFunctionalityRoleException nfre)
            {
                _log.Error(nfre.Message, nfre);
                throw nfre;
            }
            catch (SqlException sqlex)
            {
                for (int i = 0; i < sqlex.Errors.Count; i++)
                {
                    _log.Error("Index #" + i + "\n" +
                               "Message: " + sqlex.Errors[i].Message + "\n" +
                               "Error Number: " + sqlex.Errors[i].Number + "\n" +
                               "LineNumber: " + sqlex.Errors[i].LineNumber + "\n" +
                               "Source: " + sqlex.Errors[i].Source + "\n" +
                               "Procedure: " + sqlex.Errors[i].Procedure + "\n");

                    switch (sqlex.Errors[i].Number)
                    {
                    case -1:     //connection broken
                    case -2:     //timeout
                        throw new DBServerConnectionException(GlobalSetting.ResManager.GetString("DBServerConnectionError"));
                    }
                }
                throw sqlex;
            }
            catch (DbEntityValidationException e)
            {
                _log.Error(e.Message, e);
                throw e;
            }
            catch (Exception ex)
            {
                _log.Error(ex.Message, ex);
                throw ex;
            }
        }
Esempio n. 7
0
        private void InitDBData()
        {
            try
            {
                //Entity Framework

                //***** Role *****//
                using (var db = new HKSupplyContext())
                {
                    var roleAdmin = new Role
                    {
                        RoleId      = "ADMIN",
                        Description = "Application Administrator",
                        Enabled     = true,
                        Remarks     = null
                    };

                    var roleOperator = new Role
                    {
                        RoleId      = "OPERATOR",
                        Description = "Operator",
                        Enabled     = true,
                        Remarks     = null
                    };

                    db.Roles.Add(roleAdmin);
                    db.Roles.Add(roleOperator);
                    db.SaveChanges();

                    //var query = from b in db.Roles
                    //            orderby b.RoleId
                    //            select b;
                    //foreach (var item in query)
                    //{
                    //    MessageBox.Show(item.Description);
                    //}

                    //*****users *****//

                    var userAdmin = new User
                    {
                        UserLogin  = "******",
                        Name       = "Administrator",
                        Password   = PasswordHelper.GetHash("adminpwd"),
                        UserRole   = roleAdmin,
                        Enabled    = true,
                        LastLogin  = null,
                        LastLogout = null,
                        Remarks    = null
                    };

                    var userMario = new User
                    {
                        UserLogin  = "******",
                        Name       = "Mario Ruz Martínez",
                        Password   = PasswordHelper.GetHash("mariopwd"),
                        UserRole   = roleAdmin,
                        Enabled    = true,
                        LastLogin  = null,
                        LastLogout = null,
                        Remarks    = null
                    };

                    var userOp1 = new User
                    {
                        UserLogin  = "******",
                        Name       = "Operator 1",
                        Password   = PasswordHelper.GetHash("op1pwd"),
                        UserRole   = roleOperator,
                        Enabled    = true,
                        LastLogin  = null,
                        LastLogout = null,
                        Remarks    = null
                    };

                    db.Users.Add(userAdmin);
                    db.Users.Add(userMario);
                    db.Users.Add(userOp1);

                    db.SaveChanges();

                    //var queryUser = from u in db.Users
                    //                orderby u.UserLogin
                    //                select u;

                    //foreach (var item in queryUser)
                    //{
                    //    MessageBox.Show(item.UserLogin);
                    //}

                    //***** Functionalities *****//

                    var funcUM = new Functionality
                    {
                        FunctionalityName = "UserManagement",
                        Category          = "Masters",
                        FormName          = "frmUserManagement"
                    };

                    var funcRM = new Functionality
                    {
                        FunctionalityName = "RoleManagement",
                        Category          = "Masters",
                        FormName          = "frmRoleManagement"
                    };

                    var funcFM = new Functionality
                    {
                        FunctionalityName = "FunctionalityManagement",
                        Category          = "Masters",
                        FormName          = "frmFunctionalityManagement"
                    };

                    var funcMMA = new Functionality
                    {
                        FunctionalityName = "MaterialsManagement",
                        Category          = "Masters",
                        FormName          = "frmMaterialsManagement"
                    };


                    db.Functionalities.Add(funcUM);
                    db.Functionalities.Add(funcRM);
                    db.Functionalities.Add(funcFM);
                    db.Functionalities.Add(funcMMA);
                    db.SaveChanges();

                    //var queryFunc = from f in db.Functionalities
                    //                orderby f.FunctionalityName
                    //                select f;

                    //foreach (var item in queryFunc)
                    //{
                    //    MessageBox.Show(item.FunctionalityName);
                    //}

                    //***** Functionalities Role *****//
                    var adminRole = db.Roles.FirstOrDefault(r => r.RoleId.Equals("ADMIN"));
                    var opRole    = db.Roles.FirstOrDefault(r => r.RoleId.Equals("OPERATOR"));

                    var funcUserManagement          = db.Functionalities.FirstOrDefault(f => f.FunctionalityName.Equals("UserManagement"));
                    var funcRoleManagement          = db.Functionalities.FirstOrDefault(f => f.FunctionalityName.Equals("RoleManagement"));
                    var funcFunctionalityManagement = db.Functionalities.FirstOrDefault(f => f.FunctionalityName.Equals("FunctionalityManagement"));
                    var funcMaterialsManagement     = db.Functionalities.FirstOrDefault(f => f.FunctionalityName.Equals("MaterialsManagement"));

                    var fr1 = new FunctionalityRole
                    {
                        RoleId          = adminRole.RoleId,
                        FunctionalityId = funcUserManagement.FunctionalityId,
                        Read            = true,
                        New             = true,
                        Modify          = true,
                    };

                    var fr2 = new FunctionalityRole
                    {
                        RoleId          = adminRole.RoleId,
                        FunctionalityId = funcRoleManagement.FunctionalityId,
                        Read            = true,
                        New             = true,
                        Modify          = true,
                    };

                    var fr3 = new FunctionalityRole
                    {
                        RoleId          = adminRole.RoleId,
                        FunctionalityId = funcFunctionalityManagement.FunctionalityId,
                        Read            = true,
                        New             = true,
                        Modify          = true,
                    };

                    var fr4 = new FunctionalityRole
                    {
                        RoleId          = adminRole.RoleId,
                        FunctionalityId = funcMaterialsManagement.FunctionalityId,
                        Read            = true,
                        New             = true,
                        Modify          = true,
                    };

                    var fr5 = new FunctionalityRole
                    {
                        RoleId          = opRole.RoleId,
                        FunctionalityId = funcMaterialsManagement.FunctionalityId,
                        Read            = true,
                        New             = false,
                        Modify          = false,
                    };

                    db.FunctionalitiesRole.Add(fr1);
                    db.FunctionalitiesRole.Add(fr2);
                    db.FunctionalitiesRole.Add(fr3);
                    db.FunctionalitiesRole.Add(fr4);
                    db.FunctionalitiesRole.Add(fr5);
                    db.SaveChanges();
                }
            }
            catch (DbEntityValidationException e)
            {
                foreach (var eve in e.EntityValidationErrors)
                {
                    Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                                      eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                                          ve.PropertyName, ve.ErrorMessage);
                    }
                }
                throw;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Esempio n. 8
0
        public bool UpdateFamilyHK(IEnumerable <FamilyHK> familiesHkToUpdate)
        {
            try
            {
                if (familiesHkToUpdate == null)
                {
                    throw new ArgumentNullException("familiesHkToUpdate");
                }

                using (var db = new HKSupplyContext())
                {
                    using (var dbTrans = db.Database.BeginTransaction())
                    {
                        try
                        {
                            foreach (var familyHk in familiesHkToUpdate)
                            {
                                var familyHkToUpdate = db.FamiliesHK.FirstOrDefault(a => a.IdFamilyHk.Equals(familyHk.IdFamilyHk));
                                if (familyHkToUpdate != null)
                                {
                                    familyHkToUpdate.Description = familyHk.Description;
                                }
                            }

                            db.SaveChanges();
                            dbTrans.Commit();
                            return(true);
                        }
                        catch (SqlException sqlex)
                        {
                            dbTrans.Rollback();

                            for (int i = 0; i < sqlex.Errors.Count; i++)
                            {
                                _log.Error("Index #" + i + "\n" +
                                           "Message: " + sqlex.Errors[i].Message + "\n" +
                                           "Error Number: " + sqlex.Errors[i].Number + "\n" +
                                           "LineNumber: " + sqlex.Errors[i].LineNumber + "\n" +
                                           "Source: " + sqlex.Errors[i].Source + "\n" +
                                           "Procedure: " + sqlex.Errors[i].Procedure + "\n");

                                switch (sqlex.Errors[i].Number)
                                {
                                case -1:     //connection broken
                                case -2:     //timeout
                                    throw new DBServerConnectionException(GlobalSetting.ResManager.GetString("DBServerConnectionError"));
                                }
                            }
                            throw sqlex;
                        }
                        catch (DbEntityValidationException e)
                        {
                            dbTrans.Rollback();
                            _log.Error(e.Message, e);
                            throw e;
                        }
                        catch (Exception ex)
                        {
                            dbTrans.Rollback();
                            _log.Error(ex.Message, ex);
                            throw ex;
                        }
                    }
                }
            }
            catch (ArgumentNullException nrex)
            {
                _log.Error(nrex.Message, nrex);
                throw nrex;
            }
        }
Esempio n. 9
0
        public bool AddPrototypeDoc(PrototypeDoc doc)
        {
            try
            {
                if (doc == null)
                {
                    throw new ArgumentNullException(nameof(doc));
                }

                using (var db = new HKSupplyContext())
                {
                    using (var dbTrans = db.Database.BeginTransaction())
                    {
                        try
                        {
                            doc.CreateDate = DateTime.Now;
                            db.PrototypesDocs.Add(doc);

                            db.SaveChanges();
                            dbTrans.Commit();
                            return(true);
                        }
                        catch (DbEntityValidationException e)
                        {
                            dbTrans.Rollback();
                            _log.Error(e.Message, e);
                            throw e;
                        }
                        catch (SqlException sqlex)
                        {
                            dbTrans.Rollback();

                            for (int i = 0; i < sqlex.Errors.Count; i++)
                            {
                                _log.Error("Index #" + i + "\n" +
                                           "Message: " + sqlex.Errors[i].Message + "\n" +
                                           "Error Number: " + sqlex.Errors[i].Number + "\n" +
                                           "LineNumber: " + sqlex.Errors[i].LineNumber + "\n" +
                                           "Source: " + sqlex.Errors[i].Source + "\n" +
                                           "Procedure: " + sqlex.Errors[i].Procedure + "\n");

                                switch (sqlex.Errors[i].Number)
                                {
                                case -1:     //connection broken
                                case -2:     //timeout
                                    throw new DBServerConnectionException(GlobalSetting.ResManager.GetString("DBServerConnectionError"));
                                }
                            }
                            throw sqlex;
                        }
                        catch (Exception ex)
                        {
                            dbTrans.Rollback();
                            _log.Error(ex.Message, ex);
                            throw ex;
                        }
                    }
                }
            }
            catch (ArgumentNullException anex)
            {
                _log.Error(anex.Message, anex);
                throw anex;
            }
        }
Esempio n. 10
0
        public bool UpdateCustomersPricesList(IEnumerable <CustomerPriceList> pricesListToUpdate)
        {
            try
            {
                if (pricesListToUpdate == null)
                {
                    throw new ArgumentNullException(nameof(pricesListToUpdate));
                }

                using (var db = new HKSupplyContext())
                {
                    using (var dbTrans = db.Database.BeginTransaction())
                    {
                        try
                        {
                            foreach (var priceList in pricesListToUpdate)
                            {
                                priceList.IdSubVer += 1;
                                priceList.Timestamp = DateTime.Now;

                                CustomerPriceListHistory customerPriceListHistory = (CustomerPriceListHistory)priceList;
                                customerPriceListHistory.User = GlobalSetting.LoggedUser.UserLogin;

                                //Con esto marcaremos todo el objeto como modificado y actualizará todos los campos.
                                //En este caso nos interesa porque la mayoría de los campos de supplier se pueden modificar
                                db.Entry(priceList).State = EntityState.Modified;
                                db.CustomersPriceListHistory.Add(customerPriceListHistory);
                            }

                            db.SaveChanges();
                            dbTrans.Commit();
                            return(true);
                        }
                        catch (SqlException sqlex)
                        {
                            dbTrans.Rollback();

                            for (int i = 0; i < sqlex.Errors.Count; i++)
                            {
                                _log.Error("Index #" + i + "\n" +
                                           "Message: " + sqlex.Errors[i].Message + "\n" +
                                           "Error Number: " + sqlex.Errors[i].Number + "\n" +
                                           "LineNumber: " + sqlex.Errors[i].LineNumber + "\n" +
                                           "Source: " + sqlex.Errors[i].Source + "\n" +
                                           "Procedure: " + sqlex.Errors[i].Procedure + "\n");

                                switch (sqlex.Errors[i].Number)
                                {
                                case -1:     //connection broken
                                case -2:     //timeout
                                    throw new DBServerConnectionException(GlobalSetting.ResManager.GetString("DBServerConnectionError"));
                                }
                            }
                            throw sqlex;
                        }
                        catch (DbEntityValidationException e)
                        {
                            dbTrans.Rollback();
                            _log.Error(e.Message, e);
                            throw e;
                        }
                        catch (Exception ex)
                        {
                            dbTrans.Rollback();
                            _log.Error(ex.Message, ex);
                            throw ex;
                        }
                    }
                }
            }
            catch (ArgumentNullException nrex)
            {
                _log.Error(nrex.Message, nrex);
                throw nrex;
            }
        }
Esempio n. 11
0
        public Models.Store NewStore(Models.Store newStore)
        {
            try
            {
                if (newStore == null)
                {
                    throw new ArgumentNullException("newStore");
                }

                using (var db = new HKSupplyContext())
                {
                    var store = db.Stores.FirstOrDefault(s => s.IdStore.Equals(newStore.IdStore));

                    if (store != null)
                    {
                        throw new NewExistingStoreException(GlobalSetting.ResManager.GetString("StoreAlreadyExist"));
                    }
                    db.Stores.Add(newStore);
                    db.SaveChanges();

                    return(GetStoreById(newStore.IdStore));
                }
            }
            catch (ArgumentNullException nrex)
            {
                _log.Error(nrex.Message, nrex);
                throw nrex;
            }
            catch (NewExistingRoleException areex)
            {
                _log.Info(areex.Message, areex);
                throw areex;
            }
            catch (NonexistentRoleException nerex)
            {
                _log.Error(nerex.Message, nerex);
                throw nerex;
            }
            catch (DbEntityValidationException e)
            {
                _log.Error(e.Message, e);
                throw e;
            }
            catch (SqlException sqlex)
            {
                for (int i = 0; i < sqlex.Errors.Count; i++)
                {
                    _log.Error("Index #" + i + "\n" +
                               "Message: " + sqlex.Errors[i].Message + "\n" +
                               "Error Number: " + sqlex.Errors[i].Number + "\n" +
                               "LineNumber: " + sqlex.Errors[i].LineNumber + "\n" +
                               "Source: " + sqlex.Errors[i].Source + "\n" +
                               "Procedure: " + sqlex.Errors[i].Procedure + "\n");

                    switch (sqlex.Errors[i].Number)
                    {
                    case -1:     //connection broken
                    case -2:     //timeout
                        throw new DBServerConnectionException(GlobalSetting.ResManager.GetString("DBServerConnectionError"));
                    }
                }
                throw sqlex;
            }
            catch (Exception ex)
            {
                _log.Error(ex.Message, ex);
                throw ex;
            }
        }
Esempio n. 12
0
        public bool NewCustomer(Customer newCustomer)
        {
            try
            {
                if (newCustomer == null)
                {
                    throw new ArgumentNullException("newCustomer");
                }

                using (var db = new HKSupplyContext())
                {
                    using (var dbTrans = db.Database.BeginTransaction())
                    {
                        try
                        {
                            var tmpCustomer = GetCustomerById(newCustomer.IdCustomer);
                            if (tmpCustomer != null)
                            {
                                throw new Exception("Customer already exist");
                            }

                            newCustomer.IdVer     = 1;
                            newCustomer.IdSubVer  = 0;
                            newCustomer.Timestamp = DateTime.Now;

                            CustomerHistory customerHistory = (CustomerHistory)newCustomer;

                            db.Customers.Add(newCustomer);
                            db.CustomersHistory.Add(customerHistory);
                            db.SaveChanges();

                            dbTrans.Commit();
                            return(true);
                        }
                        catch (SqlException sqlex)
                        {
                            dbTrans.Rollback();

                            for (int i = 0; i < sqlex.Errors.Count; i++)
                            {
                                _log.Error("Index #" + i + "\n" +
                                           "Message: " + sqlex.Errors[i].Message + "\n" +
                                           "Error Number: " + sqlex.Errors[i].Number + "\n" +
                                           "LineNumber: " + sqlex.Errors[i].LineNumber + "\n" +
                                           "Source: " + sqlex.Errors[i].Source + "\n" +
                                           "Procedure: " + sqlex.Errors[i].Procedure + "\n");

                                switch (sqlex.Errors[i].Number)
                                {
                                case -1:     //connection broken
                                case -2:     //timeout
                                    throw new DBServerConnectionException(GlobalSetting.ResManager.GetString("DBServerConnectionError"));
                                }
                            }
                            throw sqlex;
                        }
                        catch (Exception ex)
                        {
                            dbTrans.Rollback();
                            _log.Error(ex.Message, ex);
                            throw ex;
                        }
                    }
                }
            }
            catch (ArgumentNullException anex)
            {
                _log.Error(anex.Message, anex);
                throw anex;
            }
        }
Esempio n. 13
0
        /// <summary>
        /// Deshabilitar un rol
        /// </summary>
        /// <param name="roleId"></param>
        /// <param name="remarks"></param>
        /// <returns></returns>
        public bool DisableRole(string roleId, string remarks)
        {
            try
            {
                if (roleId == null)
                {
                    throw new ArgumentNullException("roleId");
                }

                using (var db = new HKSupplyContext())
                {
                    var role = db.Roles.FirstOrDefault(r => r.RoleId.Equals(roleId));

                    if (role == null)
                    {
                        throw new NonexistentRoleException(GlobalSetting.ResManager.GetString("NoRoleExist"));
                    }

                    if (role != null)
                    {
                        role.Enabled = false;
                        role.Remarks = remarks;
                        db.SaveChanges();
                    }

                    return(true);
                }
            }
            catch (ArgumentNullException nrex)
            {
                _log.Error(nrex.Message, nrex);
                throw nrex;
            }
            catch (NonexistentRoleException nerex)
            {
                _log.Error(nerex.Message, nerex);
                throw nerex;
            }
            catch (SqlException sqlex)
            {
                for (int i = 0; i < sqlex.Errors.Count; i++)
                {
                    _log.Error("Index #" + i + "\n" +
                               "Message: " + sqlex.Errors[i].Message + "\n" +
                               "Error Number: " + sqlex.Errors[i].Number + "\n" +
                               "LineNumber: " + sqlex.Errors[i].LineNumber + "\n" +
                               "Source: " + sqlex.Errors[i].Source + "\n" +
                               "Procedure: " + sqlex.Errors[i].Procedure + "\n");

                    switch (sqlex.Errors[i].Number)
                    {
                    case -1:     //connection broken
                    case -2:     //timeout
                        throw new DBServerConnectionException(GlobalSetting.ResManager.GetString("DBServerConnectionError"));
                    }
                }
                throw sqlex;
            }
            catch (Exception ex)
            {
                _log.Error(ex.Message, ex);
                throw ex;
            }
        }
Esempio n. 14
0
        public bool UpdateItemWithDoc(ItemMt updateItem, ItemDoc itemDoc, bool newVersion = false)
        {
            try
            {
                if (updateItem == null)
                {
                    throw new ArgumentNullException(nameof(updateItem));
                }

                if (itemDoc == null)
                {
                    throw new ArgumentNullException(nameof(itemDoc));
                }

                using (var db = new HKSupplyContext())
                {
                    using (var dbTrans = db.Database.BeginTransaction())
                    {
                        try
                        {
                            ItemMt itemToUpdate = GetItem(updateItem.IdItemBcn);

                            if (itemToUpdate == null)
                            {
                                return(false);
                            }

                            //Hay que agregarlo al contexto actual para que lo actualice
                            db.ItemsMt.Attach(itemToUpdate);

                            itemToUpdate.IdSubVer += 1;
                            if (newVersion == true)
                            {
                                itemToUpdate.IdVer   += 1;
                                itemToUpdate.IdSubVer = 0;
                            }
                            itemToUpdate.Timestamp = DateTime.Now;

                            //no hacemos  un "entry" en el contexto de db y lo marcamos como modificado, sino que updatamos sólo los pocos campos
                            //que se pueden modificar de un item desde la aplicación para que EF genere el update sólo de esos campos y no de todos
                            itemToUpdate.IdDefaultSupplier = updateItem.IdDefaultSupplier;
                            itemToUpdate.IdFamilyHK        = updateItem.IdFamilyHK;
                            itemToUpdate.IdStatusProd      = updateItem.IdStatusProd;
                            itemToUpdate.IdUserAttri1      = updateItem.IdUserAttri1;
                            itemToUpdate.IdUserAttri2      = updateItem.IdUserAttri2;
                            itemToUpdate.IdUserAttri3      = updateItem.IdUserAttri3;
                            itemToUpdate.PhotoUrl          = updateItem.PhotoUrl;

                            ItemMtHistory itemHistory = (ItemMtHistory)itemToUpdate;
                            itemHistory.User = GlobalSetting.LoggedUser.UserLogin;

                            //Documento
                            itemDoc.IdVerItem    = itemToUpdate.IdVer;
                            itemDoc.IdSubVerItem = itemToUpdate.IdSubVer;
                            itemDoc.CreateDate   = DateTime.Now;

                            db.ItemsMtHistory.Add(itemHistory);
                            db.ItemsDoc.Add(itemDoc);

                            db.SaveChanges();
                            dbTrans.Commit();
                            return(true);
                        }
                        catch (DbEntityValidationException e)
                        {
                            dbTrans.Rollback();
                            _log.Error(e.Message, e);
                            throw e;
                        }
                        catch (SqlException sqlex)
                        {
                            dbTrans.Rollback();

                            for (int i = 0; i < sqlex.Errors.Count; i++)
                            {
                                _log.Error("Index #" + i + "\n" +
                                           "Message: " + sqlex.Errors[i].Message + "\n" +
                                           "Error Number: " + sqlex.Errors[i].Number + "\n" +
                                           "LineNumber: " + sqlex.Errors[i].LineNumber + "\n" +
                                           "Source: " + sqlex.Errors[i].Source + "\n" +
                                           "Procedure: " + sqlex.Errors[i].Procedure + "\n");

                                switch (sqlex.Errors[i].Number)
                                {
                                case -1:     //connection broken
                                case -2:     //timeout
                                    throw new DBServerConnectionException(GlobalSetting.ResManager.GetString("DBServerConnectionError"));
                                }
                            }
                            throw sqlex;
                        }
                        catch (Exception ex)
                        {
                            dbTrans.Rollback();
                            _log.Error(ex.Message, ex);
                            throw ex;
                        }
                    }
                }
            }
            catch (ArgumentNullException anex)
            {
                _log.Error(anex.Message, anex);
                throw anex;
            }
        }
Esempio n. 15
0
        /// <summary>
        /// Dar de alta un usuario en el sistema
        /// </summary>
        /// <param name="newUser"></param>
        /// <returns></returns>
        public User NewUser(User newUser)
        {
            try
            {
                if (newUser == null)
                {
                    throw new ArgumentNullException("newUser");
                }

                using (var db = new HKSupplyContext())
                {
                    var user = db.Users.FirstOrDefault(u => u.UserLogin.Equals(newUser.UserLogin));

                    if (user != null)
                    {
                        throw new NewExistingUserException(GlobalSetting.ResManager.GetString("InvalidUser"));
                    }

                    db.Users.Add(newUser);
                    db.SaveChanges();

                    //return GetUserByIdPassword(newUser.UserLogin, newUser.Password);
                    return(newUser);
                }
            }
            catch (ArgumentNullException anex)
            {
                _log.Error(anex.Message, anex);
                throw anex;
            }
            catch (NewExistingUserException aueex)
            {
                _log.Error(aueex.Message, aueex);
                throw aueex;
            }
            catch (NonexistentUserException neuex)
            {
                _log.Error(neuex.Message, neuex);
                throw neuex;
            }
            catch (DbEntityValidationException e)
            {
                _log.Error(e.Message, e);
                throw e;
            }
            catch (SqlException sqlex)
            {
                for (int i = 0; i < sqlex.Errors.Count; i++)
                {
                    _log.Error("Index #" + i + "\n" +
                               "Message: " + sqlex.Errors[i].Message + "\n" +
                               "Error Number: " + sqlex.Errors[i].Number + "\n" +
                               "LineNumber: " + sqlex.Errors[i].LineNumber + "\n" +
                               "Source: " + sqlex.Errors[i].Source + "\n" +
                               "Procedure: " + sqlex.Errors[i].Procedure + "\n");

                    switch (sqlex.Errors[i].Number)
                    {
                    case -1:     //connection broken
                    case -2:     //timeout
                        throw new DBServerConnectionException(GlobalSetting.ResManager.GetString("DBServerConnectionError"));
                    }
                }
                throw sqlex;
            }
            catch (Exception ex)
            {
                _log.Error(ex.Message, ex);
                throw ex;
            }
        }
Esempio n. 16
0
        public FamilyHK NewFamilyHK(FamilyHK newFamilyHK)
        {
            try
            {
                if (newFamilyHK == null)
                {
                    throw new ArgumentNullException("newFamilyHK");
                }

                using (var db = new HKSupplyContext())
                {
                    using (var dbTrans = db.Database.BeginTransaction())
                    {
                        try
                        {
                            var familiesHK = db.FamiliesHK.FirstOrDefault(a => a.IdFamilyHk.Equals(newFamilyHK.IdFamilyHk));

                            if (familiesHK != null)
                            {
                                //throw new NewExistingRoleException(GlobalSetting.ResManager.GetString("RoleAlreadyExist"));
                                throw new Exception("Families HK Already Exist");
                            }

                            db.FamiliesHK.Add(newFamilyHK);
                            db.SaveChanges();
                            dbTrans.Commit();

                            return(GetFamilyHKById(newFamilyHK.IdFamilyHk));
                        }
                        catch (SqlException sqlex)
                        {
                            dbTrans.Rollback();

                            for (int i = 0; i < sqlex.Errors.Count; i++)
                            {
                                _log.Error("Index #" + i + "\n" +
                                           "Message: " + sqlex.Errors[i].Message + "\n" +
                                           "Error Number: " + sqlex.Errors[i].Number + "\n" +
                                           "LineNumber: " + sqlex.Errors[i].LineNumber + "\n" +
                                           "Source: " + sqlex.Errors[i].Source + "\n" +
                                           "Procedure: " + sqlex.Errors[i].Procedure + "\n");

                                switch (sqlex.Errors[i].Number)
                                {
                                case -1:     //connection broken
                                case -2:     //timeout
                                    throw new DBServerConnectionException(GlobalSetting.ResManager.GetString("DBServerConnectionError"));
                                }
                            }
                            throw sqlex;
                        }
                        catch (DbEntityValidationException e)
                        {
                            dbTrans.Rollback();
                            _log.Error(e.Message, e);
                            throw e;
                        }
                        catch (Exception ex)
                        {
                            dbTrans.Rollback();
                            _log.Error(ex.Message, ex);
                            throw ex;
                        }
                    }
                }
            }
            catch (ArgumentNullException nrex)
            {
                _log.Error(nrex.Message, nrex);
                throw nrex;
            }
        }
Esempio n. 17
0
        /// <summary>
        /// Deshabilitar un usuario
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="remarks"></param>
        /// <returns></returns>
        public bool DisableUser(string userId, string remarks)
        {
            try
            {
                if (userId == null)
                {
                    throw new ArgumentNullException("userId");
                }

                using (var db = new HKSupplyContext())
                {
                    var user = db.Users.FirstOrDefault(u => u.UserLogin.Equals(userId));

                    if (user == null)
                    {
                        throw new NonexistentUserException(GlobalSetting.ResManager.GetString("InvalidUser"));
                    }

                    user.Enabled = false;
                    user.Remarks = remarks;
                    db.SaveChanges();

                    return(true);
                }
            }
            catch (ArgumentNullException anex)
            {
                _log.Error(anex.Message, anex);
                throw anex;
            }
            catch (NonexistentUserException neuex)
            {
                _log.Error(neuex.Message, neuex);
                throw neuex;
            }
            catch (SqlException sqlex)
            {
                for (int i = 0; i < sqlex.Errors.Count; i++)
                {
                    _log.Error("Index #" + i + "\n" +
                               "Message: " + sqlex.Errors[i].Message + "\n" +
                               "Error Number: " + sqlex.Errors[i].Number + "\n" +
                               "LineNumber: " + sqlex.Errors[i].LineNumber + "\n" +
                               "Source: " + sqlex.Errors[i].Source + "\n" +
                               "Procedure: " + sqlex.Errors[i].Procedure + "\n");

                    switch (sqlex.Errors[i].Number)
                    {
                    case -1:     //connection broken
                    case -2:     //timeout
                        throw new DBServerConnectionException(GlobalSetting.ResManager.GetString("DBServerConnectionError"));
                    }
                }
                throw sqlex;
            }
            catch (Exception ex)
            {
                _log.Error(ex.Message, ex);
                throw ex;
            }
        }
Esempio n. 18
0
        /// <summary>
        /// Dar de alta un FunctionalityRole
        /// </summary>
        /// <param name="newFunctionalityRole"></param>
        /// <returns></returns>
        public FunctionalityRole NewFunctionalityRole(FunctionalityRole newFunctionalityRole)
        {
            try
            {
                if (newFunctionalityRole == null)
                {
                    throw new ArgumentNullException("newFunctionalityRole");
                }

                using (var db = new HKSupplyContext())
                {
                    var functionalityRole = db.FunctionalitiesRole
                                            .Where(fr => fr.FunctionalityId.Equals(newFunctionalityRole.FunctionalityId) && fr.RoleId.Equals(newFunctionalityRole.RoleId))
                                            .FirstOrDefault();

                    if (functionalityRole != null)
                    {
                        throw new NewExistingFunctionalityRoleException(GlobalSetting.ResManager.GetString("FunctionalityRoleAlreadyExist"));
                    }

                    db.FunctionalitiesRole.Add(newFunctionalityRole);
                    db.SaveChanges();

                    return(GetFunctionalityRole(newFunctionalityRole.FunctionalityId, newFunctionalityRole.RoleId));
                }
            }
            catch (ArgumentNullException anex)
            {
                _log.Error(anex.Message, anex);
                throw anex;
            }
            catch (NewExistingFunctionalityRoleException nefre)
            {
                _log.Error(nefre.Message, nefre);
                throw nefre;
            }
            catch (SqlException sqlex)
            {
                for (int i = 0; i < sqlex.Errors.Count; i++)
                {
                    _log.Error("Index #" + i + "\n" +
                               "Message: " + sqlex.Errors[i].Message + "\n" +
                               "Error Number: " + sqlex.Errors[i].Number + "\n" +
                               "LineNumber: " + sqlex.Errors[i].LineNumber + "\n" +
                               "Source: " + sqlex.Errors[i].Source + "\n" +
                               "Procedure: " + sqlex.Errors[i].Procedure + "\n");

                    switch (sqlex.Errors[i].Number)
                    {
                    case -1:     //connection broken
                    case -2:     //timeout
                        throw new DBServerConnectionException(GlobalSetting.ResManager.GetString("DBServerConnectionError"));
                    }
                }
                throw sqlex;
            }
            catch (Exception ex)
            {
                _log.Error(ex.Message, ex);
                throw ex;
            }
        }
Esempio n. 19
0
        /// <summary>
        /// Modidificar un password de un usuario
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public bool ChangePassword(int userId, string password)
        {
            try
            {
                if (userId == 0)
                {
                    throw new ArgumentNullException("userId");
                }
                if (string.IsNullOrEmpty(password))
                {
                    throw new ArgumentNullException("password");
                }

                using (var db = new HKSupplyContext())
                {
                    var user = db.Users
                               .Include(r => r.UserRole)
                               .Where(u => u.Id.Equals(userId))
                               .FirstOrDefault();

                    if (user == null)
                    {
                        throw new NonexistentUserException(GlobalSetting.ResManager.GetString("InvalidUser"));
                    }

                    user.Password = password;
                    db.SaveChanges();
                    return(true);
                }
            }
            catch (ArgumentNullException anex)
            {
                _log.Error(anex.Message, anex);
                throw anex;
            }
            catch (NonexistentUserException neuex)
            {
                _log.Info(neuex.Message, neuex);
                throw;
            }
            catch (SqlException sqlex)
            {
                for (int i = 0; i < sqlex.Errors.Count; i++)
                {
                    _log.Error("Index #" + i + "\n" +
                               "Message: " + sqlex.Errors[i].Message + "\n" +
                               "Error Number: " + sqlex.Errors[i].Number + "\n" +
                               "LineNumber: " + sqlex.Errors[i].LineNumber + "\n" +
                               "Source: " + sqlex.Errors[i].Source + "\n" +
                               "Procedure: " + sqlex.Errors[i].Procedure + "\n");

                    switch (sqlex.Errors[i].Number)
                    {
                    case -1:     //connection broken
                    case -2:     //timeout
                        throw new DBServerConnectionException(GlobalSetting.ResManager.GetString("DBServerConnectionError"));
                    }
                }
                throw sqlex;
            }
            catch (DbEntityValidationException e)
            {
                _log.Error(e.Message, e);
                throw e;
            }
            catch (Exception ex)
            {
                _log.Error(ex.Message, ex);
                throw ex;
            }
        }
Esempio n. 20
0
        /// <summary>
        /// Modificar una colección de FunctionalityRole
        /// </summary>
        /// <param name="functionalitiesRolesToUpdate"></param>
        /// <returns></returns>
        public bool UpdateFunctionalitiesRoles(IEnumerable <FunctionalityRole> functionalitiesRolesToUpdate)
        {
            try
            {
                if (functionalitiesRolesToUpdate == null)
                {
                    throw new ArgumentNullException("functionalitiesRolesToUpdate");
                }

                using (var db = new HKSupplyContext())
                {
                    using (var dbTran = db.Database.BeginTransaction())
                    {
                        try
                        {
                            foreach (var funcRole in functionalitiesRolesToUpdate)
                            {
                                var funcRoleToUpdate = db.FunctionalitiesRole.FirstOrDefault(fr => fr.FunctionalityId.Equals(funcRole.FunctionalityId) &&
                                                                                             fr.RoleId.Equals(funcRole.RoleId));

                                if (funcRoleToUpdate != null)
                                {
                                    funcRoleToUpdate.Read   = funcRole.Read;
                                    funcRoleToUpdate.New    = funcRole.New;
                                    funcRoleToUpdate.Modify = funcRole.Modify;
                                }
                            }

                            db.SaveChanges();
                            dbTran.Commit();
                            return(true);
                        }
                        catch (SqlException sqlex)
                        {
                            dbTran.Rollback();

                            for (int i = 0; i < sqlex.Errors.Count; i++)
                            {
                                _log.Error("Index #" + i + "\n" +
                                           "Message: " + sqlex.Errors[i].Message + "\n" +
                                           "Error Number: " + sqlex.Errors[i].Number + "\n" +
                                           "LineNumber: " + sqlex.Errors[i].LineNumber + "\n" +
                                           "Source: " + sqlex.Errors[i].Source + "\n" +
                                           "Procedure: " + sqlex.Errors[i].Procedure + "\n");

                                switch (sqlex.Errors[i].Number)
                                {
                                case -1:     //connection broken
                                case -2:     //timeout
                                    throw new DBServerConnectionException(GlobalSetting.ResManager.GetString("DBServerConnectionError"));
                                }
                            }
                            throw sqlex;
                        }
                        catch (DbEntityValidationException e)
                        {
                            dbTran.Rollback();
                            _log.Error(e.Message, e);
                            throw e;
                        }
                        catch (Exception ex)
                        {
                            dbTran.Rollback();
                            _log.Error(ex.Message, ex);
                            throw ex;
                        }
                    }
                }
            }
            catch (ArgumentNullException nrex)
            {
                _log.Error(nrex.Message, nrex);
                throw nrex;
            }
        }
Esempio n. 21
0
        /// <summary>
        /// Dar de alta una funcionalidad
        /// </summary>
        /// <param name="newFunctionality"></param>
        /// <returns></returns>
        public Functionality NewFunctionality(Functionality newFunctionality)
        {
            try
            {
                if (newFunctionality == null)
                {
                    throw new ArgumentNullException();
                }

                using (var db = new HKSupplyContext())
                {
                    var functionality = db.Functionalities.FirstOrDefault(f => f.FunctionalityName.Equals(newFunctionality.FunctionalityName));

                    if (functionality != null)
                    {
                        throw new NewExistingFunctionalityException(GlobalSetting.ResManager.GetString("FunctionalityAlreadyExist"));
                    }

                    db.Functionalities.Add(newFunctionality);
                    db.SaveChanges();

                    return(GetFunctionalityByName(newFunctionality.FunctionalityName));
                }
            }
            catch (ArgumentNullException anex)
            {
                _log.Error(anex.Message, anex);
                throw anex;
            }
            catch (NewExistingFunctionalityException afeex)
            {
                _log.Error(afeex.Message, afeex);
                throw afeex;
            }
            catch (DbEntityValidationException e)
            {
                foreach (var eve in e.EntityValidationErrors)
                {
                    _log.Error("Entity of type \"" + eve.Entry.Entity.GetType().Name + "\" in state \"" + eve.Entry.State + "\" has the following validation errors:");
                    foreach (var ve in eve.ValidationErrors)
                    {
                        _log.Error("- Property: \"" + ve.PropertyName + "\", Error: \"" + ve.ErrorMessage + "\"");
                    }
                }
                throw e;
            }
            catch (SqlException sqlex)
            {
                for (int i = 0; i < sqlex.Errors.Count; i++)
                {
                    _log.Error("Index #" + i + "\n" +
                               "Message: " + sqlex.Errors[i].Message + "\n" +
                               "Error Number: " + sqlex.Errors[i].Number + "\n" +
                               "LineNumber: " + sqlex.Errors[i].LineNumber + "\n" +
                               "Source: " + sqlex.Errors[i].Source + "\n" +
                               "Procedure: " + sqlex.Errors[i].Procedure + "\n");

                    switch (sqlex.Errors[i].Number)
                    {
                    case -1:     //connection broken
                    case -2:     //timeout
                        throw new DBServerConnectionException(GlobalSetting.ResManager.GetString("DBServerConnectionError"));
                    }
                }
                throw sqlex;
            }
            catch (Exception ex)
            {
                _log.Error(ex.Message, ex);
                throw ex;
            }
        }