예제 #1
0
        public ActionResult Update(CheckInUpdateModel model)
        {
            try
            {
                using (var dataContext = new HuntingEntities())
                {
                    int languageId = (int)Session[LocalizationAttribute.SESSION_LANGUAGE_ID];
                    var language   = LanguageContext.GetLanguage(languageId);
                    var user       = AclUserContext.GetDetail(dataContext, User.Identity.Name);
                    if (user == null)
                    {
                        ContextUtils.CreateActionStateCookie(Response, ActionTypeEnum.Warning, GlobalRes.ERROR_NOT_ALLOWED);
                        return(RedirectToAction("Index", "Home"));
                    }
                    var checkIn = CheckInContext.GetDetail(dataContext, model.Id);
                    if (checkIn == null)
                    {
                        ContextUtils.CreateActionStateCookie(Response, ActionTypeEnum.Warning, GlobalRes.ERROR_NOT_FOUND);
                        return(RedirectToAction("Index", "Home"));
                    }
                    if (user.CanUpdateTerritory(checkIn.MapItem.Territory) == false)
                    {
                        ContextUtils.CreateActionStateCookie(Response, ActionTypeEnum.Warning, GlobalRes.ERROR_NOT_ALLOWED);
                        return(RedirectToAction("Index", "CheckIn", new { id = checkIn.MapItemId }));
                    }

                    CheckInContext.Validate(dataContext, model, ModelState);
                    if (ModelState.IsValid)
                    {
                        var newItemId = CheckInContext.Update(dataContext, checkIn, model, user, language);
                        if (newItemId.HasValue)
                        {
                            ContextUtils.CreateActionStateCookie(Response, ActionTypeEnum.Success, CheckInRes.SUCCESS_UPDATE);
                            return(RedirectToAction("Index", "CheckIn", new { id = model.MapItemId }));
                        }
                        else
                        {
                            ContextUtils.CreateActionStateCookie(Response, ActionTypeEnum.Warning, CheckInRes.ERROR_UPDATE);
                        }
                    }
                    model.FillCodeLists(dataContext, checkIn.MapItem);
                    return(View(model));
                }
            }
            catch (Exception exception)
            {
                logger.Error(exception, "CheckInController");
                ContextUtils.CreateActionStateCookie(Response, ActionTypeEnum.Danger, GlobalRes.ERROR_EXCEPTION);
                return(RedirectToAction("Index", "Home"));
            }
        }
예제 #2
0
 public IHttpActionResult CheckIn(CreateCheckInModel model)
 {
     try
     {
         using (var dataContext = new HuntingEntities())
         {
             var session = GetSession();
             if (session == null)
             {
                 return(Content(HttpStatusCode.Unauthorized, SESSION_INVALID_MESSAGE));
             }
             var userSession = AclUserContext.GetUserSession(dataContext, session);
             if (userSession == null)
             {
                 return(Content(HttpStatusCode.Unauthorized, SESSION_INVALID_MESSAGE));
             }
             var mapItem = MapItemContext.GetDetail(dataContext, model.MapItemId);
             if (mapItem == null)
             {
                 return(Content(HttpStatusCode.NotFound, NOT_FOUND_MESSAGE));
             }
             if (userSession.AclUser.CanViewTerritory(mapItem.Territory) == false)
             {
                 return(Content(HttpStatusCode.Forbidden, FORBIDDEN_MESSAGE));
             }
             Questionnaire questionnaire = null;
             if (model.QuestionnaireId.HasValue)
             {
                 questionnaire = QuestionnaireContext.GetDetail(dataContext, model.QuestionnaireId.Value);
             }
             if (model.IsValid(dataContext, questionnaire) == false)
             {
                 return(Content(HttpStatusCode.BadRequest, BAD_REQUEST_MESSAGE));
             }
             var updateModel = new CheckInUpdateModel(model, questionnaire);
             var pointId     = CheckInContext.Update(dataContext, null, updateModel, userSession.AclUser, null);
             if (pointId == null)
             {
                 return(Content(HttpStatusCode.InternalServerError, FAILED_MESSAGE));
             }
             return(Ok(pointId.Value.ToString()));
         }
     }
     catch (Exception exception)
     {
         logger.Error(exception, "MobileController");
         return(InternalServerError());
     }
 }
