Skip to content

Commit

Permalink
Add commands to support importing job entries individually (nus-cs210…
Browse files Browse the repository at this point in the history
…3-AY1718S2#99)

* Process resources

* Fix ImportSessionTest

* checkstyle

* Update UML diagrams

* Add UML diagram for ImportSession closeSession()

* edit

* optimize code

* fix ImportSession test case

* edit test

* Move methods, fields and constants from ImportSession to SessionData.

* Add classes to support seesionData persistent data. WIP.

* Revert "Add classes to support seesionData persistent data. WIP."

This reverts commit a9f0eff.

* Add classes to support session data persistence. WIP.

* Revert "Add classes to support session data persistence. WIP."

This reverts commit bebb8ea.

* Add import and save commands.

* Add command to list jobs

* Add acceptAll, rejectAll and switch commmands

* Add accept and reject command.

* uncomment check in date

* Modify ModelStub

* Remove unused imports
  • Loading branch information
yuhongherald authored Apr 3, 2018
1 parent bdf26a5 commit 67299ac
Show file tree
Hide file tree
Showing 36 changed files with 954 additions and 191 deletions.
Binary file not shown.
2 changes: 1 addition & 1 deletion docs/DeveloperGuide.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ image::SessionComponentClassDiagram.png[width="800"]
with a timestamp added to the back of the filename.

`ImportSession` is designed to be stateless on its own, with all data stored in `SessionData`. This helps to
support `UndoableCommand`
support `UndoableCommand`. Currently, `SessionData` is not persistent in the event of a crash.

The flow of using `ImportSession` is as follows:

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/seedu/carvicim/MainApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import seedu.carvicim.storage.UserPrefsStorage;
import seedu.carvicim.storage.XmlArchiveJobStorage;
import seedu.carvicim.storage.XmlCarvicimStorage;
import seedu.carvicim.storage.session.ImportSession;
import seedu.carvicim.ui.Ui;
import seedu.carvicim.ui.UiManager;

Expand Down Expand Up @@ -211,6 +212,7 @@ public void stop() {
} catch (IOException e) {
logger.severe("Failed to save preferences " + StringUtil.getDetails(e));
}
ImportSession.getInstance().getSessionData().freeResources();
Platform.exit();
System.exit(0);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package seedu.carvicim.commons.events.ui;

import javafx.collections.ObservableList;
import seedu.carvicim.commons.events.BaseEvent;
import seedu.carvicim.model.job.Job;

/**
* Represents a selection change in the Employee List Panel
*/
//@@author yuhongherald
public class DisplayAllJobsEvent extends BaseEvent {


private final ObservableList<Job> jobList;

public DisplayAllJobsEvent(ObservableList<Job> jobList) {
this.jobList = jobList;
}

@Override
public String toString() {
return this.getClass().getSimpleName();
}

public ObservableList<Job> getJobList() {
return jobList;
}
}
56 changes: 56 additions & 0 deletions src/main/java/seedu/carvicim/logic/commands/AcceptAllCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package seedu.carvicim.logic.commands;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import seedu.carvicim.logic.commands.exceptions.CommandException;
import seedu.carvicim.model.job.Job;
import seedu.carvicim.storage.session.ImportSession;
import seedu.carvicim.storage.session.exceptions.DataIndexOutOfBoundsException;
import seedu.carvicim.storage.session.exceptions.UnitializedException;

//@@author yuhongherald

/**
* Accepts all remaining unreviewed job entries into Servicing Manager
*/
public class AcceptAllCommand extends UndoableCommand {

public static final String COMMAND_WORD = "acceptAll";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Accepts all unreviewed job entries. "
+ "Example: " + COMMAND_WORD;

public static final String MESSAGE_SUCCESS = "%d job entries accepted!";

public String getMessageSuccess(int entries) {
return String.format(MESSAGE_SUCCESS, entries);
}

@Override
public CommandResult executeUndoableCommand() throws CommandException {
ImportSession importSession = ImportSession.getInstance();
try {
importSession.reviewAllRemainingJobEntries(true);
List<Job> jobs = new ArrayList<>(importSession.getSessionData().getReviewedJobEntries());
model.addJobs(jobs);
importSession.closeSession();
return new CommandResult(getMessageSuccess(jobs.size()));
} catch (DataIndexOutOfBoundsException e) {
throw new CommandException("Excel file has bad format. Try copying the cell values into a new excel file "
+ "before trying again");
} catch (IOException e) {
throw new CommandException("Unable to export file. Please close the application and try again.");
} catch (UnitializedException e) {
throw new CommandException(e.getMessage());
}
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof AcceptAllCommand); // instanceof handles nulls
}

}
65 changes: 65 additions & 0 deletions src/main/java/seedu/carvicim/logic/commands/AcceptCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package seedu.carvicim.logic.commands;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import seedu.carvicim.commons.core.EventsCenter;
import seedu.carvicim.commons.events.ui.DisplayAllJobsEvent;
import seedu.carvicim.logic.commands.exceptions.CommandException;
import seedu.carvicim.model.job.Job;
import seedu.carvicim.storage.session.ImportSession;
import seedu.carvicim.storage.session.exceptions.DataIndexOutOfBoundsException;
import seedu.carvicim.storage.session.exceptions.InvalidDataException;

