Skip to content

Commit

Permalink
Merge branch 'create_filtered_requestbook' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddl9 authored Mar 13, 2019
2 parents c8b590d + e4d78b8 commit 5842ebb
Show file tree
Hide file tree
Showing 29 changed files with 656 additions and 102 deletions.
15 changes: 14 additions & 1 deletion src/main/java/seedu/address/MainApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import seedu.address.model.ModelManager;
import seedu.address.model.ReadOnlyAddressBook;
import seedu.address.model.ReadOnlyHealthWorkerBook;
import seedu.address.model.ReadOnlyRequestBook;
import seedu.address.model.ReadOnlyUserPrefs;
import seedu.address.model.RequestBook;
import seedu.address.model.UserPrefs;
import seedu.address.model.util.SampleDataUtil;
import seedu.address.storage.AddressBookStorage;
Expand Down Expand Up @@ -88,12 +90,20 @@ private Model initModelManager(Storage storage, ReadOnlyUserPrefs userPrefs) {
Optional<ReadOnlyHealthWorkerBook> healthWorkerBookOptional;
ReadOnlyAddressBook initialAddressBook;
ReadOnlyHealthWorkerBook initialHealthWorkerBook;
ReadOnlyRequestBook initialRequestBook;
Optional<ReadOnlyRequestBook> requestBookOptional;

try {
addressBookOptional = storage.readAddressBook();
requestBookOptional = storage.readRequestBook();
if (!addressBookOptional.isPresent()) {
logger.info("Data file not found. Will be starting with a sample AddressBook");
}

if (!requestBookOptional.isPresent()) {
logger.info("Request file not found. Will be starting with sample RequestBook");
}
initialRequestBook = new RequestBook();
initialAddressBook = addressBookOptional.orElseGet(SampleDataUtil::getSampleAddressBook);
healthWorkerBookOptional = storage.readHealthWorkerBook();
if (!addressBookOptional.isPresent()) {
Expand All @@ -106,13 +116,16 @@ private Model initModelManager(Storage storage, ReadOnlyUserPrefs userPrefs) {
logger.warning("Data file not in the correct format. Will be starting with an empty AddressBook");
initialAddressBook = new AddressBook();
initialHealthWorkerBook = new HealthWorkerBook();
initialRequestBook = new RequestBook();
} catch (IOException e) {
logger.warning("Problem while reading from the file. Will be starting with an empty AddressBook");
initialAddressBook = new AddressBook();
initialHealthWorkerBook = new HealthWorkerBook();
initialRequestBook = new RequestBook();
}

return new ModelManager(initialAddressBook, initialHealthWorkerBook, userPrefs);
return new ModelManager(initialAddressBook, initialHealthWorkerBook, initialRequestBook,
userPrefs);
}

private void initLogging(Config config) {
Expand Down
1 change: 0 additions & 1 deletion src/main/java/seedu/address/logic/LogicManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public CommandResult execute(String commandText) throws CommandException, ParseE

return commandResult;
}

@Override
public ReadOnlyAddressBook getAddressBook() {
return model.getAddressBook();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE;

import seedu.address.logic.CommandHistory;
import seedu.address.logic.commands.AddCommand;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
Expand All @@ -16,30 +17,30 @@
/**
* Creates a new request to the request book.
*/
public class CreateCommand extends RequestCommand {
public class CreateRequestCommand extends AddCommand {

public static final String COMMAND_WORD = "create";
public static final String COMMAND_WORD = "request";

public static final String MESSAGE_USAGE = RequestCommand.COMMAND_WORD + " " + COMMAND_WORD
+ ": Creates a new request in the request book. " + "Parameters: "
+ PREFIX_NAME + "NAME "
+ PREFIX_PHONE + "PHONE "
+ PREFIX_ADDRESS + "ADDRESS "
+ PREFIX_DATE + "DATETIME"
+ PREFIX_CONDITION + "CONDITION...\n"
+ "Example: " + RequestCommand.COMMAND_WORD + " " + COMMAND_WORD + " "
+ PREFIX_NAME + "John Doe "
+ PREFIX_PHONE + "81234567 "
+ PREFIX_ADDRESS + "123, Sengkang Ave 3, #04-12, 214632 "
+ PREFIX_DATE + "01-01-2019 08:00:00 "
+ PREFIX_CONDITION + "Physiotherapy";
public static final String MESSAGE_USAGE = AddCommand.COMMAND_WORD + " " + COMMAND_WORD
+ ": Creates a new request in the request book. " + "Parameters: "
+ PREFIX_NAME + "NAME "
+ PREFIX_PHONE + "PHONE "
+ PREFIX_ADDRESS + "ADDRESS "
+ PREFIX_DATE + "DATETIME"
+ PREFIX_CONDITION + "CONDITION...\n"
+ "Example: " + RequestCommand.COMMAND_WORD + " " + COMMAND_WORD + " "
+ PREFIX_NAME + "John Doe "
+ PREFIX_PHONE + "81234567 "
+ PREFIX_ADDRESS + "123, Sengkang Ave 3, #04-12, 214632 "
+ PREFIX_DATE + "01-01-2019 08:00:00 "
+ PREFIX_CONDITION + "Physiotherapy";

public static final String MESSAGE_SUCCESS = "Created new request successfully: %1$s";
public static final String MESSAGE_DUPLICATE_REQUEST = "This request already exists.";

private final Request newRequest;

public CreateCommand(Request newRequest) {
public CreateRequestCommand(Request newRequest) {
requireNonNull(newRequest);
this.newRequest = newRequest;
}
Expand All @@ -55,12 +56,24 @@ public CreateCommand(Request newRequest) {
@Override
public CommandResult execute(Model model, CommandHistory history) throws CommandException {
requireNonNull(model);
// TODO write created request into the JSON file
// if (model.hasRequest(this.newRequest)) {
// throw new CommandException(MESSAGE_DUPLICATE_REQUEST);
// }
// model.addRequest(newRequest);
// model.commitRequestBook();
if (model.hasRequest(this.newRequest)) {
throw new CommandException(MESSAGE_DUPLICATE_REQUEST);
}
model.addRequest(newRequest);
model.commitRequestBook();
return new CommandResult(String.format(MESSAGE_SUCCESS, newRequest));
}

@Override
public void add(Model model, Object toAdd) {
model.addRequest((Request) toAdd);
model.commitRequestBook();
}

@Override
public boolean equals(Object other) {
return other == this
|| (other instanceof CreateRequestCommand
&& newRequest.equals(((CreateRequestCommand) other).newRequest));
}
}
70 changes: 58 additions & 12 deletions src/main/java/seedu/address/logic/parser/AddCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS;
import static seedu.address.logic.parser.CliSyntax.PREFIX_CONDITION;
import static seedu.address.logic.parser.CliSyntax.PREFIX_DATE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NRIC;
Expand All @@ -11,11 +13,13 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG;

import java.util.Set;
import java.util.UUID;
import java.util.stream.Stream;

import seedu.address.logic.commands.AddCommand;
import seedu.address.logic.commands.AddHealthWorkerCommand;
import seedu.address.logic.commands.AddPersonCommand;
import seedu.address.logic.commands.request.CreateRequestCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.Address;
import seedu.address.model.person.Email;
Expand All @@ -25,6 +29,9 @@
import seedu.address.model.person.Phone;
import seedu.address.model.person.healthworker.HealthWorker;
import seedu.address.model.person.healthworker.Organization;
import seedu.address.model.request.Request;
import seedu.address.model.request.RequestDate;
import seedu.address.model.request.RequestStatus;
import seedu.address.model.tag.Skills;
import seedu.address.model.tag.Tag;

Expand All @@ -42,25 +49,42 @@ public AddCommand parse(String args) throws ParseException {
CommandMode commandMode = ArgumentTokenizer.checkMode(args);
if (commandMode == CommandMode.HEALTH_WORKER) {
return parseAddHealthWorker(ArgumentTokenizer.trimMode(args));
} else if (commandMode == CommandMode.REQUEST) {
return parseAddRequest(args);
}

ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS, PREFIX_TAG);
Person person = getPersonFromArgs(args);

if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_ADDRESS, PREFIX_PHONE, PREFIX_EMAIL)
|| !argMultimap.getPreamble().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddPersonCommand.MESSAGE_USAGE));
return new AddPersonCommand(person);
}

/**
* Parses the given {@code String} of arguments in the context of the CreateRequestCommand
* and returns an CreateReqeustCommand object for execution.
* @throws ParseException if the suer input does not conform the expected format
*/
private CreateRequestCommand parseAddRequest(String args) throws ParseException {
UUID uuid = UUID.randomUUID();
String requestId = uuid.toString();

Person patient = getPersonFromArgs(args);

ArgumentMultimap argumentMultimap = ArgumentTokenizer.tokenize(args, PREFIX_DATE,
PREFIX_CONDITION);

if (!arePrefixesPresent(argumentMultimap, PREFIX_DATE, PREFIX_CONDITION)) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
CreateRequestCommand.MESSAGE_USAGE));
}

