Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed errors in Storage #3

Merged
merged 3 commits into from
Oct 5, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/main/java/seedu/ggist/storage/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
/**
* API of the Storage component
*/
public interface Storage extends AddressBookStorage, UserPrefsStorage {
public interface Storage extends TaskManagerStorage, UserPrefsStorage {

@Override
Optional<UserPrefs> readUserPrefs() throws DataConversionException, IOException;
Expand All @@ -22,18 +22,18 @@ public interface Storage extends AddressBookStorage, UserPrefsStorage {
void saveUserPrefs(UserPrefs userPrefs) throws IOException;

@Override
String getAddressBookFilePath();
String getTaskManagerFilePath();

@Override
Optional<ReadOnlyTaskManager> readAddressBook() throws DataConversionException, IOException;
Optional<ReadOnlyTaskManager> readTaskManager() throws DataConversionException, IOException;

@Override
void saveAddressBook(ReadOnlyTaskManager addressBook) throws IOException;
void saveTaskManager(ReadOnlyTaskManager taskManager) throws IOException;

/**
* Saves the current version of the Address Book to the hard disk.
* Saves the current version of the Task Manaer to the hard disk.
* Creates the data file if it is missing.
* Raises {@link DataSavingExceptionEvent} if there was an error during saving.
*/
void handleAddressBookChangedEvent(TaskManagerChangedEvent abce);
void handleTaskManagerChangedEvent(TaskManagerChangedEvent abce);
}
40 changes: 23 additions & 17 deletions src/main/java/seedu/ggist/storage/StorageManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,18 @@
public class StorageManager extends ComponentManager implements Storage {

private static final Logger logger = LogsCenter.getLogger(StorageManager.class);
private AddressBookStorage addressBookStorage;
private TaskManagerStorage taskManagerStorage;
private UserPrefsStorage userPrefsStorage;


public StorageManager(AddressBookStorage addressBookStorage, UserPrefsStorage userPrefsStorage) {
public StorageManager(TaskManagerStorage taskManagerStorage, UserPrefsStorage userPrefsStorage) {
super();
this.addressBookStorage = addressBookStorage;
this.taskManagerStorage = taskManagerStorage;
this.userPrefsStorage = userPrefsStorage;
}

public StorageManager(String addressBookFilePath, String userPrefsFilePath) {
this(new XmlAddressBookStorage(addressBookFilePath), new JsonUserPrefsStorage(userPrefsFilePath));
public StorageManager(String taskManagerFilePath, String userPrefsFilePath) {
this(new XmlTaskManagerStorage(taskManagerFilePath), new JsonUserPrefsStorage(userPrefsFilePath));
}

// ================ UserPrefs methods ==============================
Expand All @@ -51,42 +51,48 @@ public void saveUserPrefs(UserPrefs userPrefs) throws IOException {
// ================ TaskManager methods ==============================

@Override
public String getAddressBookFilePath() {
return addressBookStorage.getAddressBookFilePath();
public String getTaskManagerFilePath() {
return taskManagerStorage.getTaskManagerFilePath();
}

@Override
public Optional<ReadOnlyTaskManager> readAddressBook() throws DataConversionException, IOException {
return readAddressBook(addressBookStorage.getAddressBookFilePath());
public Optional<ReadOnlyTaskManager> readTaskManager() throws DataConversionException, IOException {
return readTaskManager(taskManagerStorage.getTaskManagerFilePath());
}

@Override
public Optional<ReadOnlyTaskManager> readAddressBook(String filePath) throws DataConversionException, IOException {
public Optional<ReadOnlyTaskManager> readTaskManager(String filePath) throws DataConversionException, IOException {
logger.fine("Attempting to read data from file: " + filePath);
return addressBookStorage.readAddressBook(filePath);
return taskManagerStorage.readTaskManager(filePath);
}

@Override
public void saveAddressBook(ReadOnlyTaskManager addressBook) throws IOException {
saveAddressBook(addressBook, addressBookStorage.getAddressBookFilePath());
public void saveTaskManager(ReadOnlyTaskManager taskManager) throws IOException {
saveTaskManager(taskManager, taskManagerStorage.getTaskManagerFilePath());
}

@Override
public void saveAddressBook(ReadOnlyTaskManager addressBook, String filePath) throws IOException {
public void saveTaskManager(ReadOnlyTaskManager taskManager, String filePath) throws IOException {
logger.fine("Attempting to write to data file: " + filePath);
addressBookStorage.saveAddressBook(addressBook, filePath);
taskManagerStorage.saveTaskManager(taskManager, filePath);
}


@Override
@Subscribe
public void handleAddressBookChangedEvent(TaskManagerChangedEvent event) {
public void handleTaskManagerChangedEvent(TaskManagerChangedEvent event) {
logger.info(LogsCenter.getEventHandlingLogMessage(event, "Local data changed, saving to file"));
try {
saveAddressBook(event.data);
saveTaskManager(event.data);
} catch (IOException e) {
raise(new DataSavingExceptionEvent(e));
}
}

@Override
public void saveTaskManagerk(ReadOnlyTaskManager taskManager, String filePath) throws IOException {
// TODO Auto-generated method stub

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,38 @@
/**
* Represents a storage for {@link seedu.ggist.model.TaskManager}.
*/
public interface AddressBookStorage {
public interface TaskManagerStorage {

/**
* Returns the file path of the data file.
*/
String getAddressBookFilePath();
String getTaskManagerFilePath();

/**
* Returns TaskManager data as a {@link ReadOnlyTaskManager}.
* Returns {@code Optional.empty()} if storage file is not found.
* @throws DataConversionException if the data in storage is not in the expected format.
* @throws IOException if there was any problem when reading from the storage.
*/
Optional<ReadOnlyTaskManager> readAddressBook() throws DataConversionException, IOException;
Optional<ReadOnlyTaskManager> readTaskManager() throws DataConversionException, IOException;

/**
* @see #getAddressBookFilePath()
* @see #getTaskManagerFilePath()
*/
Optional<ReadOnlyTaskManager> readAddressBook(String filePath) throws DataConversionException, IOException;
Optional<ReadOnlyTaskManager> readTaskManager(String filePath) throws DataConversionException, IOException;

/**
* Saves the given {@link ReadOnlyTaskManager} to the storage.
* @param addressBook cannot be null.
* @param taskManager cannot be null.
* @throws IOException if there was any problem writing to the file.
*/
void saveAddressBook(ReadOnlyTaskManager addressBook) throws IOException;
void saveTaskManager(ReadOnlyTaskManager taskManager) throws IOException;

/**
* @see #saveAddressBook(ReadOnlyTaskManager)
* @see #saveTaskManager(ReadOnlyTaskManager)
*/
void saveAddressBook(ReadOnlyTaskManager addressBook, String filePath) throws IOException;
void saveTaskManagerk(ReadOnlyTaskManager taskManager, String filePath) throws IOException;

void saveTaskManager(ReadOnlyTaskManager taskManager, String filePath) throws IOException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
/**
* JAXB-friendly version of the Task.
*/
public class XmlAdaptedPerson {
public class XmlAdaptedTask {

@XmlElement(required = true)
private String name;
Expand All @@ -30,19 +30,21 @@ public class XmlAdaptedPerson {
/**
* No-arg constructor for JAXB use.
*/
public XmlAdaptedPerson() {}
public XmlAdaptedTask() {}


/**
* Converts a given Task into this class for JAXB use.
*
* @param source future changes to this will not affect the created XmlAdaptedPerson
* @param source future changes to this will not affect the created XmlAdaptedTask
*/
public XmlAdaptedPerson(ReadOnlyTask source) {
name = source.getName().fullName;
phone = source.getPhone().value;
email = source.getEmail().value;
address = source.getAddress().value;
public XmlAdaptedTask(ReadOnlyTask source) {
taskName = source.getTaskName().taskName;
date = source.getDate().value;
startTime = source.getStartTime().value;
endTime = source.getEndTime().value;
priority = source.getPriority().value;
frequency = source.getFrequency().value;
tagged = new ArrayList<>();
for (Tag tag : source.getTags()) {
tagged.add(new XmlAdaptedTag(tag));
Expand All @@ -55,15 +57,17 @@ public XmlAdaptedPerson(ReadOnlyTask source) {
* @throws IllegalValueException if there were any data constraints violated in the adapted person
*/
public Task toModelType() throws IllegalValueException {
final List<Tag> personTags = new ArrayList<>();
final List<Tag> taskTags = new ArrayList<>();
for (XmlAdaptedTag tag : tagged) {
personTags.add(tag.toModelType());
taskTags.add(tag.toModelType());
}
final Name name = new Name(this.name);
final Phone phone = new Phone(this.phone);
final Email email = new Email(this.email);
final Address address = new Address(this.address);
final UniqueTagList tags = new UniqueTagList(personTags);
return new Task(name, phone, email, address, tags);
final TaskName taskName = new TaskName(this.taskName);
final Date date = new Date(this.date);
final Time startTime = new Time(this.startTime);
final Time endTime = new Time(this.endTime);
final Priority priority = new Priority(this.priority);
final Frequency frequency = new Frequency(this.frequency);
final UniqueTagList tags = new UniqueTagList(taskTags);
return new Task(taskName, date, startTime, endTime, priority, frequency, tags);
}
}
6 changes: 3 additions & 3 deletions src/main/java/seedu/ggist/storage/XmlFileStorage.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class XmlFileStorage {
/**
* Saves the given addressbook data to the specified file.
*/
public static void saveDataToFile(File file, XmlSerializableAddressBook addressBook)
public static void saveDataToFile(File file, XmlSerializableTaskManager addressBook)
throws FileNotFoundException {
try {
XmlUtil.saveDataToFile(file, addressBook);
Expand All @@ -27,10 +27,10 @@ public static void saveDataToFile(File file, XmlSerializableAddressBook addressB
/**
* Returns address book in the file or an empty address book
*/
public static XmlSerializableAddressBook loadDataFromSaveFile(File file) throws DataConversionException,
public static XmlSerializableTaskManager loadDataFromSaveFile(File file) throws DataConversionException,
FileNotFoundException {
try {
return XmlUtil.getDataFromFile(file, XmlSerializableAddressBook.class);
return XmlUtil.getDataFromFile(file, XmlSerializableTaskManager.class);
} catch (JAXBException e) {
throw new DataConversionException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,28 @@
* An Immutable TaskManager that is serializable to XML format
*/
@XmlRootElement(name = "addressbook")
public class XmlSerializableAddressBook implements ReadOnlyTaskManager {
public class XmlSerializableTaskManager implements ReadOnlyTaskManager {

@XmlElement
private List<XmlAdaptedPerson> persons;
private List<XmlAdaptedTask> tasks;
@XmlElement
private List<Tag> tags;

{
persons = new ArrayList<>();
tasks = new ArrayList<>();
tags = new ArrayList<>();
}

/**
* Empty constructor required for marshalling
*/
public XmlSerializableAddressBook() {}
public XmlSerializableTaskManager() {}

/**
* Conversion
*/
public XmlSerializableAddressBook(ReadOnlyTaskManager src) {
persons.addAll(src.getPersonList().stream().map(XmlAdaptedPerson::new).collect(Collectors.toList()));
public XmlSerializableTaskManager(ReadOnlyTaskManager src) {
tasks.addAll(src.getTaskList().stream().map(XmlAdaptedTask::new).collect(Collectors.toList()));
tags = src.getTagList();
}

Expand All @@ -56,11 +56,11 @@ public UniqueTagList getUniqueTagList() {
}

@Override
public UniqueTaskList getUniquePersonList() {
public UniqueTaskList getUniqueTaskList() {
UniqueTaskList lists = new UniqueTaskList();
for (XmlAdaptedPerson p : persons) {
for (XmlAdaptedTask t : tasks) {
try {
lists.add(p.toModelType());
lists.add(t.toModelType());
} catch (IllegalValueException e) {
//TODO: better error handling
}
Expand All @@ -69,8 +69,8 @@ public UniqueTaskList getUniquePersonList() {
}

@Override
public List<ReadOnlyTask> getPersonList() {
return persons.stream().map(p -> {
public List<ReadOnlyTask> getTaskList() {
return tasks.stream().map(p -> {
try {
return p.toModelType();
} catch (IllegalValueException e) {
Expand Down
Loading