diff --git a/gsrs-module-substance-example/src/test/java/example/substance/RelationshipInvertFullStackTest.java b/gsrs-module-substance-example/src/test/java/example/substance/RelationshipInvertFullStackTest.java index ee5aad85e..28f9ea7fb 100644 --- a/gsrs-module-substance-example/src/test/java/example/substance/RelationshipInvertFullStackTest.java +++ b/gsrs-module-substance-example/src/test/java/example/substance/RelationshipInvertFullStackTest.java @@ -659,6 +659,66 @@ public void add2SubstancesWithNoRelationshipThenAddRelationshipShouldResultInBiD } + + @Test + public void add2SubstancesWithNoRelationshipThenAdd2RelationshipsOfSameTypeWithDifferentQualifiersShouldResultInBiDirectionalRelationshipsWithExpectedOriginiatorUUID() throws Exception { + UUID uuid1 = UUID.randomUUID(); + UUID uuid2 = UUID.randomUUID(); + String foo_bar = "foo->bar"; + String bar_foo = "bar->foo"; + + new SubstanceBuilder() + .addName("sub1") + .setUUID(uuid1) + .buildJsonAnd(this::assertCreatedAPI); + new SubstanceBuilder() + .addName("sub2") + .setUUID(uuid2) + .buildJsonAnd(this::assertCreatedAPI); + + + Substance sub1Fetched = substanceEntityService.get(uuid1).get(); + assertEquals("1", sub1Fetched.version); + + Substance sub2Fetched = substanceEntityService.get(uuid2).get(); + assertEquals("1", sub2Fetched.version); + + + sub1Fetched.toBuilder() + .addRelationshipTo(sub2Fetched, foo_bar, relnew1->relnew1.interactionType="Test123") + .addRelationshipTo(sub2Fetched, foo_bar, relnew2->relnew2.interactionType="Test456") + .buildJsonAnd(this::assertUpdatedAPI); + + + sub1Fetched = substanceEntityService.get(uuid1).get(); + assertEquals("2", sub1Fetched.version); + + sub2Fetched = substanceEntityService.get(uuid2).get(); +// assertEquals("2", sub2Fetched.version); + + assertEquals(2, sub1Fetched.relationships.size()); + assertEquals(2, sub2Fetched.relationships.size()); + + assertEquals(uuid2.toString(), sub1Fetched.relationships.get(0).relatedSubstance.refuuid); + assertEquals(foo_bar, sub1Fetched.relationships.get(0).type); + + assertEquals(uuid2.toString(), sub1Fetched.relationships.get(1).relatedSubstance.refuuid); + assertEquals(foo_bar, sub1Fetched.relationships.get(1).type); + + String oid1 = sub1Fetched.relationships.get(0).originatorUuid; + String oid2 = sub2Fetched.relationships.get(0).originatorUuid; + assertEquals(uuid1.toString(), sub2Fetched.relationships.get(0).relatedSubstance.refuuid); + assertEquals(bar_foo, sub2Fetched.relationships.get(0).type); + assertEquals(oid1, oid2); + + String oid1b = sub1Fetched.relationships.get(1).originatorUuid; + String oid2b = sub2Fetched.relationships.get(1).originatorUuid; + assertEquals(uuid1.toString(), sub2Fetched.relationships.get(1).relatedSubstance.refuuid); + assertEquals(bar_foo, sub2Fetched.relationships.get(1).type); + assertEquals(oid1b, oid2b); + + } + /* * 1. Create a new alternative definition and link back to some primary definition 1.1. Confirm it makes the new alt record [yes] diff --git a/gsrs-module-substance-example/src/test/java/example/substance/RelationshipInvertTest.java b/gsrs-module-substance-example/src/test/java/example/substance/RelationshipInvertTest.java index 2055731b4..f9e059192 100644 --- a/gsrs-module-substance-example/src/test/java/example/substance/RelationshipInvertTest.java +++ b/gsrs-module-substance-example/src/test/java/example/substance/RelationshipInvertTest.java @@ -380,7 +380,7 @@ public void addRelationshipAfterAddingEachSubstanceShouldAddInvertedRelationship assertEquals(1, createInverseEvents.size()); assertEquals(TryToCreateInverseRelationshipEvent.builder() .relationshipIdToInvert(updatedSubstance.relationships.get(0).uuid) - .creationMode(TryToCreateInverseRelationshipEvent.CreationMode.CREATE_IF_MISSING) + .creationMode(TryToCreateInverseRelationshipEvent.CreationMode.CREATE_IF_MISSING_DEEP_CHECK) .fromSubstance(UUID.fromString(uuidA)) .toSubstance(updatedSubstance.uuid) .originatorUUID(updatedSubstance.relationships.get(0).uuid) diff --git a/gsrs-module-substances-core/src/main/java/gsrs/module/substance/processors/RelationshipProcessor.java b/gsrs-module-substances-core/src/main/java/gsrs/module/substance/processors/RelationshipProcessor.java index fd6906973..071dd9aa4 100644 --- a/gsrs-module-substances-core/src/main/java/gsrs/module/substance/processors/RelationshipProcessor.java +++ b/gsrs-module-substances-core/src/main/java/gsrs/module/substance/processors/RelationshipProcessor.java @@ -98,7 +98,7 @@ public void prePersist(Relationship thisRelationship) { if (otherSubstanceReference != null && otherSubstanceReference.refuuid !=null) { event.setFromSubstance(UUID.fromString(otherSubstanceReference.refuuid)); } - event.setCreationMode(TryToCreateInverseRelationshipEvent.CreationMode.CREATE_IF_MISSING); + event.setCreationMode(TryToCreateInverseRelationshipEvent.CreationMode.CREATE_IF_MISSING_DEEP_CHECK); eventPublisher.publishEvent(event); }); diff --git a/gsrs-module-substances-core/src/main/java/gsrs/module/substance/processors/SubstanceProcessor.java b/gsrs-module-substances-core/src/main/java/gsrs/module/substance/processors/SubstanceProcessor.java index 2b7278b4b..ba9f6a808 100644 --- a/gsrs-module-substances-core/src/main/java/gsrs/module/substance/processors/SubstanceProcessor.java +++ b/gsrs-module-substances-core/src/main/java/gsrs/module/substance/processors/SubstanceProcessor.java @@ -105,7 +105,7 @@ private void addWaitingRelationships(Substance obj){ if(owner!=null) { eventPublisher.publishEvent( TryToCreateInverseRelationshipEvent.builder() - .creationMode(TryToCreateInverseRelationshipEvent.CreationMode.CREATE_IF_MISSING) + .creationMode(TryToCreateInverseRelationshipEvent.CreationMode.CREATE_IF_MISSING_DEEP_CHECK) .originatorUUID(UUID.fromString(r.originatorUuid)) .toSubstance(owner.uuid) .fromSubstance(obj.uuid) diff --git a/gsrs-module-substances-core/src/main/java/gsrs/module/substance/processors/TryToCreateInverseRelationshipEvent.java b/gsrs-module-substances-core/src/main/java/gsrs/module/substance/processors/TryToCreateInverseRelationshipEvent.java index 53f3452c2..cdb4c6a90 100644 --- a/gsrs-module-substances-core/src/main/java/gsrs/module/substance/processors/TryToCreateInverseRelationshipEvent.java +++ b/gsrs-module-substances-core/src/main/java/gsrs/module/substance/processors/TryToCreateInverseRelationshipEvent.java @@ -32,7 +32,7 @@ public class TryToCreateInverseRelationshipEvent { private UUID originatorUUID; private UUID relationshipIdToInvert; - private CreationMode creationMode = CreationMode.CREATE_IF_MISSING; + private CreationMode creationMode = CreationMode.CREATE_IF_MISSING_DEEP_CHECK; private UUID newRelationshipId; @@ -42,7 +42,18 @@ public enum CreationMode{ @Override public boolean shouldAdd(Relationship r, Substance from, Substance to){ for (Relationship rOld : from.relationships) { - if (r.type.equals(rOld.type) && r.relatedSubstance.isEquivalentTo(rOld.relatedSubstance)) { + if (r.isEquivalentBaseRelationship(rOld)) { + return false; + } + } + return true; + } + }, + CREATE_IF_MISSING_DEEP_CHECK{ + @Override + public boolean shouldAdd(Relationship r, Substance from, Substance to){ + for (Relationship rOld : from.relationships) { + if (r.isEquivalentFullRelationship(rOld)) { return false; } } diff --git a/gsrs-module-substances-core/src/main/java/gsrs/module/substance/processors/UpdateInverseRelationshipEvent.java b/gsrs-module-substances-core/src/main/java/gsrs/module/substance/processors/UpdateInverseRelationshipEvent.java index e5c14f3c3..70601d170 100644 --- a/gsrs-module-substances-core/src/main/java/gsrs/module/substance/processors/UpdateInverseRelationshipEvent.java +++ b/gsrs-module-substances-core/src/main/java/gsrs/module/substance/processors/UpdateInverseRelationshipEvent.java @@ -39,7 +39,7 @@ public TryToCreateInverseRelationshipEvent toCreateEvent() { // set the from substance AND to substance to be the same, causing unintentional self-referencing // relationships. This is fixed in 3.0.3. return TryToCreateInverseRelationshipEvent.builder() - .creationMode(CreationMode.CREATE_IF_MISSING) + .creationMode(CreationMode.CREATE_IF_MISSING_DEEP_CHECK) .relationshipIdToInvert(relationshipIdThatWasUpdated) .originatorUUID(originatorUUID) .fromSubstance(substanceIdToUpdate) diff --git a/gsrs-module-substances-core/src/main/java/ix/ginas/modelBuilders/AbstractSubstanceBuilder.java b/gsrs-module-substances-core/src/main/java/ix/ginas/modelBuilders/AbstractSubstanceBuilder.java index 5bee7f92c..e6016e566 100644 --- a/gsrs-module-substances-core/src/main/java/ix/ginas/modelBuilders/AbstractSubstanceBuilder.java +++ b/gsrs-module-substances-core/src/main/java/ix/ginas/modelBuilders/AbstractSubstanceBuilder.java @@ -421,9 +421,14 @@ public T setToPublic(){ } public T addRelationshipTo(Substance relatedSubstance, String type){ + return addRelationshipTo(relatedSubstance,type,(rr)->{}); + } + public T addRelationshipTo(Substance relatedSubstance, String type, Consumer mutate){ Relationship rel = new Relationship(); rel.type = type; rel.relatedSubstance = relatedSubstance.asSubstanceReference(); + mutate.accept(rel); + return addRelationship(rel); } diff --git a/gsrs-module-substances-core/src/main/java/ix/ginas/models/v1/Relationship.java b/gsrs-module-substances-core/src/main/java/ix/ginas/models/v1/Relationship.java index ea45493a6..9c8b1609d 100644 --- a/gsrs-module-substances-core/src/main/java/ix/ginas/models/v1/Relationship.java +++ b/gsrs-module-substances-core/src/main/java/ix/ginas/models/v1/Relationship.java @@ -12,8 +12,12 @@ import ix.ginas.models.utils.RelationshipUtil; import javax.persistence.*; + +import static org.assertj.core.api.Assertions.assertThatIllegalStateException; + import java.util.ArrayList; import java.util.List; +import java.util.Optional; @JSONEntity(title = "Relationship", isFinal = true) @@ -188,6 +192,28 @@ public boolean isEquivalentBaseRelationship(Relationship other){ return false; } + @JsonIgnore + public boolean isEquivalentFullRelationship(Relationship other){ + if (other.isEquivalentBaseRelationship(this)) { + if( (other.comments+"").equals(this.comments+"") && + (other.qualification+"").equals(this.qualification+"") && + (other.interactionType+"").equals(this.interactionType+"") && + (Optional.ofNullable(other.amount).map(oa -> oa.toString()).orElse("") + .equals( + Optional.ofNullable(this.amount).map(oa -> oa.toString()).orElse("")))) { + if((this.mediatorSubstance==null && other.mediatorSubstance==null)) { + return true; + }else if(this.mediatorSubstance!=null && other.mediatorSubstance!=null) { + return this.mediatorSubstance.isEquivalentTo(other.mediatorSubstance); + }else { + return false; + } + + } + } + return false; + } + public String toSimpleString(){ return type + ":" + relatedSubstance.refPname; }