Skip to content

Commit

Permalink
Merge 8e3e38b into ef1dc62
Browse files Browse the repository at this point in the history
  • Loading branch information
trufflepirate committed Nov 5, 2018
2 parents ef1dc62 + 8e3e38b commit 372f97c
Show file tree
Hide file tree
Showing 25 changed files with 444 additions and 137 deletions.
106 changes: 103 additions & 3 deletions src/main/java/seedu/address/logic/commands/job/ManageJobCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@
import seedu.address.logic.commands.Command;
import seedu.address.logic.commands.CommandResult;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.logic.parser.ParserUtil;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.Model;
import seedu.address.model.job.JobName;
import seedu.address.model.job.exceptions.JobNotFoundException;
import seedu.address.model.job.exceptions.JobOngoingException;
import seedu.address.model.machine.MachineName;
import seedu.address.model.machine.exceptions.MachineNotFoundException;

/**
* Command used to manage jobs (eg: starting a Print)
Expand All @@ -19,7 +25,11 @@ public class ManageJobCommand extends Command {
public static final String OPTION_RESTART = "restart";
public static final String OPTION_CANCEL = "cancel";
public static final String OPTION_DELETE = "delete";
public static final String OPTION_MOVE = "move";
public static final String OPTION_SHIFT = "shift";

public static final String SHIFT_OPTION_UP = "up";
public static final String SHIFT_OPTION_DOWN = "down";

public static final String MESSAGE_USAGE =
COMMAND_WORD + ": starts/restarts/cancels/deletes a particular job\n" + "Example: " + COMMAND_WORD
Expand All @@ -30,15 +40,23 @@ public class ManageJobCommand extends Command {
private static final String MESSAGE_DELETED_JOB = "The print has been deleted";
private static final String MESSAGE_NO_SUCH_JOB = "No such print found";
private static final String MESSAGE_NO_SUCH_OPTION = "No such options. Only use: start, restart, cancel.";
private static final String MESSAGE_ACCESS_DENIED = "Non admin user is not allowed to delete jobs in maker manager";
private static final String MESSAGE_ACCESS_DENIED_1 = "Non admin user is not allowed to ";
private static final String MESSAGE_ACCESS_DENIED_2 = " Jobs in Maker Manager";
private static final String MESSAGE_MACHINE_NOT_FOUND = "Machine not found in MakerManager!";
private static final String MESSAGE_MOVED_JOB = "Job moved to ";
private static final String MESSAGE_SHIFTED = "Job Shifted!";
private static final String MESSAGE_SHIFTED_NO_SUCH_OPTION = "Only shift up or shift down allowed";
private static final String MESSAGE_JOB_ONGOING = "Unable to Modify ongoing Job";

private JobName name;
private String option;
private String operand2;

public ManageJobCommand(JobName name, String option) {
public ManageJobCommand(JobName name, String option, String operand2) {
requireNonNull(name);
this.name = name;
this.option = option;
this.operand2 = operand2;
}

@Override
Expand All @@ -49,7 +67,89 @@ public CommandResult execute(Model model, CommandHistory history) throws Command
if (model.findJob(this.name) == null) {
throw new CommandException(MESSAGE_NO_SUCH_JOB);
}
switch (option) {

case OPTION_START:
model.startJob(name);
model.commitAddressBook();
model.updateFilteredMachineList(PREDICATE_SHOW_ALL_MACHINES);
return new CommandResult(MESSAGE_STARTED_JOB);

case OPTION_RESTART:
model.restartJob(name);
model.commitAddressBook();
model.updateFilteredMachineList(PREDICATE_SHOW_ALL_MACHINES);
return new CommandResult(MESSAGE_RESTARTED_JOB);

case OPTION_CANCEL:
model.cancelJob(name);
model.commitAddressBook();
model.updateFilteredMachineList(PREDICATE_SHOW_ALL_MACHINES);
return new CommandResult(MESSAGE_CANCELLED_JOB);

case OPTION_DELETE:
if (!model.isLoggedIn()) {
throw new CommandException(MESSAGE_ACCESS_DENIED_1 + OPTION_DELETE + MESSAGE_ACCESS_DENIED_2);
}
try {
model.deleteJob(name);
} catch (JobOngoingException joe) {
throw new CommandException(MESSAGE_JOB_ONGOING);
}
model.commitAddressBook();
model.updateFilteredMachineList(PREDICATE_SHOW_ALL_MACHINES);
return new CommandResult(MESSAGE_DELETED_JOB);

case OPTION_MOVE:
if (!model.isLoggedIn()) {
throw new CommandException(MESSAGE_ACCESS_DENIED_1 + OPTION_MOVE + MESSAGE_ACCESS_DENIED_2);
}
//parsing operand2
try {
MachineName targetMachineName = ParserUtil.parseMachineName(operand2);
model.moveJob(name, targetMachineName);
model.commitAddressBook();
return new CommandResult(MESSAGE_MOVED_JOB + targetMachineName.toString());

} catch (ParseException pe) {
throw new CommandException(pe.getMessage());
} catch (MachineNotFoundException mfe) {
throw new CommandException(MESSAGE_MACHINE_NOT_FOUND);
} catch (JobOngoingException je) {
throw new CommandException(MESSAGE_JOB_ONGOING);
}

case OPTION_SHIFT:
if (!model.isLoggedIn()) {
throw new CommandException(MESSAGE_ACCESS_DENIED_1 + OPTION_SHIFT + MESSAGE_ACCESS_DENIED_2);
}
//parsing operand2
try {
switch (operand2) {
case SHIFT_OPTION_UP:
model.shiftJob(name, 1);
model.commitAddressBook();
return new CommandResult(MESSAGE_SHIFTED);
case SHIFT_OPTION_DOWN:
model.shiftJob(name, -1);
model.commitAddressBook();
return new CommandResult(MESSAGE_SHIFTED);

default:
throw new CommandException(MESSAGE_SHIFTED_NO_SUCH_OPTION);
}
} catch (JobNotFoundException jfe) {
throw new CommandException(MESSAGE_NO_SUCH_JOB);
} catch (JobOngoingException jo) {
throw new CommandException(MESSAGE_JOB_ONGOING);
}

default:
return new CommandResult(MESSAGE_NO_SUCH_OPTION);
}

// TODO: 11/5/2018 REMOVE IF CASE SWITCH WORKS
/*
if (option.equals(OPTION_START)) {
model.startJob(name);
model.commitAddressBook();
Expand Down Expand Up @@ -77,7 +177,7 @@ public CommandResult execute(Model model, CommandHistory history) throws Command
} else {
return new CommandResult(MESSAGE_NO_SUCH_OPTION);
}

*/
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,23 @@
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.job.JobName;
import seedu.address.model.job.exceptions.JobNotFoundException;
import seedu.address.model.job.exceptions.JobOngoingException;

