forked from AY1920S1-CS2103-F09-3/main
/
ViewCommitCommand.java
69 lines (57 loc) · 3.11 KB
/
ViewCommitCommand.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package seedu.address.logic.commands.storage;
import static java.util.Objects.requireNonNull;
import static seedu.address.logic.commands.storage.ViewCommitHistoryCommand.MESSAGE_NO_COMMIT_HISTORY;
import javafx.collections.ObservableList;
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.CommandResult;
import seedu.address.model.Model;
import seedu.address.model.semester.Semester;
import seedu.address.model.studyplan.StudyPlan;
import seedu.address.model.versiontracking.Commit;
import seedu.address.model.versiontracking.CommitList;
import seedu.address.model.versiontracking.exception.StudyPlanCommitManagerNotFoundException;
import seedu.address.ui.ResultViewType;
/**
* Represents a command for the user to view the version of the current active study plan for a particular commit.
* This does not discard the commits after the specified commit.
*/
public class ViewCommitCommand extends Command {
public static final String COMMAND_WORD = "viewcommit";
public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Views the study plan commit identified by the index number used in the displayed commit list.\n"
+ "Parameters: PLAN_INDEX.COMMIT_NUMBER (both must be non-negative integers)\n";
public static final String MESSAGE_SUCCESS = "Here is your study plan for this commit. Please do not modify it.";
public static final String MESSAGE_NO_SUCH_COMMIT = "The commit index you've entered is invalid!";
public static final String MESSAGE_NO_STUDYPLAN = "There's no active study plan. Create now!";
public static final String MESSAGE_NOT_ACTIVE_STUDYPLAN = "The study plan index does not match the active one.";
private int studyPlanIndex;
private int commitNumber;
public ViewCommitCommand(int studyPlanIndex, int commitNumber) {
this.studyPlanIndex = studyPlanIndex;
this.commitNumber = commitNumber;
}
@Override
public CommandResult execute(Model model) {
requireNonNull(model);
StudyPlan activeStudyPlan = model.getActiveStudyPlan();
if (activeStudyPlan == null) {
return new CommandResult(MESSAGE_NO_STUDYPLAN);
}
// if the index is different from active study plan, throw an error
if (activeStudyPlan.getIndex() != studyPlanIndex) {
return new CommandResult(MESSAGE_NOT_ACTIVE_STUDYPLAN);
}
try {
int activeStudyPlanIndex = activeStudyPlan.getIndex();
CommitList commitList = model.getCommitListByStudyPlanIndex(activeStudyPlanIndex);
Commit commit = commitList.getCommitByIndex(commitNumber);
StudyPlan studyPlan = commit.getStudyPlan();
ObservableList<Semester> semesters = studyPlan.getSemesters().asUnmodifiableObservableList();
return new CommandResult<>(MESSAGE_SUCCESS, ResultViewType.STUDY_PLAN, semesters);
} catch (StudyPlanCommitManagerNotFoundException e) {
return new CommandResult(MESSAGE_NO_COMMIT_HISTORY);
} catch (IndexOutOfBoundsException e) {
return new CommandResult(MESSAGE_NO_SUCH_COMMIT);
}
}
}