public bool FillInfoScreen(Screen model)
        {
            model.Timestamp = DateTime.Now;

            foreach (var screenSchedule in model.ScreenSchedule)
            {
                screenSchedule.HasClose = false;
                screenSchedule.IsFullScreen = false;
                screenSchedule.StartTime = null;
                screenSchedule.EndTime = null;
                screenSchedule.Day = null;
                screenSchedule.Year = null;
                screenSchedule.UserId = model.UserId;
            }
            return true;
        }
        public ResponseMessageModel DoUpsert(SignageDigitalEntities db, Screen model)
        {
            if (model.ScreenId > 0)
            {
                var modelOld = db.Screen.Single(e => e.ScreenId == model.ScreenId);
                modelOld.Name = model.Name;
                modelOld.CatScreenSizeId = model.CatScreenSizeId;
                modelOld.UserId = model.UserId;
                modelOld.Timestamp = DateTime.Now;

                for (var i = modelOld.ScreenSchedule.Count-1; i >= 0; i--)
                {
                    var screenOld = modelOld.ScreenSchedule.ElementAt(i);
                    var screenNew = model.ScreenSchedule.FirstOrDefault(e => e.ScreenId == screenOld.ScreenId);

                    if (screenNew == null)
                    {
                        db.ScreenSchedule.Remove(screenOld);
                    }
                    else
                    {
                        screenOld.CopyValues(screenNew);
                        model.ScreenSchedule.Remove(screenNew);
                    }
                }

                foreach (var screenSchedule in model.ScreenSchedule)
                {
                    modelOld.ScreenSchedule.Add(screenSchedule);
                }

            }
            else
            {
                db.Screen.Add(model);
            }

            db.SaveChanges();

            return new ResponseMessageModel
            {
                HasError = false,
                Title = ResShared.TITLE_REGISTER_SUCCESS,
                Message = ResShared.INFO_REGISTER_SAVED,
            };
        }
        public ActionResult DoUpsert(Screen model)
        {
            try
            {
                var service = new ScreenService();
                model.UserId = User.Identity.GetUserId();

                if (service.FillInfoScreen(model) == false)
                {
                    return Json(new ResponseMessageModel
                    {
                        HasError = false,
                        Title = ResShared.TITLE_REGISTER_FAILED,
                        Message = ResMediaRep.ERROR_NOMEDIA_INFO
                    });
                }

                ModelState.Clear();
                ValidateModel(model);
                if (ModelState.IsValid == false)
                {
                    return Json(new ResponseMessageModel
                    {
                        HasError = true,
                        Title = ResShared.TITLE_REGISTER_FAILED,
                        Message = ResShared.ERROR_INVALID_MODEL
                    });
                }

                var respMsg = service.DoUpsert(Db, model);
                respMsg.UrlToGo = Url.Action("Index", "Device", new {area = ""});
                return Json(respMsg);

            }
            catch (Exception ex)
            {
                SharedLogger.LogError(ex, model.Name, model.CatScreenSizeId, model.UserId);
                return Json(new ResponseMessageModel
                {
                    HasError = true,
                    Title = ResShared.TITLE_REGISTER_FAILED,
                    Message = ResShared.ERROR_UNKOWN
                });
            }
        }
 public ActionResult Upsert(int? id)
 {
     try
     {
         Screen model;
         if (id.HasValue)
         {
             model = Db.Screen.Single(e => e.ScreenId == id);
             ViewBag.Screens = new JavaScriptSerializer().Serialize(model.ScreenSchedule.Select(e => new
             {
                 x = e.PositionX,
                 y = e.PositionY,
                 z = e.PositionZ,
                 w = e.Width,
                 h = e.Height,
                 active = false,
                 selected = false,
                 name = e.Channel.Name,
                 idChannel = e.ChannelId,
                 id = e.ScreenId
             }));
         }
         else
         {
             ViewBag.Screens = "[]";
             model = new Screen();
         }
         var serviceJs = new CatSerializeService();
         ViewBag.ScreenSizes = serviceJs.GetScreenSize(Db);
         ViewBag.Channels = serviceJs.GetChannels(Db);
         return View(model);
     }
     catch (Exception ex)
     {
         ViewBag.Error = ex.Message;
         return View();
     }
 }