diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/impl/ExamRecordDAO.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/impl/ExamRecordDAO.java index e9101847..505a2077 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/impl/ExamRecordDAO.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/dao/impl/ExamRecordDAO.java @@ -409,18 +409,28 @@ public class ExamRecordDAO { return Result.tryCatch(() -> { // fist check if it is not already existing - final List records = this.examRecordMapper.selectByExample() - .where(ExamRecordDynamicSqlSupport.lmsSetupId, isEqualTo(exam.lmsSetupId)) - .and(ExamRecordDynamicSqlSupport.externalId, isEqualTo(exam.externalId)) - .build() - .execute(); - - // if there is already an existing imported exam for the quiz, this is - // used to save instead of create a new one - if (records != null && records.size() > 0) { - final ExamRecord examRecord = records.get(0); - // if the same institution tries to import an exam that already exists throw an error - if (exam.institutionId.equals(examRecord.getInstitutionId())) { + if (exam.lmsSetupId != null) { + final List records = this.examRecordMapper.selectByExample() + .where(lmsSetupId, isEqualTo(exam.lmsSetupId)) + .and(externalId, isEqualTo(exam.externalId)) + .build() + .execute(); + // if there is already an existing imported exam for the quiz, this is + // used to save instead of create a new one + if (records != null && !records.isEmpty()) { + final ExamRecord examRecord = records.get(0); + // if the same institution tries to import an exam that already exists throw an error + if (exam.institutionId.equals(examRecord.getInstitutionId())) { + throw new DuplicateResourceException(EntityType.EXAM, exam.externalId); + } + } + } else { + final Long nameCount = this.examRecordMapper.countByExample() + .where(institutionId, isEqualTo(exam.institutionId)) + .and(quizName, isEqualTo(exam.name)) + .build() + .execute(); + if (nameCount > 0) { throw new DuplicateResourceException(EntityType.EXAM, exam.externalId); } } diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/exam/impl/ExamAdminServiceImpl.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/exam/impl/ExamAdminServiceImpl.java index 99c90337..8e9935d6 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/exam/impl/ExamAdminServiceImpl.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/exam/impl/ExamAdminServiceImpl.java @@ -202,7 +202,7 @@ public class ExamAdminServiceImpl implements ExamAdminService { @Override public Result isRestricted(final Exam exam) { - if (exam == null) { + if (exam == null || exam.lmsSetupId == null) { return Result.of(false); } diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/SEBRestrictionServiceImpl.java b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/SEBRestrictionServiceImpl.java index 68a6c106..74ed6d38 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/SEBRestrictionServiceImpl.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/servicelayer/lms/impl/SEBRestrictionServiceImpl.java @@ -78,6 +78,10 @@ public class SEBRestrictionServiceImpl implements SEBRestrictionService { @Override public boolean checkSebRestrictionSet(final Exam exam) { + if (exam == null || exam.lmsSetupId == null) { + return false; + } + final LmsSetup lmsSetup = this.lmsAPIService .getLmsSetup(exam.lmsSetupId) .getOr(null); @@ -93,6 +97,10 @@ public class SEBRestrictionServiceImpl implements SEBRestrictionService { @Override @Transactional public Result getSEBRestrictionFromExam(final Exam exam) { + if (exam == null || exam.lmsSetupId == null) { + return Result.ofError(new NoSEBRestrictionException("exam or lms setup has null reference")); + } + return Result.tryCatch(() -> { // load the config keys from restriction and merge with new generated config keys final Collection generatedKeys = this.examConfigService diff --git a/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ExamAdministrationController.java b/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ExamAdministrationController.java index e017a300..8ec76573 100644 --- a/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ExamAdministrationController.java +++ b/src/main/java/ch/ethz/seb/sebserver/webservice/weblayer/api/ExamAdministrationController.java @@ -19,6 +19,7 @@ import java.util.stream.Collectors; import javax.validation.Valid; +import ch.ethz.seb.sebserver.webservice.servicelayer.lms.impl.NoSEBRestrictionException; import org.apache.commons.lang3.StringUtils; import org.joda.time.DateTime; import org.mybatis.dynamic.sql.SqlTable; @@ -735,6 +736,10 @@ public class ExamAdministrationController extends EntityController { private Result applySEBRestriction(final Exam exam, final boolean restrict) { + if (exam == null || exam.lmsSetupId == null) { + return Result.ofError(new NoSEBRestrictionException("exam or lms setup has null reference")); + } + return Result.tryCatch(() -> { final LmsSetup lmsSetup = this.lmsAPIService.getLmsSetup(exam.lmsSetupId) .getOrThrow();