public override bool DeleteData(int id, out Library.DTO.Notification notification)
        {
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            try
            {
                using (QCReportMngEntities context = CreateContext())
                {
                    QCReport dbItem = context.QCReport.FirstOrDefault(o => o.QCReportID == id);
                    if (dbItem == null)
                    {
                        notification.Message = "Report not found!";
                        return(false);
                    }
                    else
                    {
                        context.QCReport.Remove(dbItem);
                        context.SaveChanges();

                        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);
            }
        }
Beispiel #2
0
        public void DTO2DB(DTO.QCReport dtoItem, ref QCReport dbItem, string _tempFolder, int userId)
        {
            // map fields
            AutoMapper.Mapper.Map <DTO.QCReport, QCReport>(dtoItem, dbItem);
            dbItem.UpdatedDate  = DateTime.Now;
            dbItem.UpdatedBy    = dtoItem.UpdatedBy;
            dbItem.InspectedDay = dtoItem.InspectedDay.ConvertStringToDateTime();
            if (dtoItem.AttachedFile_HasChanged)
            {
                dbItem.FileUD = (new Module.Framework.DAL.DataFactory()).CreateFilePointer(_tempFolder, dtoItem.AttachedFile_NewFile, dtoItem.FileUD);
            }

            // map qc report image
            if (dtoItem.QCReportImages != null)
            {
                // check for child rows deleted
                foreach (QCReportImage dbDraw in dbItem.QCReportImage.ToArray())
                {
                    if (!dtoItem.QCReportImages.Select(o => o.QCReportImageID).Contains(dbDraw.QCReportImageID))
                    {
                        dbItem.QCReportImage.Remove(dbDraw);
                    }
                }

                // map child rows
                foreach (DTO.QCReportImage dtoImage in dtoItem.QCReportImages)
                {
                    QCReportImage dbImage;
                    if (dtoImage.QCReportImageID <= 0)
                    {
                        dbImage = new QCReportImage();
                        dbItem.QCReportImage.Add(dbImage);
                        dtoItem.FileUD = dtoImage.FileUD;
                    }
                    else
                    {
                        dbImage = dbItem.QCReportImage.FirstOrDefault(o => o.QCReportImageID == dtoImage.QCReportImageID);
                    }
                    //dtoItem.dbImage.Last();
                    //dbImage.UpdatedDate = DateTime.Now;
                    if (dbImage != null)
                    {
                        AutoMapper.Mapper.Map <DTO.QCReportImage, QCReportImage>(dtoImage, dbImage);
                    }
                }
                if (dtoItem.QCReportImages.Count > 0)
                {
                    var item = dtoItem.QCReportImages.Count - 1;
                    dtoItem.FileUD = dtoItem.QCReportImages[item].FileUD;
                }
            }

            // map qc report document
            if (dtoItem.QCReportDocuments != null)
            {
                // check for child rows deleted
                foreach (QCReportDocument dbDocument in dbItem.QCReportDocument.ToArray())
                {
                    if (!dtoItem.QCReportDocuments.Select(o => o.QCReportDocumentID).Contains(dbDocument.QCReportDocumentID))
                    {
                        dbItem.QCReportDocument.Remove(dbDocument);
                    }
                }

                // map child rows
                foreach (DTO.QCReportDocument dtoDocument in dtoItem.QCReportDocuments)
                {
                    QCReportDocument dbDocument;
                    if (dtoDocument.QCReportDocumentID <= 0)
                    {
                        dbDocument = new QCReportDocument();
                        dbItem.QCReportDocument.Add(dbDocument);
                        dtoItem.FileUD = dtoDocument.FileUD;
                    }
                    else
                    {
                        dbDocument = dbItem.QCReportDocument.FirstOrDefault(o => o.QCReportDocumentID == dtoDocument.QCReportDocumentID);
                    }

                    if (dbDocument != null)
                    {
                        AutoMapper.Mapper.Map <DTO.QCReportDocument, QCReportDocument>(dtoDocument, dbDocument);

                        dbDocument.UpdatedBy   = userId;
                        dbDocument.UpdatedDate = DateTime.Now;
                    }
                }
                if (dtoItem.QCReportDocuments.Count > 0)
                {
                    var item = dtoItem.QCReportDocuments.Count - 1;
                    dtoItem.FileUD = dtoItem.QCReportDocuments[item].FileUD;
                }
            }

            // map qc detail
            if (dtoItem.QCReportDetails != null)
            {
                //check for child rows deleted
                foreach (QCReportDetail dbDetail in dbItem.QCReportDetail.ToArray())
                {
                    if (!dtoItem.QCReportDetails.Select(o => o.QCReportDetailID).Contains(dbDetail.QCReportDetailID))
                    {
                        dbItem.QCReportDetail.Remove(dbDetail);
                    }
                }

                //map child row
                foreach (DTO.QCReportDetail dtoDetail in dtoItem.QCReportDetails)
                {
                    QCReportDetail dbDetail;
                    if (dtoDetail.QCReportDetailID <= 0)
                    {
                        dbDetail = new QCReportDetail();
                        dbItem.QCReportDetail.Add(dbDetail);
                    }
                    else
                    {
                        dbDetail = dbItem.QCReportDetail.FirstOrDefault(o => o.QCReportDetailID == dtoDetail.QCReportDetailID);
                    }

                    if (dbDetail != null)
                    {
                        AutoMapper.Mapper.Map <DTO.QCReportDetail, QCReportDetail>(dtoDetail, dbDetail);
                    }
                }
            }

            // map qc defect
            if (dtoItem.QCReportDefects != null)
            {
                //check for child rows deleted
                foreach (QCReportDefect dbDefect in dbItem.QCReportDefect.ToArray())
                {
                    if (!dtoItem.QCReportDefects.Select(o => o.QCReportDefectID).Contains(dbDefect.QCReportDefectID))
                    {
                        dbItem.QCReportDefect.Remove(dbDefect);
                    }
                }

                //map child row
                foreach (DTO.QCReportDefect dtoDefect in dtoItem.QCReportDefects)
                {
                    QCReportDefect dbDefect;
                    if (dtoDefect.QCReportDefectID <= 0)
                    {
                        dbDefect = new QCReportDefect();
                        dbItem.QCReportDefect.Add(dbDefect);
                    }
                    else
                    {
                        dbDefect = dbItem.QCReportDefect.FirstOrDefault(o => o.QCReportDefectID == dtoDefect.QCReportDefectID);
                    }

                    if (dbDefect != null)
                    {
                        AutoMapper.Mapper.Map <DTO.QCReportDefect, QCReportDefect>(dtoDefect, dbDefect);
                    }
                }
            }
        }
        public override bool UpdateData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification)
        {
            DTO.QCReport dtoQCReport = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.QCReport>();
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            try
            {
                using (QCReportMngEntities context = CreateContext())
                {
                    QCReport dbItem = null;
                    if (id == 0)
                    {
                        dbItem = new QCReport();
                        context.QCReport.Add(dbItem);
                    }
                    else
                    {
                        dbItem = context.QCReport.FirstOrDefault(o => o.QCReportID == id);
                    }

                    if (dbItem == null)
                    {
                        notification.Message = "QC Report not found!";
                        return(false);
                    }
                    else
                    {
                        // process image
                        foreach (DTO.QCReportImage dtoImage in dtoQCReport.QCReportImages)
                        {
                            if (dtoImage.HasChange)
                            {
                                dtoImage.FileUD = fwFactory.CreateFilePointer(FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\", dtoImage.NewFile, dtoImage.FileUD);
                            }
                        }

                        // process qc report document
                        foreach (DTO.QCReportDocument dtoDocument in dtoQCReport.QCReportDocuments)
                        {
                            if (dtoDocument.QCReportDocument_HasChange)
                            {
                                dtoDocument.FileUD = fwFactory.CreateFilePointer(FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\", dtoDocument.QCReportDocument_NewFile, dtoDocument.FileUD);
                            }
                        }

                        // remove Technical Image
                        foreach (QCReportImage dbImage in context.QCReportImage.Local.Where(o => o.QCReport == null).ToList())
                        {
                            if (!string.IsNullOrEmpty(dbImage.FileUD))
                            {
                                fwFactory.RemoveImageFile(dbImage.FileUD);
                            }
                        }

                        // remove Technical Document
                        foreach (QCReportDocument dbDocument in context.QCReportDocument.Local.Where(o => o.QCReport == null).ToList())
                        {
                            if (!string.IsNullOrEmpty(dbDocument.FileUD))
                            {
                                fwFactory.RemoveImageFile(dbDocument.FileUD);
                            }
                        }

                        context.QCReportImage.Local.Where(o => o.QCReport == null).ToList().ForEach(o => context.QCReportImage.Remove(o));
                        context.QCReportDocument.Local.Where(o => o.QCReport == null).ToList().ForEach(o => context.QCReportDocument.Remove(o));

                        //convert dto to db
                        converter.DTO2DB(dtoQCReport, ref dbItem, FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\", userId);
                        dbItem.UpdatedBy   = userId;
                        dbItem.UpdatedDate = DateTime.Now;

                        context.SaveChanges();
                        dtoItem = GetData(userId, dbItem.QCReportID, dbItem.FactoryOrderDetailID, out notification).Data;
                        return(true);
                    }
                }
            }
            catch (Exception ex)
            {
                notification = new Library.DTO.Notification()
                {
                    Message = ex.Message, Type = Library.DTO.NotificationType.Error
                };
                return(false);
            }
        }