From 1f6df79de0523debed206b6d2798071beaba3c42 Mon Sep 17 00:00:00 2001 From: Radheshhathwar <radheshhathwar.bh@gmail.com> Date: Wed, 8 Nov 2023 12:50:09 +0530 Subject: [PATCH] Attendance null check changes --- .../controller/StudentResultController.java | 5 ++ .../service/DataImporterService.java | 84 ++++++++++++++----- .../service/StudentResultService.java | 39 +++++++++ 3 files changed, 108 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/tarento/upsmf/examsAndAdmissions/controller/StudentResultController.java b/src/main/java/com/tarento/upsmf/examsAndAdmissions/controller/StudentResultController.java index 3cb56b2..3792f2a 100644 --- a/src/main/java/com/tarento/upsmf/examsAndAdmissions/controller/StudentResultController.java +++ b/src/main/java/com/tarento/upsmf/examsAndAdmissions/controller/StudentResultController.java @@ -77,6 +77,11 @@ public class StudentResultController { return new ResponseEntity<>(studentResultService.processBulkResultUploadWithExternals(file, fileType), HttpStatus.OK); } + @PutMapping("/bulkUploadRevised") + public ResponseEntity<ResponseDto> processBulkResultUploadWithRevisedMarks(@RequestParam("file") MultipartFile file, @RequestParam("fileType") String fileType) { + return new ResponseEntity<>(studentResultService.processBulkResultUploadWithRevisedMarks(file, fileType), HttpStatus.OK); + } + @GetMapping("/manageResults") public ResponseEntity<ResponseDto> getExamResultsByExamCycle( @RequestParam Long examCycle) { diff --git a/src/main/java/com/tarento/upsmf/examsAndAdmissions/service/DataImporterService.java b/src/main/java/com/tarento/upsmf/examsAndAdmissions/service/DataImporterService.java index 2334d29..6544c97 100644 --- a/src/main/java/com/tarento/upsmf/examsAndAdmissions/service/DataImporterService.java +++ b/src/main/java/com/tarento/upsmf/examsAndAdmissions/service/DataImporterService.java @@ -144,15 +144,15 @@ public class DataImporterService { if (columnValue != null && !columnValue.isEmpty()) { if (columnType == Date.class) { SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy"); - SimpleDateFormat timeFormat = new SimpleDateFormat("h:mm a"); + SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm"); // Use "HH:mm" for 24-hour time format try { - if (columnName.equals("Start Time") || columnName.equals("End Time")) { + if (columnName.equalsIgnoreCase("Start Time") || columnName.equalsIgnoreCase("End Time")) { Date time = timeFormat.parse(columnValue); - map.put(columnName, timeFormat.format(time)); // Format the time as a string - } else if (columnName.equals("Start Date") || columnName.equals("End Date")) { + map.put(columnName, time); // Parse the time directly as a Date + } else if (columnName.equalsIgnoreCase("Start Date") || columnName.equalsIgnoreCase("End Date")) { Date date = dateFormat.parse(columnValue); - map.put(columnName, dateFormat.format(date)); // Format the date as a string + map.put(columnName, date); // Parse the date directly as a Date } } catch (ParseException e) { e.printStackTrace(); // Handle parsing exceptions @@ -455,6 +455,65 @@ public class DataImporterService { return resultDto; } + public ValidationResultDto convertResultDtoListToEntitiesRevisedMarks(List<StudentResult> dtoList, StudentResultRepository repository) { + List<StudentResult> entityList = new ArrayList<>(); + List<String> validationErrors = new ArrayList<>(); + + for (StudentResult dto : dtoList) { + boolean isDuplicate = checkIfDataExists(dto); + //fetch data from student_exam_registration and set the flag there, if the record is not found break and return data invalid + if (isDuplicate) { + if (!DataValidation.isFirstNameValid(dto.getFirstName())) { + validationErrors.add("- First Name is invalid: " + dto.getFirstName() + " First name has to contain alphabetic values only"); + } + if (!DataValidation.isLastNameValid(dto.getLastName())) { + validationErrors.add("- Last Name is invalid: " + dto.getLastName() + " Last name has to contain alphabetic values only"); + } + if (!DataValidation.isEnrollmentNumberValid(dto.getEnrollmentNumber())) { + validationErrors.add("- Enrollment Number is invalid: " + dto.getEnrollmentNumber() + " Enrollment number has to contain numerical values prefixed with EN"); + } + if (!DataValidation.isMarksValid(dto.getExternalMarks())) { + validationErrors.add("- External marks is invalid: " + dto.getExternalMarks() + " Marks have to be within 0 and 100"); + } + if (!DataValidation.isPassingMarksValid(dto.getPassingExternalMarks())) { + validationErrors.add("- Passing External Marks is invalid: " + dto.getPassingExternalMarks() + " Marks have to be within 0 and 100"); + } + if (!DataValidation.isMarksValid(dto.getExternalMarksObtained())) { + validationErrors.add("- External Marks Obtained is invalid: " + dto.getExternalMarksObtained() + " Marks have to be within 0 and 100"); + } + } + if (validationErrors.isEmpty()) { + StudentResult existingEntity = repository.findByFirstNameAndLastNameAndEnrollmentNumber(dto.getFirstName(), dto.getLastName(), dto.getEnrollmentNumber()); + List<StudentResult> marks = calculateResult(existingEntity.getInternalMarks(), existingEntity.getPassingInternalMarks(), existingEntity.getInternalMarksObtained(), existingEntity.getPracticalMarks(), + existingEntity.getPassingPracticalMarks(), existingEntity.getPracticalMarksObtained(), dto.getExternalMarks(), dto.getPassingExternalMarks(), dto.getExternalMarksObtained()); + + existingEntity.setExternalMarks(dto.getExternalMarks()); + existingEntity.setPassingExternalMarks(dto.getPassingExternalMarks()); + existingEntity.setExternalMarksObtained(dto.getExternalMarksObtained()); + existingEntity.setFinalMarkFlag(true); + existingEntity.setRevisedFinalMarkFlag(true); + existingEntity.setTotalMarks(marks.get(0).getTotalMarks()); + existingEntity.setPassingTotalMarks(marks.get(0).getPassingTotalMarks()); + existingEntity.setTotalMarksObtained(marks.get(0).getTotalMarksObtained()); + existingEntity.setResult(marks.get(0).getResult()); + existingEntity.setGrade(marks.get(0).getGrade()); + + entityList.add(existingEntity); + } + } + + ValidationResultDto resultDto = new ValidationResultDto(); + if (!validationErrors.isEmpty()) { + resultDto.setValid(false); + resultDto.setValidationErrors(validationErrors); + } else { + resultDto.setValid(true); + repository.saveAll(entityList); + resultDto.setSavedEntities(entityList); + } + return resultDto; + } + private List<StudentResult> calculateResult( Integer internalMarks, Integer passingInternalMarks, Integer internalMarksObtained, @@ -519,24 +578,9 @@ public class DataImporterService { if (!DataValidation.isCourseNameValid(dto.getCourse())) { validationErrors.add("- Course is invalid: " + dto.getCourse()); } -// if (!DataValidation.isDateValid(dto.getStartDate())) { -// validationErrors.add("- Start Date is invalid: " + dto.getStartDate()); -// } -// if (!DataValidation.isDateValid(dto.getEndDate())) { -// validationErrors.add("- End Date is invalid: " + dto.getEndDate()); -// } if (!DataValidation.isExamValid(dto.getExamName())) { validationErrors.add("- Exam Name is invalid: " + dto.getExamName()); } -// if (!DataValidation.isDateValid(dto.getDate())) { -// validationErrors.add("- Date is invalid: " + dto.getDate()); -// } -// if (!DataValidation.isTimeFormatValid(String.valueOf(dto.getStartTime()))) { -// validationErrors.add("- Start Time is invalid: " + dto.getStartTime()); -// } -// if (!DataValidation.isTimeFormatValid(String.valueOf(dto.getEndTime()))) { -// validationErrors.add("- End Time is invalid: " + dto.getEndTime()); -// } if (!DataValidation.isMarksBetweenOneAndHundred(dto.getMaximumMarks())) { validationErrors.add("- Maximum Marks should be between 1 and 100: " + dto.getMaximumMarks()); } diff --git a/src/main/java/com/tarento/upsmf/examsAndAdmissions/service/StudentResultService.java b/src/main/java/com/tarento/upsmf/examsAndAdmissions/service/StudentResultService.java index db43097..397c7e5 100644 --- a/src/main/java/com/tarento/upsmf/examsAndAdmissions/service/StudentResultService.java +++ b/src/main/java/com/tarento/upsmf/examsAndAdmissions/service/StudentResultService.java @@ -675,6 +675,45 @@ public class StudentResultService { return ResponseDto.setErrorResponse(response, "INTERNAL_ERROR", "An unexpected error occurred.", HttpStatus.INTERNAL_SERVER_ERROR); } } + public ResponseDto processBulkResultUploadWithRevisedMarks(MultipartFile file, String fileType) { + ResponseDto response = new ResponseDto(Constants.API_BULK_UPLOAD_RESULTS); + + try { + JSONArray jsonArray; + + switch (fileType.toLowerCase()) { + case Constants.CSV: + jsonArray = dataImporterService.csvToJson(file,columnConfig); + break; + case Constants.EXCEL: + jsonArray = dataImporterService.excelToJson(file); + break; + default: + // Handle unsupported file type + return ResponseDto.setErrorResponse(response, "UNSUPPORTED_FILE_TYPE", "Unsupported file type", HttpStatus.BAD_REQUEST); + } + String[] selectedColumns = { "First Name", "Last Name", "Enrolment Number","External Marks", "Passing External Marks", "External Marks Obtained" }; + JSONArray filteredJsonArray = dataImporterService.filterColumns(jsonArray, selectedColumns); + List<StudentResult> dtoList = dataImporterService.convertJsonToDtoList(filteredJsonArray, StudentResult.class); + ValidationResultDto validationResult = dataImporterService.convertResultDtoListToEntitiesRevisedMarks(dtoList, studentResultRepository); + + if (validationResult.isValid()) { + response.put(Constants.MESSAGE, "Bulk upload success"); + response.put("Data",validationResult.getSavedEntities()); + response.setResponseCode(HttpStatus.OK); + } else { + if (!validationResult.getValidationErrors().isEmpty()) { + response.put("validationErrors", validationResult.getValidationErrors()); + response.setResponseCode(HttpStatus.BAD_REQUEST); + } else { + return ResponseDto.setErrorResponse(response, "INTERNAL_ERROR", "An unexpected error occurred.", HttpStatus.INTERNAL_SERVER_ERROR); + } + } + return response; + } catch (Exception e) { + return ResponseDto.setErrorResponse(response, "INTERNAL_ERROR", "An unexpected error occurred.", HttpStatus.INTERNAL_SERVER_ERROR); + } + } public ResponseDto getResultsByInstituteAndExamCycle(Long instituteId, Long examCycleId) { -- GitLab