Exemplo n.º 1
0
        public void  DTO2DB_ProductTesting(DTO.ProductTestingDTO dtoItem, ref ProductTest dbItem, string TmpFile, int userID)
        {
            //ADD and Remove file
            foreach (ProductTestFile dbFile in dbItem.ProductTestFile.ToArray())
            {
                if (!dtoItem.productTestFileDTOs.Select(o => o.ProductTestFileID).Contains(dbFile.ProductTestFileID))
                {
                    if (!string.IsNullOrEmpty(dbFile.FileUD))
                    {
                        // remove file
                        fwFactory.RemoveImageFile(dbFile.FileUD);
                    }
                    dbItem.ProductTestFile.Remove(dbFile);
                }
            }
            foreach (DTO.ProductTestFileDTO dtoFile in dtoItem.productTestFileDTOs)
            {
                ProductTestFile dbFile;
                if (dtoFile.ProductTestFileID <= 0)
                {
                    dbFile = new ProductTestFile();
                    dbItem.ProductTestFile.Add(dbFile);
                }
                else
                {
                    dbFile = dbItem.ProductTestFile.FirstOrDefault(o => o.ProductTestFileID == dtoFile.ProductTestFileID);
                }

                if (dbFile != null)
                {
                    // change or add file
                    if (dtoFile.ScanHasChange)
                    {
                        dtoFile.FileUD = fwFactory.CreateNoneImageFilePointer(TmpFile, dtoFile.ScanNewFile, dtoFile.FileUD, dtoFile.FriendlyName);
                    }
                    AutoMapper.Mapper.Map <DTO.ProductTestFileDTO, ProductTestFile>(dtoFile, dbFile);
                }
            }


            if (dtoItem.productTestFileDTOs != null)
            {
                foreach (var item in dbItem.ProductTestFile.ToArray())
                {
                    if (!dtoItem.productTestFileDTOs.Select(s => s.ProductTestFileID).Contains(item.ProductTestFileID))
                    {
                        dbItem.ProductTestFile.Remove(item);
                    }
                }

                foreach (var item in dtoItem.productTestFileDTOs)
                {
                    ProductTestFile dbProductFile = new ProductTestFile();
                    if (item.ProductTestFileID < 0)
                    {
                        dbItem.ProductTestFile.Add(dbProductFile);
                    }
                    else
                    {
                        dbProductFile = dbItem.ProductTestFile.Where(s => s.ProductTestFileID == item.ProductTestFileID).FirstOrDefault();
                    }
                    if (dbProductFile != null)
                    {
                        AutoMapper.Mapper.Map <DTO.ProductTestFileDTO, ProductTestFile>(item, dbProductFile);
                    }
                }
            }

            if (dtoItem.productTestStandardDTOs != null)
            {
                foreach (var item in dbItem.ProductTestUsingTestStandard.ToArray())
                {
                    if (!dtoItem.productTestStandardDTOs.Select(o => o.ProductTestUsingTestStandardID).Contains(item.ProductTestUsingTestStandardID))
                    {
                        dbItem.ProductTestUsingTestStandard.Remove(item);
                    }
                }

                foreach (var item in dtoItem.productTestStandardDTOs)
                {
                    ProductTestUsingTestStandard dbTestStandard = new ProductTestUsingTestStandard();
                    if (item.ProductTestUsingTestStandardID < 0)
                    {
                        dbItem.ProductTestUsingTestStandard.Add(dbTestStandard);
                    }
                    else
                    {
                        dbTestStandard = dbItem.ProductTestUsingTestStandard.Where(s => s.ProductTestUsingTestStandardID == item.ProductTestUsingTestStandardID).FirstOrDefault();
                    }
                    if (dbTestStandard != null)
                    {
                        AutoMapper.Mapper.Map <DTO.ProductTestStandardDTO, ProductTestUsingTestStandard>(item, dbTestStandard);
                    }
                }
            }

            AutoMapper.Mapper.Map <DTO.ProductTestingDTO, ProductTest>(dtoItem, dbItem);

            if (!string.IsNullOrEmpty(dtoItem.TestDate))
            {
                if (DateTime.TryParse(dtoItem.TestDate, nl, System.Globalization.DateTimeStyles.None, out tmpDate))
                {
                    dbItem.TestDate = tmpDate;
                }
            }

            if (!string.IsNullOrEmpty(dtoItem.UpdatedDate))
            {
                if (DateTime.TryParse(dtoItem.UpdatedDate, nl, System.Globalization.DateTimeStyles.None, out tmpDate))
                {
                    dbItem.UpdatedDate = tmpDate;
                }
            }
            //dbItem.UpdatedDate = dtoItem.UpdatedDate.ConvertStringToDateTime();
            //dbItem.TestDate = dtoItem.TestDate.ConvertStringToDateTime();
        }
Exemplo n.º 2
0
        public override bool UpdateData(int userId, int id, ref object dtoItem, out Notification notification)
        {
            notification = new Notification()
            {
                Type = NotificationType.Success
            };
            DTO.ProductTestingDTO dtoItems = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.ProductTestingDTO>();
            try
            {
                using (var context = CreatContex())
                {
                    ProductTest dbItem = null;
                    using (DbContextTransaction scope = context.Database.BeginTransaction())
                    {
                        context.Database.ExecuteSqlCommand("SELECT * FROM ProductTest WITH (TABLOCKX, HOLDLOCK)");
                        try
                        {
                            if (id == 0)
                            {
                                dbItem = new ProductTest();
                                context.ProductTest.Add(dbItem);
                            }
                            else
                            {
                                dbItem = context.ProductTest.Where(o => o.ProductTestID == id).FirstOrDefault();
                            }
                            if (dbItem == null)
                            {
                                notification.Message = "Data Not found !";
                                return(false);
                            }
                            else
                            {
                                converter.DTO2DB_ProductTesting(dtoItems, ref dbItem, FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\", userId);

                                //remove
                                context.ProductTestFile.Local.Where(o => o.ProductTest == null).ToList().ForEach(o => context.ProductTestFile.Remove(o));
                                context.ProductTestUsingTestStandard.Local.Where(o => o.ProductTest == null).ToList().ForEach(o => context.ProductTestUsingTestStandard.Remove(o));

                                dbItem.UpdatedBy   = userId;
                                dbItem.UpdatedDate = DateTime.Now;

                                context.SaveChanges();

                                //Create code of id
                                dbItem.ProductTestUD = "P" + dbItem.ProductTestID.ToString("D9");
                                context.SaveChanges();
                            }
                        }
                        catch (Exception ex)
                        {
                            throw ex;
                        }
                        finally
                        {
                            scope.Commit();
                        }
                    }
                    dtoItem = GetData(dbItem.ProductTestID, out notification).Data;
                }
                return(true);
            }
            catch (Exception ex)
            {
                notification.Type    = Library.DTO.NotificationType.Error;
                notification.Message = ex.Message;
                notification.DetailMessage.Add(ex.Message);
                if (ex.GetBaseException() != null)
                {
                    notification.DetailMessage.Add(ex.GetBaseException().Message);
                }
                return(false);
            }
        }