Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
anhefti committed Nov 10, 2023
1 parent 1498fa4 commit 6ad231f
Showing 1 changed file with 19 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,7 @@ public Result<Collection<Exam>> getExamsForStatus(
return Result.tryCatch(() -> {

final List<String> stateNames = (status != null && status.length > 0)
? Arrays.asList(status)
.stream().map(s -> s.name())
? Arrays.stream(status).map(Enum::name)
.collect(Collectors.toList())
: null;
return this.examRecordDAO
Expand Down Expand Up @@ -443,7 +442,7 @@ public void releaseAgedLocks() {

if (lockedRecords != null && !lockedRecords.isEmpty()) {
final long millisecondsNow = Utils.getMillisecondsNow();
lockedRecords.stream().forEach(record -> {
lockedRecords.forEach(record -> {
try {
final String lastUpdateString = record.getLastupdate();
if (StringUtils.isNotBlank(lastUpdateString)) {
Expand All @@ -454,7 +453,7 @@ public void releaseAgedLocks() {
}
}
} catch (final Exception e) {
log.warn("Failed to release aged write lock for exam: {} cause:", record, e.getMessage());
log.warn("Failed to release aged write lock for exam: {} cause: {}", record, e.getMessage());
}
});
}
Expand Down Expand Up @@ -522,7 +521,7 @@ public Result<Collection<EntityKey>> delete(final Set<EntityKey> all) {
return Collections.emptyList();
}

// notify exam deletion listener about following deletion, to cleanup stuff before deletion
// notify exam deletion listener about following deletion, to clean up stuff before deletion
this.applicationEventPublisher.publishEvent(new ExamDeletionEvent(ids));

this.examRecordMapper.deleteByExample()
Expand All @@ -531,8 +530,7 @@ public Result<Collection<EntityKey>> delete(final Set<EntityKey> all) {
.execute();

// delete all additional attributes
ids.stream()
.forEach(id -> this.additionalAttributesDAO.deleteAll(EntityType.EXAM, id));
ids.forEach(id -> this.additionalAttributesDAO.deleteAll(EntityType.EXAM, id));

return ids.stream()
.map(id -> new EntityKey(id, EntityType.EXAM))
Expand All @@ -549,7 +547,7 @@ public Set<EntityDependency> getDependencies(final BulkAction bulkAction) {
}

// define the select function in case of source type
Function<EntityKey, Result<Collection<EntityDependency>>> selectionFunction;
final Function<EntityKey, Result<Collection<EntityDependency>>> selectionFunction;
switch (bulkAction.sourceType) {
case INSTITUTION:
selectionFunction = this::allIdsOfInstitution;
Expand Down Expand Up @@ -744,11 +742,9 @@ private EntityDependency getDependency(final Exam exam, final EntityKey parent)
}

private Result<Collection<Exam>> toDomainModel(final Collection<ExamRecord> records) {
return Result.tryCatch(() -> {
return records.stream()
.map(rec -> this.toDomainModel(rec).getOrThrow())
.collect(Collectors.toList());
});
return Result.tryCatch(() -> records.stream()
.map(rec -> this.toDomainModel(rec).getOrThrow())
.collect(Collectors.toList()));
}

private Result<Exam> toDomainModel(final ExamRecord record) {
Expand All @@ -771,7 +767,9 @@ private Result<Exam> toDomainModel(final ExamRecord record) {
.getAdditionalAttributes(EntityType.EXAM, record.getId())
.getOrThrow()
.stream()
.collect(Collectors.toMap(rec -> rec.getName(), rec -> rec.getValue()));
.collect(Collectors.toMap(
AdditionalAttributeRecord::getName,
AdditionalAttributeRecord::getValue));

return new Exam(
record.getId(),
Expand Down Expand Up @@ -824,21 +822,20 @@ private QuizData saveAdditionalQuizAttributes(final Long examId, final QuizData
additionalAttributes.put(QuizData.QUIZ_ATTR_START_URL, quizData.startURL);
}

additionalAttributes.entrySet().forEach(entry -> {
final String value = entry.getValue();
additionalAttributes.forEach((key, value) -> {
if (value == null) {
this.additionalAttributesDAO.delete(
EntityType.EXAM,
examId,
entry.getKey());
key);
} else {
this.additionalAttributesDAO.saveAdditionalAttribute(
EntityType.EXAM,
examId,
entry.getKey(),
value)
EntityType.EXAM,
examId,
key,
value)
.onError(error -> log.error("Failed to save additional quiz attribute: {}",
entry.getKey(),
key,
error));
}
});
Expand Down

0 comments on commit 6ad231f

Please sign in to comment.