Name name = ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME).get());
Phone phone = ParserUtil.parsePhone(argMultimap.getValue(PREFIX_PHONE).get());
Email email = ParserUtil.parseEmail(argMultimap.getValue(PREFIX_EMAIL).get());
Address address = ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS).get());
Set<Tag> tagList = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG));
RequestDate requestDate =
ParserUtil.parseRequestDate(argumentMultimap.getValue(PREFIX_DATE).get());

Person person = new Person(name, phone, email, address, tagList);
Set<Tag> conditions = ParserUtil.parseTags(argumentMultimap.getAllValues(PREFIX_CONDITION));

return new CreateRequestCommand(new Request(requestId, patient, null, requestDate, conditions,
new RequestStatus("PENDING")));

return new AddPersonCommand(person);
}

/**
Expand Down Expand Up @@ -99,6 +123,28 @@ private AddHealthWorkerCommand parseAddHealthWorker(String args) throws ParseExc
return new AddHealthWorkerCommand(healthWorker);
}

/**
* Extracts a Person object from the given object and returns it.
* @throws ParseException if there are invalid/unfilled fields.
*/
private Person getPersonFromArgs(String args) throws ParseException {
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS, PREFIX_TAG);

if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_ADDRESS, PREFIX_PHONE, PREFIX_EMAIL)
|| !argMultimap.getPreamble().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddPersonCommand.MESSAGE_USAGE));
}

