Skip to content

Commit

Permalink
Add logging to some Commands
Browse files Browse the repository at this point in the history
  • Loading branch information
marclzh committed Nov 7, 2022
1 parent a89f024 commit 9a3b6f7
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import static seedu.address.logic.parser.CliSyntax.PREFIX_DATE;

import java.util.List;
import java.util.logging.Logger;

import seedu.address.MainApp;
import seedu.address.commons.core.LogsCenter;
import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
Expand All @@ -30,6 +33,8 @@ public class AddAppointmentCommand extends Command {
public static final String DATE_MISSING = "No date & time given! "
+ "Appointments must have date & time, formatted in d/dd-MM-yyyy HHmm.";

private static final Logger logger = LogsCenter.getLogger(MainApp.class);

private final Index index;
private final String appointmentDate;

Expand All @@ -42,18 +47,27 @@ public class AddAppointmentCommand extends Command {
public AddAppointmentCommand(Index index, String appointmentDate) {
requireAllNonNull(index, appointmentDate);

assert index != null;
assert appointmentDate != null;

this.index = index;
this.appointmentDate = appointmentDate;

logger.info("Appointment created for index: " + this.index.getOneBased()
+ " & Appointment Date: " + this.appointmentDate);
}

@Override
public CommandResult execute(Model model) throws CommandException {
List<Person> lastShownList = CommandUtil.prepareFilteredList(model, index);

if (!Appointment.isFutureDate(appointmentDate)) {
logger.warning("Appointment Date is in the future.");
throw new CommandException(Appointment.MESSAGE_DATE_PAST);
}

assert Appointment.isFutureDate(appointmentDate);

Person personToEdit = lastShownList.get(index.getZeroBased());
Person editedPerson = new Person(personToEdit.getName(), personToEdit.getBirthdate(),
personToEdit.getPhone(), personToEdit.getEmail(),
Expand All @@ -62,6 +76,9 @@ public CommandResult execute(Model model) throws CommandException {

model.setPerson(personToEdit, editedPerson);
model.updateFilteredPersonList(Model.PREDICATE_SHOW_ALL_PERSONS);

logger.info("Appointment has been added successfully.");

return new CommandResult(String.format(MESSAGE_SUCCESS,
personToEdit.getName().toString()) + ": " + appointmentDate);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package seedu.address.logic.commands;

import java.util.List;
import java.util.logging.Logger;

import seedu.address.MainApp;
import seedu.address.commons.core.LogsCenter;
import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
Expand All @@ -21,10 +24,19 @@ public class ClearAppointmentCommand extends Command {

public static final String MESSAGE_SUCCESS = "Appointment cleared!";

private static final Logger logger = LogsCenter.getLogger(MainApp.class);

private final Index index;

/**
* Constructs a ClearAppointmentCommand to clear an appointment for a patient.
*
* @param index Index of the patient.
*/
public ClearAppointmentCommand(Index index) {
this.index = index;

logger.info("ClearAppointmentCommand created with index: " + this.index.getOneBased());
}

@Override
Expand All @@ -39,6 +51,9 @@ public CommandResult execute(Model model) throws CommandException {

model.setPerson(personToEdit, editedPerson);
model.updateFilteredPersonList(Model.PREDICATE_SHOW_ALL_PERSONS);

logger.info("Appointment has been cleared successfully.");

return new CommandResult(MESSAGE_SUCCESS);
}

Expand Down
9 changes: 9 additions & 0 deletions src/main/java/seedu/address/logic/commands/ClearCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import static java.util.Objects.requireNonNull;

import java.util.logging.Logger;

import seedu.address.MainApp;
import seedu.address.commons.core.LogsCenter;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.AddressBook;
import seedu.address.model.Model;
Expand All @@ -14,16 +18,21 @@ public class ClearCommand extends Command {
public static final String COMMAND_WORD = "clear";
public static final String MESSAGE_SUCCESS = "Patient list has been cleared!";

private static final Logger logger = LogsCenter.getLogger(MainApp.class);

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);

if (model.isRecordListDisplayed()) {
logger.warning("Patient List View not currently displayed.");
throw new CommandException(MESSAGE_ADDRESS_BOOK_COMMAND_PREREQUISITE);
}

model.setAddressBook(new AddressBook());

logger.info("Patient List has been cleared successfully.");

return new CommandResult(MESSAGE_SUCCESS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

import static java.util.Objects.requireNonNull;

import java.util.logging.Logger;

import seedu.address.MainApp;
import seedu.address.commons.core.LogsCenter;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;

Expand All @@ -17,16 +21,21 @@ public class ClearRecordCommand extends Command {

public static final String MESSAGE_SUCCESS = "Patient's record list has been cleared!";

private static final Logger logger = LogsCenter.getLogger(MainApp.class);

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);

if (!model.isRecordListDisplayed()) {
logger.warning("Patient List View is not currently being displayed.");
throw new CommandException(MESSAGE_RECORD_COMMAND_PREREQUISITE);
}

model.clearRecords();

logger.info("Records have been cleared.");

return new CommandResult(MESSAGE_SUCCESS,
false, false, true);
}
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/seedu/address/logic/commands/CommandUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
import static seedu.address.logic.commands.Command.MESSAGE_ADDRESS_BOOK_COMMAND_PREREQUISITE;

import java.util.List;
import java.util.logging.Logger;

import seedu.address.MainApp;
import seedu.address.commons.core.LogsCenter;
import seedu.address.commons.core.Messages;
import seedu.address.commons.core.index.Index;
import seedu.address.logic.commands.exceptions.CommandException;
Expand All @@ -15,6 +18,9 @@
* Contains utility methods for the Command classes.
*/
public class CommandUtil {

private static final Logger logger = LogsCenter.getLogger(MainApp.class);

/**
* Prepares the model for index based commands.
*
Expand All @@ -26,13 +32,15 @@ public static List<Person> prepareFilteredList(Model model, Index index) throws
requireNonNull(model);

if (model.isRecordListDisplayed()) {
logger.warning("Patient List View is not currently being displayed.");
throw new CommandException(MESSAGE_ADDRESS_BOOK_COMMAND_PREREQUISITE);
}

List<Person> lastShownList = model.getFilteredPersonList();

// Check if index given is out of bounds
if (index.getZeroBased() >= lastShownList.size()) {
logger.warning("Patient index specified is invalid.");
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/seedu/address/model/person/Appointment.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public static boolean isValidDateFormat(String testDate) {
try {
LocalDateTime.parse(testDate, DATE_FORMAT);
} catch (DateTimeParseException e) {

return false;
}
return true;
Expand All @@ -59,8 +60,8 @@ public static boolean isValidDateFormat(String testDate) {
/**
* Returns true if the date is in the past.
*
* @param testDate Date to be tested
* @return
* @param testDate Date to be tested.
* @return True if date is passed.
*/
public static boolean isFutureDate(String testDate) {
LocalDateTime currDate = LocalDateTime.now();
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/seedu/address/model/record/Record.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Logger;

import seedu.address.MainApp;
import seedu.address.commons.core.LogsCenter;

/**
* Represents a single record in the record list of a Person object.
Expand All @@ -22,6 +26,9 @@ public class Record implements Comparable<Record> {
public static final String MESSAGE_INVALID_DATE_FORMAT = "Record dates have to be of format dd-MM-yyyy HHmm!"
+ "Please also ensure this is a valid date!";
public static final String MESSAGE_FUTURE_DATE = "Record dates must not be later than the current date!";

private static final Logger logger = LogsCenter.getLogger(MainApp.class);

/* Data Fields */
public final String record;
private final LocalDateTime recordDate;
Expand All @@ -39,6 +46,7 @@ public Record(LocalDateTime recordDate, String record, Set<Medication> meds) {
this.recordDate = recordDate;
this.record = record;
this.medications = meds;
logger.info("Record created with record date: " + this.recordDate + " with record " + this.record);
}

/**
Expand Down Expand Up @@ -66,6 +74,7 @@ public static boolean isValidDateFormat(String testDate) {
try {
LocalDateTime.parse(testDate, DATE_FORMAT);
} catch (DateTimeParseException e) {
logger.warning("Record date is invalid");
return false;
}
return true;
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/seedu/address/model/record/RecordList.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@
import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;

import java.util.logging.Logger;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import seedu.address.MainApp;
import seedu.address.commons.core.LogsCenter;
import seedu.address.model.person.exceptions.DuplicateRecordException;
import seedu.address.model.person.exceptions.RecordNotFoundException;

/**
* Represents a record list in the address book.
*/
public class RecordList {
private static final Logger logger = LogsCenter.getLogger(MainApp.class);
/* Record List variables */
// count of the record list should be accessed through the ArrayList#size() method.
private final ObservableList<Record> recordList = FXCollections.observableArrayList();
Expand All @@ -21,11 +26,13 @@ public class RecordList {
/**
* Adds a record to the RecordList.
* The person must not already exist in the list.
*
* @param toAdd Record to add.
*/
public void add(Record toAdd) {
requireNonNull(toAdd);
if (contains(toAdd)) {
logger.warning("Duplicate record detected.");
throw new DuplicateRecordException();
}
recordList.add(toAdd);
Expand Down Expand Up @@ -74,10 +81,12 @@ public void set(Record target, Record editedRecord) {

int index = recordList.indexOf(target);
if (index == -1) {
logger.warning("Index input is negative and invalid.");
throw new RecordNotFoundException();
}

if (!target.isSameRecord(editedRecord) && this.contains(editedRecord)) {
logger.warning("Record is duplicate.");
throw new DuplicateRecordException();
}

Expand All @@ -103,6 +112,7 @@ public boolean equals(Object other) {
public void delete(Record record) {
requireNonNull(record);
if (!recordList.remove(record)) {
logger.warning("Record not found");
throw new RecordNotFoundException();
}
recordList.remove(record);
Expand Down

0 comments on commit 9a3b6f7

Please sign in to comment.