예제 #3
0
        public static void Validate(HuntingEntities dataContext, CheckInUpdateModel model, ModelStateDictionary modelState)
        {
            if (model.QuestionnaireId.HasValue && model.QuestionList != null)
            {
                var questionDict = dataContext.Questions.Where(item => item.QuestionnaireId == model.QuestionnaireId.Value).ToDictionary(item => item.Id, item => item);
                for (int index = 0; index < model.QuestionList.Count; index++)
                {
                    var questionModel = model.QuestionList[index];
                    if (questionDict.ContainsKey(questionModel.Id))
                    {
                        var question = questionDict[questionModel.Id];
                        if (question.IsRequired)
                        {
                            switch (question.QuestionTypeEx)
                            {
                            case QuestionTypeEnum.DropDown:
                                if (questionModel.OptionId == null)
                                {
                                    modelState.AddModelError(string.Format("QuestionList[{0}].OptionId", index), ValidationRes.VALIDATION_REQUIRED);
                                }
                                break;

                            case QuestionTypeEnum.Number:
                            case QuestionTypeEnum.TextArea:
                            case QuestionTypeEnum.TextBox:
                                if (string.IsNullOrWhiteSpace(questionModel.StringValue))
                                {
                                    modelState.AddModelError(string.Format("QuestionList[{0}].StringValue", index), ValidationRes.VALIDATION_REQUIRED);
                                }
                                break;
                            }
                        }
                        if (question.QuestionTypeEx == QuestionTypeEnum.Number)
                        {
                            if (string.IsNullOrWhiteSpace(questionModel.StringValue) == false)
                            {
                                var doubleValue = ContextUtils.ParseFloatString(questionModel.StringValue);
                                if (doubleValue == null)
                                {
                                    modelState.AddModelError(string.Format("QuestionList[{0}].StringValue", index), ValidationRes.VALIDATION_DOUBLE);
                                }
                            }
                        }
                    }
                }
            }
        }
예제 #4
0
 // GET: CheckIn/Create
 public ActionResult Create(int mapItemId)
 {
     try
     {
         using (var dataContext = new HuntingEntities())
         {
             int languageId = (int)Session[LocalizationAttribute.SESSION_LANGUAGE_ID];
             var language   = LanguageContext.GetLanguage(languageId);
             var user       = AclUserContext.GetDetail(dataContext, User.Identity.Name);
             if (user == null)
             {
                 ContextUtils.CreateActionStateCookie(Response, ActionTypeEnum.Warning, GlobalRes.ERROR_NOT_ALLOWED);
                 return(RedirectToAction("Index", "Home"));
             }
             var mapItem = MapItemContext.GetDetail(dataContext, mapItemId);
             if (mapItem == null)
             {
                 ContextUtils.CreateActionStateCookie(Response, ActionTypeEnum.Warning, GlobalRes.ERROR_NOT_FOUND);
                 return(RedirectToAction("Index", "Home"));
             }
             if (user.CanViewTerritory(mapItem.Territory) == false)
             {
                 ContextUtils.CreateActionStateCookie(Response, ActionTypeEnum.Warning, GlobalRes.ERROR_NOT_ALLOWED);
                 return(RedirectToAction("Index", "Home"));
             }
             var model = new CheckInUpdateModel()
             {
                 IsCreate  = true,
                 MapItemId = mapItemId,
                 CheckTime = ContextUtils.FormatDateTime(DateTime.Now, language, true),
             };
             model.FillCodeLists(dataContext, mapItem);
             return(View(model));
         }
     }
     catch (Exception exception)
     {
         logger.Error(exception, "CheckInController");
         ContextUtils.CreateActionStateCookie(Response, ActionTypeEnum.Danger, GlobalRes.ERROR_EXCEPTION);
         return(RedirectToAction("Index", "Home"));
     }
 }
예제 #5
0
        public static int?Update(HuntingEntities dataContext, CheckIn updateItem, CheckInUpdateModel model, AclUser user, Language language)
        {
            try
            {
                if (model.IsCreate)
                {
                    updateItem = new CheckIn()
                    {
                        SysCreated = DateTime.Now,
                        SysCreator = user.Id,
                        MapItemId  = model.MapItemId,
                    };
                    dataContext.CheckIns.Add(updateItem);
                }
                if (model.CheckDateTime == null)
                {
                    model.CheckDateTime = ContextUtils.ParseDateTimeString(model.CheckTime, language).Value;
                }

                updateItem.CheckTime       = model.CheckDateTime.Value;
                updateItem.QuestionnaireId = model.QuestionnaireId;
                updateItem.Note            = model.Note;

                updateItem.SysEditor  = user.Id;
                updateItem.SysUpdated = DateTime.Now;

                dataContext.Answers.RemoveRange(updateItem.Answers);
                if (model.QuestionnaireId.HasValue)
                {
                    ProcessQuestionnaireAnswers(dataContext, updateItem, model);
                }

                dataContext.SaveChanges();
                return(updateItem.Id);
            }
            catch (Exception exception)
            {
                logger.Error(exception, "Update");
                return(null);
            }
        }
