Exemplo n.º 1
0
        public void DTO2DB_ProductBreakDown(DTO.ProductBreakDownData dtoItem, ref ProductBreakDown dbItem, string tempFile, int userId)
        {
            // ProductBreakDown
            AutoMapper.Mapper.Map <DTO.ProductBreakDownData, ProductBreakDown>(dtoItem, dbItem);

            // ProductBreakDown: Category [Progress on Database]
            foreach (var dbCategory in dbItem.ProductBreakDownCategory.ToArray())
            {
                if (!dtoItem.ProductBreakDownCategory.Select(o => o.ProductBreakDownCategoryID).Contains(dbCategory.ProductBreakDownCategoryID))
                {
                    // Category image
                    foreach (var dbCategoryImage in dbCategory.ProductBreakDownCategoryImage.ToArray())
                    {
                        if (!string.IsNullOrEmpty(dbCategoryImage.FileUD))
                        {
                            // Remove Files
                            fwFactory.RemoveImageFile(dbCategoryImage.FileUD);
                        }
                        // Remove ProductBreakDownCategoryImage
                        dbCategory.ProductBreakDownCategoryImage.Remove(dbCategoryImage);
                    }

                    // Category type
                    foreach (var dbCategoryType in dbCategory.ProductBreakDownCategoryType.ToArray())
                    {
                        // Detail
                        foreach (var dbDetail in dbCategoryType.ProductBreakDownDetail.ToArray())
                        {
                            dbCategoryType.ProductBreakDownDetail.Remove(dbDetail);
                        }

                        dbCategory.ProductBreakDownCategoryType.Remove(dbCategoryType);
                    }

                    // Remove ProductBreakDownCategory
                    dbItem.ProductBreakDownCategory.Remove(dbCategory);
                }
            }

            // ProductBreakDown: Category [Progress on DTO]
            foreach (var dtoCategory in dtoItem.ProductBreakDownCategory)
            {
                ProductBreakDownCategory dbCategory;

                if (dtoCategory.ProductBreakDownCategoryID < 0)
                {
                    dbCategory = new ProductBreakDownCategory();
                    dbItem.ProductBreakDownCategory.Add(dbCategory);
                }
                else
                {
                    dbCategory = dbItem.ProductBreakDownCategory.FirstOrDefault(o => o.ProductBreakDownCategoryID == dtoCategory.ProductBreakDownCategoryID);
                }

                if (dbCategory != null)
                {
                    AutoMapper.Mapper.Map <DTO.ProductBreakDownCategoryData, ProductBreakDownCategory>(dtoCategory, dbCategory);

                    // Category image
                    foreach (var dbCategoryImage in dbCategory.ProductBreakDownCategoryImage.ToArray())
                    {
                        if (!dtoCategory.ProductBreakDownCategoryImage.Select(o => o.ProductBreakDownCategoryImageID).Contains(dbCategoryImage.ProductBreakDownCategoryImageID))
                        {
                            if (!string.IsNullOrEmpty(dbCategoryImage.FileUD))
                            {
                                fwFactory.RemoveImageFile(dbCategoryImage.FileUD);
                            }
                            dbCategory.ProductBreakDownCategoryImage.Remove(dbCategoryImage);
                        }
                    }

                    // Category type
                    foreach (var dbCategoryType in dbCategory.ProductBreakDownCategoryType.ToArray())
                    {
                        if (!dtoCategory.ProductBreakDownCategoryType.Select(o => o.ProductBreakDownCategoryTypeID).Contains(dbCategoryType.ProductBreakDownCategoryTypeID))
                        {
                            // Remove detail
                            foreach (var dbDetail in dbCategoryType.ProductBreakDownDetail.ToArray())
                            {
                                dbCategoryType.ProductBreakDownDetail.Remove(dbDetail);
                            }

                            dbCategory.ProductBreakDownCategoryType.Remove(dbCategoryType);
                        }
                    }

                    // Category image
                    foreach (var dtoCategoryImage in dtoCategory.ProductBreakDownCategoryImage)
                    {
                        ProductBreakDownCategoryImage dbCategoryImage;

                        if (dtoCategoryImage.ProductBreakDownCategoryImageID < 0)
                        {
                            dbCategoryImage = new ProductBreakDownCategoryImage();
                            dbCategory.ProductBreakDownCategoryImage.Add(dbCategoryImage);
                        }
                        else
                        {
                            dbCategoryImage = dbCategory.ProductBreakDownCategoryImage.FirstOrDefault(o => o.ProductBreakDownCategoryImageID == dtoCategoryImage.ProductBreakDownCategoryImageID);
                        }

                        if (dbCategoryImage != null)
                        {
                            // Progress category image
                            if (dtoCategoryImage.HasChange.HasValue && dtoCategoryImage.HasChange.Value)
                            {
                                dbCategoryImage.FileUD = fwFactory.CreateFilePointer(tempFile, dtoCategoryImage.NewFile, dtoCategoryImage.FileUD, dtoCategoryImage.FriendlyName);
                            }

                            AutoMapper.Mapper.Map <DTO.ProductBreakDownCategoryImageData, ProductBreakDownCategoryImage>(dtoCategoryImage, dbCategoryImage);
                        }
                    }

                    // Category type
                    foreach (var dtoCategoryType in dtoCategory.ProductBreakDownCategoryType)
                    {
                        ProductBreakDownCategoryType dbCategoryType;

                        if (dtoCategoryType.ProductBreakDownCategoryTypeID < 0)
                        {
                            dbCategoryType = new ProductBreakDownCategoryType();
                            dbCategory.ProductBreakDownCategoryType.Add(dbCategoryType);
                        }
                        else
                        {
                            dbCategoryType = dbCategory.ProductBreakDownCategoryType.FirstOrDefault(o => o.ProductBreakDownCategoryTypeID == dtoCategoryType.ProductBreakDownCategoryTypeID);
                        }

                        if (dbCategoryType != null)
                        {
                            AutoMapper.Mapper.Map <DTO.ProductBreakDownCategoryTypeData, ProductBreakDownCategoryType>(dtoCategoryType, dbCategoryType);

                            foreach (var dbDetail in dbCategoryType.ProductBreakDownDetail.ToArray())
                            {
                                if (!dtoCategoryType.ProductBreakDownDetail.Select(s => s.ProductBreakDownDetailID).Contains(dbDetail.ProductBreakDownDetailID))
                                {
                                    dbCategoryType.ProductBreakDownDetail.Remove(dbDetail);
                                }
                            }

                            foreach (var dtoDetail in dtoCategoryType.ProductBreakDownDetail)
                            {
                                ProductBreakDownDetail dbDetail;

                                if (dtoDetail.ProductBreakDownDetailID <= 0)
                                {
                                    dbDetail = new ProductBreakDownDetail();
                                    dbCategoryType.ProductBreakDownDetail.Add(dbDetail);
                                }
                                else
                                {
                                    dbDetail = dbCategoryType.ProductBreakDownDetail.FirstOrDefault(o => o.ProductBreakDownDetailID == dtoDetail.ProductBreakDownDetailID);
                                }

                                if (dbDetail != null)
                                {
                                    AutoMapper.Mapper.Map <DTO.ProductBreakDownDetailData, ProductBreakDownDetail>(dtoDetail, dbDetail);
                                }
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        public object UpdateData(int userId, Hashtable filters, out Notification notification)
        {
            DTO.EditFormData data = new DTO.EditFormData();

            notification = new Notification()
            {
                Type = NotificationType.Success
            };

            try
            {
                int typeUpdate = (filters.ContainsKey("typeGet") && filters["typeGet"] != null && !string.IsNullOrEmpty(filters["typeGet"].ToString())) ? Convert.ToInt32(filters["typeGet"].ToString()) : 0;
                int id         = (filters.ContainsKey("id") && filters["id"] != null && !string.IsNullOrEmpty(filters["id"].ToString())) ? Convert.ToInt32(filters["id"].ToString()) : 0;

                switch (typeUpdate)
                {
                case 1:     // Update data ProductBreakDownDefaultCategory
                    DTO.ProductBreakDownDefaultCategoryData dtoItem_1 = ((Newtonsoft.Json.Linq.JObject)filters["dataView"]).ToObject <DTO.ProductBreakDownDefaultCategoryData>();

                    using (var context = CreateContext())
                    {
                        ProductBreakDownDefaultCategory dbItem;

                        if (id == 0)
                        {
                            dbItem = new ProductBreakDownDefaultCategory();
                            context.ProductBreakDownDefaultCategory.Add(dbItem);
                        }
                        else
                        {
                            dbItem = context.ProductBreakDownDefaultCategory.FirstOrDefault(o => o.ProductBreakDownDefaultCategoryID == id);
                        }

                        // Check dbItem is null
                        if (dbItem == null)
                        {
                            notification.Type    = NotificationType.Error;
                            notification.Message = "Can not find data";

                            return(null);
                        }

                        // Mapping data
                        converter.DTO2DB_ProductBreakDownDefaultCategory(dtoItem_1, ref dbItem);

                        // Update createdBy, createdDate, updatedBy, updatedDate
                        if (id == 0)
                        {
                            dbItem.CreatedBy   = userId;
                            dbItem.CreatedDate = DateTime.Now;
                        }
                        dbItem.UpdatedBy   = userId;
                        dbItem.UpdatedDate = DateTime.Now;

                        // Update display order
                        //int displayOrder = context.ProductBreakDownDefaultCategory.Count();
                        //if (id == 0 && !dtoItem_1.DisplayOrder.HasValue)
                        //{
                        //    dbItem.DisplayOrder = displayOrder + 1;
                        //}
                        //else
                        //{
                        //    dbItem.DisplayOrder = displayOrder;
                        //}

                        context.SaveChanges();

                        filters["id"] = dbItem.ProductBreakDownDefaultCategoryID;
                    }

                    return(GetData(filters, out notification).DefaultData);

                case 2:     // Update data ProductBreakDown
                    DTO.ProductBreakDownData dtoItem_2 = ((Newtonsoft.Json.Linq.JObject)filters["dataView"]).ToObject <DTO.ProductBreakDownData>();

                    using (var context = CreateContext())
                    {
                        ProductBreakDown dbItem;

                        if (id == 0)
                        {
                            dbItem = new ProductBreakDown();
                            context.ProductBreakDown.Add(dbItem);
                        }
                        else
                        {
                            dbItem = context.ProductBreakDown.FirstOrDefault(o => o.ProductBreakDownID == id);
                        }

                        // Check dbItem is null
                        if (dbItem == null)
                        {
                            notification.Type    = NotificationType.Error;
                            notification.Message = "Can not find data";

                            return(null);
                        }

                        converter.DTO2DB_ProductBreakDown(dtoItem_2, ref dbItem, FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\", userId);

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

                        context.ProductBreakDownDetail.Local.Where(o => o.ProductBreakDownCategoryType == null).ToList().ForEach(o => context.ProductBreakDownDetail.Remove(o));
                        context.ProductBreakDownCategoryType.Local.Where(o => o.ProductBreakDownCategory == null).ToList().ForEach(o => context.ProductBreakDownCategoryType.Remove(o));
                        context.ProductBreakDownCategoryImage.Local.Where(o => o.ProductBreakDownCategory == null).ToList().ForEach(o => context.ProductBreakDownCategoryImage.Remove(o));
                        context.ProductBreakDownCategory.Local.Where(o => o.ProductBreakDown == null).ToList().ForEach(o => context.ProductBreakDownCategory.Remove(o));

                        context.SaveChanges();

                        filters["id"] = dbItem.ProductBreakDownID;
                    }

                    return(GetData(filters, out notification).MainData);

                default:
                    break;
                }
            }
            catch (Exception ex)
            {
                notification.Type    = NotificationType.Error;
                notification.Message = Library.Helper.GetInnerException(ex).Message;
            }

            return(data);
        }