fixed OrientationCheck

This commit is contained in:
anhefti 2022-05-05 10:11:31 +02:00
parent 0f8910bb3f
commit ca1cf4b329

View file

@ -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<Long> 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();