-
Notifications
You must be signed in to change notification settings - Fork 548
/
Copy pathFindTaskCommandParser.java
157 lines (143 loc) · 7.06 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package dash.logic.parser.taskcommand;
import static dash.commons.core.Messages.MESSAGE_ARGUMENT_EMPTY;
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.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
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 = parseTagsForFind(argMultimap.getAllValues(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(MESSAGE_ARGUMENT_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(MESSAGE_ARGUMENT_EMPTY);
}
TaskDate taskDateArg = ParserUtil.parseTaskDate(argMultimap.getValue(PREFIX_TASK_DATE).get(), true);
findTaskDescriptor.setDate(taskDateArg);
}
if (tagPresent) {
if (argMultimap.getValue(PREFIX_TAG).get().isEmpty()) {
throw new ParseException(MESSAGE_ARGUMENT_EMPTY);
}
parseTagsForFind(argMultimap.getAllValues(PREFIX_TAG)).ifPresent(findTaskDescriptor::setTags);
}
if (personPresent) {
if (argMultimap.getValue(PREFIX_PERSON).get().isEmpty()) {
throw new ParseException(MESSAGE_ARGUMENT_EMPTY);
}
List<String> personKeywords = new ArrayList<>();
if (argMultimap.getValue(PREFIX_PERSON).isPresent()) {
Set<Index> personIndices;
try {
personIndices = ParserUtil.parsePersonIndex(argMultimap.getAllValues(PREFIX_PERSON));
} catch (ParseException pe) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
AssignPeopleCommand.MESSAGE_USAGE),
pe);
} catch (NumberFormatException nfe) {
throw new ParseException(MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}
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);
}
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(MESSAGE_ARGUMENT_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;
}
/**
* Parses {@code Collection<String> tags} into a {@code Set<Tag>} if {@code tags} is non-empty.
* If {@code tags} contain only one element which is an empty string, it will be parsed into a
* {@code Set<Tag>} containing zero tags.
*/
private Optional<List<String>> parseTagsForFind(Collection<String> tags) throws ParseException {
if (tags.isEmpty()) {
return Optional.empty();
}
List<String> tagList = new ArrayList<>();
if (tags.size() == 1 && tags.contains("")) {
tagList = Collections.emptyList();
} else {
tagList.addAll(tags);
}
return Optional.of(ParserUtil.parseTagList(tagList));
}
}