Skip to content

Commit

Permalink
JPA Refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: Avgustin Marinov <Avgustin.Marinov@bosch.com>
  • Loading branch information
avgustinmm committed Nov 29, 2024
1 parent ebcb6a0 commit daf2cfa
Show file tree
Hide file tree
Showing 27 changed files with 326 additions and 910 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import jakarta.persistence.Version;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.eclipse.hawkbit.repository.jpa.model.helper.AfterTransactionCommitExecutorHolder;
Expand All @@ -38,11 +39,12 @@
import org.springframework.security.core.context.SecurityContextHolder;

/**
* Holder of the base attributes common to all entities.
* Base hawkBit entity class containing the common attributes.
*/
@NoArgsConstructor(access = AccessLevel.PROTECTED) // Default constructor needed for JPA entities.
@Setter
@Getter
@MappedSuperclass
@Access(AccessType.FIELD)
@EntityListeners({ AuditingEntityListener.class, EntityInterceptorListener.class })
public abstract class AbstractJpaBaseEntity implements BaseEntity {

Expand All @@ -51,111 +53,70 @@ public abstract class AbstractJpaBaseEntity implements BaseEntity {
@Serial
private static final long serialVersionUID = 1L;

@Setter
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;

@Column(name = "created_by", updatable = false, nullable = false, length = USERNAME_FIELD_LENGTH)
private String createdBy;
@Column(name = "created_at", updatable = false, nullable = false)
private long createdAt;
@Column(name = "last_modified_by", nullable = false, length = USERNAME_FIELD_LENGTH)
private String lastModifiedBy;
@Column(name = "last_modified_at", nullable = false)
private long lastModifiedAt;

@Setter
@Version
@Column(name = "optlock_revision")
private int optLockRevision;

@Override
@Access(AccessType.PROPERTY)
@Column(name = "created_by", updatable = false, nullable = false, length = USERNAME_FIELD_LENGTH)
public String getCreatedBy() {
return createdBy;
}

@Override
@Access(AccessType.PROPERTY)
@Column(name = "created_at", updatable = false, nullable = false)
public long getCreatedAt() {
return createdAt;
}

@Override
@Access(AccessType.PROPERTY)
@Column(name = "last_modified_by", nullable = false, length = USERNAME_FIELD_LENGTH)
public String getLastModifiedBy() {
return lastModifiedBy;
}

@Override
@Access(AccessType.PROPERTY)
@Column(name = "last_modified_at", nullable = false)
public long getLastModifiedAt() {
return lastModifiedAt;
}

@Override
public int getOptLockRevision() {
return optLockRevision;
}

@LastModifiedDate
public void setLastModifiedAt(final long lastModifiedAt) {
@CreatedBy
public void setCreatedBy(final String createdBy) {
if (isController()) {
return;
}

this.lastModifiedAt = lastModifiedAt;
}
this.createdBy = "CONTROLLER_PLUG_AND_PLAY";

@LastModifiedBy
public void setLastModifiedBy(final String lastModifiedBy) {
if (isController()) {
// In general modification audit entry is not changed by the controller.
// However, we want to stay consistent with EnableJpaAuditing#modifyOnCreate=true.
this.lastModifiedBy = this.createdBy;
return;
}

this.lastModifiedBy = lastModifiedBy;
this.createdBy = createdBy;
}

@CreatedDate
public void setCreatedAt(final long createdAt) {
this.createdAt = createdAt;

// In general modification audit entry is not changed by the controller.
// However, we want to stay consistent with
// EnableJpaAuditing#modifyOnCreate=true.
// However, we want to stay consistent with EnableJpaAuditing#modifyOnCreate=true.
if (isController()) {
this.lastModifiedAt = createdAt;
}
}

@CreatedBy
public void setCreatedBy(final String createdBy) {
@LastModifiedBy
public void setLastModifiedBy(final String lastModifiedBy) {
if (isController()) {
this.createdBy = "CONTROLLER_PLUG_AND_PLAY";

// In general modification audit entry is not changed by the
// controller. However, we want to stay consistent with
// EnableJpaAuditing#modifyOnCreate=true.
this.lastModifiedBy = this.createdBy;
return;
}

this.createdBy = createdBy;
this.lastModifiedBy = lastModifiedBy;
}

@Override
public Long getId() {
return id;
@LastModifiedDate
public void setLastModifiedAt(final long lastModifiedAt) {
if (isController()) {
return;
}

this.lastModifiedAt = lastModifiedAt;
}

/**
* Defined equals/hashcode strategy for the repository in general is that an
* entity is equal if it has the same {@link #getId()} and
* Defined equals/hashcode strategy for the repository in general is that an entity is equal if it has the same {@link #getId()} and
* {@link #getOptLockRevision()} and class.
*
* @see java.lang.Object#hashCode()
*/
@Override
// Exception squid:S864 - generated code
Expand All @@ -170,11 +131,8 @@ public int hashCode() {
}

/**
* Defined equals/hashcode strategy for the repository in general is that an
* entity is equal if it has the same {@link #getId()} and
* Defined equals/hashcode strategy for the repository in general is that an entity is equal if it has the same {@link #getId()} and
* {@link #getOptLockRevision()} and class.
*
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(final Object obj) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,17 @@
* Holder of the base attributes common to all tenant aware entities.
*/
@NoArgsConstructor(access = AccessLevel.PROTECTED) // Default constructor needed for JPA entities.
@Setter
@Getter
@MappedSuperclass
// Eclipse link MultiTenant support
@TenantDiscriminatorColumn(name = "tenant", length = 40)
@Multitenant(MultitenantType.SINGLE_TABLE)
public abstract class AbstractJpaTenantAwareBaseEntity extends AbstractJpaBaseEntity implements TenantAwareBaseEntity {

@Serial
private static final long serialVersionUID = 1L;

@Setter
@Getter
@Column(name = "tenant", nullable = false, insertable = false, updatable = false, length = 40)
@Size(min = 1, max = 40)
@NotNull
Expand Down Expand Up @@ -74,13 +75,10 @@ public boolean equals(final Object obj) {
}
final AbstractJpaTenantAwareBaseEntity other = (AbstractJpaTenantAwareBaseEntity) obj;
if (tenant == null) {
if (other.tenant != null) {
return false;
}
} else if (!tenant.equals(other.tenant)) {
return false;
return other.tenant == null;
} else {
return tenant.equals(other.tenant);
}
return true;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,25 @@
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.eclipse.hawkbit.artifact.repository.model.AbstractDbArtifact;
import org.eclipse.hawkbit.repository.model.Artifact;
import org.eclipse.hawkbit.repository.model.SoftwareModule;

/**
* JPA implementation of {@link Artifact}.
*/
@Table(name = "sp_artifact", indexes = { @Index(name = "sp_idx_artifact_01", columnList = "tenant,software_module"),
@Index(name = "sp_idx_artifact_02", columnList = "tenant,sha1_hash"),
@Index(name = "sp_idx_artifact_prim", columnList = "tenant,id") })
@NoArgsConstructor(access = AccessLevel.PUBLIC) // Default constructor needed for JPA entities.
@Setter
@Getter
@Table(name = "sp_artifact",
indexes = {
@Index(name = "sp_idx_artifact_01", columnList = "tenant,software_module"),
@Index(name = "sp_idx_artifact_02", columnList = "tenant,sha1_hash"),
@Index(name = "sp_idx_artifact_prim", columnList = "tenant,id") })
@Entity
// exception squid:S2160 - BaseEntity equals/hashcode is handling correctly for sub entities
@SuppressWarnings("squid:S2160")
Expand All @@ -43,94 +52,42 @@ public class JpaArtifact extends AbstractJpaTenantAwareBaseEntity implements Art
@Serial
private static final long serialVersionUID = 1L;

@Column(name = "sha1_hash", length = 40, nullable = false, updatable = false)
@Size(min = 1, max = 40)
@NotNull
private String sha1Hash;
@ManyToOne(optional = false, cascade = { CascadeType.PERSIST }, fetch = FetchType.LAZY)
@JoinColumn(
name = "software_module", nullable = false, updatable = false,
foreignKey = @ForeignKey(value = ConstraintMode.CONSTRAINT, name = "fk_assigned_sm"))
private JpaSoftwareModule softwareModule;

@Column(name = "provided_file_name", length = 256, updatable = false)
@Size(min = 1, max = 256)
@NotNull
private String filename;

@ManyToOne(optional = false, cascade = { CascadeType.PERSIST }, fetch = FetchType.LAZY)
@JoinColumn(name = "software_module", nullable = false, updatable = false, foreignKey = @ForeignKey(value = ConstraintMode.CONSTRAINT, name = "fk_assigned_sm"))
private JpaSoftwareModule softwareModule;

@Column(name = "md5_hash", length = 32, updatable = false, nullable = true)
private String md5Hash;

@Column(name = "sha1_hash", length = 40, nullable = false, updatable = false)
@Size(min = 1, max = 40)
@NotNull
private String sha1Hash;

@Column(name = "sha256_hash", length = 64, updatable = false, nullable = true)
private String sha256Hash;

@Column(name = "file_size", updatable = false)
private long size;

/**
* Default constructor needed for JPA entities..
*/
public JpaArtifact() {
// Default constructor needed for JPA entities.
}

/**
* Constructs artifact.
*
* @param sha1Hash that is the link to the {@link AbstractDbArtifact} entity.
* @param filename that is used by {@link AbstractDbArtifact} store.
* @param softwareModule of this artifact
*/
public JpaArtifact(@NotEmpty final String sha1Hash, @NotNull final String filename,
final SoftwareModule softwareModule) {
public JpaArtifact(@NotEmpty final String sha1Hash, @NotNull final String filename, final SoftwareModule softwareModule) {
this.sha1Hash = sha1Hash;
this.filename = filename;
this.softwareModule = (JpaSoftwareModule) softwareModule;
this.softwareModule.addArtifact(this);
}

@Override
public String getFilename() {
return filename;
}

@Override
public SoftwareModule getSoftwareModule() {
return softwareModule;
}

@Override
public String getMd5Hash() {
return md5Hash;
}

@Override
public String getSha1Hash() {
return sha1Hash;
}

@Override
public String getSha256Hash() {
return sha256Hash;
}

public void setSha256Hash(final String sha256Hash) {
this.sha256Hash = sha256Hash;
}

@Override
public long getSize() {
return size;
}

public void setSize(final long size) {
this.size = size;
}

public void setSha1Hash(final String sha1Hash) {
this.sha1Hash = sha1Hash;
}

public void setMd5Hash(final String md5Hash) {
this.md5Hash = md5Hash;
}
}
}
Loading

0 comments on commit daf2cfa

Please sign in to comment.