/**
* Swaps two jobs in the maker manager queue
*/
public class SwapJobsCommand extends Command {
public static final String COMMAND_WORD = "swapJobs";
public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Swaps jobs with jobname1 and jobname2"
+ " Parameters: "
+ PREFIX_NAME + "JOB NAME 1" + " "
+ PREFIX_MACHINE + "JOB NAME 2"
+ "\nEXAMPLE: \n" + COMMAND_WORD + " "
+ PREFIX_NAME + "iDCP project" + " "
public static final String MESSAGE_USAGE =
COMMAND_WORD + ": Swaps jobs with jobname1 and jobname2" + " Parameters: " + PREFIX_NAME + "JOB NAME 1" + " "
+ PREFIX_MACHINE + "JOB NAME 2" + "\nEXAMPLE: \n" + COMMAND_WORD + " " + PREFIX_NAME + "iDCP project" + " "
+ PREFIX_MACHINE + "iDCP";

public static final String MESSAGE_SUCCESS = "Jobs swapped: %1$s";
private static final String MESSAGE_ACCESS_DENIED =
"Non admin user is not allowed to swap jobs";

private static final String MESSAGE_ACCESS_DENIED = "Non admin user is not allowed to swap jobs";
private static final String MESSAGE_JOB_NOT_FOUND = "1 or more Jobs entered does not not exist!";
private static final String MESSAGE_JOB_ONGOING = "Ongoing Jobs cannot be swapped!";

private final JobName jobName1;
private final JobName jobName2;
Expand All @@ -54,17 +52,22 @@ public CommandResult execute(Model model, CommandHistory history) throws Command
if (!model.isLoggedIn()) {
throw new CommandException(MESSAGE_ACCESS_DENIED);
}

model.swapJobs(jobName1, jobName2);
try {
model.swapJobs(jobName1, jobName2);
} catch (JobNotFoundException jfe) {
throw new CommandException(MESSAGE_JOB_NOT_FOUND);
} catch (JobOngoingException joe) {
throw new CommandException(MESSAGE_JOB_ONGOING);
}
model.commitAddressBook();
return new CommandResult(String.format(MESSAGE_SUCCESS, jobName1 + " and " + jobName2));
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof SwapJobsCommand // instanceof handles nulls
&& jobName1.equals(((SwapJobsCommand) other).jobName1)
&& jobName2.equals(((SwapJobsCommand) other).jobName2));
|| (other instanceof SwapJobsCommand // instanceof handles nulls
&& jobName1.equals(((SwapJobsCommand) other).jobName1) && jobName2
.equals(((SwapJobsCommand) other).jobName2));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,31 @@ public class ManageJobCommandParser implements Parser<ManageJobCommand> {
/**
* Parses the given {@code String} of arguments in the context of the ManageJobCommand
* and returns a ManageJobCommand object for execution.
*
* @throws ParseException if the user input does not conform the expected format
*/
public ManageJobCommand parse(String args) throws ParseException {
String trimmedArgs = args.trim();
if (trimmedArgs.isEmpty()) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, ManageJobCommand.MESSAGE_USAGE));
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, ManageJobCommand.MESSAGE_USAGE));
}

