Exemplo n.º 1
0
        public bool UpdateData(int id, ref DTO.MaterialTypeMng.MaterialType dtoItem, int userId, out Library.DTO.Notification notification)
        {
            notification      = new Notification();
            notification.Type = NotificationType.Success;

            int    number;
            string indexName;

            try
            {
                using (var context = CreateContext())
                {
                    MaterialType materialType = null;

                    if (id == 0)
                    {
                        materialType = new MaterialType();

                        context.MaterialType.Add(materialType);
                    }
                    else
                    {
                        materialType = context.MaterialType.FirstOrDefault(o => o.MaterialTypeID == id);
                    }

                    if (materialType == null)
                    {
                        notification.Message = "Material Type not found!";

                        return(false);
                    }
                    else
                    {
                        //hangtag image
                        Module.Framework.DAL.DataFactory fwFactory = new Module.Framework.DAL.DataFactory();
                        // Fixed bug check dtoItem.HangTagFileHasChange has value after get value - 21 FEB 2018.
                        if (dtoItem.HangTagFileHasChange.HasValue)
                        {
                            dtoItem.HangTagFile = fwFactory.CreateNoneImageFilePointer(FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\", dtoItem.NewHangTagFile, dtoItem.HangTagFile);
                        }
                        converter.DTO2BD_MaterialType(dtoItem, ref materialType);
                        if (id <= 0)
                        {
                            // Generate code.
                            using (var trans = context.Database.BeginTransaction())
                            {
                                context.Database.ExecuteSqlCommand("SELECT * FROM MaterialType WITH (TABLOCKX, HOLDLOCK)");

                                try
                                {
                                    var newCode = context.MaterialTypeMng_function_GenerateCode().FirstOrDefault();

                                    if (!"**".Equals(newCode))
                                    {
                                        materialType.MaterialTypeUD = newCode;

                                        context.SaveChanges();
                                    }
                                    else
                                    {
                                        notification.Type    = NotificationType.Error;
                                        notification.Message = "Auto generated code exceed maximum option: [ZZ]";
                                    }
                                }
                                catch (Exception ex)
                                {
                                    trans.Rollback();
                                    throw ex;
                                }
                                finally
                                {
                                    trans.Commit();
                                }
                            }
                        }
                        else
                        {
                            context.SaveChanges();
                        }

                        dtoItem = GetData(materialType.MaterialTypeID, out notification).Data;

                        return(true);
                    }
                }
            }
            catch (DataException exData)
            {
                notification.Type = NotificationType.Error;
                ErrorHelper.DataExceptionParser(exData, out number, out indexName);

                if (number == 2601 && !string.IsNullOrEmpty(indexName))
                {
                    if ("MaterialTypeUDUnique".Equals(indexName))
                    {
                        notification.Message = "The Material Type Code is already exists.";
                    }
                }
                else
                {
                    notification.Message = exData.Message;
                }

                return(false);
            }
            catch (Exception ex)
            {
                notification.Type    = NotificationType.Error;
                notification.Message = ex.Message;

                return(false);
            }
        }
Exemplo n.º 2
0
 public void DTO2BD_MaterialType(DTO.MaterialTypeMng.MaterialType dtoItem, ref MaterialType dbItem)
 {
     AutoMapper.Mapper.Map <DTO.MaterialTypeMng.MaterialType, MaterialType>(dtoItem, dbItem);
 }