Exemplo n.º 1
0
        public bool UpdateData(int id, ref object 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);
                        materialType.UpdatedBy   = userId;
                        materialType.UpdatedDate = DateTime.Now;
                    }
                    else
                    {
                        var item = context.MaterialTypeMng_MaterialTypeCheck_View.Where(o => o.MaterialTypeID == id).FirstOrDefault();
                        //CheckPermission
                        if (item.isUsed.Value == true)
                        {
                            throw new Exception("You can't update because it used in item other!");
                        }
                        materialType             = context.MaterialType.FirstOrDefault(o => o.MaterialTypeID == id);
                        materialType.UpdatedBy   = userId;
                        materialType.UpdatedDate = DateTime.Now;
                    }

                    DTO.MaterialTypes dtoMaterialType = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.MaterialTypes>();

                    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 (dtoMaterialType.HangTagFileHasChange.HasValue)
                        {
                            dtoMaterialType.HangTagFile = fwFactory.CreateNoneImageFilePointer(FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\", dtoMaterialType.NewHangTagFile, dtoMaterialType.HangTagFile);
                        }
                        converter.DTO2BD_MaterialType(dtoMaterialType, 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.MaterialTypes dtoItem, ref MaterialType dbItem)
 {
     AutoMapper.Mapper.Map <DTO.MaterialTypes, MaterialType>(dtoItem, dbItem);
 }