-
Notifications
You must be signed in to change notification settings - Fork 548
/
Copy pathFindTaskCommandParser.java
136 lines (123 loc) · 6.29 KB
/
FindTaskCommandParser.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package dash.logic.parser.taskcommand;
import static dash.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static dash.commons.core.Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX;
import static dash.logic.parser.CliSyntax.PREFIX_COMPLETION_STATUS;
import static dash.logic.parser.CliSyntax.PREFIX_PERSON;
import static dash.logic.parser.CliSyntax.PREFIX_TAG;
import static dash.logic.parser.CliSyntax.PREFIX_TASK_DATE;
import static dash.logic.parser.CliSyntax.PREFIX_TASK_DESCRIPTION;
import static java.util.Objects.requireNonNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import dash.commons.core.index.Index;
import dash.logic.commands.taskcommand.AssignPeopleCommand;
import dash.logic.commands.taskcommand.FindTaskCommand;
import dash.logic.commands.taskcommand.FindTaskCommand.FindTaskDescriptor;
import dash.logic.parser.ArgumentMultimap;
import dash.logic.parser.ArgumentTokenizer;
import dash.logic.parser.ParserRequiringPersonList;
import dash.logic.parser.ParserUtil;
import dash.logic.parser.exceptions.ParseException;
import dash.model.person.Person;
import dash.model.task.TaskDate;
import javafx.collections.ObservableList;
/**
* Parses input arguments and creates a new FindCommand object
*/
public class FindTaskCommandParser implements ParserRequiringPersonList<FindTaskCommand> {
/**
* Parses the given {@code String} of arguments in the context of the FindCommand
* and returns a FindCommand object for execution.
*
* @throws ParseException if the user input does not conform the expected format
*/
@Override
public FindTaskCommand parse(String args, ObservableList<Person> filteredPersonList) throws ParseException {
requireNonNull(args);
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_TASK_DESCRIPTION, PREFIX_TASK_DATE, PREFIX_TAG, PREFIX_PERSON,
PREFIX_COMPLETION_STATUS);
String preamble = argMultimap.getPreamble();
Index index;
FindTaskDescriptor findTaskDescriptor = new FindTaskDescriptor();
boolean descPresent = argMultimap.getValue(PREFIX_TASK_DESCRIPTION).isPresent();
boolean datePresent = argMultimap.getValue(PREFIX_TASK_DATE).isPresent();
boolean tagPresent = argMultimap.getValue(PREFIX_TAG).isPresent();
boolean personPresent = argMultimap.getValue(PREFIX_PERSON).isPresent();
boolean completionStatusPresent = argMultimap.getValue(PREFIX_COMPLETION_STATUS).isPresent();
if (preamble.isEmpty() && !descPresent && !tagPresent && !datePresent && !personPresent
&& !completionStatusPresent) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, FindTaskCommand.MESSAGE_USAGE));
} else if (!preamble.isEmpty()) {
String[] preambleKeywords = preamble.split("\\s+");
findTaskDescriptor.setDesc(Arrays.asList(preambleKeywords));
}
//if both preamble and desc prefix specified, desc prefix will override
if (descPresent) {
if (argMultimap.getValue(PREFIX_TASK_DESCRIPTION).get().isEmpty()) {
throw new ParseException("Arguments cannot be empty");
}
String[] nameKeywords = argMultimap.getValue(PREFIX_TASK_DESCRIPTION).get().split("\\s+");
findTaskDescriptor.setDesc(Arrays.asList(nameKeywords));
}
if (datePresent) {
if (argMultimap.getValue(PREFIX_TASK_DATE).get().isEmpty()) {
throw new ParseException("Arguments cannot be empty");
}
TaskDate taskDateArg = ParserUtil.parseTaskDateToEdit(argMultimap.getValue(PREFIX_TASK_DATE).get());
findTaskDescriptor.setDate(taskDateArg);
}
if (tagPresent) {
if (argMultimap.getValue(PREFIX_TAG).get().isEmpty()) {
throw new ParseException("Arguments cannot be empty");
}
String[] tagKeywords = argMultimap.getValue(PREFIX_TAG).get().split("\\s+");
findTaskDescriptor.setTags(Arrays.asList(tagKeywords));
}
if (personPresent) {
if (argMultimap.getValue(PREFIX_PERSON).get().isEmpty()) {
throw new ParseException(MESSAGE_INVALID_COMMAND_FORMAT + "\n"
+ FindTaskCommand.MESSAGE_USAGE);
}
try {
index = ParserUtil.parseIndex(argMultimap.getValue(PREFIX_PERSON).get());
} catch (ParseException pe) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
FindTaskCommand.MESSAGE_USAGE),
pe);
}
List<String> personKeywords = new ArrayList<>();
if (argMultimap.getValue(PREFIX_PERSON).isPresent()) {
Set<Index> personIndices = ParserUtil.parsePersonIndex(argMultimap.getAllValues(PREFIX_PERSON));
Set<String> peopleNames = new HashSet<>();
for (Index i : personIndices) {
if (i.getZeroBased() < 0 || i.getZeroBased() >= filteredPersonList.size()) {
throw new ParseException(MESSAGE_INVALID_PERSON_DISPLAYED_INDEX + "\n"
+ FindTaskCommand.MESSAGE_USAGE);
}
Person person = (filteredPersonList.get(i.getZeroBased()));
peopleNames.add(person.getName().fullName);
}
personKeywords = new ArrayList<>(peopleNames);
}
findTaskDescriptor.setPerson(personKeywords);
}
if (completionStatusPresent) {
if (argMultimap.getValue(PREFIX_COMPLETION_STATUS).get().isEmpty()) {
throw new ParseException("Arguments cannot be empty");
}
String[] completionStatusKeywords = argMultimap.getValue(PREFIX_COMPLETION_STATUS).get().split("\\s+");
String firstKeyword = completionStatusKeywords[0];
findTaskDescriptor.setCompletionStatus(ParserUtil.parseCompletionStatus(firstKeyword).get());
}
return new FindTaskCommand(findTaskDescriptor);
}
@Override
public FindTaskCommand parse(String userInput) throws ParseException {
return null;
}
}