public void OperationAddTest() { testStorage = new Storage("OpUnittest.xml", "OpUnittestsettings.xml"); testTaskList = testStorage.LoadTasksFromFile(); OperationAdd Op = new OperationAdd(testTask, sortType); result = Op.Execute(testTaskList, testStorage); Assert.AreEqual("Added new task \"test\" successfully.", result.FeedbackString); return; }
public void OperationAddFailTest() { testStorage = new Storage("OpUnittest.xml", "OpUnittestsettings.xml"); testTaskList = testStorage.LoadTasksFromFile(); OperationAdd Op = new OperationAdd(null, sortType); result = Op.Execute(testTaskList, testStorage); Assert.AreEqual(result.FeedbackString, "Failed to add task!"); return; }
/// <summary> /// Executes this operation. Returns the currently displayed list back as a Response /// with the new sort type. /// </summary> /// <param name="taskList">The task list which derived operations may operate on.</param> /// <param name="storageIO">The storage controller to use to store task data.</param> /// <returns>Response with the currently displayed list and the new sort type.</returns> public override Response Execute(List<Task> taskList, Storage storageIO) { this.storageIO = storageIO; Response response; // sorting is done On-The-Fly in TaskListViewControl. if(sortType == SortType.DEFAULT) response = new Response(Result.FAILURE, sortType, this.GetType(), currentListedTasks); else response = new Response(Result.SUCCESS, sortType, this.GetType(), currentListedTasks); return response; }
public void OperationDeleteMultipleTest() { testStorage = new Storage("OpUnittest.xml", "OpUnittestsettings.xml"); testTaskList = testStorage.LoadTasksFromFile(); int[] index = new int[2] { 1, 2 }; OperationAdd Op = new OperationAdd(testTask, sortType); Op.Execute(testTaskList, testStorage); Op = new OperationAdd(testTaskNew, sortType); Op.Execute(testTaskList, testStorage); OperationDelete Op1 = new OperationDelete("", index, null, null, null, false, SearchType.NONE, sortType); result = Op1.Execute(testTaskList, testStorage); Assert.AreEqual("Deleted all indicated tasks successfully.", result.FeedbackString); return; }
/// <summary> /// Executes the operation and adds it to the operation history. /// This operation tries to schedule a task within the given parameters. /// </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) { Response response; SetMembers(taskList, storageIO); RetrieveParameters(); if (!IsTaskDurationWithinRange() || taskDurationAmount == 0) { response = new Response(Result.INVALID_TASK, sortType, typeof(OperationSchedule), currentListedTasks); } else { response = TryScheduleTask(); } return response; }
/// <summary> /// Executes the operation according to this operation's parameters. /// </summary> /// <param name="taskList">List of task this method 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) { Response response = null; SetMembers(taskList, storageIO); List<Task> searchResults = SearchForTasks(searchString, false, startTime, endTime, searchType); if (searchResults.Count == 0) response = new Response(Result.FAILURE, sortType, this.GetType()); else { currentListedTasks = new List<Task>(searchResults); string[] criteria; SetArgumentsForSearchFeedbackString(out criteria, searchString, startTime, endTime, searchType); response = new Response(Result.SUCCESS, sortType, this.GetType(), currentListedTasks, criteria); } return response; }
/// <summary> /// Executes the operation and adds it to the global operation history. /// </summary> /// <param name="taskList">List of task this method 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); Operation redoOp = GetLastRevertedOperation(); if (redoOp == null) return new Response(Result.FAILURE, sortType, this.GetType()); Response result = redoOp.Redo(taskList, storageIO); if (result == null) return result; if (result.IsSuccessful()) { undoStack.Push(redoOp); result = new Response(Result.SUCCESS, sortType, typeof(OperationRedo), currentListedTasks); } else result = new Response(Result.FAILURE, sortType, typeof(OperationRedo), currentListedTasks); return result; }
public void OperationDeleteRangeFailTest() { testStorage = new Storage("OpUnittest.xml", "OpUnittestsettings.xml"); testTaskList = testStorage.LoadTasksFromFile(); int[] index = new int[2] { 1, 4 }; OperationDelete Op1; OperationAdd Op = new OperationAdd(testTask, sortType); result = Op.Execute(testTaskList, testStorage); Op1 = new OperationDelete("", index, null, null, null, false, SearchType.NONE, sortType); result = Op1.Execute(testTaskList, testStorage); Assert.AreEqual("Invalid task index!", result.FeedbackString); index = new int[2] { 1, 1 }; Op1 = new OperationDelete("", index, null, null, null, false, SearchType.NONE, sortType); result = Op1.Execute(testTaskList, testStorage); Assert.AreEqual("Deleted task \"test\" successfully.", result.FeedbackString); return; }
/// <summary> /// Executes the operation and adds it to the operation history. /// </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); Func<Task, TimeSpan, Response> action = PostponeTask; object[] args = { postponeDuration }; Response response = null; response = CheckIfIndexesAreValid(startIndex, endIndex); if (response != null) return response; if (!hasIndex) response = ExecuteBySearch(taskName, startTime, endTime, isAll, searchType, action, args); else if (hasIndex) response = ExecuteByIndex(startIndex, endIndex, action, args); else response = new Response(Result.FAILURE, sortType, this.GetType()); if (response.IsSuccessful()) AddToOperationHistory(); return response; }
/// <summary> /// Updates and sorts the display according to the input Response. /// </summary> /// <param name="response">The response to update the display with.</param> /// <returns>The displayed list of tasks after updating the display with the input Response.</returns> public List<Task> UpdateDisplay(Response response) { if (response.TasksToBeDisplayed == null) return displayedTasks; // don't update display list. displayedTasks = response.TasksToBeDisplayed; switch (response.FormatType) { case SortType.NAME: displayedTasks.Sort(Task.CompareByName); SetGroupingByName(); break; case SortType.DATE_TIME: displayedTasks.Sort(Task.CompareByDateTime); SetGroupingByDateTime(); break; default: Logger.Info("No change in sorting format", "UpdateDisplay::TaskListViewControl"); break; } this.SetObjects(displayedTasks); List<Task> reorderedList = GenerateReorderedList(); return reorderedList; }
/// <summary> /// Undoes this operation. /// </summary> /// <param name="taskList">List of task this method 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 undo operation.</returns> public override Response Undo(List<Task> taskList, Storage storageIO) { SetMembers(taskList, storageIO); Response response = null; for (int i = 0; i < executedTasks.Count; i++) { Task taskToUndo = executedTasks.Dequeue(); response = PostponeTask(taskToUndo, postponeDuration.Negate()); if (!response.IsSuccessful()) return response; } if (response == null) response = new Response(Result.FAILURE, sortType, this.GetType()); return response; }
public override bool AllowSkipOver(Response response) { if (response.IsInvalidTask()) return true; else return false; }
/// <summary> /// Executes the given Delegate for every Task in the given list of Tasks, with each of the Tasks as the first parameter. /// Returns the Response indicating the result of this operation. /// </summary> /// <param name="tasks">The list of tasks to operate on.</param> /// <param name="action">The delegate to invoke.</param> /// <param name="args">An optional list of additional parameters to invoke with the delegate /// which will be added after the Task parameter.</param> /// <returns>Response indicating the result of the operation.</returns> private Response ExecuteOnAll(List<Task> tasks, Delegate action, params object[] args) { Response response = null; for (int i = tasks.Count - 1; i >= 0; i--) { Task task = tasks[i]; if (task == null) response = new Response(Result.FAILURE, sortType, this.GetType(), currentListedTasks); var parameters = AddTaskToParameters(args, task); response = InvokeAction(action, parameters); if (!response.IsSuccessful() && !AllowSkipOver(response)) return response; } response = new Response(Result.SUCCESS_MULTIPLE, sortType, this.GetType(), currentListedTasks); return response; }
/// <summary> /// Indicates whether the Operation should allow a multiple-task /// execution to continue if one of the tasks execute unsuccessfully. /// This method can be overriden to specify when this condition should be allowed. /// If it is not overriden, it will return false by default. /// </summary> /// <param name="response"></param> /// <returns>False</returns> public virtual bool AllowSkipOver(Response response) { return false; }
/// <summary> /// Executes a Delegate action on one or more tasks chosen by the currently displayed index. /// The indices provided must be valid for the current displayed list. /// </summary> /// <param name="startIndex">The start index of the tasks for the Delegate to invoke with.</param> /// <param name="endIndex">The end index of the tasks for the Delegate to invoke with.</param> /// <param name="action">The Delegate method to be invoked which must accept a Task as the first parameter.</param> /// <param name="args">Any subsequent parameters to be passed into the Delegate method and then invoked.</param> /// <returns>Response indicating the result of the operation.</returns> protected Response ExecuteByIndex(int startIndex, int endIndex, Delegate action, params object[] args) { Response response = null; Debug.Assert(startIndex >= 0 && endIndex < currentListedTasks.Count(), "Invalid indexes were passed to ExecuteByIndex!"); for (int i = endIndex; i >= startIndex; i--) { Task task = currentListedTasks[i]; if (task == null) response = new Response(Result.FAILURE, sortType, this.GetType(), currentListedTasks); else { var parameters = AddTaskToParameters(args, task); response = InvokeAction(action, parameters); if (!response.IsSuccessful() && !AllowSkipOver(response)) return response; } } if (startIndex != endIndex) response = new Response(Result.SUCCESS_MULTIPLE, sortType, this.GetType(), currentListedTasks); return response; }
/// <summary> /// Invokes the given delegate with the given parameters. /// </summary> /// <param name="action">Delegate to invoke.</param> /// <param name="parameters">Parameters to invoke the delegate with.</param> /// <returns>The Response returned by the Delegate after it is invoked.</returns> private static Response InvokeAction(Delegate action, object[] parameters) { Response response; try { response = (Response)action.DynamicInvoke(parameters); } catch (System.Exception ex) { response = new Response(Result.EXCEPTION_FAILURE); Logger.Error(ex, "InvokeAction::Operation"); } return response; }
/// <summary> /// Updates the current display list to show all tasks in the given search results list. /// </summary> /// <param name="searchResults">The search result list of tasks to display.</param> /// <param name="searchString">The search string to display in the feedback string for the user.</param> /// <param name="startTime">The limiting start time of the search.</param> /// <param name="endTime">The limiting end time of the search.</param> /// <param name="searchType">The search type of the search.</param> /// <returns>Response containing the new list of tasks to be displayed.</returns> protected Response DisplaySearchResults( List<Task> searchResults, string searchString, DateTime? startTime, DateTime? endTime, SearchType searchType ) { Response response; if (searchResults.Count == 0) response = new Response(Result.FAILURE, sortType, typeof(OperationSearch)); else { currentListedTasks = new List<Task>(searchResults); string[] criteria; SetArgumentsForSearchFeedbackString(out criteria, searchString, startTime, endTime, searchType); response = new Response(Result.SUCCESS, sortType, typeof(OperationSearch), currentListedTasks, criteria); } return response; }
/// <summary> /// This method tries to find a free time slot within the spceified schedule datetime range /// to schedule the task. /// </summary> /// <returns>The appropriate response object, depending on whether a free slot could be found</returns> private Response TryScheduleTask() { Response response = new Response(Result.FAILURE, sortType, typeof(OperationSchedule), currentListedTasks); DateTime tryStartTime = startDateTime; DateTime tryEndTime = new DateTime(); int numberOfIterations = 0; bool isSlotFound = false; // loop through all tasks to find earliest possible fitting time while (!isSlotFound && tryEndTime <= ((DateTime)endDateTime)) { int numberOfSetsToLoop = 1; switch (taskDurationType) { case TimeRangeType.HOUR: tryStartTime = startDateTime.AddHours(numberOfIterations); tryEndTime = tryStartTime.AddHours(taskDurationAmount); break; case TimeRangeType.DAY: case TimeRangeType.WEEK: case TimeRangeType.MONTH: numberOfSetsToLoop = GetNumberOfLoops(taskDurationType, tryStartTime); break; } isSlotFound = IsTimeSlotFreeOfTasks(numberOfSetsToLoop, tryStartTime, ref tryEndTime); if (isSlotFound) { response = ScheduleTaskAtSlot(taskName, tryStartTime, tryEndTime); } tryStartTime = tryStartTime.AddDays(1); numberOfIterations++; } return response; }
public void OperationSearchTest() { testStorage = new Storage("OpUnittest.xml", "OpUnittestsettings.xml"); testTaskList = testStorage.LoadTasksFromFile(); DateTime timeTest; timeTest = DateTime.ParseExact("10/15/2013 5:00 AM", formats, new CultureInfo("en-US"), DateTimeStyles.None); DateTimeSpecificity specific = new DateTimeSpecificity(); TaskDeadline testDeadline = new TaskDeadline("test", timeTest, specific); OperationAdd Op1 = new OperationAdd(testDeadline, sortType); OperationSearch Op2 = new OperationSearch("SearchConditionCannotBeMatching",DateTime.Now,timeTest.AddDays(1),specific,SearchType.NONE,SortType.DEFAULT); result = Op2.Execute(testTaskList, testStorage); Assert.AreEqual("No matching tasks found!", result.FeedbackString); result = Op1.Execute(testTaskList, testStorage); result = Op2.Execute(testTaskList, testStorage); Assert.AreEqual("No matching tasks found!", result.FeedbackString); return; }
public void OperationSortTest() { testStorage = new Storage("OpUnittest.xml", "OpUnittestsettings.xml"); testTaskList = testStorage.LoadTasksFromFile(); OperationSort Op = new OperationSort(SortType.NAME); result = Op.Execute(testTaskList, testStorage); Assert.AreEqual("Sorting by name.", result.FeedbackString); return; }
public void OperationUndoAddTest() { testStorage = new Storage("OpUnittest.xml", "OpUnittestsettings.xml"); testTaskList = testStorage.LoadTasksFromFile(); OperationAdd Op = new OperationAdd(testTask, sortType); Op.Execute(testTaskList, testStorage); result = Op.Undo(testTaskList, testStorage); Assert.AreEqual(result.FormatType.ToString(), "DEFAULT"); return; }