Skip to content

Commit

Permalink
prepare for userService imports
Browse files Browse the repository at this point in the history
  • Loading branch information
julianlxs committed Dec 29, 2024
1 parent 4770d35 commit 639ffd8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.unistuttgart.iste.meitrex.assignment_service.exception;

// will be deleted, now in userService
public class UserServiceConnectionException extends Exception {
public UserServiceConnectionException(String message) {
super(message);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package de.unistuttgart.iste.meitrex.assignment_service.service;


import de.unistuttgart.iste.meitrex.assignment_service.exception.*;
import de.unistuttgart.iste.meitrex.assignment_service.exception.ExternalPlatformConnectionException;
import de.unistuttgart.iste.meitrex.assignment_service.exception.ManualMappingRequiredException;
import de.unistuttgart.iste.meitrex.user_service.exception.UserServiceConnectionException;
import de.unistuttgart.iste.meitrex.assignment_service.persistence.entity.*;
import de.unistuttgart.iste.meitrex.assignment_service.persistence.mapper.AssignmentMapper;
import de.unistuttgart.iste.meitrex.assignment_service.persistence.repository.GradingRepository;
Expand All @@ -18,9 +20,6 @@
import jakarta.persistence.EntityNotFoundException;
import jakarta.validation.ValidationException;
import lombok.RequiredArgsConstructor;
import org.springframework.graphql.client.GraphQlClient;
import org.springframework.graphql.client.ClientGraphQlResponse;
import reactor.core.publisher.SynchronousSink;
import org.springframework.stereotype.Service;
import org.json.*;

Expand All @@ -47,7 +46,8 @@ public class GradingService {
private final ManualMappingInstanceRepository manualMappingInstanceRepository;
private final UnfinishedGradingRepository unfinishedGradingRepository;

private final GraphQlClient userServiceClient;
private final UserServiceClient userServiceClient;
private final CourseServiceClient courseServiceClient;

// these need to be set!
private static final String authToken = "";
Expand Down Expand Up @@ -100,7 +100,7 @@ public void importGradingsForAssignment(final UUID assignmentId, final LoggedInU
// return; TODO return or throw wrapped exception?
}

final List<Map<String, Object>> meitrexStudentInfoList;
final List<UserInfo> meitrexStudentInfoList;
try {
meitrexStudentInfoList = getMeitrexStudentInfoList();
} catch (UserServiceConnectionException e){
Expand All @@ -124,7 +124,7 @@ public void importGradingsForAssignment(final UUID assignmentId, final LoggedInU
* @param assignmentEntity the assignment id which the gradings belong to
* @return List of parsed grading entities
*/
private List<GradingEntity> parseStringIntoGradingEntityList(final String string, final AssignmentEntity assignmentEntity, final List<Map<String, Object>> meitrexStudentInfoList) {
private List<GradingEntity> parseStringIntoGradingEntityList(final String string, final AssignmentEntity assignmentEntity, final List<UserInfo> meitrexStudentInfoList) {
JSONArray gradingArray = new JSONArray(string);
final List<GradingEntity> gradingEntityList = new ArrayList<>(gradingArray.length());
GradingEntity gradingEntity;
Expand All @@ -149,7 +149,7 @@ private List<GradingEntity> parseStringIntoGradingEntityList(final String string
* @param assignmentEntity the assignment id which the grading belongs to
* @return parsed grading entity
*/
private GradingEntity parseIntoGradingEntity(final JSONObject jsonObject, final AssignmentEntity assignmentEntity, final List<Map<String, Object>> meitrexStudentInfoList) throws ManualMappingRequiredException, ExternalPlatformConnectionException {
private GradingEntity parseIntoGradingEntity(final JSONObject jsonObject, final AssignmentEntity assignmentEntity, final List<UserInfo> meitrexStudentInfoList) throws ManualMappingRequiredException, ExternalPlatformConnectionException {
final GradingEntity gradingEntity = new GradingEntity();

String externalStudentId = jsonObject.getString("studentId"); // TODO match this to Meitrex student id
Expand Down Expand Up @@ -287,7 +287,7 @@ private void logGradingImported(final GradingEntity gradingEntity) {
topicPublisher.notifyUserWorkedOnContent(userProgressLogEvent);
}

private UUID getStudentIdFromExternalStudentId(final String externalStudentId, final List<Map<String, Object>> meitrexStudentInfoList) throws ManualMappingRequiredException, ExternalPlatformConnectionException {
private UUID getStudentIdFromExternalStudentId(final String externalStudentId, final List<UserInfo> meitrexStudentInfoList) throws ManualMappingRequiredException, ExternalPlatformConnectionException {
Optional<StudentMappingEntity> studentMappingEntity = studentMappingRepository.findById(externalStudentId);
if (studentMappingEntity.isPresent()) {
return studentMappingEntity.get().getMeitrexStudentId();
Expand All @@ -297,33 +297,33 @@ private UUID getStudentIdFromExternalStudentId(final String externalStudentId, f
return newMeitrexStudentId;
}

private UUID findNewStudentIdFromExternalStudentId(final String externalStudentId, final List<Map<String, Object>> meitrexStudentInfoList) throws ManualMappingRequiredException, ExternalPlatformConnectionException {
private UUID findNewStudentIdFromExternalStudentId(final String externalStudentId, final List<UserInfo> meitrexStudentInfoList) throws ManualMappingRequiredException, ExternalPlatformConnectionException {
JSONObject externalStudentInfo = getExternalStudentInfo(externalStudentId);

// list is fetched from user service at the beginning, rather than for each grading
// final List<Map<String, Object>> meitrexStudentInfoList = getMeitrexStudentInfoList();
// final List<UserInfo> meitrexStudentInfoList = getMeitrexStudentInfoList();

Object lastName = externalStudentInfo.get("lastname");
Object firstName = externalStudentInfo.get("firstname");

// filter by last name
List<Map<String,Object>> filteredByLastName = meitrexStudentInfoList.stream()
.filter(userInfo -> userInfo.get("lastName").equals(lastName))
List<UserInfo> filteredByLastName = meitrexStudentInfoList.stream()
.filter(userInfo -> userInfo.getLastName().equals(lastName))
.toList();
if (filteredByLastName.isEmpty()) {
throw new ManualMappingRequiredException(externalStudentInfo);
} else if (filteredByLastName.size() == 1) {
return (UUID) filteredByLastName.getFirst().get("id");
return (UUID) filteredByLastName.getFirst().getId();
}

// filter by first name, if there are still multiple candidates
List<Map<String,Object>> filteredByFirstName = filteredByLastName.stream()
.filter(userInfo -> userInfo.get("firstName").equals(firstName))
List<UserInfo> filteredByFirstName = filteredByLastName.stream()
.filter(userInfo -> userInfo.getFirstName().equals(firstName))
.toList();
if (filteredByFirstName.isEmpty()) {
throw new ManualMappingRequiredException(externalStudentInfo);
} else if (filteredByFirstName.size() == 1) {
return (UUID) filteredByFirstName.getFirst().get("id");
return (UUID) filteredByFirstName.getFirst().getId();
}

// filter by more attributes like email, matriculation number etc. if there are still more candidates
Expand Down Expand Up @@ -351,42 +351,16 @@ private JSONObject getExternalStudentInfo(final String externalStudentId) throws


// TODO this whole thing should be in userService rather than here
private List<Map<String, Object>> getMeitrexStudentInfoList() throws UserServiceConnectionException {
String query = "findAllUserInfos"; // TODO doesn't exist currently
String queryName = "findAllUserInfos";
List<Map<String, Object>> meitrexStudentInfoList = userServiceClient.document(query)
.execute()
.handle((ClientGraphQlResponse result, SynchronousSink<List<Map<String, Object>>> sink)
-> handleGraphQlResponse(result, sink, queryName))
.retry(3)
.block();

if (meitrexStudentInfoList == null) {
throw new UserServiceConnectionException("Error fetching userInfo from UserService");
}

return meitrexStudentInfoList;
}

private void handleGraphQlResponse(final ClientGraphQlResponse result, final SynchronousSink<List<Map<String, Object>>> sink, final String queryName) {
if (!result.isValid()) {
sink.error(new UserServiceConnectionException(result.getErrors().toString()));
return;
}
private List<UserInfo> getMeitrexStudentInfoList() throws UserServiceConnectionException {

List<Map<String, Object>> retrievedUserInfos = result.field(queryName).getValue();
final List<UUID> userIds = courseServiceClient.queryUserIds(); // TODO needs to be queried from course service

if (retrievedUserInfos == null) {
sink.error(new UserServiceConnectionException("Error fetching userInfo from UserService: Missing field in response."));
return;
}
if (retrievedUserInfos.isEmpty()) {
sink.error(new UserServiceConnectionException("Error fetching userInfo from UserService: Field in response is empty."));
}
final List<UserInfo> meitrexStudentInfoList = userServiceClient.queryUserInfos(userIds);

sink.next(retrievedUserInfos);
return meitrexStudentInfoList;
}


public List<String> saveStudentMappings(final UUID courseId, final List<StudentMappingInput> studentMappingInputs, final LoggedInUser currentUser) {
try {
validateUserHasAccessToCourse(currentUser, LoggedInUser.UserRoleInCourse.ADMINISTRATOR, courseId);
Expand All @@ -406,7 +380,7 @@ public List<String> saveStudentMappings(final UUID courseId, final List<StudentM
studentMappingRepository.saveAll(entityList);

// retries parsing all unfinishedGradingEntities
final List<Map<String, Object>> meitrexStudentInfoList;
final List<UserInfo> meitrexStudentInfoList;
try {
meitrexStudentInfoList = getMeitrexStudentInfoList();
} catch (UserServiceConnectionException e){
Expand Down

0 comments on commit 639ffd8

Please sign in to comment.