/// <summary> /// Populates the drop down lists in CreateEdit form, which take data from database /// </summary> /// <param name="model"></param> private void PopulateDropDownLists(WorkItemDataInputModel model) { model.Categories = this.dataContext.Categories.Where(o => o.CreatedByUserId == this.CurrentUser.UserId).ToList(); }
/// <summary> /// Creates recurring items from a given reccurence pattern and time period /// </summary> /// <param name="workItem">The work item</param> /// <param name="model">The model</param> private void CreateOccurrences(WorkItem workItem, WorkItemDataInputModel model) { var occurrenceService = new OccurrenceService(); var occurrenceDates = occurrenceService.GetOccurrenceDates(workItem); workItem.RecurringItems = new List<RecurringItem>(); foreach (var date in occurrenceDates) { var originalDate = new DateTime(date.Year, date.Month, date.Day, model.RecurringItemStart.Value.Hour, model.RecurringItemStart.Value.Minute, model.RecurringItemStart.Value.Second); if (!this.dataContext.RecurringItems.Any(r => r.OriginalDate == originalDate)) { workItem.RecurringItems.Add(new RecurringItem { Start = new DateTime(date.Year, date.Month, date.Day, model.RecurringItemStart.Value.Hour, model.RecurringItemStart.Value.Minute, model.RecurringItemStart.Value.Second), End = new DateTime(date.Year, date.Month, date.Day, model.RecurringItemEnd.Value.Hour, model.RecurringItemEnd.Value.Minute, model.RecurringItemEnd.Value.Second), OriginalDate = originalDate, Exception = false, Subject = workItem.Subject, Body = workItem.Body, Location = workItem.Location, UpdatedAt = DateTime.Now }); } } }
/// <summary> /// Handle logic errors in create and edit input forms /// </summary> /// <param name="model">The model</param> /// <returns>Bool value indicating whether model contains logic errors or not</returns> private bool HandleLogicErrors(WorkItemDataInputModel model) { var hasErrors = false; if (model.WorkItemType == WorkItemType.None) { ModelState.AddModelError("", "Uzdevuma tips ir obligāts lauks"); hasErrors = true; } if ((model.Reminder == Reminder.Driving || model.Reminder == Reminder.Walking) && model.Location == null) { ModelState.AddModelError("", "Lai izvēlētos šo atgādinājumu, ir jānorāda uzdevuma atrašanās vietas adrese"); hasErrors = true; } if (model.WorkItemType == WorkItemType.Appointment && (model.StartDate > model.EndDate)) { ModelState.AddModelError("", "Uzdevuma beigu laikam jābūt vienādam vai lielākam par sākuma laiku"); hasErrors = true; } return hasErrors; }
private void AddCategories(WorkItemDataInputModel model, WorkItem workItem) { foreach (var categoryId in model.SelectedCategoryIds) { var category = this.dataContext.Categories.Find(categoryId); workItem.Categories.Add(category); } }
public ActionResult Edit(WorkItemDataInputModel model) { PopulateDropDownLists(model); if (ModelState.IsValid) { var hasErrors = HandleLogicErrors(model); if (hasErrors == true) return View(model); WorkItem workItem = this.dataContext.WorkItems.Where(o => o.Id == model.WorkItemId).FirstOrDefault(); if (workItem != null) { model.UpdateWorkItem(workItem); //update categories var categories = workItem.Categories.ToList(); categories.ForEach(category => workItem.Categories.Remove(category)); AddCategories(model, workItem); if (workItem.isRecurring == true) { var newPattern = model.TransformToRecurrencePattern(); if (workItem.RecurrencePattern == null) { workItem.RecurrencePattern = newPattern; workItem.RecurrenceType = model.RecurrenceType; CreateOccurrences(workItem, model); } else { //Check if recurrence pattern has not changed var existingPattern = workItem.RecurrencePattern; int mismatch = 0; if (existingPattern.Interval != newPattern.Interval) mismatch = 1; if ((int)existingPattern.DayOfWeekMask != (int)newPattern.DayOfWeekMask) mismatch = 1; if ((int)existingPattern.Instance != (int)newPattern.Instance) mismatch = 1; if (existingPattern.DayOfMonth != newPattern.DayOfMonth) mismatch = 1; if ((int)existingPattern.MonthOfYear != (int)newPattern.MonthOfYear) mismatch = 1; if (mismatch == 1) { //if the pattern has changed delete all of the old recurring items, save the new pattern and asociate it with the work item RemoveRecurringItems(workItem); this.dataContext.WIRecurrencePatterns.Remove(existingPattern); workItem.RecurrencePattern = newPattern; workItem.RecurrenceType = model.RecurrenceType; CreateOccurrences(workItem, model); } else { //if pattern hasn`t changed maybe the time span of the pattern has changed, if so, update the datetime values and remove unnecessary recurring items (or add them) if (model.StartDate != workItem.StartDateTime || model.EndDate != workItem.EndDateTime) { foreach (var recurringItem in workItem.RecurringItems .Where(o => o.Start < workItem.StartDateTime || o.End > workItem.EndDateTime) .ToList()) { this.dataContext.RecurringItems.Remove(recurringItem); } workItem.StartDateTime = model.StartDate; workItem.StartDateTime = model.EndDate; CreateOccurrences(workItem, model); } } } } else { if (workItem.RecurrencePattern != null) { var existingPattern = workItem.RecurrencePattern; RemoveRecurringItems(workItem); this.dataContext.WIRecurrencePatterns.Remove(existingPattern); workItem.RecurrenceType = null; } } var message = ""; if (workItem.Reminder != Reminder.None && workItem.isRecurring == false) { try { this.scheduler.SetWorkItemReminderData(this.scheduler, workItem); var result = this.scheduler.ScheduleReminder(); message = this.scheduler.HandleReminderServiceResult(result); } catch (Exception ex) { message = "Atgādinājuma ieplānošana beigusies ar kļūdu! " + ex.Message; } } else if (workItem.Reminder != Reminder.None && workItem.isRecurring == true) { foreach (var recurringItem in workItem.RecurringItems) { try { this.scheduler.SetRecurringItemReminderData(this.scheduler, workItem, recurringItem); var result = this.scheduler.ScheduleReminder(); message = this.scheduler.HandleReminderServiceResult(result); } catch (Exception ex) { message = "Atgādinājuma ieplānošana beigusies ar kļūdu! " + ex.Message; } } } if (message.Length != 0) { ModelState.AddModelError("", "Kļūda: " + message); return View("CreateEdit", model); } workItem.UpdatedAt = DateTime.Now; workItem.UpdatedByUserId = this.CurrentUser.UserId; this.dataContext.SaveChanges(); TempData["Message"] = "Uzdevums " + workItem.Subject + " veiksmīgi atjaunots"; TempData["Alert-Level"] = "alert-success"; return RedirectToAction("Index"); } ViewBag.PostBackMethod = "Edit"; ModelState.AddModelError("", "Uzdevums netika atrasts!"); return View("CreateEdit", model); } ViewBag.PostBackMethod = "Edit"; ModelState.AddModelError("", "Lūdzu, izlabojiet kļūdas un atkārtoti nospiediet Saglabāt"); return View("CreateEdit", model); }
/// <summary> /// Displays the work item edit form /// </summary> /// <param name="id">The work item id</param> /// <returns>The edit view</returns> public ActionResult Edit(int id = 0) { WorkItem workItem = this.dataContext.WorkItems.Find(id); if (workItem == null || workItem.CreatedByUserId != this.CurrentUser.UserId) { return HttpNotFound(); } var model = new WorkItemDataInputModel(workItem); PopulateDropDownLists(model); foreach (var category in workItem.Categories) { model.SelectedCategoryIds.Add((int)category.Id); } ViewBag.Title = "Rediģēt"; ViewBag.Pagetitle = "Rediģēt uzdevumu " + workItem.Subject; ViewBag.PostBackMethod = "Edit"; return View("CreateEdit", model); }
public ActionResult Create(WorkItemDataInputModel model) { PopulateDropDownLists(model); if (ModelState.IsValid) { var hasErrors = HandleLogicErrors(model); if (hasErrors == true) return View(model); var workItem = model.TransformToWorkItem(); workItem.CreatedByUserId = this.CurrentUser.UserId; workItem.UpdatedByUserId = this.CurrentUser.UserId; workItem.Categories = new List<Navigate.Models.Classifiers.Category>(); AddCategories(model, workItem); if (workItem.isRecurring == true) { workItem.RecurrencePattern = model.TransformToRecurrencePattern(); workItem.RecurrenceType = model.RecurrenceType; CreateOccurrences(workItem, model); } this.dataContext.WorkItems.Add(workItem); var message = ""; if (workItem.Reminder != Reminder.None && workItem.isRecurring == false) { try { this.scheduler.SetWorkItemReminderData(this.scheduler, workItem); var result = this.scheduler.ScheduleReminder(); message = this.scheduler.HandleReminderServiceResult(result); } catch (Exception ex) { message = "Atgādinājuma ieplānošana beigusies ar kļūdu! " + ex.Message; } } else if (workItem.Reminder != Reminder.None && workItem.isRecurring == true) { foreach (var recurringItem in workItem.RecurringItems) { try { this.scheduler.SetRecurringItemReminderData(this.scheduler, workItem, recurringItem); var result = this.scheduler.ScheduleReminder(); message = this.scheduler.HandleReminderServiceResult(result); } catch (Exception ex) { message = "Atgādinājuma ieplānošana beigusies ar kļūdu! " + ex.Message; } } } if (message.Length != 0) { ModelState.AddModelError("", "Kļūda: " + message); return View("CreateEdit", model); } this.dataContext.SaveChanges(); TempData["Message"] = "Uzdevums " + workItem.Subject + " veiksmīgi izveidots"; TempData["Alert-Level"] = "alert-success"; return RedirectToAction("Index"); } ModelState.AddModelError("", "Lūdzu, izlabojiet kļūdas un atkārtoti nospiediet Saglabāt"); return View("CreateEdit", model); }
public ActionResult Create(string start = null, string end = null) { var model = new WorkItemDataInputModel(); if (!String.IsNullOrEmpty(start) && !String.IsNullOrEmpty(end)) { DateTime dtStart = DateTime.ParseExact(start, "dd-MM-yyyy HH:mm", CultureInfo.InvariantCulture); DateTime dtEnd = DateTime.ParseExact(end, "dd-MM-yyyy HH:mm", CultureInfo.InvariantCulture); model.StartDate = dtStart; model.EndDate = dtEnd; model.WorkItemType = WorkItemType.Appointment; } PopulateDropDownLists(model); model.Origin = this.CurrentUser.BaseLocation; ViewBag.Title = "Izveidot"; ViewBag.Pagetitle = "Izveidot uzdevumu"; ViewBag.PostBackMethod = "Create"; return View("CreateEdit", model); }