diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/PaginationServiceImpl.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/PaginationServiceImpl.java index eadf45e1..090e3cac 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/PaginationServiceImpl.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/PaginationServiceImpl.java @@ -32,6 +32,7 @@ import ch.ethz.seb.sebserver.webservice.datalayer.batis.mapper.ClientConnectionR import ch.ethz.seb.sebserver.webservice.datalayer.batis.mapper.ClientEventRecordDynamicSqlSupport; import ch.ethz.seb.sebserver.webservice.datalayer.batis.mapper.ConfigurationNodeRecordDynamicSqlSupport; import ch.ethz.seb.sebserver.webservice.datalayer.batis.mapper.ExamRecordDynamicSqlSupport; +import ch.ethz.seb.sebserver.webservice.datalayer.batis.mapper.ExamTemplateRecordDynamicSqlSupport; import ch.ethz.seb.sebserver.webservice.datalayer.batis.mapper.InstitutionRecordDynamicSqlSupport; import ch.ethz.seb.sebserver.webservice.datalayer.batis.mapper.LmsSetupRecordDynamicSqlSupport; import ch.ethz.seb.sebserver.webservice.datalayer.batis.mapper.SebClientConfigRecordDynamicSqlSupport; @@ -203,11 +204,11 @@ public class PaginationServiceImpl implements PaginationService { if (StringUtils.isNotBlank(sortColumnName)) { switch (sortOrder) { case DESCENDING: { - PageHelper.orderBy(sortColumnName + " DESC"); + PageHelper.orderBy(sortColumnName + " DESC, id DESC"); break; } default: { - PageHelper.orderBy(sortColumnName); + PageHelper.orderBy(sortColumnName + ", id"); break; } } @@ -265,6 +266,18 @@ public class PaginationServiceImpl implements PaginationService { this.sortColumnMapping.put(LmsSetupRecordDynamicSqlSupport.lmsSetupRecord.name(), lmsSetupTableMap); this.defaultSortColumn.put(LmsSetupRecordDynamicSqlSupport.lmsSetupRecord.name(), Domain.LMS_SETUP.ATTR_ID); + // Exam Template Table + final Map examTemplateTableMap = new HashMap<>(); + examTemplateTableMap.put(Entity.FILTER_ATTR_INSTITUTION, institutionNameRef); + examTemplateTableMap.put(Domain.EXAM_TEMPLATE.ATTR_NAME, ExamTemplateRecordDynamicSqlSupport.name.name()); + examTemplateTableMap.put(Domain.EXAM_TEMPLATE.ATTR_EXAM_TYPE, + ExamTemplateRecordDynamicSqlSupport.examType.name()); + + this.sortColumnMapping.put(ExamTemplateRecordDynamicSqlSupport.examTemplateRecord.name(), examTemplateTableMap); + this.defaultSortColumn.put( + ExamTemplateRecordDynamicSqlSupport.examTemplateRecord.name(), + Domain.EXAM_TEMPLATE.ATTR_ID); + // Exam Table final Map examTableMap = new HashMap<>(); examTableMap.put(Entity.FILTER_ATTR_INSTITUTION, institutionNameRef); diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/impl/ExamTemplateDAOImpl.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/impl/ExamTemplateDAOImpl.java index ce75daa1..0671d0ac 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/impl/ExamTemplateDAOImpl.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/impl/ExamTemplateDAOImpl.java @@ -22,6 +22,9 @@ import java.util.stream.Collectors; import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.StringUtils; +import org.mybatis.dynamic.sql.SqlBuilder; +import org.mybatis.dynamic.sql.select.MyBatis3SelectModelAdapter; +import org.mybatis.dynamic.sql.select.QueryExpressionDSL; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; @@ -42,6 +45,7 @@ import ch.ethz.seb.sebserver.gbl.util.Result; import ch.ethz.seb.sebserver.webservice.datalayer.batis.mapper.ExamTemplateRecordDynamicSqlSupport; import ch.ethz.seb.sebserver.webservice.datalayer.batis.mapper.ExamTemplateRecordMapper; import ch.ethz.seb.sebserver.webservice.datalayer.batis.mapper.IndicatorRecordDynamicSqlSupport; +import ch.ethz.seb.sebserver.webservice.datalayer.batis.mapper.InstitutionRecordDynamicSqlSupport; import ch.ethz.seb.sebserver.webservice.datalayer.batis.model.ExamTemplateRecord; import ch.ethz.seb.sebserver.webservice.servicelayer.bulkaction.impl.BulkAction; import ch.ethz.seb.sebserver.webservice.servicelayer.dao.AdditionalAttributesDAO; @@ -140,24 +144,38 @@ public class ExamTemplateDAOImpl implements ExamTemplateDAO { final FilterMap filterMap, final Predicate predicate) { - return Result.tryCatch(() -> this.examTemplateRecordMapper - .selectByExample() - .where( - ExamTemplateRecordDynamicSqlSupport.institutionId, - isEqualToWhenPresent(filterMap.getInstitutionId())) - .and( - ExamTemplateRecordDynamicSqlSupport.name, - isLikeWhenPresent(filterMap.getExamTemplateName())) - .and( - ExamTemplateRecordDynamicSqlSupport.examType, - isEqualToWhenPresent(filterMap.getString(ExamTemplate.FILTER_ATTR_EXAM_TYPE))) - .build() - .execute() - .stream() - .map(this::toDomainModel) - .flatMap(DAOLoggingSupport::logAndSkipOnError) - .filter(predicate) - .collect(Collectors.toList())); + return Result.tryCatch(() -> { + final QueryExpressionDSL>>.QueryExpressionWhereBuilder whereClause = + (filterMap.getBoolean(FilterMap.ATTR_ADD_INSITUTION_JOIN)) + ? this.examTemplateRecordMapper + .selectByExample() + .join(InstitutionRecordDynamicSqlSupport.institutionRecord) + .on(InstitutionRecordDynamicSqlSupport.id, + SqlBuilder.equalTo(ExamTemplateRecordDynamicSqlSupport.institutionId)) + .where( + ExamTemplateRecordDynamicSqlSupport.institutionId, + isEqualToWhenPresent(filterMap.getInstitutionId())) + : this.examTemplateRecordMapper + .selectByExample() + .where( + ExamTemplateRecordDynamicSqlSupport.institutionId, + isEqualToWhenPresent(filterMap.getInstitutionId())); + + return whereClause + .and( + ExamTemplateRecordDynamicSqlSupport.name, + isLikeWhenPresent(filterMap.getExamTemplateName())) + .and( + ExamTemplateRecordDynamicSqlSupport.examType, + isEqualToWhenPresent(filterMap.getString(ExamTemplate.FILTER_ATTR_EXAM_TYPE))) + .build() + .execute() + .stream() + .map(this::toDomainModel) + .flatMap(DAOLoggingSupport::logAndSkipOnError) + .filter(predicate) + .collect(Collectors.toList()); + }); } @Override diff --git a/src/test/java/ch/ethz/seb/sebserver/webservice/integration/api/admin/AdministrationAPIIntegrationTester.java b/src/test/java/ch/ethz/seb/sebserver/webservice/integration/api/admin/AdministrationAPIIntegrationTester.java index 5722dd74..b053c2d8 100644 --- a/src/test/java/ch/ethz/seb/sebserver/webservice/integration/api/admin/AdministrationAPIIntegrationTester.java +++ b/src/test/java/ch/ethz/seb/sebserver/webservice/integration/api/admin/AdministrationAPIIntegrationTester.java @@ -15,6 +15,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. import java.util.Collection; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.stream.Collectors; @@ -248,11 +249,12 @@ public abstract class AdministrationAPIIntegrationTester { } protected String getOrderedUUIDs(final Collection list) { - return list + final List l = list .stream() .map(userInfo -> userInfo.getModelId()) - .collect(Collectors.toList()) - .toString(); + .collect(Collectors.toList()); + l.sort((s1, s2) -> s1.compareTo(s2)); + return l.toString(); } } diff --git a/src/test/java/ch/ethz/seb/sebserver/webservice/integration/api/admin/UserAPITest.java b/src/test/java/ch/ethz/seb/sebserver/webservice/integration/api/admin/UserAPITest.java index 5d3b8495..272101d9 100644 --- a/src/test/java/ch/ethz/seb/sebserver/webservice/integration/api/admin/UserAPITest.java +++ b/src/test/java/ch/ethz/seb/sebserver/webservice/integration/api/admin/UserAPITest.java @@ -268,7 +268,7 @@ public class UserAPITest extends AdministrationAPIIntegrationTester { assertTrue(userInfos.numberOfPages == 1); assertNotNull(userInfos.content); assertTrue(userInfos.content.size() == 3); - assertEquals("[user5, user2, user1]", getOrderedUUIDs(userInfos.content)); + assertEquals("[user1, user2, user5]", getOrderedUUIDs(userInfos.content)); } @Test @@ -347,7 +347,7 @@ public class UserAPITest extends AdministrationAPIIntegrationTester { assertTrue(userInfos.numberOfPages == 2); assertNotNull(userInfos.content); assertTrue(userInfos.content.size() == 3); - assertEquals("[user7, user6, user4]", getOrderedUUIDs(userInfos.content)); + assertEquals("[user4, user6, user7]", getOrderedUUIDs(userInfos.content)); } @Test