forked from se-edu/addressbook-level25
-
Notifications
You must be signed in to change notification settings - Fork 249
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
[T6A3][T14-C2] Chen Rui Wen #852
Open
ruiwen905
wants to merge
2
commits into
nus-cs2103-AY1617S1:master
Choose a base branch
from
ruiwen905:T6A3
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package seedu.addressbook.logic; | ||
|
||
import seedu.addressbook.commands.Command; | ||
import seedu.addressbook.commands.CommandResult; | ||
import seedu.addressbook.data.AddressBook; | ||
import seedu.addressbook.data.person.ReadOnlyPerson; | ||
import seedu.addressbook.parser.Parser; | ||
import seedu.addressbook.storage.StorageFile; | ||
import seedu.addressbook.storage.Storage; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Represents the main Logic of the AddressBook. | ||
*/ | ||
public class Logic { | ||
|
||
|
||
private Storage storage; | ||
private AddressBook addressBook; | ||
|
||
/** The list of person shown to the user most recently. */ | ||
private List<? extends ReadOnlyPerson> lastShownList = Collections.emptyList(); | ||
|
||
public Logic() throws Exception{ | ||
setStorage(initializeStorage()); | ||
setAddressBook(storage.load()); | ||
} | ||
|
||
Logic(Storage saveFile, AddressBook addressBook){ | ||
setStorage(saveFile); | ||
setAddressBook(addressBook); | ||
} | ||
|
||
void setStorage(Storage saveFile){ | ||
this.storage = saveFile; | ||
} | ||
|
||
void setAddressBook(AddressBook addressBook){ | ||
this.addressBook = addressBook; | ||
} | ||
|
||
/** | ||
* Creates the StorageFile object based on the user specified path (if any) or the default storage path. | ||
* @throws StorageFile.InvalidStorageFilePathException if the target file path is incorrect. | ||
*/ | ||
private StorageFile initializeStorage() throws StorageFile.InvalidStorageFilePathException { | ||
return new StorageFile(); | ||
} | ||
|
||
public String getStorageFilePath() { | ||
return storage.getPath(); | ||
} | ||
|
||
/** | ||
* Unmodifiable view of the current last shown list. | ||
*/ | ||
public List<ReadOnlyPerson> getLastShownList() { | ||
return Collections.unmodifiableList(lastShownList); | ||
} | ||
|
||
protected void setLastShownList(List<? extends ReadOnlyPerson> newList) { | ||
lastShownList = newList; | ||
} | ||
|
||
/** | ||
* Parses the user command, executes it, and returns the result. | ||
* @throws Exception if there was any problem during command execution. | ||
*/ | ||
public CommandResult execute(String userCommandText) throws Exception { | ||
Command command = new Parser().parseCommand(userCommandText); | ||
CommandResult result = execute(command); | ||
recordResult(result); | ||
return result; | ||
} | ||
|
||
/** | ||
* Executes the command, updates storage, and returns the result. | ||
* | ||
* @param command user command | ||
* @return result of the command | ||
* @throws Exception if there was any problem during command execution. | ||
*/ | ||
private CommandResult execute(Command command) throws Exception { | ||
command.setData(addressBook, lastShownList); | ||
CommandResult result = command.execute(); | ||
storage.save(addressBook); | ||
return result; | ||
} | ||
|
||
/** Updates the {@link #lastShownList} if the result contains a list of Persons. */ | ||
private void recordResult(CommandResult result) { | ||
final Optional<List<? extends ReadOnlyPerson>> personList = result.getRelevantPersons(); | ||
if (personList.isPresent()) { | ||
lastShownList = personList.get(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package seedu.addressbook.storage; | ||
|
||
import seedu.addressbook.data.AddressBook; | ||
import seedu.addressbook.storage.StorageFile.StorageOperationException; | ||
|
||
/* abstraction for Logic and StorageFile | ||
* to reduce dependency | ||
* */ | ||
|
||
public abstract class Storage { | ||
/** Default file path used if the user doesn't provide the file name. */ | ||
public static final String DEFAULT_STORAGE_FILEPATH = "addressbook.txt"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't seem to help if you create a class for cloud storage. Only keep requisite members and methods in abstract class. |
||
|
||
public abstract void save(AddressBook addressBook) throws StorageOperationException; | ||
public abstract AddressBook load() throws StorageOperationException; | ||
public abstract String getPath(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
package seedu.addressbook.storage; | ||
|
||
import seedu.addressbook.data.AddressBook; | ||
import seedu.addressbook.data.exception.IllegalValueException; | ||
import seedu.addressbook.storage.jaxb.AdaptedAddressBook; | ||
|
||
import javax.xml.bind.JAXBContext; | ||
import javax.xml.bind.JAXBException; | ||
import javax.xml.bind.Marshaller; | ||
import javax.xml.bind.Unmarshaller; | ||
import java.io.*; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
||
/** | ||
* Represents the file used to store address book data. | ||
*/ | ||
public class StorageFile extends Storage { | ||
|
||
/** Default file path used if the user doesn't provide the file name. */ | ||
public static final String DEFAULT_STORAGE_FILEPATH = "addressbook.txt"; | ||
|
||
/* Note: Note the use of nested classes below. | ||
* More info https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html | ||
*/ | ||
|
||
/** | ||
* Signals that the given file path does not fulfill the storage filepath constraints. | ||
*/ | ||
public static class InvalidStorageFilePathException extends IllegalValueException { | ||
public InvalidStorageFilePathException(String message) { | ||
super(message); | ||
} | ||
} | ||
|
||
/** | ||
* Signals that some error has occured while trying to convert and read/write data between the application | ||
* and the storage file. | ||
*/ | ||
public static class StorageOperationException extends Exception { | ||
public StorageOperationException(String message) { | ||
super(message); | ||
} | ||
} | ||
|
||
private final JAXBContext jaxbContext; | ||
|
||
public final Path path; | ||
|
||
/** | ||
* @throws InvalidStorageFilePathException if the default path is invalid | ||
*/ | ||
public StorageFile() throws InvalidStorageFilePathException { | ||
this(DEFAULT_STORAGE_FILEPATH); | ||
} | ||
|
||
/** | ||
* @throws InvalidStorageFilePathException if the given file path is invalid | ||
*/ | ||
public StorageFile(String filePath) throws InvalidStorageFilePathException { | ||
try { | ||
jaxbContext = JAXBContext.newInstance(AdaptedAddressBook.class); | ||
} catch (JAXBException jaxbe) { | ||
throw new RuntimeException("jaxb initialisation error"); | ||
} | ||
|
||
path = Paths.get(filePath); | ||
if (!isValidPath(path)) { | ||
throw new InvalidStorageFilePathException("Storage file should end with '.txt'"); | ||
} | ||
} | ||
|
||
/** | ||
* Returns true if the given path is acceptable as a storage file. | ||
* The file path is considered acceptable if it ends with '.txt' | ||
*/ | ||
private static boolean isValidPath(Path filePath) { | ||
return filePath.toString().endsWith(".txt"); | ||
} | ||
|
||
/** | ||
* Saves all data to this storage file. | ||
* | ||
* @throws StorageOperationException if there were errors converting and/or storing data to file. | ||
*/ | ||
public void save(AddressBook addressBook) throws StorageOperationException { | ||
|
||
/* Note: Note the 'try with resource' statement below. | ||
* More info: https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html | ||
*/ | ||
try (final Writer fileWriter = | ||
new BufferedWriter(new FileWriter(path.toFile()))) { | ||
|
||
final AdaptedAddressBook toSave = new AdaptedAddressBook(addressBook); | ||
final Marshaller marshaller = jaxbContext.createMarshaller(); | ||
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); | ||
marshaller.marshal(toSave, fileWriter); | ||
|
||
} catch (IOException ioe) { | ||
throw new StorageOperationException("Error writing to file: " + path + " error: " + ioe.getMessage()); | ||
} catch (JAXBException jaxbe) { | ||
throw new StorageOperationException("Error converting address book into storage format"); | ||
} | ||
} | ||
|
||
/** | ||
* Loads data from this storage file. | ||
* | ||
* @throws StorageOperationException if there were errors reading and/or converting data from file. | ||
*/ | ||
public AddressBook load() throws StorageOperationException { | ||
try (final Reader fileReader = | ||
new BufferedReader(new FileReader(path.toFile()))) { | ||
|
||
final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); | ||
final AdaptedAddressBook loaded = (AdaptedAddressBook) unmarshaller.unmarshal(fileReader); | ||
// manual check for missing elements | ||
if (loaded.isAnyRequiredFieldMissing()) { | ||
throw new StorageOperationException("File data missing some elements"); | ||
} | ||
return loaded.toModelType(); | ||
|
||
/* Note: Here, we are using an exception to create the file if it is missing. However, we should minimize | ||
* using exceptions to facilitate normal paths of execution. If we consider the missing file as a 'normal' | ||
* situation (i.e. not truly exceptional) we should not use an exception to handle it. | ||
*/ | ||
|
||
// create empty file if not found | ||
} catch (FileNotFoundException fnfe) { | ||
final AddressBook empty = new AddressBook(); | ||
save(empty); | ||
return empty; | ||
|
||
// other errors | ||
} catch (IOException ioe) { | ||
throw new StorageOperationException("Error writing to file: " + path); | ||
} catch (JAXBException jaxbe) { | ||
throw new StorageOperationException("Error parsing file data format"); | ||
} catch (IllegalValueException ive) { | ||
throw new StorageOperationException("File contains illegal data values; data type constraints not met"); | ||
} | ||
} | ||
|
||
public String getPath() { | ||
return path.toString(); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Format error for header comments. All classes, interfaces, and non-trivial methods should have javadoc format header comments.