private void btnTestDeserialize_Click(object sender, EventArgs e) { var account = new Account(); using (var fs = new FileStream(string.Format(FolderPath, typeof(Account)).Replace("ToDo.", ""), FileMode.Open, FileAccess.Read)) { FileIO.LoadFromFile(fs, ref account); fs.Close(); } var project = new Project(); using (var fs = new FileStream(string.Format(FolderPath, typeof(Project)).Replace("ToDo.", ""), FileMode.Open, FileAccess.Read)) { FileIO.LoadFromFile(fs, ref project); fs.Close(); } var task = new Task(); using (var fs = new FileStream(string.Format(FolderPath, typeof(Task)).Replace("ToDo.", ""), FileMode.Open, FileAccess.Read)) { FileIO.LoadFromFile(fs, ref task); fs.Close(); } var reminder = new Reminder(); using (var fs = new FileStream(string.Format(FolderPath, typeof(Reminder)).Replace("ToDo.", ""), FileMode.Open, FileAccess.Read)) { FileIO.LoadFromFile(fs, ref reminder); fs.Close(); } var location = new Location(); using (var fs = new FileStream(string.Format(FolderPath, typeof(Location)).Replace("ToDo.", ""), FileMode.Open, FileAccess.Read)) { FileIO.LoadFromFile(fs, ref location); fs.Close(); } }
/// <summary> /// Comparer which compares two tasks by their task name, and returns an /// integer representing their compare position. /// </summary> /// <param name="a">First task to compare.</param> /// <param name="b">Second task to compare.</param> /// <returns>-1 if x is less than y, 1 if x is more than y, 0 if they are equal</returns> public static int CompareByDateTime(Task a, Task b) { // A [DONE] task always sorts after an undone task. if (a.DoneState == true && b.DoneState == false) { return 1; } else if (b.DoneState == true && a.DoneState == false) { return -1; } // If they have the same state, continue sort by DateTime. if (a is TaskFloating) { if (b is TaskFloating) { return a.TaskName.CompareTo(b.TaskName); } else { return 1; } } else if (b is TaskFloating) { return -1; } DateTime aDT, bDT; if (a is TaskEvent) { aDT = ((TaskEvent)a).StartDateTime; } else { aDT = ((TaskDeadline)a).EndDateTime; } if (b is TaskEvent) { bDT = ((TaskEvent)b).StartDateTime; } else { bDT = ((TaskDeadline)b).EndDateTime; } return DateTime.Compare(aDT, bDT); }
private void btnTestSerialize_Click(object sender, EventArgs e) { var account = new Account { Inbox = new List<Task> { new Task { Title = "Inbox Task 1" } }, Projects = new List<Project>(), SmartLists = null }; account.Projects.Add(new Project { Name = "Project 1", Tasks = new List<Task> { new Task { Title = "Project Task 1", StartDate = DateTime.Today, Reminders = new List<Reminder> { new Reminder { DateTime = DateTime.Today + TimeSpan.FromHours(1) } }, Tags = new List<Tag>() { new Tag() { Name = "Test" } } } } }); using (var fs = new FileStream(string.Format(FolderPath, typeof(Account)).Replace("ToDo.", ""), FileMode.Create, FileAccess.Write)) { FileIO.SaveToFile(fs, account); fs.Close(); } var project = new Project(); using (var fs = new FileStream(string.Format(FolderPath, typeof(Project)).Replace("ToDo.", ""), FileMode.Create, FileAccess.Write)) { FileIO.SaveToFile(fs, project); fs.Close(); } var task = new Task(); using (var fs = new FileStream(string.Format(FolderPath, typeof(Task)).Replace("ToDo.", ""), FileMode.Create, FileAccess.Write)) { FileIO.SaveToFile(fs, task); fs.Close(); } var reminder = new Reminder(); using (var fs = new FileStream(string.Format(FolderPath, typeof(Reminder)).Replace("ToDo.", ""), FileMode.Create, FileAccess.Write)) { FileIO.SaveToFile(fs, reminder); fs.Close(); } var location = new Location(); using (var fs = new FileStream(string.Format(FolderPath, typeof(Location)).Replace("ToDo.", ""), FileMode.Create, FileAccess.Write)) { FileIO.SaveToFile(fs, location); fs.Close(); } }
/// <summary> /// Deletes a task from the task file. /// </summary> /// <param name="taskToDelete">The task to delete.</param> /// <returns>True if operation was successful; False if not.</returns> internal bool RemoveTaskFromFile(Task taskToDelete) { XDocument doc = XDocument.Load(taskStorageFile); var taskNode = from node in doc.Descendants("Task") let attr = node.Attribute("id") where attr != null && attr.Value == taskToDelete.ID.ToString() select node; if (taskNode == null) return false; try { taskNode.ToList().ForEach(x => x.Remove()); } catch (Exception e) { Logger.Error(e, "RemoveTaskFromFile::Storage"); return false; } doc.Save(taskStorageFile); return true; }
/// <summary> /// Marks a task in the task file. /// </summary> /// <param name="taskToMarkAsDone">The task to mark.</param> /// <param name="done">Task will be marked as done if true, undone if false.</param> /// <returns>True if operation was successful; False if not.</returns> internal bool MarkTaskAs(Task taskToMarkAsDone, bool done) { XDocument doc = XDocument.Load(taskStorageFile); var task = from node in doc.Descendants("Task") let attr = node.Attribute("id") where attr != null && attr.Value == taskToMarkAsDone.ID.ToString() select node; if (task == null) return false; try { if(done) task.First().Element("Done").ReplaceNodes("True"); else task.First().Element("Done").ReplaceNodes("False"); } catch (Exception e) { Logger.Error(e, "MarkTaskAs::Storage"); return false; } doc.Save(taskStorageFile); return true; }
/// <summary> /// Postpones a task by the specified duration. /// </summary> /// <param name="taskToPostpone">The task to postpone.</param> /// <param name="postponeDuration">A timespan representing the duration to postpone the task.</param> /// <returns></returns> private Response PostponeTask(Task taskToPostpone, TimeSpan postponeDuration) { if (taskToPostpone.Postpone(postponeDuration) == false) return new Response(Result.INVALID_TASK, sortType, this.GetType(), currentListedTasks); else executedTasks.Enqueue(taskToPostpone); if (storageIO.UpdateTask(taskToPostpone)) return GenerateStandardSuccessResponse(taskToPostpone); else return GenerateXMLFailureResponse(); }
/// <summary> /// Modifies a task in the list specified by SetMembers by replacing /// it with the a new given new task. /// Does not preserve the hash and actual object of the original task. /// </summary> /// <param name="taskToModify">The old task to be modified.</param> /// <param name="newTask">The new task to replace the old task.</param> /// <returns></returns> private Response ModifyTask(Task taskToModify, Task newTask) { Response response = null; response = DeleteTask(taskToModify); if (response.IsSuccessful()) response = AddTask(newTask); if (response.IsSuccessful()) return new Response(Result.SUCCESS, sortType, typeof(OperationModify), currentListedTasks); else return response; }
/// <summary> /// Marks a task's [done] state depending on the given flag. Marks as done if true, undone if false. /// </summary> /// <param name="taskToMark">Task to mark.</param> /// <param name="doneState">Flag indiciating whether to mark as done or undone.</param> /// <returns></returns> protected Response MarkTaskAs(Task taskToMark, bool doneState) { SetMembers(taskList, storageIO); if (taskToMark.DoneState == doneState) return new Response(Result.INVALID_TASK, sortType, this.GetType()); else taskToMark.DoneState = doneState; executedTasks.Enqueue(taskToMark); if (storageIO.MarkTaskAs(taskToMark, doneState)) return GenerateStandardSuccessResponse(taskToMark); else return GenerateXMLFailureResponse(); }
/// <summary> /// Accepts an array of objects and returns the same array of objects with /// a Task inserted in the first (zeroth-index) position. /// </summary> /// <param name="args">The given array of objects.</param> /// <param name="task">The task to insert into the first position.</param> /// <returns>The output array with the Task inserted.</returns> private static object[] AddTaskToParameters(object[] args, Task task) { if (args == null) return new object[1] { task }; var parameterList = args.ToList(); parameterList.Insert(0, task); var parameters = parameterList.ToArray(); return parameters; }
/// <summary> /// Removes a task from the system. The deleted task is removed from file /// using the Storage controller specified with SetMembers. /// </summary> /// <param name="taskToDelete"></param> /// <returns></returns> protected Response DeleteTask(Task taskToDelete) { try { taskList.Remove(taskToDelete); AddToOperationHistory(taskToDelete); if (currentListedTasks.Contains(taskToDelete)) currentListedTasks.Remove(taskToDelete); if (storageIO.RemoveTaskFromFile(taskToDelete)) { Logger.Info("Deleted a task successfully.", "DeleteTask::Operation"); return GenerateStandardSuccessResponse(taskToDelete); } else return GenerateXMLFailureResponse(); } catch (Exception e) { Logger.Error(e, "DeleteTask::Operation"); return new Response(Result.FAILURE, sortType, typeof(OperationAdd), currentListedTasks); } }
/// <summary> /// Generates and returns a standard success Response indicating that the specified task has been /// successfully executed on by this Operation. /// </summary> /// <param name="task">The task to generate the success response for.</param> /// <returns></returns> protected Response GenerateStandardSuccessResponse(Task task) { string[] args = new string[1]; args[0] = task.TaskName; return new Response(Result.SUCCESS, sortType, this.GetType(), currentListedTasks, args); }
/// <summary> /// Adds a task to the system. The newly added task is written to file /// using the Storage controller specified with SetMembers. /// </summary> /// <param name="taskToAdd">The task to add.</param> /// <returns>Response indicating the result of the operation.</returns> protected Response AddTask(Task taskToAdd) { try { if (TaskIsInvalid(taskToAdd)) { Logger.Warning("Attempted to add an invalid task.", "AddTask::Operation"); return new Response(Result.FAILURE, sortType, typeof(OperationAdd), currentListedTasks); } taskList.Add(taskToAdd); AddToOperationHistory(taskToAdd); bool success = storageIO.AddTaskToFile(taskToAdd); if (success) { currentListedTasks.Add(taskToAdd); Logger.Info("Added a new task successfully.", "AddTask::Operation"); return GenerateStandardSuccessResponse(taskToAdd); } else return GenerateXMLFailureResponse(); } catch (Exception e) { Logger.Error(e, "AddTask::Operation"); return new Response(Result.FAILURE, sortType, typeof(OperationAdd), currentListedTasks); } }
/// <summary> /// Derived constructor to create an Add Operation. /// </summary> /// <param name="addTask">The task to add when this Operation is executed.</param> /// <param name="sortType">The type of sorting to use on the displayed task after executing this Operation.</param> /// <returns></returns> public OperationAdd(Task addTask, SortType sortType) : base(sortType) { newTask = addTask; }
/// <summary> /// This method creates an event task and schedules it at the time slot. /// </summary> /// <param name="taskName">Name of the task</param> /// <param name="startTime">Start time of the time slot</param> /// <param name="endTime">End time of the time slot</param> /// <returns>The appropriate response object, depending on whether he task could be scheduled at the time slot</returns> private Response ScheduleTaskAtSlot(string taskName, DateTime startTime, DateTime endTime) { Response response; scheduledTask = new TaskEvent(taskName, startTime, endTime, searchSpecificity); response = AddTask(scheduledTask); if (response.IsSuccessful()) { AddToOperationHistory(); } return response; }
/// <summary> /// Updates a task in the task file. /// </summary> /// <param name="taskToUpdate">The task to update.</param> /// <returns>True if operation was successful; False if not.</returns> internal bool UpdateTask(Task taskToUpdate) { XDocument doc = XDocument.Load(taskStorageFile); var task = from node in doc.Descendants("Task") let attr = node.Attribute("id") where attr != null && attr.Value == taskToUpdate.ID.ToString() select node; if (task == null) return false; try { XElement taskNode = task.First(); taskNode.ReplaceWith(taskToUpdate.ToXElement()); } catch (Exception e) { Logger.Error(e, "UpdateTask::Storage"); return false; } doc.Save(taskStorageFile); return true; }
/// <summary> /// This method adds the specified task to the history executed tasks for this operation. /// </summary> /// <param name="task">The task to add to history.</param> /// <returns></returns> private void AddToOperationHistory(Task task) { executedTasks.Enqueue(task); }
/// <summary> /// Appends a task to the task file. /// </summary> /// <param name="taskToAdd">The task to append.</param> /// <returns>True if operation was successful; False if not.</returns> internal bool AddTaskToFile(Task taskToAdd) { try { XDocument doc = XDocument.Load(taskStorageFile); XElement newTaskElem = taskToAdd.ToXElement(); doc.Root.Add(newTaskElem); doc.Save(taskStorageFile); } catch (Exception e) { Logger.Error(e, "AddTaskToFile::Storage"); AlertBox.Show("A problem was encoutered saving the new task to file."); return false; } return true; }
/// <summary> /// This method checks for the validity of the specified task. /// </summary> /// <param name="taskToCheck">The Task to check validity of.</param> /// <returns>True if invalid. False if valid.</returns> private bool TaskIsInvalid(Task taskToCheck) { if (taskToCheck == null) return true; if (!IsValidString(taskToCheck.TaskName)) return true; return false; }
/// <summary> /// Executes the operation and adds it to the operation history. /// Modifies the task indicated by the index range to the new /// parameters in this operation. If a parameter is left empty or null, /// that parameter will remain unchanged in the new task. /// </summary> /// <param name="taskList">List of task this operation will operate on.</param> /// <param name="storageIO">Storage controller that will be used to store neccessary data.</param> /// <returns>Response indicating the result of the operation execution.</returns> public override Response Execute(List<Task> taskList, Storage storageIO) { SetMembers(taskList, storageIO); Response response = null; // No index, do a search instead if (!hasIndex && !isAll) { SetMembers(taskList, storageIO); List<Task> searchResults = SearchForTasks(taskName, false, startTime, endTime, searchType); response = DisplaySearchResults(searchResults, taskName, startTime, endTime, searchType); } else { response = CheckIfIndexesAreValid(startIndex, endIndex); if (response != null) return response; if (MultipleTasksSelected()) return new Response(Result.INVALID_TASK, sortType, this.GetType()); oldTask = currentListedTasks[startIndex]; // copy over taskName from indexed task if didn't specify a name if (!IsValidString(taskName)) taskName = oldTask.TaskName; // copy over date/times from indexed task if didn't specify time else if (startTime == null && endTime == null) oldTask.CopyDateTimes(ref startTime, ref endTime, ref isSpecific); newTask = Task.CreateNewTask(taskName, startTime, endTime, isSpecific); response = ModifyTask(oldTask, newTask); } if (response.IsSuccessful()) { AddToOperationHistory(); } return response; }
/// <summary> /// Comparer which compares two tasks by their task name, and returns an /// integer representing their compare position. /// </summary> /// <param name="x">First task to compare.</param> /// <param name="y">Second task to compare.</param> /// <returns>-1 if x is less than y, 1 if x is more than y, 0 if they are equal</returns> public static int CompareByName(Task x, Task y) { int compare = x.TaskName.CompareTo(y.TaskName); if (compare == 0) { return CompareByDateTime(x, y); } return compare; }