diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/datalayer/checks/OrientationTableDuplicatesCheck.java b/src/main/java/ch/ethz/seb/sebserver/webservice/datalayer/checks/OrientationTableDuplicatesCheck.java index 0a391e44..ad1a226f 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/datalayer/checks/OrientationTableDuplicatesCheck.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/datalayer/checks/OrientationTableDuplicatesCheck.java @@ -11,7 +11,9 @@ package ch.ethz.seb.sebserver.webservice.datalayer.checks; import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.stream.Collectors; +import org.mybatis.dynamic.sql.SqlBuilder; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; @@ -19,6 +21,7 @@ import org.springframework.transaction.annotation.Transactional; import ch.ethz.seb.sebserver.gbl.profile.WebServiceProfile; import ch.ethz.seb.sebserver.gbl.util.Result; import ch.ethz.seb.sebserver.webservice.DBIntegrityCheck; +import ch.ethz.seb.sebserver.webservice.datalayer.batis.mapper.OrientationRecordDynamicSqlSupport; import ch.ethz.seb.sebserver.webservice.datalayer.batis.mapper.OrientationRecordMapper; import ch.ethz.seb.sebserver.webservice.datalayer.batis.model.OrientationRecord; @@ -68,10 +71,15 @@ public class OrientationTableDuplicatesCheck implements DBIntegrityCheck { } if (tryFix) { - toDelete + final List checkedToDelete = toDelete + .stream() + .filter(this::doubleCheck) + .collect(Collectors.toList()); + + checkedToDelete .stream() .forEach(this.orientationRecordMapper::deleteByPrimaryKey); - return "Fixed duplicates by deletion: " + toDelete; + return "Fixed duplicates by deletion: " + checkedToDelete + " from findings:" + toDelete; } else { return "Found duplicates: " + toDelete; } @@ -79,6 +87,24 @@ public class OrientationTableDuplicatesCheck implements DBIntegrityCheck { }); } + private boolean doubleCheck(final Long id) { + try { + final OrientationRecord selectByPrimaryKey = this.orientationRecordMapper.selectByPrimaryKey(id); + final Long count = this.orientationRecordMapper.countByExample() + .where( + OrientationRecordDynamicSqlSupport.configAttributeId, + SqlBuilder.isEqualTo(selectByPrimaryKey.getConfigAttributeId())) + .and( + OrientationRecordDynamicSqlSupport.templateId, + SqlBuilder.isEqualTo(selectByPrimaryKey.getTemplateId())) + .build() + .execute(); + return count != null && count.longValue() > 1; + } catch (final Exception e) { + return false; + } + } + @Override public String toString() { final StringBuilder builder = new StringBuilder();