//@@author yuhongherald

/**
* Accepts an unreviewed job entry using job number and adds into servicing manager
*/
public class AcceptCommand extends UndoableCommand {

public static final String COMMAND_WORD = "accept";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Accepts job entry using job number. "
+ "Example: " + COMMAND_WORD + " JOB_NUMBER";

public static final String MESSAGE_SUCCESS = "Job #%d accepted!";

private int jobNumber;

public AcceptCommand(int jobNumber) {
this.jobNumber = jobNumber;
}

public String getMessageSuccess() {
return String.format(MESSAGE_SUCCESS, jobNumber);
}

@Override
public CommandResult executeUndoableCommand() throws CommandException {
ImportSession importSession = ImportSession.getInstance();
try {
importSession.getSessionData().reviewJobEntryUsingJobNumber(jobNumber, true, "");

} catch (DataIndexOutOfBoundsException e) {
throw new CommandException("Excel file has bad format. Try copying the cell values into a new excel file "
+ "before trying again");
} catch (InvalidDataException e) {
throw new CommandException(e.getMessage());
}
ObservableList<Job> jobList = FXCollections.observableList(
ImportSession.getInstance().getSessionData().getUnreviewedJobEntries());
if (!model.isViewingImportedJobs()) {
model.switchJobView();
}
EventsCenter.getInstance().post(
new DisplayAllJobsEvent(FXCollections.unmodifiableObservableList(jobList)));
return new CommandResult(getMessageSuccess());
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof AcceptCommand); // instanceof handles nulls
}

}
19 changes: 15 additions & 4 deletions src/main/java/seedu/carvicim/logic/commands/CommandWords.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,15 @@ public class CommandWords implements Serializable {
SetCommand.COMMAND_WORD,
UndoCommand.COMMAND_WORD,
ThemeCommand.COMMAND_WORD,
SortCommand.COMMAND_WORD
SortCommand.COMMAND_WORD,
ImportCommand.COMMAND_WORD,
SaveCommand.COMMAND_WORD,
ListJobCommand.COMMAND_WORD,
SwitchCommand.COMMAND_WORD,
AcceptAllCommand.COMMAND_WORD,
RejectAllCommand.COMMAND_WORD,
RejectCommand.COMMAND_WORD,
AcceptCommand.COMMAND_WORD
};

