Skip to content

Commit

Permalink
Tutorial groups: Improve functionality of import button (#8782)
Browse files Browse the repository at this point in the history
  • Loading branch information
az108 authored Aug 3, 2024
1 parent 305aa74 commit 206aecc
Show file tree
Hide file tree
Showing 12 changed files with 384 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,20 @@ public Set<TutorialGroupRegistrationImportDTO> importRegistrations(Course course

// === Step 1: Try to find all tutorial groups with the mentioned title. Create them if they do not exist yet ===
Set<TutorialGroupRegistrationImportDTO> registrationsWithTitle = filterOutWithoutTitle(registrations, failedRegistrations);
// Add registrations to failedRegistrations if the tutorial group already exists
var titlesMentionedInRegistrations = registrations.stream().map(TutorialGroupRegistrationImportDTO::title).filter(Objects::nonNull).map(String::trim)
.collect(Collectors.toSet());

var foundTutorialGroups = tutorialGroupRepository.findAllByCourseId(course.getId()).stream()
.filter(tutorialGroup -> titlesMentionedInRegistrations.contains(tutorialGroup.getTitle())).collect(Collectors.toSet());

registrationsWithTitle.forEach(registration -> {
if (foundTutorialGroups.stream().anyMatch(tutorialGroup -> tutorialGroup.getTitle().equals(registration.title().trim()))) {
if (registration.student() != null && !StringUtils.hasText(registration.student().registrationNumber()) && !StringUtils.hasText(registration.student().login())) {
failedRegistrations.add(registration);
}
}
});
Map<String, TutorialGroup> tutorialGroupTitleToTutorialGroup = findOrCreateTutorialGroups(course, registrationsWithTitle).stream()
.collect(Collectors.toMap(TutorialGroup::getTitle, Function.identity()));

Expand Down Expand Up @@ -416,6 +430,26 @@ private Set<TutorialGroup> findOrCreateTutorialGroups(Course course, Set<Tutoria
tutorialGroup.setTeachingAssistant(requestingUser);
tutorialGroup.setIsOnline(false);
tutorialGroup.setCampus("Campus");

// Set additional fields from registrations
Optional<TutorialGroupRegistrationImportDTO> registrationOpt = registrations.stream().filter(r -> title.equals(r.title())).findFirst();
registrationOpt.ifPresent(registration -> {
if (registration.campus() != null && !registration.campus().isEmpty()) {
tutorialGroup.setCampus(registration.campus());
}
if (registration.capacity() != null) {
tutorialGroup.setCapacity(registration.capacity());
}
if (registration.language() != null && !registration.language().isEmpty()) {
tutorialGroup.setLanguage(registration.language());
}
if (registration.additionalInformation() != null && !registration.additionalInformation().isEmpty()) {
tutorialGroup.setAdditionalInformation(registration.additionalInformation());
}
if (registration.isOnline() != null) {
tutorialGroup.setIsOnline(registration.isOnline());
}
});
return tutorialGroup;
}).collect(Collectors.toSet());

Expand Down Expand Up @@ -709,12 +743,12 @@ public String exportTutorialGroupsToJSON(Long courseId, List<String> selectedFie
writeTutorialGroupJSON(groupData, tutorialGroup, selectedFields);
if (selectedFields.contains("Students")) {
if ((tutorialGroup.getRegistrations() != null) && (!tutorialGroup.getRegistrations().isEmpty())) {
List<Map<String, Object>> studentsList = new ArrayList<>();
for (TutorialGroupRegistration registration : tutorialGroup.getRegistrations()) {
User student = registration.getStudent();
groupData.put("Students",
groupData.get("Students") != null ? groupData.get("Students") + convertStudentToString(student) : convertStudentToString(student));
studentsList.add(convertStudentToMap(student));
}
groupData.put("Students", groupData.get("Students").toString().substring(0, groupData.get("Students").toString().length() - 1));
groupData.put("Students", studentsList);
}
}
exportData.add(groupData);
Expand All @@ -724,12 +758,20 @@ public String exportTutorialGroupsToJSON(Long courseId, List<String> selectedFie
return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(exportData);
}

private void writeTutorialGroupJSON(Map<String, Object> groupData, TutorialGroup tutorialGroup, List<String> selectedFields) throws JsonProcessingException {
private void writeTutorialGroupJSON(Map<String, Object> groupData, TutorialGroup tutorialGroup, List<String> selectedFields) {
for (String field : selectedFields) {
groupData.put(field, getCSVInput(tutorialGroup, field));
}
}

private Map<String, Object> convertStudentToMap(User student) {
Map<String, Object> studentMap = new LinkedHashMap<>();
studentMap.put("RegistrationNumber", getValueOrDefault(student.getRegistrationNumber()));
studentMap.put("FirstName", getValueOrDefault(student.getFirstName()));
studentMap.put("LastName", getValueOrDefault(student.getLastName()));
return studentMap;
}

private String getValueOrDefault(Object value) {
return value != null ? value.toString() : "";
}
Expand Down Expand Up @@ -830,15 +872,4 @@ public Set<User> getStudentsRegisteredForTutorial(Set<TutorialGroupRegistration>
}
return students;
}

private String convertStudentToString(User student) {
StringBuilder studentString = new StringBuilder();
studentString.append(student.getRegistrationNumber());
studentString.append(" ");
studentString.append(student.getFirstName());
studentString.append(" ");
studentString.append(student.getLastName());
studentString.append(",");
return studentString.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -604,14 +604,16 @@ public enum TutorialGroupImportErrors {
*/
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public record TutorialGroupRegistrationImportDTO(@Nullable String title, @Nullable StudentDTO student, @Nullable Boolean importSuccessful,
@Nullable TutorialGroupImportErrors error) {
@Nullable TutorialGroupImportErrors error, @Nullable String campus, @Nullable Integer capacity, @Nullable String language, @Nullable String additionalInformation,
@Nullable Boolean isOnline) {

public TutorialGroupRegistrationImportDTO withImportResult(boolean importSuccessful, TutorialGroupImportErrors error) {
return new TutorialGroupRegistrationImportDTO(title(), student(), importSuccessful, error);
return new TutorialGroupRegistrationImportDTO(title(), student(), importSuccessful, error, campus(), capacity(), language(), additionalInformation(), isOnline());
}

public TutorialGroupRegistrationImportDTO(@Nullable String title, @Nullable StudentDTO student) {
this(title, student, null, null);
public TutorialGroupRegistrationImportDTO(@Nullable String title, @Nullable StudentDTO student, @Nullable String campus, @Nullable Integer capacity,
@Nullable String language, @Nullable String additionalInformation, @Nullable Boolean isOnline) {
this(title, student, null, null, campus, capacity, language, additionalInformation, isOnline);
}

@Override
Expand Down Expand Up @@ -640,7 +642,9 @@ public int hashCode() {

@Override
public String toString() {
return "TutorialGroupRegistrationImportDTO{" + "title='" + title + '\'' + ", student=" + student + ", importSuccessful=" + importSuccessful + ", error=" + error + '}';
return "TutorialGroupRegistrationImportDTO{" + "title='" + title + '\'' + ", student=" + student + ", importSuccessful=" + importSuccessful + ", error=" + error
+ ", campus=" + campus + ", capacity=" + capacity + ", language=" + language + ", additionalInformation=" + additionalInformation + ", isOnline=" + isOnline
+ '}';
}
}
}
Loading

0 comments on commit 206aecc

Please sign in to comment.