Skip to content

Commit

Permalink
repo: entity classes using RContainerId use simplified id-based eq/hash
Browse files Browse the repository at this point in the history
equals/hashCode is based only on ID value and nothing else, passes tests
  • Loading branch information
virgo47 committed Jun 8, 2020
1 parent 381c22f commit 11c3628
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 221 deletions.
@@ -1,12 +1,25 @@
/*
* Copyright (c) 2010-2015 Evolveum and contributors
* Copyright (c) 2010-2020 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/

package com.evolveum.midpoint.repo.sql.data.common.container;

import static com.evolveum.midpoint.schema.util.CertCampaignTypeUtil.norm;

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import javax.persistence.*;
import javax.xml.datatype.XMLGregorianCalendar;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Persister;
import org.jetbrains.annotations.NotNull;

import com.evolveum.midpoint.prism.PrismContainerValue;
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.repo.sql.SqlRepositoryServiceImpl;
Expand All @@ -29,24 +42,6 @@
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.xml.ns._public.common.common_3.AccessCertificationCaseType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.AccessCertificationWorkItemType;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Persister;
import org.jetbrains.annotations.NotNull;

import javax.persistence.*;
import javax.xml.datatype.XMLGregorianCalendar;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

import static com.evolveum.midpoint.schema.util.CertCampaignTypeUtil.norm;

/**
* @author lazyman
* @author mederly
*/

@JaxbType(type = AccessCertificationCaseType.class)
@Entity
Expand All @@ -62,8 +57,6 @@ public class RAccessCertificationCase implements Container<RAccessCertificationC

private static final Trace LOGGER = TraceManager.getTrace(RAccessCertificationCase.class);

public static final String F_OWNER = "owner";

private Boolean trans;

private byte[] fullObject;
Expand Down Expand Up @@ -116,7 +109,7 @@ public Integer getId() {

@JaxbName(localPart = "workItem")
@OneToMany(mappedBy = "owner", orphanRemoval = true)
@Cascade({org.hibernate.annotations.CascadeType.ALL})
@Cascade({ org.hibernate.annotations.CascadeType.ALL })
public Set<RAccessCertificationWorkItem> getWorkItems() {
return workItems;
}
Expand Down Expand Up @@ -254,6 +247,7 @@ public void setFullObject(byte[] fullObject) {
}

// Notes to equals/hashCode: don't include trans nor owner
/*
@Override
public boolean equals(Object o) {
if (this == o)
Expand Down Expand Up @@ -285,6 +279,26 @@ public int hashCode() {
reviewRequestedTimestamp, reviewDeadline, remediedTimestamp, currentStageOutcome, iteration, stageNumber,
outcome);
}
*/

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof RAccessCertificationCase)) {
return false;
}

RAccessCertificationCase that = (RAccessCertificationCase) o;
return Objects.equals(ownerOid, that.ownerOid)
&& Objects.equals(id, that.id);
}

@Override
public int hashCode() {
return Objects.hash(ownerOid, id);
}

