|
| 1 | +package seedu.address.logic.commands; |
| 2 | + |
| 3 | +import static java.util.Objects.requireNonNull; |
| 4 | +import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS; |
| 5 | + |
| 6 | +import java.io.IOException; |
| 7 | +import java.util.HashSet; |
| 8 | +import java.util.Optional; |
| 9 | +import java.util.logging.Logger; |
| 10 | + |
| 11 | +import seedu.address.MainApp; |
| 12 | +import seedu.address.commons.core.LogsCenter; |
| 13 | +import seedu.address.commons.exceptions.DataConversionException; |
| 14 | +import seedu.address.logic.CommandHistory; |
| 15 | +import seedu.address.model.AddressBook; |
| 16 | +import seedu.address.model.Model; |
| 17 | +import seedu.address.model.ReadOnlyAddressBook; |
| 18 | +import seedu.address.model.util.SampleDataUtil; |
| 19 | +import seedu.address.storage.AddressBookStorage; |
| 20 | +import seedu.address.storage.JsonAddressBookStorage; |
| 21 | +import seedu.address.storage.ParsedInOut; |
| 22 | +import seedu.address.storage.StorageManager; |
| 23 | + |
| 24 | +/** |
| 25 | + * Imports records to a text file. |
| 26 | + */ |
| 27 | +public class ImportCommand extends Command { |
| 28 | + |
| 29 | + public static final String COMMAND_WORD = "import"; |
| 30 | + |
| 31 | + public static final String MESSAGE_USAGE = COMMAND_WORD |
| 32 | + + ": Imports specific patients by index to text file in the \"data\" folder, " |
| 33 | + + "overwriting if filename exists \n" |
| 34 | + + "Parameters: FILENAME INDEX_RANGE(must be a positive integer)\n" |
| 35 | + + "Example: " + COMMAND_WORD + " records1.json + 1-5" |
| 36 | + + "Example: " + COMMAND_WORD + " records1.json + 1,3,5"; |
| 37 | + |
| 38 | + public static final String MESSAGE_SUCCESS = "Imported the records!"; |
| 39 | + |
| 40 | + private final ParsedInOut parsedInput; |
| 41 | + |
| 42 | + public ImportCommand(ParsedInOut parsedInput) { |
| 43 | + this.parsedInput = parsedInput; |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public CommandResult execute(Model model, CommandHistory history) { |
| 48 | + requireNonNull(model); |
| 49 | + readFile(model, parsedInput.getParsedIndex()); |
| 50 | + model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS); |
| 51 | + model.commitAddressBook(); |
| 52 | + return new CommandResult(MESSAGE_SUCCESS); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * readFile() appends the current address book with the contents of the file. |
| 57 | + */ |
| 58 | + private void readFile(Model model, HashSet<Integer> parsedIndex) { |
| 59 | + AddressBookStorage importStorage = new JsonAddressBookStorage(parsedInput.getFile().toPath()); |
| 60 | + |
| 61 | + StorageManager importStorageManager = new StorageManager(importStorage, null); |
| 62 | + |
| 63 | + final Logger logger = LogsCenter.getLogger(MainApp.class); |
| 64 | + |
| 65 | + Optional<ReadOnlyAddressBook> importOptional; |
| 66 | + ReadOnlyAddressBook importData; |
| 67 | + |
| 68 | + try { |
| 69 | + importOptional = importStorageManager.readAddressBook(); |
| 70 | + if (!importOptional.isPresent()) { |
| 71 | + logger.info("Data file not found. Will be starting with a sample AddressBook"); |
| 72 | + } |
| 73 | + importData = importOptional.orElseGet(SampleDataUtil::getSampleAddressBook); |
| 74 | + } catch (DataConversionException e) { |
| 75 | + logger.warning("Data file not in the correct format."); |
| 76 | + importData = new AddressBook(); |
| 77 | + } catch (IOException e) { |
| 78 | + logger.warning("Problem while reading from the file."); |
| 79 | + importData = new AddressBook(); |
| 80 | + } |
| 81 | + |
| 82 | + for (int i = 0; i < importData.getPersonList().size(); i++) { |
| 83 | + if (parsedIndex.contains(i) && !model.hasPerson(importData.getPersonList().get(i))) { |
| 84 | + model.addPerson(importData.getPersonList().get(i)); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments