Beispiel #1
0
        public NotifyConditionViewModel ReadNotificationCondition(int id)
        {
            var n     = context.NotifyConditions.FirstOrDefault(a => a.Id == id);
            var query = new NotifyConditionViewModel
            {
                Id           = n.Id,
                ObjectName   = n.ObjectName,
                EncodedMsg   = n.EncodedMsg,
                Subject      = n.Subject,
                AlertMeFor   = n.AlertMeFor,
                AlertMeUntil = n.AlertMeUntil,
                ColumnName   = n.ColumnName,
                CompanyId    = n.CompanyId,
                NotifyRef    = n.NotifyRef,
                Sms          = n.Sms,
                TableName    = n.TableName,
                filter       = n.filter,
                Users        = n.Users == null ? null : n.Users.Split(',').ToList(),
                CustEmail    = n.CustEmail,
                Event        = (byte)n.Event,
                EventValue   = n.EventValue,
                CreatedTime  = n.CreatedTime,
                CreatedUser  = n.CreatedUser,
                ModifiedTime = n.ModifiedTime,
                ModifiedUser = n.ModifiedUser
            };

            return(query);
        }
        //SaveNotification
        public ActionResult SaveNotification(NotifyConditionViewModel model, IEnumerable <FilterGridViewModel> grid1, OptionsViewModel moreInfo)
        {
            List <Error> errors = new List <Error>();
            var          msg    = "OK";

            if (ModelState.IsValid)
            {
                if (ServerValidationEnabled)
                {
                    errors = _hrUnitOfWork.SiteRepository.CheckForm(new CheckParm
                    {
                        CompanyId    = CompanyId,
                        ObjectName   = "Notify",
                        TableName    = "NotifyConditions",
                        Columns      = Models.Utils.GetColumnViews(ModelState),
                        ParentColumn = "CompanyId",
                        Culture      = Language
                    });

                    if (errors.Count() > 0)
                    {
                        foreach (var e in errors)
                        {
                            foreach (var errorMsg in e.errors)
                            {
                                ModelState.AddModelError(errorMsg.field, errorMsg.message);
                            }
                        }

                        return(Json(Models.Utils.ParseFormErrors(ModelState)));
                    }
                }
            }
            else
            {
                return(Json(Models.Utils.ParseFormErrors(ModelState)));
            }

            int[] reminderEvent = { (int)Events.IsDueIn, (int)Events.IsDueTo, (int)Events.WasDueThisAmountofTimeAgo };

            var NotifyCondition = _hrUnitOfWork.Repository <NotifyCondition>().Where(a => a.Id == model.Id).FirstOrDefault();

            if (NotifyCondition == null)
            {
                NotifyCondition = new NotifyCondition();

                var parm = new Models.AutoMapperParm
                {
                    Destination = NotifyCondition,
                    Source      = model,
                    ObjectName  = "Notify",
                    Options     = moreInfo,
                    Transtype   = TransType.Insert
                };

                AutoMapper mapper = new Models.AutoMapper(parm, _hrUnitOfWork, User.Identity);
                mapper.Map();
                NotifyCondition.CompanyId   = CompanyId;
                NotifyCondition.CreatedTime = DateTime.Now;
                NotifyCondition.CreatedUser = UserName;
                NotifyCondition.ColumnName  = model.ColumnName;
                NotifyCondition.TableName   = model.TableName;
                NotifyCondition.ObjectName  = model.ObjectName;
                NotifyCondition.Version     = model.Version;
                NotifyCondition.Message     = model.EncodedMsg.Replace("&nbsp;", " ").Replace("&lt;", "<").Replace("&gt;", ">");
                NotifyCondition.EventValue  = model.EventValue;
                Events ev = (Events)Enum.ToObject(typeof(Events), model.Event);
                NotifyCondition.Event = ev;
                List <string> ListOfUsers = new List <string>();
                if (model.Users != null)
                {
                    foreach (var item in model.Users)
                    {
                        var users = db.Users.Where(a => a.Id == item).Select(a => a.UserName).FirstOrDefault();
                        ListOfUsers.Add(users);
                    }
                }
                NotifyCondition.Users = string.Join(",", ListOfUsers.ToArray());
                if (model.Message != null)
                {
                    var ListOfFields = GetFields(model.Message).Distinct().ToList();
                    if (model.HasEmpId && !ListOfFields.Contains("EmpId") && reminderEvent.Contains(model.Event))
                    {
                        ListOfFields.Add("EmpId");
                    }

                    NotifyCondition.Fields = string.Join(",", ListOfFields.ToArray());
                }


                /// Check Filter
                var columns = _hrUnitOfWork.NotificationRepository.GetColumnList(model.TableName, model.ObjectName, model.Version, model.divType, CompanyId, User.Identity.GetCulture());
                if (!String.IsNullOrEmpty(NotifyCondition.filter))
                {
                    var message = ApplyFilter(NotifyCondition.filter, columns);
                    if (!String.IsNullOrEmpty(message))
                    {
                        ModelState.AddModelError("filter", message);
                        return(Json(Models.Utils.ParseFormErrors(ModelState)));
                    }
                }

                if (grid1 != null)
                {
                    SaveFilterGrid(grid1, NotifyCondition);
                }

                _hrUnitOfWork.NotificationRepository.Add(NotifyCondition);
            }
            else
            {
                var parm = new Models.AutoMapperParm
                {
                    Destination = NotifyCondition,
                    Source      = model,
                    ObjectName  = "Notify",
                    Options     = moreInfo,
                    Transtype   = TransType.Update
                };

                AutoMapper mapper = new Models.AutoMapper(parm, _hrUnitOfWork, User.Identity);
                mapper.Map();
                NotifyCondition.CompanyId = User.Identity.GetDefaultCompany();
                NotifyCondition.Message   = model.EncodedMsg.Replace("&nbsp;", " ").Replace("&lt;", "<").Replace("&gt;", ">");
                Events ev = (Events)Enum.ToObject(typeof(Events), model.Event);
                NotifyCondition.Event      = ev;
                NotifyCondition.EventValue = model.EventValue;
                //NotifyCondition.CreatedTime = DateTime.Now;
                //NotifyCondition.CreatedUser = UserName;
                //NotifyCondition.ColumnName = model.ColumnName;
                //NotifyCondition.TableName = model.TableName;
                //NotifyCondition.ObjectName = model.ObjectName;
                // NotifyCondition.Version = model.Version;
                //Events ev = (Events)Enum.ToObject(typeof(Events), model.Event);
                //NotifyCondition.Event = ev;
                List <string> ListOfUsers = new List <string>();
                if (model.Users != null)
                {
                    foreach (var item in model.Users)
                    {
                        //var users = db.Users.Where(a => a.UserName == item).Select(a => a.UserName).FirstOrDefault();
                        ListOfUsers.Add(item);
                    }
                }
                NotifyCondition.Users = string.Join(",", ListOfUsers.ToArray());
                if (model.Message != null)
                {
                    var ListOfFields = GetFields(model.Message).Distinct().ToList();
                    if (model.HasEmpId && !ListOfFields.Contains("EmpId") && reminderEvent.Contains(model.Event))
                    {
                        ListOfFields.Add("EmpId");
                    }
                    NotifyCondition.Fields = string.Join(",", ListOfFields.ToArray());
                }
                //mapper.AddNotification(NotifyCondition, model.DateTimeValue, model.SourceId);
                /// Check Filter
                var columns = _hrUnitOfWork.NotificationRepository.GetColumnList(model.TableName, model.ObjectName, model.Version, model.divType, CompanyId, User.Identity.GetCulture());
                if (!String.IsNullOrEmpty(NotifyCondition.filter))
                {
                    var message = ApplyFilter(NotifyCondition.filter, columns);
                    if (!String.IsNullOrEmpty(message))
                    {
                        ModelState.AddModelError("filter", message);
                        return(Json(Models.Utils.ParseFormErrors(ModelState)));
                    }
                }

                if (grid1 != null)
                {
                    SaveFilterGrid(grid1, NotifyCondition);
                }
                _hrUnitOfWork.NotificationRepository.Attach(NotifyCondition);
                _hrUnitOfWork.NotificationRepository.Entry(NotifyCondition).State = EntityState.Modified;
            }
            errors = SaveChanges(Language);

            if (errors.Count() > 0)
            {
                foreach (var item in errors)
                {
                    msg = item.errors.Select(a => a.message).FirstOrDefault();
                }
                return(Json(msg));
            }

            return(Json(msg));
        }