public final HashMap<String, String> commands;
Expand Down Expand Up @@ -126,7 +134,7 @@ public String getCommandKey(String value) throws CommandWordException {
*/
public void setCommandWord(String currentWord, String newWord) throws CommandWordException {
requireNonNull(currentWord, newWord);
checkCommandWordValidity(currentWord, newWord);
throwExceptionIfCommandWordsNotValid(currentWord, newWord);
if (isDefaultCommandWord(currentWord)) {
commands.remove(currentWord);
commands.put(currentWord, newWord);
Expand All @@ -146,9 +154,12 @@ public void setCommandWord(String currentWord, String newWord) throws CommandWor
}

/**
* throws a (@code CommandWordException) if (@code currentWord) or (@code newWord) is not valid
* throws a (@code CommandWordException) if:
* 1. Both words are the same
* 2. (@code newWord) overwrites the default word for another command
* 3. (@code newWord) is already in use
*/
private void checkCommandWordValidity(String currentWord, String newWord) throws CommandWordException {
private void throwExceptionIfCommandWordsNotValid(String currentWord, String newWord) throws CommandWordException {
if (currentWord.equals(newWord)) {
throw new CommandWordException(getMessageNoChange());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import seedu.carvicim.storage.session.exceptions.DataIndexOutOfBoundsException;
import seedu.carvicim.storage.session.exceptions.FileAccessException;
import seedu.carvicim.storage.session.exceptions.FileFormatException;
import seedu.carvicim.storage.session.exceptions.UnitializedException;

//@@author yuhongherald
/**
Expand Down Expand Up @@ -45,7 +46,7 @@ public CommandResult executeUndoableCommand() throws CommandException {
try {
importSession.initializeSession(filePath);
} catch (FileAccessException e) {
e.printStackTrace();
throw new CommandException(e.getMessage());
} catch (FileFormatException e) {
throw new CommandException("Excel file first row headers are not defined properly. "
+ "Type 'help' to read more.");
Expand All @@ -61,6 +62,8 @@ public CommandResult executeUndoableCommand() throws CommandException {
+ "before trying again");
} catch (IOException e) {
throw new CommandException("Unable to export file. Please close the application and try again.");
} catch (UnitializedException e) {
throw new CommandException(e.getMessage());
}
}

Expand Down
71 changes: 71 additions & 0 deletions src/main/java/seedu/carvicim/logic/commands/ImportCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package seedu.carvicim.logic.commands;

import static java.util.Objects.requireNonNull;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import seedu.carvicim.commons.core.EventsCenter;
import seedu.carvicim.commons.events.ui.DisplayAllJobsEvent;
import seedu.carvicim.logic.commands.exceptions.CommandException;
import seedu.carvicim.model.job.Job;
import seedu.carvicim.storage.session.ImportSession;
import seedu.carvicim.storage.session.exceptions.FileAccessException;
import seedu.carvicim.storage.session.exceptions.FileFormatException;

//@@author yuhongherald

/**
* Attempts to import specified file into Servicing Manager
*/
public class ImportCommand extends UndoableCommand {

public static final String COMMAND_WORD = "import";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Imports an excel file for reviewing. "
+ "Parameters: FILEPATH\n"
+ "Example: " + COMMAND_WORD + "yourfile.xls";

public static final String MESSAGE_SUCCESS = "%s has been imported, with %d job entries!";

private final String filePath;

public ImportCommand(String filePath) {
requireNonNull(filePath);
this.filePath = filePath;
}

public String getMessageSuccess(int entries) {
return String.format(MESSAGE_SUCCESS, filePath, entries);
}

@Override
public CommandResult executeUndoableCommand() throws CommandException {
ImportSession importSession = ImportSession.getInstance();
try {
importSession.initializeSession(filePath);
} catch (FileAccessException e) {
throw new CommandException(e.getMessage());
} catch (FileFormatException e) {
throw new CommandException("Excel file first row headers are not defined properly. "
+ "Type 'help' to read more.");
}

ObservableList<Job> jobList = FXCollections.observableList(
ImportSession.getInstance().getSessionData().getUnreviewedJobEntries());
if (!model.isViewingImportedJobs()) {
model.switchJobView();
}
EventsCenter.getInstance().post(
new DisplayAllJobsEvent(FXCollections.unmodifiableObservableList(jobList)));
return new CommandResult(getMessageSuccess(importSession.getSessionData()
.getUnreviewedJobEntries().size()));
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof ImportCommand // instanceof handles nulls
&& filePath.equals(((ImportCommand) other).filePath));
}

}
34 changes: 34 additions & 0 deletions src/main/java/seedu/carvicim/logic/commands/ListJobCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package seedu.carvicim.logic.commands;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import seedu.carvicim.commons.core.EventsCenter;
import seedu.carvicim.commons.events.ui.DisplayAllJobsEvent;
import seedu.carvicim.model.job.Job;
import seedu.carvicim.storage.session.ImportSession;

/**
* Lists all jobs in the carvicim book to the user.
*/
//@@author yuhongherald
public class ListJobCommand extends Command {

public static final String COMMAND_WORD = "listj";

public static final String MESSAGE_SUCCESS = "Listed all jobs";


@Override
public CommandResult execute() {
ObservableList<Job> jobList;
if (model.isViewingImportedJobs()) {
jobList = FXCollections.observableList(
ImportSession.getInstance().getSessionData().getUnreviewedJobEntries());
} else {
jobList = model.getFilteredJobList();
}
EventsCenter.getInstance().post(
new DisplayAllJobsEvent(FXCollections.unmodifiableObservableList(jobList)));
return new CommandResult(MESSAGE_SUCCESS);
}
}
Loading

0 comments on commit 67299ac

Please sign in to comment.