Skip to content

Commit

Permalink
Added Storage logic for schedule, modified storage logic for Person t…
Browse files Browse the repository at this point in the history
…o accomodate person ID and existence.

TODO: Add/modify unit tests.
  • Loading branch information
shuiherng committed Oct 14, 2018
1 parent 7fe11a0 commit 2f4096c
Show file tree
Hide file tree
Showing 26 changed files with 649 additions and 85 deletions.
51 changes: 39 additions & 12 deletions src/main/java/seedu/address/MainApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.IOException;
import java.nio.file.Path;
import java.text.ParseException;
import java.util.Optional;
import java.util.logging.Logger;

Expand All @@ -23,12 +24,7 @@
import seedu.address.model.*;
import seedu.address.model.AddressBookModelManager;
import seedu.address.model.util.SampleDataUtil;
import seedu.address.storage.AddressBookStorage;
import seedu.address.storage.JsonUserPrefsStorage;
import seedu.address.storage.Storage;
import seedu.address.storage.StorageManager;
import seedu.address.storage.UserPrefsStorage;
import seedu.address.storage.XmlAddressBookStorage;
import seedu.address.storage.*;
import seedu.address.ui.Ui;
import seedu.address.ui.UiManager;

Expand All @@ -45,13 +41,14 @@ public class MainApp extends Application {
protected Logic logic;
protected Storage storage;
protected AddressBookModel addressBookModel;
protected ScheduleModel scheduleModel;
protected Config config;
protected UserPrefs userPrefs;


@Override
public void init() throws Exception {
logger.info("=============================[ Initializing AddressBook ]===========================");
logger.info("=============================[ Initializing PatientBook ]===========================");
super.init();

AppParameters appParameters = AppParameters.parse(getParameters());
Expand All @@ -60,18 +57,48 @@ public void init() throws Exception {
UserPrefsStorage userPrefsStorage = new JsonUserPrefsStorage(config.getUserPrefsFilePath());
userPrefs = initPrefs(userPrefsStorage);
AddressBookStorage addressBookStorage = new XmlAddressBookStorage(userPrefs.getAddressBookFilePath());
storage = new StorageManager(addressBookStorage, userPrefsStorage);
ScheduleStorage scheduleStorage = new XmlScheduleStorage(userPrefs.getScheduleFilePath());
storage = new StorageManager(addressBookStorage, scheduleStorage, userPrefsStorage);

initLogging(config);

addressBookModel = initModelManager(storage, userPrefs);
scheduleModel = initScheduleModel(storage, userPrefs);

logic = new LogicManager(addressBookModel);
logic = new LogicManager(addressBookModel, scheduleModel);

ui = new UiManager(logic, config, userPrefs);

initEventsCenter();
}
/**
* Returns a {@code ScheduleModelManager} with the data from [@code storage}'s scheduel and {@code userPrefs}. <br>
* The data fromthe sample schedule will be used instead if {@code storage}'s schedule is not found,
* or an empty schedule will be used instead if errors occur when reading {@code storage}'s schedule.
*/
private ScheduleModel initScheduleModel(Storage storage, UserPrefs userPrefs){
Optional<ReadOnlySchedule> scheduleOptional;
ReadOnlySchedule initialData;

try {
scheduleOptional = storage.readSchedule();
if (!scheduleOptional.isPresent()) {
logger.info("Data file not found. Will be staring with sample Schedule.");
}
initialData = scheduleOptional.orElseGet(SampleDataUtil::getSampleSchedule);
} catch (DataConversionException e) {
logger.warning("Data file not in the correct format. Will be starting with an empty Schedule.");
initialData = new Schedule();
} catch (IOException e) {
logger.warning("Error reading from file. Will be starting with an empty schedule.");
initialData = new Schedule();
} catch (ParseException e) {
logger.warning("Error parsing dates from file. Will be starting with an empty schedule.");
initialData = new Schedule();
}

return new ScheduleModelManager(initialData, userPrefs);
}

/**
* Returns a {@code AddressBookModelManager} with the data from {@code storage}'s address book and {@code userPrefs}. <br>
Expand All @@ -84,11 +111,11 @@ private AddressBookModel initModelManager(Storage storage, UserPrefs userPrefs)
try {
addressBookOptional = storage.readAddressBook();
if (!addressBookOptional.isPresent()) {
logger.info("Data file not found. Will be starting with a sample AddressBook");
logger.info("Data file not found. Will be starting with a sample AddressBook.");
}
initialData = addressBookOptional.orElseGet(SampleDataUtil::getSampleAddressBook);
} catch (DataConversionException e) {
logger.warning("Data file not in the correct format. Will be starting with an empty AddressBook");
logger.warning("Data file not in the correct format. Will be starting with an empty AddressBook.");
initialData = new AddressBook();
} catch (IOException e) {
logger.warning("Problem while reading from the file. Will be starting with an empty AddressBook");
Expand Down Expand Up @@ -176,7 +203,7 @@ private void initEventsCenter() {

@Override
public void start(Stage primaryStage) {
logger.info("Starting AddressBook " + MainApp.VERSION);
logger.info("Starting PatientBook " + MainApp.VERSION);
ui.start(primaryStage);
}

Expand Down
13 changes: 8 additions & 5 deletions src/main/java/seedu/address/logic/LogicManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.logic.parser.AddressBookParser;
import seedu.address.logic.parser.PatientBookParser;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.AddressBookModel;
import seedu.address.model.ScheduleModel;
import seedu.address.model.person.Person;

/**
Expand All @@ -20,20 +21,22 @@ public class LogicManager extends ComponentManager implements Logic {
private final Logger logger = LogsCenter.getLogger(LogicManager.class);

private final AddressBookModel addressBookModel;
private final ScheduleModel scheduleModel;
private final CommandHistory history;
private final AddressBookParser addressBookParser;
private final PatientBookParser patientBookParser;

public LogicManager(AddressBookModel addressBookModel) {
public LogicManager(AddressBookModel addressBookModel, ScheduleModel scheduleModel) {
this.addressBookModel = addressBookModel;
this.scheduleModel = scheduleModel;
history = new CommandHistory();
addressBookParser = new AddressBookParser();
patientBookParser = new PatientBookParser();
}

@Override
public CommandResult execute(String commandText) throws CommandException, ParseException {
logger.info("----------------[USER COMMAND][" + commandText + "]");
try {
Command command = addressBookParser.parseCommand(commandText);
Command command = patientBookParser.parseCommand(commandText);
return command.execute(addressBookModel, history);
} finally {
history.add(commandText);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
/**
* Parses user input.
*/
public class AddressBookParser {
public class PatientBookParser {

/**
* Used for initial separation of command word and args.
Expand Down
14 changes: 0 additions & 14 deletions src/main/java/seedu/address/model/Model.java

This file was deleted.

15 changes: 11 additions & 4 deletions src/main/java/seedu/address/model/UserPrefs.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
public class UserPrefs {

private GuiSettings guiSettings;
private Path addressBookFilePath = Paths.get("data" , "addressbook.xml");
private Path addressBookFilePath = Paths.get("addressBookData" , "addressbook.xml");
private Path scheduleFilePath = Paths.get("scheduleData", "schedule.xml");

public UserPrefs() {
setGuiSettings(500, 500, 0, 0);
Expand All @@ -38,6 +39,10 @@ public void setAddressBookFilePath(Path addressBookFilePath) {
this.addressBookFilePath = addressBookFilePath;
}

public Path getScheduleFilePath() { return scheduleFilePath; }

public void setScheduleFilePath(Path scheduleFilePath) { this.scheduleFilePath = scheduleFilePath; }

@Override
public boolean equals(Object other) {
if (other == this) {
Expand All @@ -50,19 +55,21 @@ public boolean equals(Object other) {
UserPrefs o = (UserPrefs) other;

return Objects.equals(guiSettings, o.guiSettings)
&& Objects.equals(addressBookFilePath, o.addressBookFilePath);
&& Objects.equals(addressBookFilePath, o.addressBookFilePath)
&& Objects.equals(scheduleFilePath, o.scheduleFilePath);
}

@Override
public int hashCode() {
return Objects.hash(guiSettings, addressBookFilePath);
return Objects.hash(guiSettings, addressBookFilePath, scheduleFilePath);
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Gui Settings : " + guiSettings.toString());
sb.append("\nLocal data file location : " + addressBookFilePath);
sb.append("\nLocal address book data file location : " + addressBookFilePath);
sb.append("\nLocal schedule data file location : " + scheduleFilePath);
return sb.toString();
}

Expand Down
20 changes: 19 additions & 1 deletion src/main/java/seedu/address/model/event/EventID.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package seedu.address.model.event;

import static java.util.Objects.requireNonNull;

/**
* Represents a EventID for a ScheduleEvent in the calendar for patient book.
* EventID only increases, and each event is bound to its EventID forever.
*/
public class EventID {

public static final String MESSAGE_EVENTID_CONSTRAINTS =
"Event ID should start with e, followed by a sequence of integers.";

// EventID iterator
private static int idCounter = 0;

Expand All @@ -17,8 +22,21 @@ public class EventID {
*/
public EventID() { value = getNewID(); }

/**
* Constructs an {@code EventID} based on an existing ID.
*/
public EventID(String value) {
requireNonNull(value);

this.value = value;
int idValue = Integer.parseInt(value.substring(1, value.length()-1));
if (idValue >= idCounter) {
idCounter = idValue + 1;
}
}

private static String getNewID() {
String id = new String("e"+Integer.toString(idCounter));
String id = "e"+Integer.toString(idCounter);
idCounter += 1;
return id;
}
Expand Down
22 changes: 17 additions & 5 deletions src/main/java/seedu/address/model/event/ScheduleEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Objects;
import java.util.Set;
import javafx.util.Pair;
Expand All @@ -19,6 +21,13 @@
*/
public class ScheduleEvent {

// Standard datetime String format to be used by this application
public static final SimpleDateFormat sdf;

static {
sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy", Locale.ENGLISH);
}

// Enumerated Variable to represent calendar event attributes
private enum ScheduleEventProperty {
DATETIME, PERSONID, DETAILS, TAGS}
Expand All @@ -29,12 +38,15 @@ private enum ScheduleEventProperty {
private final EventID id;

/**
*
* Every field must be present and not null.
*/
public ScheduleEvent(Pair<Calendar, Calendar> date, PersonID personID, String details, Set<Tag> tags) {
requireAllNonNull(date, personID, details, tags);
this.id = new EventID();
this(new EventID(), date, personID, details, tags);
}

public ScheduleEvent(EventID eventID, Pair<Calendar, Calendar> date, PersonID personID, String details, Set<Tag> tags) {
requireAllNonNull(eventID, date, personID, details, tags);
this.id = eventID;
this.attributes = new HashMap<>();

Pair<Calendar, Calendar> scheduleEventDate = new Pair<>(date.getKey(), date.getValue());
Expand Down Expand Up @@ -108,9 +120,9 @@ public String toString() {
builder.append("ScheduleEvent (Appointment) for PersonID: ")
.append(getID())
.append(" for daterange ")
.append(getDate().getKey())
.append(sdf.format(getDate().getKey().getTime()))
.append(" to ")
.append(getDate().getValue())
.append(sdf.format(getDate().getValue().getTime()))
.append("\nDetails: ")
.append(getDetails())
.append("\nTags: ");
Expand Down
16 changes: 7 additions & 9 deletions src/main/java/seedu/address/model/person/Person.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ private enum PersonProperty {NAME, PHONE, EMAIL, ADDRESS, TAGS}
*
* Every field must be present and not null.
*/
public Person(Name name, Phone phone, Email email, Address address, Set<Tag> tags) {
requireAllNonNull(name, phone, email, address, tags);

public Person(PersonID personID, Name name, Phone phone, Email email, Address address, boolean exists, Set<Tag> tags) {
requireAllNonNull(personID, name, phone, email, address, tags);
this.id = new PersonID();
this.exists = true;
this.attributes = new HashMap<>();
Expand All @@ -51,16 +52,13 @@ public Person(Name name, Phone phone, Email email, Address address, Set<Tag> tag

Set<Tag> personTags = new HashSet<>(tags); // adds all tags into here
this.attributes.put(PersonProperty.TAGS, personTags);
}

/*
this.name = name;
this.phone = phone;
this.email = email;
this.address = address;
this.tags.addAll(tags);
*/
public Person(Name name, Phone phone, Email email, Address address, Set<Tag> tags) {
this(new PersonID(), name, phone, email, address, true, tags);
}


public PersonID getID() {
return this.id;
}
Expand Down
19 changes: 18 additions & 1 deletion src/main/java/seedu/address/model/person/PersonID.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package seedu.address.model.person;

import static java.util.Objects.requireNonNull;
/**
* Represents a Person's PersonID in the address book.
* PersonID only increases, and a person is bound to their PersonID forever.
*/
public class PersonID {

public static final String MESSAGE_PERSONID_CONSTRAINTS =
"Person ID should start with p, followed by a sequence of integers.";

// PersonID iterator
private static int idCounter = 0;

Expand All @@ -19,8 +23,21 @@ public PersonID() {
value = getNewID();
}

/**
* Constructs a {@code PersonID} from an existing ID.
*/
public PersonID(String value) {
requireNonNull(value);

this.value = value;
int idValue = Integer.parseInt(value.substring(1, value.length()-1));
if (idValue >= idCounter) {
idCounter = idValue + 1;
}
}

private static String getNewID(){
String id = new String("p"+Integer.toString(idCounter));
String id = "p"+Integer.toString(idCounter);
idCounter += 1;
return id;
}
Expand Down
Loading

0 comments on commit 2f4096c

Please sign in to comment.