Name name = ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME).get());
Phone phone = ParserUtil.parsePhone(argMultimap.getValue(PREFIX_PHONE).get());
Email email = ParserUtil.parseEmail(argMultimap.getValue(PREFIX_EMAIL).get());
Address address = ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS).get());
Set<Tag> tagList = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG));

return new Person(name, phone, email, address, tagList);
}

/**
* Returns true if none of the prefixes contains empty {@code Optional} values in the given
* {@code ArgumentMultimap}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -135,7 +136,7 @@ private static int findPrefixPosition(String argsString, String prefix, int from
private static ArgumentMultimap extractArguments(String argsString, List<PrefixPosition> prefixPositions) {

// Sort by start position
prefixPositions.sort((prefix1, prefix2) -> prefix1.getStartPosition() - prefix2.getStartPosition());
prefixPositions.sort(Comparator.comparingInt(PrefixPosition::getStartPosition));

// Insert a PrefixPosition to represent the preamble
PrefixPosition preambleMarker = new PrefixPosition(new Prefix(""), 0);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/seedu/address/logic/parser/CliSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public class CliSyntax {
public static final Prefix PREFIX_DATE = new Prefix("dt/");
public static final Prefix PREFIX_HEALTHWORKER = new Prefix("hw/");
public static final Prefix PREFIX_STATUS = new Prefix("st/");
public static final Prefix PREFIX_CONDITION = new Prefix("o/");
public static final Prefix PREFIX_PATIENT = new Prefix("pt/");
public static final Prefix PREFIX_CONDITION = new Prefix("c/");

// Additional prefixes for HealthWorker and Patient class
public static final Prefix PREFIX_ORGANIZATION = new Prefix("o/");
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import seedu.address.model.person.Nric;
import seedu.address.model.person.Phone;
import seedu.address.model.person.healthworker.Organization;
import seedu.address.model.request.RequestDate;
import seedu.address.model.tag.Skills;
import seedu.address.model.tag.Specialisation;
import seedu.address.model.tag.Tag;
Expand Down Expand Up @@ -54,6 +55,16 @@ public static Name parseName(String name) throws ParseException {
return new Name(trimmedName);
}

/**
* Parses a {@code String id} and returns it's respective index in the addressbook patient list.
* @return
*/
public static int parsePatientIndex(String id) {
requireNonNull(id);
String trimmedId = id.trim();
return Integer.parseInt(trimmedId);
}

/**
* Parses a {@code String phone} into a {@code Phone}.
* Leading and trailing whitespaces will be trimmed.
Expand Down Expand Up @@ -190,4 +201,20 @@ public static Skills parseSpecialisations(Collection<String> specialisations)
}
return skills;
}

/**
* Parses {@code String date} into a {@code RequestDate}.
* Leading and trailing whitespaces will be trimmed.
*
* @throws ParseException if the given {@code date} is invalid.
*/
public static RequestDate parseRequestDate(String date) throws ParseException {
requireNonNull(date);
String trimmedDate = date.trim();
if (!RequestDate.isValidDate(trimmedDate)) {
throw new ParseException(RequestDate.MESSAGE_DATE_CONSTRAINTS);
}

return new RequestDate(trimmedDate);
}
}
17 changes: 17 additions & 0 deletions src/main/java/seedu/address/model/AddressBook.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,23 @@ protected void indicateModified() {
invalidationListenerManager.callListeners(this);
}

//// util methods
/**
* Returns an unmodifiable view of the healthworkers list.
* This list will not contain any duplicate healthworkers
*/
public ObservableList<HealthWorker> getHealthWorkerList() {
return healthWorkers.asUnmodifiableObservableList();
}

public Person getPersonAt(int index) {
return this.persons.getAt(index);
}

public HealthWorker getHealthWorkerAt(int index) {
return this.healthWorkers.getAt(index);
}

@Override
public String toString() {
return persons.asUnmodifiableObservableList().size() + " persons";
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,6 @@ public interface Model {
* request in the request book.
*/
void setRequest(Request target, Request editedRequest);

void commitRequestBook();
}
Loading

0 comments on commit 5842ebb

Please sign in to comment.