# HG changeset patch # User casties # Date 1476385664 -7200 # Node ID c181cb6f1761d3191dbe013d404e665ec29368ad # Parent 86c3431092573b9ba121e9a3efefb01fb6288ba6 first version of equalsStructure to test when saving entities. diff -r 86c343109257 -r c181cb6f1761 src/main/java/org/mpi/openmind/repository/bo/Entity.java --- a/src/main/java/org/mpi/openmind/repository/bo/Entity.java Thu Oct 13 19:02:24 2016 +0200 +++ b/src/main/java/org/mpi/openmind/repository/bo/Entity.java Thu Oct 13 21:07:44 2016 +0200 @@ -714,7 +714,9 @@ /** * Returns the named attribute. - * + * + * Returns null if no attribute of this name exists. + * * @param name * @return */ @@ -1019,8 +1021,6 @@ public String toString() { String rowIdString = (this.getRowId() == null) ? "" : "rowId=" + this.getRowId() + ", "; String idString = (this.getId() == null) ? "" : "id=" + getId() + ", "; - - return "Entity[" + rowIdString + idString + "oc=" + this.getObjectClass() + ", ov=" + this.getOwnValue() + ", user=" + getUser() + ", sysStatus=" + this.getSystemStatus() + ", type=" + this.getType() + " att.size=" + this.attributes.size() + "]"; @@ -1084,60 +1084,154 @@ } } - public Relation containsSrcRel(Relation other){ - - for(Relation ownRel : getSourceRelations()){ - if(ownRel.equalsContent(other)){ - return ownRel; - } - } - return null; - } + /** + * Returns if this entity has this source relation. + * + * Compares the relations by content. + * + * @param other + * @return + */ + public Relation containsSrcRel(Relation other) { + for (Relation ownRel : getSourceRelations()) { + if (ownRel.equalsContent(other)) { + return ownRel; + } + } + return null; + } - public Relation containsTarRel(Relation other){ - for(Relation ownRel : getTargetRelations()){ - if(ownRel.equalsContent(other)){ - return ownRel; - } - } - return null; - } + /** + * Returns if this entity has this target relation. + * + * Compares the relations by content. + * + * @param other + * @return + */ + public Relation containsTarRel(Relation other) { + for (Relation ownRel : getTargetRelations()) { + if (ownRel.equalsContent(other)) { + return ownRel; + } + } + return null; + } - public boolean equalsContent(Entity other) throws Exception{ - - if(this.isLightweight() || other.isLightweight()){ - throw new Exception(" It is not possible to comparate the content of two lightWeight entities."); - } - - if(this.getAttributes().size() != other.getAttributes().size() || - this.sourceRelations.size() != other.sourceRelations.size() || - this.targetRelations.size() != other.targetRelations.size()){ - return false; - } - - for(Attribute att : this.getAttributes()){ - Attribute otherAtt = other.getAttributeByName(att.getName()); - if(!att.equalsContent(otherAtt)){ - return false; - } - } - - for(Relation rel : other.getSourceRelations()){ - if(containsSrcRel(rel) == null){ - return false; - } - } - - for(Relation rel : other.getTargetRelations()){ - if(this.containsTarRel(rel) == null){ - return false; - } - } - - - return true; - } + /** + * Returns if this entity has the same content as another entity. + * + * Compares contents of Attributes and Relations. + * + * @param other + * @return + * @throws Exception + */ + public boolean equalsContent(Entity other) throws Exception { + + if (this.isLightweight() || other.isLightweight()) { + throw new Exception("It is not possible to compare the content of lightWeight entities."); + } + + // compare number of attributes and relations + if (this.getAttributes().size() != other.getAttributes().size() + || this.sourceRelations.size() != other.sourceRelations.size() + || this.targetRelations.size() != other.targetRelations.size()) { + return false; + } + + // compare attributes + for (Attribute att : this.getAttributes()) { + Attribute otherAtt = other.getAttributeByName(att.getName()); + if (!att.equalsContent(otherAtt)) { + return false; + } + } + + // compare source relations + for (Relation rel : other.getSourceRelations()) { + if (containsSrcRel(rel) == null) { + return false; + } + } + + // compare target relations + for (Relation rel : other.getTargetRelations()) { + if (containsTarRel(rel) == null) { + return false; + } + } + + return true; + } + /** + * Returns if this entity has the same structure as another entity. + * + * Compares only names of Attributes and Relations. + * + * @param other + * @return + * @throws Exception + */ + public boolean equalsStructure(Entity other) throws Exception { + + if (this.isLightweight() || other.isLightweight()) { + throw new Exception("It is not possible to compare the structure of lightWeight entities."); + } + + // compare number of attributes and relations + if (this.sourceRelations.size() != other.sourceRelations.size() + || this.targetRelations.size() != other.targetRelations.size()) { + logger.debug("entities have different number of relations"); + return false; + } + + // compare attributes + if (this.attributes.size() >= other.attributes.size()) { + for (Attribute att : this.getAttributes()) { + Attribute otherAtt = other.getAttributeByName(att.getName()); + if (otherAtt == null || StringUtils.isEmpty(otherAtt.getOwnValue())) { + // other attribute is empty + if (!StringUtils.isEmpty(att.getOwnValue())) { + logger.debug("entities have different attributes"); + return false; + } + } + } + } else { + // other had more attributes - check from the other side + for (Attribute att : other.getAttributes()) { + Attribute thisAtt = this.getAttributeByName(att.getName()); + if (thisAtt == null || StringUtils.isEmpty(thisAtt.getOwnValue())) { + // this attribute is empty + if (!StringUtils.isEmpty(att.getOwnValue())) { + logger.debug("entities have different attributes"); + return false; + } + } + } + } + + // compare source relations + for (Relation rel : other.getSourceRelations()) { + if (containsSrcRel(rel) == null) { + logger.debug("entities have different source relations"); + return false; + } + } + + // compare target relations + for (Relation rel : other.getTargetRelations()) { + if (containsTarRel(rel) == null) { + logger.debug("entities have different target relations"); + return false; + } + } + + return true; + } + public void refreshEnt(Entity other, WrapperService ws) throws Exception{ //Attributes diff -r 86c343109257 -r c181cb6f1761 src/main/java/org/mpi/openmind/repository/bo/Relation.java --- a/src/main/java/org/mpi/openmind/repository/bo/Relation.java Thu Oct 13 19:02:24 2016 +0200 +++ b/src/main/java/org/mpi/openmind/repository/bo/Relation.java Thu Oct 13 21:07:44 2016 +0200 @@ -227,20 +227,25 @@ this.targetId = targetId; } - public boolean equalsContent(Relation other){ - if(other == null){ - return false; - } - - if(StringUtils.equals(getOwnValue(), other.getOwnValue()) && - OMUtils.equals(getSourceId(), other.getSourceId()) && - OMUtils.equals(getTargetId(), other.getTargetId())){ - return true; - } - - return false; - } - + /** + * Returns if this Relation is equal to another Relation. + * + * Compares ownvalue (=type), source id and target id. + * + * @param other + * @return + */ + public boolean equalsContent(Relation other) { + if (other == null) { + return false; + } + if (StringUtils.equals(getOwnValue(), other.getOwnValue()) + && OMUtils.equals(getSourceId(), other.getSourceId()) + && OMUtils.equals(getTargetId(), other.getTargetId())) { + return true; + } + return false; + } @Override public String toString() { diff -r 86c343109257 -r c181cb6f1761 src/main/java/org/mpi/openmind/repository/services/AbstractPersistenceService.java --- a/src/main/java/org/mpi/openmind/repository/services/AbstractPersistenceService.java Thu Oct 13 19:02:24 2016 +0200 +++ b/src/main/java/org/mpi/openmind/repository/services/AbstractPersistenceService.java Thu Oct 13 21:07:44 2016 +0200 @@ -616,6 +616,10 @@ } // set the first current entity as previous Entity previousEntity = previousEntityList.get(0); + // compare old and new entity + if (! previousEntity.equalsStructure(entity)) { + logger.warn("Entity to save has different structure! old="+previousEntity+" new="+entity); + } logger.info("Saving previous entity: " + previousEntity); this.persistEntityAsPrevious(session, previousEntity); }