package io.trygvis.model; import javax.persistence.*; import java.util.*; import java.util.regex.*; @Entity public class Task { @Id @SequenceGenerator(name = "task_seq", sequenceName = "task_seq") @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "task_seq") private Long id; @ManyToOne private Queue queue; private Date scheduled; private Date lastRun; private int runCount; private Date completed; private String arguments; private static final Pattern pattern = Pattern.compile(" "); @SuppressWarnings("UnusedDeclaration") private Task() { } public Task(Queue queue, Date scheduled, String... arguments) { this.queue = queue; this.scheduled = scheduled; StringBuilder builder = new StringBuilder(arguments.length * 100); for (String argument : arguments) { if (pattern.matcher(argument).matches()) { throw new RuntimeException("Bad argument: '" + argument + "'."); } builder.append(argument).append(' '); } this.arguments = builder.toString(); } public Long getId() { return id; } public String[] getArguments() { return arguments.split(" "); } public Date getScheduled() { return scheduled; } public Date getLastRun() { return lastRun; } public int getRunCount() { return runCount; } public Date getCompleted() { return completed; } public boolean isDone() { return completed != null; } public void registerRun() { lastRun = new Date(); runCount++; } public void registerComplete(Date completed) { this.completed = completed; } public String toString() { return "Task{" + "id=" + id + ", queue=" + queue + ", scheduled=" + scheduled + ", lastRun=" + lastRun + ", runCount=" + runCount + ", completed=" + completed + ", arguments='" + arguments + '\'' + '}'; } }