@Override
public String toString() {
Expand Down Expand Up @@ -322,9 +336,9 @@ public static RAccessCertificationCase toRepo(String ownerOid, AccessCertificati
return rCase;
}

private static RAccessCertificationCase toRepo(RAccessCertificationCase rCase, AccessCertificationCaseType case1,
RepositoryContext context) throws DtoTranslationException {
rCase.setTransient(null); // we don't try to advise hibernate - let it do its work, even if it would cost some SELECTs
private static RAccessCertificationCase toRepo(RAccessCertificationCase rCase,
AccessCertificationCaseType case1, RepositoryContext context) throws DtoTranslationException {
rCase.setTransient(null); // we don't try to advise hibernate - let it do its work, even if it would cost some SELECTs
rCase.setId(RUtil.toInteger(case1.getId()));
rCase.setObjectRef(RUtil.jaxbRefToEmbeddedRepoRef(case1.getObjectRef(), context.relationRegistry));
rCase.setTargetRef(RUtil.jaxbRefToEmbeddedRepoRef(case1.getTargetRef(), context.relationRegistry));
Expand Down Expand Up @@ -376,6 +390,5 @@ public static AccessCertificationCaseType createJaxb(byte[] fullObject, PrismCon
LOGGER.debug("Couldn't parse certification case because of unexpected exception ({}):\nData: {}", e, xml);
throw e;
}
//aCase.asPrismContainerValue().removeReference(AccessCertificationCaseType.F_CAMPAIGN_REF);
}
}
@@ -1,12 +1,24 @@
/*
* Copyright (c) 2010-2019 Evolveum and contributors
* Copyright (c) 2010-2020 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/

package com.evolveum.midpoint.repo.sql.data.common.container;

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import javax.persistence.*;
import javax.xml.datatype.XMLGregorianCalendar;

import org.apache.commons.lang.Validate;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Persister;
import org.hibernate.annotations.Where;

import com.evolveum.midpoint.repo.sql.data.RepositoryContext;
import com.evolveum.midpoint.repo.sql.data.common.Metadata;
import com.evolveum.midpoint.repo.sql.data.common.RObject;
Expand All @@ -21,51 +33,17 @@
import com.evolveum.midpoint.repo.sql.query.definition.JaxbPath;
import com.evolveum.midpoint.repo.sql.query.definition.JaxbType;
import com.evolveum.midpoint.repo.sql.query.definition.OwnerIdGetter;
import com.evolveum.midpoint.repo.sql.query.definition.QueryEntity;
import com.evolveum.midpoint.repo.sql.query2.definition.IdQueryProperty;
import com.evolveum.midpoint.repo.sql.query2.definition.NotQueryable;
import com.evolveum.midpoint.repo.sql.util.DtoTranslationException;
import com.evolveum.midpoint.repo.sql.util.IdGeneratorResult;
import com.evolveum.midpoint.repo.sql.util.MidPointSingleTablePersister;
import com.evolveum.midpoint.repo.sql.util.RUtil;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.xml.ns._public.common.common_3.AssignmentType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;
import org.apache.commons.lang.Validate;
import org.hibernate.annotations.*;

import javax.persistence.*;
import javax.persistence.Entity;
import javax.persistence.ForeignKey;
import javax.persistence.Index;
import javax.persistence.Table;
import javax.xml.datatype.XMLGregorianCalendar;
import java.util.HashSet;
import java.util.Set;

/**
* @author lazyman
*/
@JaxbType(type = AssignmentType.class)
@Entity
@QueryEntity(
entities = {
// @VirtualEntity(
// jaxbName = @JaxbName(localPart = "metadata"),
// jaxbType = MetadataType.class,
// jpaName = "",
// jpaType = Serializable.class // dummy value (ignored)
// )
// ,
// @VirtualEntity(
// jaxbName = @JaxbName(localPart = "construction"),
// jaxbType = ConstructionType.class,
// jpaName = "",
// jpaType = Serializable.class // dummy value (ignored)
// )
}
)
@IdClass(RContainerId.class)
// TODO prefix last 4 index names with "iAssignment" (some day)
@Table(name = "m_assignment", indexes = {
Expand All @@ -76,18 +54,11 @@
@Index(name = "iTargetRefTargetOid", columnList = "targetRef_targetOid"),
@Index(name = "iTenantRefTargetOid", columnList = "tenantRef_targetOid"),
@Index(name = "iOrgRefTargetOid", columnList = "orgRef_targetOid"),
@Index(name = "iResourceRefTargetOid", columnList = "resourceRef_targetOid")})
@Index(name = "iResourceRefTargetOid", columnList = "resourceRef_targetOid") })
@Persister(impl = MidPointSingleTablePersister.class)
public class RAssignment implements Container, Metadata<RAssignmentReference> {
public class RAssignment implements Container<RObject>, Metadata<RAssignmentReference> {

public static final String F_OWNER = "owner";
/**
* enum identifier of object class which owns this assignment. It's used because we have to
* distinguish between assignments and inducements (all of them are the same kind) in {@link com.evolveum.midpoint.repo.sql.data.common.RAbstractRole}.
*/
public static final String F_ASSIGNMENT_OWNER = "assignmentOwner";

private static final Trace LOGGER = TraceManager.getTrace(RAssignment.class);

private Boolean trans;

Expand Down Expand Up @@ -180,8 +151,8 @@ public REmbeddedReference getResourceRef() {
}

@com.evolveum.midpoint.repo.sql.query.definition.Any(jaxbNameLocalPart = "extension")
@OneToOne(optional = true, orphanRemoval = true)
@Cascade({org.hibernate.annotations.CascadeType.ALL})
@OneToOne(orphanRemoval = true)
@Cascade({ org.hibernate.annotations.CascadeType.ALL })
@JoinColumns(value = {
@JoinColumn(name = "extOid", referencedColumnName = "owner_owner_oid"),
@JoinColumn(name = "extId", referencedColumnName = "owner_id")
Expand All @@ -202,7 +173,7 @@ public Integer getOrder() {

@Where(clause = RAssignmentReference.REFERENCE_TYPE + "= 0")
@OneToMany(fetch = FetchType.LAZY, mappedBy = RAssignmentReference.F_OWNER, orphanRemoval = true)
@Cascade({org.hibernate.annotations.CascadeType.ALL})
@Cascade({ org.hibernate.annotations.CascadeType.ALL })
@JaxbPath(itemPath = { @JaxbName(localPart = "metadata"), @JaxbName(localPart = "createApproverRef") })
public Set<RAssignmentReference> getCreateApproverRef() {
if (createApproverRef == null) {
Expand Down Expand Up @@ -235,7 +206,7 @@ public REmbeddedReference getModifierRef() {

@Where(clause = RAssignmentReference.REFERENCE_TYPE + "= 1")
@OneToMany(fetch = FetchType.LAZY, mappedBy = RAssignmentReference.F_OWNER, orphanRemoval = true)
@Cascade({org.hibernate.annotations.CascadeType.ALL})
@Cascade({ org.hibernate.annotations.CascadeType.ALL })
@JaxbPath(itemPath = { @JaxbName(localPart = "metadata"), @JaxbName(localPart = "modifyApproverRef") })
public Set<RAssignmentReference> getModifyApproverRef() {
if (modifyApproverRef == null) {
Expand Down Expand Up @@ -268,10 +239,10 @@ public String getLifecycleState() {
@CollectionTable(name = "m_assignment_policy_situation",
foreignKey = @ForeignKey(name = "fk_assignment_policy_situation"),
joinColumns = {
@JoinColumn(name = "assignment_oid", referencedColumnName = "owner_oid"),
@JoinColumn(name = "assignment_id", referencedColumnName = "id")
})
@Cascade({org.hibernate.annotations.CascadeType.ALL})
@JoinColumn(name = "assignment_oid", referencedColumnName = "owner_oid"),
@JoinColumn(name = "assignment_id", referencedColumnName = "id")
})
@Cascade({ org.hibernate.annotations.CascadeType.ALL })
public Set<String> getPolicySituation() {
return policySituation;
}
Expand All @@ -280,7 +251,6 @@ public void setPolicySituation(Set<String> policySituation) {
this.policySituation = policySituation;
}


public void setLifecycleState(String lifecycleState) {
this.lifecycleState = lifecycleState;
}
Expand Down Expand Up @@ -382,27 +352,30 @@ public void setResourceRef(REmbeddedReference resourceRef) {
this.resourceRef = resourceRef;
}

/*
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (this == o) { return true; }
if (o == null || getClass() != o.getClass()) { return false; }
RAssignment that = (RAssignment) o;
if (activation != null ? !activation.equals(that.activation) : that.activation != null) return false;
if (extension != null ? !extension.equals(that.extension) : that.extension != null) return false;
if (targetRef != null ? !targetRef.equals(that.targetRef) : that.targetRef != null) return false;
if (assignmentOwner != null ? !assignmentOwner.equals(that.assignmentOwner) : that.assignmentOwner != null)
if (activation != null ? !activation.equals(that.activation) : that.activation != null) { return false; }
if (extension != null ? !extension.equals(that.extension) : that.extension != null) { return false; }
if (targetRef != null ? !targetRef.equals(that.targetRef) : that.targetRef != null) { return false; }
if (assignmentOwner != null ? !assignmentOwner.equals(that.assignmentOwner) : that.assignmentOwner != null) {
return false;
if (order != null ? !order.equals(that.order) : that.order != null)
}
if (order != null ? !order.equals(that.order) : that.order != null) { return false; }
if (tenantRef != null ? !tenantRef.equals(that.tenantRef) : that.tenantRef != null) { return false; }
if (orgRef != null ? !orgRef.equals(that.orgRef) : that.orgRef != null) { return false; }
if (resourceRef != null ? !resourceRef.equals(that.resourceRef) : that.resourceRef != null) { return false; }
if (lifecycleState != null ? !lifecycleState.equals(that.lifecycleState) : that.lifecycleState != null) { return false; }
if (policySituation != null ? !policySituation.equals(that.policySituation) : that.policySituation != null) {
return false;
if (tenantRef != null ? !tenantRef.equals(that.tenantRef) : that.tenantRef != null) return false;
if (orgRef != null ? !orgRef.equals(that.orgRef) : that.orgRef != null) return false;
if (resourceRef != null ? !resourceRef.equals(that.resourceRef) : that.resourceRef != null) return false;
if (lifecycleState != null ? !lifecycleState.equals(that.lifecycleState) : that.lifecycleState != null) return false;
if (policySituation != null ? !policySituation.equals(that.policySituation) : that.policySituation != null) return false;
}
if (!MetadataFactory.equals(this, that)) return false;
if (!MetadataFactory.equals(this, that)) { return false; }
return true;
}
Expand All @@ -423,9 +396,29 @@ public int hashCode() {
return result;
}
*/

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof RAssignment)) {
return false;
}

RAssignment that = (RAssignment) o;
return Objects.equals(ownerOid, that.ownerOid)
&& Objects.equals(id, that.id);
}

@Override
public int hashCode() {
return Objects.hash(ownerOid, id);
}

public static void fromJaxb(AssignmentType jaxb, RAssignment repo, RObject parent,
RepositoryContext repositoryContext) throws DtoTranslationException{
RepositoryContext repositoryContext) throws DtoTranslationException {
fromJaxb(jaxb, repo, repositoryContext, null);
repo.setOwner(parent);
}
Expand All @@ -437,7 +430,7 @@ public static void fromJaxb(AssignmentType jaxb, RAssignment repo, ObjectType pa
}

private static void fromJaxb(AssignmentType jaxb, RAssignment repo, RepositoryContext repositoryContext,
IdGeneratorResult generatorResult) throws DtoTranslationException {
IdGeneratorResult generatorResult) throws DtoTranslationException {
Validate.notNull(repo, "Repo object must not be null.");
Validate.notNull(jaxb, "JAXB object must not be null.");

Expand Down

0 comments on commit 11c3628

Please sign in to comment.