예제 #6
0
 public ActionResult SelectQuestionnaireAjax(CheckInUpdateModel model)
 {
     try
     {
         using (var dataContext = new HuntingEntities())
         {
             int languageId = (int)Session[LocalizationAttribute.SESSION_LANGUAGE_ID];
             var language   = LanguageContext.GetLanguage(languageId);
             var user       = AclUserContext.GetDetail(dataContext, User.Identity.Name);
             if (user == null)
             {
                 return(PartialView(@"AjaxError", GlobalRes.ERROR_NOT_ALLOWED));
             }
             ModelState.Clear();
             if (model.QuestionnaireId.HasValue)
             {
                 var questionnaire = QuestionnaireContext.GetDetail(dataContext, model.QuestionnaireId.Value);
                 if (questionnaire == null)
                 {
                     return(PartialView(@"AjaxError", GlobalRes.ERROR_NOT_FOUND));
                 }
                 if (user.CanViewTerritory(questionnaire.MapItemType.Territory) == false)
                 {
                     return(PartialView(@"AjaxError", GlobalRes.ERROR_NOT_ALLOWED));
                 }
                 model.FillQuestionnaire(null, questionnaire, language);
                 model.FillCodeLists(dataContext, null);
             }
             return(PartialView(model));
         }
     }
     catch (Exception exception)
     {
         logger.Error(exception, "CheckInController");
         return(PartialView(@"AjaxError", GlobalRes.ERROR_EXCEPTION));
     }
 }
예제 #7
0
        private static void ProcessQuestionnaireAnswers(HuntingEntities dataContext, CheckIn updateItem, CheckInUpdateModel model)
        {
            var questionDict = dataContext.Questions.
                               Where(item => item.QuestionnaireId == model.QuestionnaireId && item.IsDeleted == false).
                               ToDictionary(item => item.Id, item => item);

            foreach (var questionModel in model.QuestionList)
            {
                var question = questionDict[questionModel.Id];
                if (question != null)
                {
                    if (question.QuestionTypeEx == QuestionTypeEnum.Checkbox)
                    {
                        if (questionModel.BoolValue == true)
                        {
                            var answer = new Answer()
                            {
                                BoolValue  = questionModel.BoolValue,
                                QuestionId = question.Id,
                            };
                            updateItem.Answers.Add(answer);
                        }
                    }
                    if (question.QuestionTypeEx == QuestionTypeEnum.CheckboxList)
                    {
                        if (questionModel.CheckBoxList != null)
                        {
                            foreach (var checkBoxModel in questionModel.CheckBoxList)
                            {
                                if (checkBoxModel.BoolValue == true)
                                {
                                    var answer = new Answer()
                                    {
                                        OptionId   = checkBoxModel.Id,
                                        QuestionId = question.Id,
                                    };
                                    updateItem.Answers.Add(answer);
                                }
                            }
                        }
                    }
                    if (question.QuestionTypeEx == QuestionTypeEnum.DropDown)
                    {
                        if (questionModel.OptionId.HasValue)
                        {
                            var answer = new Answer()
                            {
                                OptionId   = questionModel.OptionId,
                                QuestionId = question.Id,
                            };
                            updateItem.Answers.Add(answer);
                        }
                    }
                    if (question.QuestionTypeEx == QuestionTypeEnum.Number)
                    {
                        if (string.IsNullOrWhiteSpace(questionModel.StringValue) == false)
                        {
                            var answer = new Answer()
                            {
                                FloatValue = ContextUtils.ParseFloatString(questionModel.StringValue),
                                QuestionId = question.Id,
                            };
                            updateItem.Answers.Add(answer);
                        }
                    }
                    if (question.QuestionTypeEx == QuestionTypeEnum.TextArea)
                    {
                        if (string.IsNullOrWhiteSpace(questionModel.StringValue) == false)
                        {
                            var answer = new Answer()
                            {
                                StringValue = questionModel.StringValue,
                                QuestionId  = question.Id,
                            };
                            updateItem.Answers.Add(answer);
                        }
                    }
                    if (question.QuestionTypeEx == QuestionTypeEnum.TextBox)
                    {
                        if (string.IsNullOrWhiteSpace(questionModel.StringValue) == false)
                        {
                            var answer = new Answer()
                            {
                                StringValue = questionModel.StringValue,
                                QuestionId  = question.Id,
                            };
                            updateItem.Answers.Add(answer);
                        }
                    }
                }
            }
        }