.... /** * No comments for you. * It was hard to write, so it * should be hard to read. * */ @Entity @Table(name = "schedule") @Getter @Setter @DynamicUpdate @JsonIgnoreProperties(ignoreUnknown = true) @JsonInclude(JsonInclude.Include.NON_EMPTY) public class ScheduleItem extends AbstractSpaceRelatedEntity implements Serializable { @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "reporterid", nullable = false) ReporterName reporter; @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "locationid", nullable = false) ScheduleLocation location; @Column(name = "intime", nullable = false) Date inTime; @Column(name = "indate") @Temporal(TemporalType.DATE) @JsonIgnore Date inDate; @Column(name = "outtime", nullable = false) Date outTime; @Column(name = "outdate") @Temporal(TemporalType.DATE) @JsonIgnore Date outDate; @Column(name = "note", length = 512) String note; @Column(name = "rtypeenum", nullable = false) @Enumerated(EnumType.ORDINAL) @NotNull @JsonIgnore ReminderTypeEnum reminderType; @Column(name = "rtime") @JsonIgnore Date reminderTime; @Column(name = "rsent", nullable = false) @JsonIgnore Boolean sent = false; @Column(name = "createdon", nullable = false) @JsonIgnore Date createdOn; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "createdby", updatable = false, insertable = false) @JsonIgnore ReporterName createdByName; @Column(name = "createdby") Long createdBy; @Column(name = "editedon") @JsonIgnore Date editedTime; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "editedby", updatable = false, insertable = false) @JsonIgnore ReporterName editedByName; @Column(name = "editedby") Long editedBy; @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "recurid", updatable = false, insertable = false) @JsonIgnore LRecurringSchedule recur; @Column(name = "recurid") Long recurId; @Column(name = "tlength") Long tLength = 0l; public ScheduleItem(ReporterName reporter, ScheduleLocation location, Date inTime, Date outTime, String note, ReminderTypeEnum reminderType, ReporterName createdByName) { this(reporter, location, inTime, outTime, note, reminderType, createdByName, null); } public ScheduleItem(ReporterName reporter, ScheduleLocation location, Date inTime, Date outTime, String note, ReminderTypeEnum reminderType, ReporterName createdByName, LRecurringSchedule recur) { this.spaceId = reporter.getSpace(); this.reporter = reporter; this.location = location; this.inTime = inTime; this.outTime = outTime; this.note = note; this.reminderType = reminderType; this.createdByName = createdByName; this.createdBy = createdByName.getId(); if(recur != null){ this.recur = recur; this.recurId = recur.getId(); } } public ScheduleItem() { this.createdOn = new Date(); } public ScheduleItem(ReporterName createdByName, Long spaceId) { this(); this.createdByName = createdByName; this.createdBy = createdByName.getId(); this.spaceId = spaceId; } @JsonIgnore public Long getLocationId() { ScheduleLocation l = getLocation(); if (l != null) { return l.getId(); } return null; } @JsonIgnore public Long getReporterId() { ReporterName r = getReporter(); if (r != null) { return r.getId(); } return null; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; if (!super.equals(o)) return false; ScheduleItem that = (ScheduleItem) o; if (!reporter.getId().equals(that.reporter.getId())) return false; if (!location.getId().equals(that.location.getId())) return false; if (!inTime.equals(that.inTime)) return false; if (!outTime.equals(that.outTime)) return false; if (note != null ? !note.equals(that.note) : that.note != null) return false; return !(reminderTime != null ? !reminderTime.equals(that.reminderTime) : that.reminderTime != null); } @Override public int hashCode() { int result = super.hashCode(); result = 31 * result + reporter.getId().hashCode(); result = 31 * result + location.getId().hashCode(); result = 31 * result + inTime.hashCode(); result = 31 * result + outTime.hashCode(); result = 31 * result + (note != null ? note.hashCode() : 0); result = 31 * result + (reminderTime != null ? reminderTime.hashCode() : 0); return result; } public void setReminderType(ReminderTypeEnum type){ this.reminderType = type; } @PrePersist private void prePersist() { createdOn = new Date(); calculateValues(); updateReminderTime(); } @PreUpdate private void calculateValues() { updateReminderTime(); if (outTime != null) { //difference in milliseconds between the times long diff = outTime.getTime() - inTime.getTime(); tLength = diff / 1000; } else { tLength = 0l; } } public void updateReminderTime() { if(reminderType == null || inTime == null){ return; } if (reminderType == ReminderTypeEnum.NONE) { reminderTime = null; } else { long t = inTime.getTime(); reminderTime = new Date(t + reminderType.getMilliSecondsOffset()); } } public String getDays(){ return recur != null ? recur.getDays() : null; } public RecurTypeEnum getRecurType(){ return recur != null ? recur.getRecurType() : null; } public LocalDate getEnd() { return recur != null ? recur.getEnd() : null; } public void setRecur(LRecurringSchedule recur) { this.recur = recur; this.setRecurId(recur != null ? recur.getId() : null); } @Override public String toString() { return "ScheduleItem{" + "id=" + id + ", inTime=" + inTime + ", outTime=" + outTime + ", note='" + note + '\'' + ", reminderType=" + reminderType + ", recur=" + recur + ", recurid=" + recurId + '}'; } }