Esempio n. 1
0
        public bool DeleteData(int userId, int id, out Library.DTO.Notification notification)
        {
            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            try
            {
                // check permission
                if (id > 0 && fwFactory.CheckPLCPermission(userId, id) == 0)
                {
                    throw new Exception("Current user don't have access permission for the selected plc data");
                }

                using (FactoryPLCMngEntities context = CreateContext())
                {
                    // check if can delete PLC
                    string loadingPlanUD = context.FactoryPLCMng_function_CheckIfCanDeletePLC(id).FirstOrDefault();
                    if (!string.IsNullOrEmpty(loadingPlanUD))
                    {
                        throw new Exception("Current PLC already exists in loading plan: " + loadingPlanUD);
                    }

                    // everything ok, delete the plc
                    PLC dbItem = context.PLC.FirstOrDefault(o => o.PLCID == id);
                    if (dbItem == null)
                    {
                        throw new Exception("PLC not found");
                    }

                    // remove image
                    foreach (PLCImage dbImage in dbItem.PLCImage)
                    {
                        if (!string.IsNullOrEmpty(dbImage.ImageFile))
                        {
                            fwFactory.RemoveImageFile(dbImage.ImageFile);
                        }
                    }
                    context.PLC.Remove(dbItem);
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                notification.Type    = Library.DTO.NotificationType.Error;
                notification.Message = ex.Message;
                return(false);
            }

            return(true);
        }
Esempio n. 2
0
        public override bool UpdateData(int userId, int id, ref object dtoItem, out Library.DTO.Notification notification)
        {
            DTO.PLC dtoPLC = ((Newtonsoft.Json.Linq.JObject)dtoItem).ToObject <DTO.PLC>();

            notification = new Library.DTO.Notification()
            {
                Type = Library.DTO.NotificationType.Success
            };
            try
            {
                using (FactoryPLCMngEntities context = CreateContext())
                {
                    // check if user can update this information
                    int?permissionResult = context.FactoryPLCMng_function_CheckUserPermission(userId, dtoPLC.OfferLineID, dtoPLC.OfferLineSparepartID, dtoPLC.BookingID).FirstOrDefault();
                    if (!permissionResult.HasValue && permissionResult.Value == 0)
                    {
                        throw new Exception("Current user dont have permission to edit this data (data permission validation failed)");
                    }
                    var checkImg = dtoPLC.PLCImages.FirstOrDefault(o => o.ImageTypeID == 15);
                    if (checkImg == null)
                    {
                        throw new Exception("Please add Overral Image !!!");
                    }
                    PLC dbItem = null;
                    if (id == 0)
                    {
                        if (!dtoPLC.BookingID.HasValue || !dtoPLC.FactoryID.HasValue)
                        {
                            throw new Exception("You have to select Booking and Factory before create PLC");
                        }
                        int bookingID = dtoPLC.BookingID.Value;
                        int factoryID = dtoPLC.FactoryID.Value;
                        if (dtoPLC.OfferLineID.HasValue)
                        {
                            int offerLineID = dtoPLC.OfferLineID.Value;
                            if (context.PLC.FirstOrDefault(o => o.OfferLineID == offerLineID && o.BookingID == bookingID && o.FactoryID == factoryID) != null)
                            {
                                throw new Exception("Item already has PLC");
                            }
                        }
                        else if (dtoPLC.OfferLineSparepartID.HasValue)
                        {
                            int offerLineSparepartID = dtoPLC.OfferLineSparepartID.Value;
                            if (context.PLC.FirstOrDefault(o => o.OfferLineSparepartID == offerLineSparepartID && o.BookingID == bookingID && o.FactoryID == factoryID) != null)
                            {
                                throw new Exception("Item already has PLC");
                            }
                        }
                        else if (dtoPLC.OfferLineSampleProductID.HasValue)
                        {
                            int offerLineSampleProductID = dtoPLC.OfferLineSampleProductID.Value;
                            if (context.PLC.FirstOrDefault(o => o.OfferLineSampleProductID == offerLineSampleProductID && o.BookingID == bookingID && o.FactoryID == factoryID) != null)
                            {
                                throw new Exception("Item already has PLC");
                            }
                        }

                        dbItem = new PLC();
                        context.PLC.Add(dbItem);
                    }
                    else
                    {
                        dbItem = context.PLC.FirstOrDefault(o => o.PLCID == id);
                    }

                    if (dbItem == null)
                    {
                        notification.Message = "PLC not found!";
                        return(false);
                    }
                    else
                    {
                        // check concurrency
                        if (dbItem.ConcurrencyFlag != null && !dbItem.ConcurrencyFlag.SequenceEqual(Convert.FromBase64String(dtoPLC.ConcurrencyFlag)))
                        {
                            throw new Exception(Library.Helper.TEXT_CONCURRENCY_CONFLICT);
                        }

                        converter.DTO2DB(dtoPLC, ref dbItem, FrameworkSetting.Setting.AbsoluteUserTempFolder + userId.ToString() + @"\");
                        dbItem.UpdatedBy   = userId;
                        dbItem.UpdatedDate = DateTime.Now;

                        // remove orphan
                        context.PLCImage.Local.Where(o => o.PLC == null).ToList().ForEach(o => context.PLCImage.Remove(o));

                        context.SaveChanges();

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