Skip to content

Commit

Permalink
Fix equals, hashcode, and data annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
davidpbauer committed Apr 9, 2024
1 parent 1ebcf60 commit 4805567
Show file tree
Hide file tree
Showing 8 changed files with 230 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,32 @@
package org.sakaiproject.attendance.api.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Type;
import org.hibernate.proxy.HibernateProxy;
import org.sakaiproject.springframework.data.PersistableEntity;

import javax.persistence.*;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import java.io.Serializable;
import java.time.Instant;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/**
Expand All @@ -42,10 +55,10 @@
@Entity
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Table(name = "ATTENDANCE_EVENT_T")
@Data
@ToString(exclude = {"attendanceSite", "records"})
@EqualsAndHashCode(of = "id")
@NoArgsConstructor
@Getter
@Setter
@RequiredArgsConstructor
@ToString
@AllArgsConstructor
public class AttendanceEvent implements Serializable, PersistableEntity<Long> {
private static final long serialVersionUID = 1L;
Expand All @@ -65,9 +78,11 @@ public class AttendanceEvent implements Serializable, PersistableEntity<Long> {

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "A_SITE_ID")
@ToString.Exclude
private AttendanceSite attendanceSite;

@OneToMany(mappedBy = "attendanceEvent", cascade = CascadeType.ALL, orphanRemoval = true)
@ToString.Exclude
private Set<AttendanceRecord> records = new HashSet<>(0);

// Copy constructor
Expand All @@ -76,4 +91,20 @@ public AttendanceEvent(AttendanceEvent attendanceEvent) {
this.startDateTime = attendanceEvent.startDateTime;
this.attendanceSite = attendanceEvent.attendanceSite;
}

@Override
public final boolean equals(Object o) {
if (this == o) return true;
if (o == null) return false;
Class<?> oEffectiveClass = o instanceof HibernateProxy ? ((HibernateProxy) o).getHibernateLazyInitializer().getPersistentClass() : o.getClass();
Class<?> thisEffectiveClass = this instanceof HibernateProxy ? ((HibernateProxy) this).getHibernateLazyInitializer().getPersistentClass() : this.getClass();
if (thisEffectiveClass != oEffectiveClass) return false;
AttendanceEvent that = (AttendanceEvent) o;
return getId() != null && Objects.equals(getId(), that.getId());
}

@Override
public final int hashCode() {
return this instanceof HibernateProxy ? ((HibernateProxy) this).getHibernateLazyInitializer().getPersistentClass().hashCode() : getClass().hashCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,27 @@
package org.sakaiproject.attendance.api.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.proxy.HibernateProxy;
import org.sakaiproject.springframework.data.PersistableEntity;

import javax.persistence.*;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.Objects;

/**
* The AttendanceGrade earned for the all AttendanceItems
Expand All @@ -38,10 +49,10 @@
@Entity
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Table(name = "ATTENDANCE_GRADE_T")
@Data
@ToString(exclude = {"attendanceSite"})
@EqualsAndHashCode(of = "id")
@NoArgsConstructor
@Getter
@Setter
@RequiredArgsConstructor
@ToString
@AllArgsConstructor
public class AttendanceGrade implements Serializable, PersistableEntity<Long> {
private static final long serialVersionUID = 1L;
Expand All @@ -63,10 +74,27 @@ public class AttendanceGrade implements Serializable, PersistableEntity<Long> {

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "A_SITE_ID")
@ToString.Exclude
private AttendanceSite attendanceSite;

public AttendanceGrade(AttendanceSite aS, String userId) {
this.attendanceSite = aS;
this.userID = userId;
}

@Override
public final boolean equals(Object o) {
if (this == o) return true;
if (o == null) return false;
Class<?> oEffectiveClass = o instanceof HibernateProxy ? ((HibernateProxy) o).getHibernateLazyInitializer().getPersistentClass() : o.getClass();
Class<?> thisEffectiveClass = this instanceof HibernateProxy ? ((HibernateProxy) this).getHibernateLazyInitializer().getPersistentClass() : this.getClass();
if (thisEffectiveClass != oEffectiveClass) return false;
AttendanceGrade that = (AttendanceGrade) o;
return getId() != null && Objects.equals(getId(), that.getId());
}

@Override
public final int hashCode() {
return this instanceof HibernateProxy ? ((HibernateProxy) this).getHibernateLazyInitializer().getPersistentClass().hashCode() : getClass().hashCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,31 @@
package org.sakaiproject.attendance.api.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.proxy.HibernateProxy;
import org.sakaiproject.springframework.data.PersistableEntity;

import javax.persistence.*;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Index;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.Objects;

/**
* An AttendanceRecord for a specific user for a specific AttendanceEvent
Expand All @@ -42,10 +57,10 @@
@Index(name = "ATTEN_EVENT_STATUS_I", columnList = "A_EVENT_ID, STATUS"),
@Index(name = "ATTEN_EVENT_USER_STATUS_I", columnList = "A_EVENT_ID, USER_ID, STATUS")
})
@Data
@ToString(exclude = {"attendanceEvent"})
@EqualsAndHashCode(of = "id")
@NoArgsConstructor
@Getter
@Setter
@RequiredArgsConstructor
@ToString
@AllArgsConstructor
public class AttendanceRecord implements Serializable, PersistableEntity<Long> {
private static final long serialVersionUID = 1L;
Expand All @@ -58,6 +73,7 @@ public class AttendanceRecord implements Serializable, PersistableEntity<Long> {

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "A_EVENT_ID")
@ToString.Exclude
private AttendanceEvent attendanceEvent;

@Column(name = "USER_ID", length = 99)
Expand All @@ -76,4 +92,20 @@ public AttendanceRecord(AttendanceEvent e, String uId, Status s) {
this.userID = uId;
this.status = s;
}

@Override
public final boolean equals(Object o) {
if (this == o) return true;
if (o == null) return false;
Class<?> oEffectiveClass = o instanceof HibernateProxy ? ((HibernateProxy) o).getHibernateLazyInitializer().getPersistentClass() : o.getClass();
Class<?> thisEffectiveClass = this instanceof HibernateProxy ? ((HibernateProxy) this).getHibernateLazyInitializer().getPersistentClass() : this.getClass();
if (thisEffectiveClass != oEffectiveClass) return false;
AttendanceRecord that = (AttendanceRecord) o;
return getId() != null && Objects.equals(getId(), that.getId());
}

@Override
public final int hashCode() {
return this instanceof HibernateProxy ? ((HibernateProxy) this).getHibernateLazyInitializer().getPersistentClass().hashCode() : getClass().hashCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,30 @@
package org.sakaiproject.attendance.api.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.proxy.HibernateProxy;
import org.sakaiproject.attendance.api.util.AttendanceConstants;
import org.sakaiproject.springframework.data.PersistableEntity;

import javax.persistence.*;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/**
Expand All @@ -42,10 +54,10 @@
@Entity
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Table(name = "ATTENDANCE_SITE_T")
@Data
@Getter
@Setter
@RequiredArgsConstructor
@ToString
@EqualsAndHashCode(of = "id")
@NoArgsConstructor
@AllArgsConstructor
public class AttendanceSite implements Serializable, PersistableEntity<Long> {
private static final long serialVersionUID = 1L;
Expand Down Expand Up @@ -85,10 +97,27 @@ public class AttendanceSite implements Serializable, PersistableEntity<Long> {
private Boolean showCommentsToStudents = Boolean.FALSE;

@OneToMany(mappedBy = "attendanceSite", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<AttendanceStatus> attendanceStatuses = new HashSet<>(0);
@ToString.Exclude
private Set<AttendanceStatus> attendanceStatuses = new HashSet<>(0);

public AttendanceSite(String siteID) {
this.siteID = siteID;
this.gradebookItemName = AttendanceConstants.GRADEBOOK_ITEM_NAME;
}

@Override
public final boolean equals(Object o) {
if (this == o) return true;
if (o == null) return false;
Class<?> oEffectiveClass = o instanceof HibernateProxy ? ((HibernateProxy) o).getHibernateLazyInitializer().getPersistentClass() : o.getClass();
Class<?> thisEffectiveClass = this instanceof HibernateProxy ? ((HibernateProxy) this).getHibernateLazyInitializer().getPersistentClass() : this.getClass();
if (thisEffectiveClass != oEffectiveClass) return false;
AttendanceSite that = (AttendanceSite) o;
return getId() != null && Objects.equals(getId(), that.getId());
}

@Override
public final int hashCode() {
return this instanceof HibernateProxy ? ((HibernateProxy) this).getHibernateLazyInitializer().getPersistentClass().hashCode() : getClass().hashCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,29 @@
package org.sakaiproject.attendance.api.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.proxy.HibernateProxy;
import org.sakaiproject.springframework.data.PersistableEntity;

import javax.persistence.*;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.Objects;

/**
* An AttendanceStatus is a wrapper around the Status enum type defining meta information on individual Statuses.
Expand All @@ -37,10 +50,10 @@
@Entity
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Table(name = "ATTENDANCE_STATUS_T")
@Data
@ToString(exclude = {"attendanceSite"})
@EqualsAndHashCode(of = "id")
@NoArgsConstructor
@Getter
@Setter
@RequiredArgsConstructor
@ToString
@AllArgsConstructor
public class AttendanceStatus implements Serializable, PersistableEntity<Long> {
private static final long serialVersionUID = 1L;
Expand All @@ -63,6 +76,7 @@ public class AttendanceStatus implements Serializable, PersistableEntity<Long> {

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "A_SITE_ID")
@ToString.Exclude
private AttendanceSite attendanceSite;

// Create a copy constructor
Expand All @@ -72,4 +86,20 @@ public AttendanceStatus(AttendanceStatus attendanceStatus) {
this.sortOrder = attendanceStatus.getSortOrder();
this.attendanceSite = attendanceStatus.getAttendanceSite();
}

@Override
public final boolean equals(Object o) {
if (this == o) return true;
if (o == null) return false;
Class<?> oEffectiveClass = o instanceof HibernateProxy ? ((HibernateProxy) o).getHibernateLazyInitializer().getPersistentClass() : o.getClass();
Class<?> thisEffectiveClass = this instanceof HibernateProxy ? ((HibernateProxy) this).getHibernateLazyInitializer().getPersistentClass() : this.getClass();
if (thisEffectiveClass != oEffectiveClass) return false;
AttendanceStatus that = (AttendanceStatus) o;
return getId() != null && Objects.equals(getId(), that.getId());
}

@Override
public final int hashCode() {
return this instanceof HibernateProxy ? ((HibernateProxy) this).getHibernateLazyInitializer().getPersistentClass().hashCode() : getClass().hashCode();
}
}
Loading

0 comments on commit 4805567

Please sign in to comment.