String [] temp = trimmedArgs.split(" ");
String[] temp = trimmedArgs.split(" ");

if (temp.length != 2) {
if (temp.length < 2) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, ManageJobCommand.MESSAGE_USAGE));
}

try {
JobName name = new JobName(temp[0]);
String option = temp[1];
return new ManageJobCommand(name, option);
if (temp.length == 3) {
String operand2 = temp[2];
return new ManageJobCommand(name, option, operand2);
} else {
String operand2 = "";
return new ManageJobCommand(name, option, operand2);
}
} catch (Exception IllegalArgumentException) {
throw new ParseException(String.format(MESSAGE_ILLEGAL_JOB_NAME, ManageJobCommand.MESSAGE_USAGE));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package seedu.address.logic.parser.queue;

import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_MACHINE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME;

import java.util.Optional;
import java.util.logging.Logger;
import java.util.stream.Stream;

import seedu.address.commons.core.LogsCenter;
import seedu.address.logic.commands.queue.SwapJobsCommand;
import seedu.address.logic.parser.ArgumentMultimap;
import seedu.address.logic.parser.ArgumentTokenizer;
import seedu.address.logic.parser.Parser;
import seedu.address.logic.parser.Prefix;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.job.JobName;

/**
* Parses input arguments and creates a new SwapJobCommand object
Expand All @@ -27,32 +24,30 @@ public class SwapJobsCommandParser implements Parser<SwapJobsCommand> {
/**
* Parses the given {@code userInput} of arguments in the context
* of SwapJobsCommandParser and returns a SwapJobCommand object
*
* @throws ParseException if the user input does not conform to the expected
* format
* format
*/
public SwapJobsCommand parse(String userInput) throws ParseException {
logger.info("User input : " + userInput);
ArgumentMultimap argMultiMap =
ArgumentTokenizer.tokenize(userInput, PREFIX_NAME, PREFIX_MACHINE);


if (!arePrefixesPresent(argMultiMap, PREFIX_NAME, PREFIX_MACHINE)
|| !argMultiMap.getPreamble().isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
SwapJobsCommand.MESSAGE_USAGE));
String trimmedArgs = userInput.trim();
if (trimmedArgs.isEmpty()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, SwapJobsCommand.MESSAGE_USAGE));
}

Optional<String> jobName1 = argMultiMap.getValue(PREFIX_NAME);
Optional<String> jobName2 = argMultiMap.getValue(PREFIX_MACHINE);

String[] temp = trimmedArgs.split(" ");

if (!jobName1.isPresent() || !jobName2.isPresent()) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
SwapJobsCommand.MESSAGE_USAGE));
if (temp.length < 2) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, SwapJobsCommand.MESSAGE_USAGE));
}


return new SwapJobsCommand(jobName1.get(), jobName2.get());
try {
String jobName1 = temp[0];
String jobName2 = temp[1];
return new SwapJobsCommand(jobName1, jobName2);
} catch (IllegalArgumentException ile) {
throw new ParseException(JobName.MESSAGE_JOBNAME_CONSTRAINTS);
}
}


Expand Down
Loading

0 comments on commit 372f97c

Please sign in to comment.