Skip to content

Commit

Permalink
Merge pull request #610 from bcgov/develop/alex-GRAD2-2464
Browse files Browse the repository at this point in the history
Develop/alex grad2 2464
  • Loading branch information
kamal-mohammed authored Jan 24, 2024
2 parents c8732ff + 3f4c000 commit 8ed6e1b
Show file tree
Hide file tree
Showing 15 changed files with 430 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ca.bc.gov.educ.api.gradstudent.config;

import ca.bc.gov.educ.api.gradstudent.exception.EntityNotFoundException;
import ca.bc.gov.educ.api.gradstudent.util.ApiResponseMessage.MessageTypeEnum;
import ca.bc.gov.educ.api.gradstudent.util.ApiResponseModel;
import ca.bc.gov.educ.api.gradstudent.util.GradBusinessRuleException;
Expand Down Expand Up @@ -55,7 +56,7 @@ protected ResponseEntity<Object> handleAuthorizationErrors(Exception ex, WebRequ
return new ResponseEntity<>(ApiResponseModel.ERROR(null, message), HttpStatus.FORBIDDEN);
}

@ExceptionHandler(value = { GradBusinessRuleException.class })
@ExceptionHandler(value = { GradBusinessRuleException.class, EntityNotFoundException.class })
protected ResponseEntity<Object> handleGradBusinessException(Exception ex, WebRequest request) {
ApiResponseModel<?> response = ApiResponseModel.ERROR(null);
validation.ifErrors(response::addErrorMessages);
Expand All @@ -64,7 +65,8 @@ protected ResponseEntity<Object> handleGradBusinessException(Exception ex, WebRe
response.addMessageItem(ex.getLocalizedMessage(), MessageTypeEnum.ERROR);
}
validation.clear();
return new ResponseEntity<>(response, HttpStatus.UNPROCESSABLE_ENTITY);
HttpStatus httpStatus = (ex instanceof EntityNotFoundException) ? HttpStatus.NOT_FOUND : HttpStatus.UNPROCESSABLE_ENTITY;
return new ResponseEntity<>(response, httpStatus);
}

@ExceptionHandler(value = { OptimisticEntityLockException.class })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public class CommonController {

private static final Logger logger = LoggerFactory.getLogger(CommonController.class);

private static final String STATUS_CODE="Status Code";
private static final String STATUS_CODE = "Status Code";
private static final String BEARER = "Bearer ";

@Autowired
CommonService commonService;
Expand Down Expand Up @@ -276,4 +277,36 @@ public ResponseEntity<List<UUID>> getDeceasedStudentIDs(@RequestBody List<UUID>
return response.GET(commonService.getDeceasedStudentIDs(studentIds));
}

@PostMapping (EducGradStudentApiConstants.GRAD_STUDENT_OPTIONAL_PROGRAMS)
@PreAuthorize(PermissionsConstants.UPDATE_GRADUATION_STUDENT_OPTIONAL_PROGRAM)
@Operation(summary = "Create Student Optional Grad Program by Student ID", description = "Create Student Optional Grad Program by Student ID", tags = { "Optional Student Graduation Status" })
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK")})
public ResponseEntity<GraduationStudentRecord> createStudentGradOptionalProgram(@PathVariable UUID studentID, @RequestBody StudentOptionalProgram gradStudentOptionalProgram,
@RequestHeader(name="Authorization") String accessToken) {
logger.debug("Create student Optional Grad Program for Student ID: {}", studentID);
graduationStatusService.createStudentGradOptionalProgram(studentID, gradStudentOptionalProgram);
return response.GET(graduationStatusService.getGraduationStatus(studentID, accessToken.replace(BEARER, "")));
}

@PutMapping (EducGradStudentApiConstants.GRAD_STUDENT_OPTIONAL_PROGRAM_UPDATE)
@PreAuthorize(PermissionsConstants.UPDATE_GRADUATION_STUDENT_OPTIONAL_PROGRAM)
@Operation(summary = "Update Student Optional Grad Program for Student ID", description = "Update Student Optional Grad Program for Student ID", tags = { "Optional Student Graduation Status" })
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK")})
public ResponseEntity<GraduationStudentRecord> updateStudentGradOptionalProgram(@PathVariable UUID studentID, @PathVariable UUID optionalProgramID, @RequestBody StudentOptionalProgram gradStudentOptionalProgram,
@RequestHeader(name="Authorization") String accessToken) {
logger.debug("Create student Optional Grad Program for Student ID: {}", studentID);
graduationStatusService.updateStudentGradOptionalProgram(studentID, optionalProgramID, gradStudentOptionalProgram);
return response.GET(graduationStatusService.getGraduationStatus(studentID, accessToken.replace(BEARER, "")));
}

@DeleteMapping (EducGradStudentApiConstants.GRAD_STUDENT_OPTIONAL_PROGRAM_DELETE)
@PreAuthorize(PermissionsConstants.UPDATE_GRADUATION_STUDENT_OPTIONAL_PROGRAM)
@Operation(summary = "Delete Student Optional Grad Program by Student ID", description = "Delete Student Optional Grad Program by Student ID", tags = { "Optional Student Graduation Status" })
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK")})
public ResponseEntity<GraduationStudentRecord> deleteStudentGradOptionalProgram(@PathVariable UUID studentID, @PathVariable UUID optionalProgramID, @RequestParam (value = "careerProgramID", required = false) String careerProgramID,
@RequestHeader(name="Authorization") String accessToken) {
logger.debug("Delete student Optional Program for Student ID: {}", studentID);
graduationStatusService.deleteStudentGradOptionalProgram(studentID, optionalProgramID, careerProgramID);
return response.GET(graduationStatusService.getGraduationStatus(studentID, accessToken.replace(BEARER, "")));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ca.bc.gov.educ.api.gradstudent.exception;

public class EntityNotFoundException extends Exception {
import ca.bc.gov.educ.api.gradstudent.util.GradBusinessRuleException;

public class EntityNotFoundException extends GradBusinessRuleException {


public EntityNotFoundException() {
Expand All @@ -11,11 +13,4 @@ public EntityNotFoundException(String message) {
super(message);
}

public EntityNotFoundException(String message, Throwable cause) {
super(message, cause);
}

public EntityNotFoundException(Throwable cause) {
super(cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
@Data
@EqualsAndHashCode(callSuper = false)
@Component
public class GraduationStudentRecord extends BaseModel{
public class GraduationStudentRecord extends BaseModel {

private String studentGradData;
private String pen;
Expand Down Expand Up @@ -44,5 +44,6 @@ public class GraduationStudentRecord extends BaseModel{
private Date adultStartDate;

private List<StudentCareerProgram> careerPrograms;
private List<StudentOptionalProgram> optionalPrograms;
private List<GradRequirement> nonGradReasons;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
@Data
@EqualsAndHashCode(callSuper = false)
@Component
public class StudentOptionalProgram extends BaseModel{
public class StudentOptionalProgram extends BaseModel {

private UUID id;
private String pen;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,8 @@ void updateStudentGuidPenXrefRecord(

@Query("select c.studentID from GraduationStudentRecordEntity c where c.studentStatus = :statusCode and c.studentID in :studentIDList")
List<UUID> filterGivenStudentsByStatusCode(@Param("studentIDList") List<UUID> studentIDs, @Param("statusCode") String statusCode);

@Modifying
@Query("update GraduationStudentRecordEntity e set e.recalculateGradStatus = :recalculateGradStatus, e.recalculateProjectedGrad = :recalculateProjectedGrad where e.studentID = :studentGuid")
void updateGradStudentRecalculationFlags(@Param(value = "studentGuid") UUID studentGuid, @Param(value = "recalculateGradStatus") String recalculateGradStatus, @Param(value = "recalculateProjectedGrad") String recalculateProjectedGrad);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ public interface StudentCareerProgramRepository extends JpaRepository<StudentCar
List<StudentCareerProgramEntity> existsByCareerProgramCode(String cpCode);

void deleteByStudentID(UUID studentID);

void deleteStudentCareerProgramEntityByStudentIDAndId(UUID studentGuid, UUID id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,12 @@ public class GraduationStatusService {

private static final String CREATE_USER = "createUser";
private static final String CREATE_DATE = "createDate";
private static final String UPDATE_USER = "updateUser";
private static final String UPDATE_DATE = "updateDate";
private static final String GRAD_ALG = "GRADALG";
private static final String USER_EDIT = "USEREDIT";
private static final String USER_CREATE = "USEREDIT";
private static final String USER_DELETE = "USERDELETE";
private static final String USER_UNDO_CMPL = "USERUNDOCMPL";

final
Expand Down Expand Up @@ -147,6 +151,8 @@ public GraduationStudentRecord getGraduationStatus(UUID studentID, String access

List<StudentCareerProgramEntity> studentCareerProgramEntities = gradStudentCareerProgramRepository.findByStudentID(studentID);
gradStatus.setCareerPrograms(gradStudentCareerProgramTransformer.transformToDTO(studentCareerProgramEntities));
List<StudentOptionalProgram> studentOptionalPrograms = getStudentGradOptionalProgram(studentID, accessToken);
gradStatus.setOptionalPrograms(studentOptionalPrograms);
return gradStatus;
} else {
return null;
Expand Down Expand Up @@ -593,6 +599,10 @@ private void validateStudentStatus(String studentStatus) {
}
}

private void validateStudent(GraduationStudentRecord graduationStudentRecord) {
validateStudentStatus(graduationStudentRecord.getStudentStatus());
}

private void validateProgram(GraduationStudentRecordEntity sourceEntity, String accessToken) {
GradProgram gradProgram = webClient.get()
.uri(String.format(constants.getGradProgramNameUrl(), sourceEntity.getProgram()))
Expand Down Expand Up @@ -778,8 +788,69 @@ public StudentOptionalProgram saveStudentGradOptionalProgram(StudentOptionalProg
return null;
}
}

@Transactional
public StudentOptionalProgram createStudentGradOptionalProgram(UUID studentID, StudentOptionalProgram gradStudentOptionalProgram) throws EntityNotFoundException {
gradStudentOptionalProgram.setStudentID(studentID);
validateStudent(getGraduationStatus(studentID));
StudentOptionalProgramEntity sourceObject = gradStudentOptionalProgramTransformer.transformToEntity(gradStudentOptionalProgram);
sourceObject.setUpdateUser(null); //this change is just till idir login is fixed
StudentOptionalProgramEntity gradEnity = new StudentOptionalProgramEntity();
logger.debug(" -> Creating Student Optional Program Entity for student ID: {}", studentID);
BeanUtils.copyProperties(sourceObject, gradEnity, CREATE_USER, CREATE_DATE);
gradEnity.setOptionalProgramCompletionDate(sourceObject.getOptionalProgramCompletionDate());
StudentOptionalProgramEntity gradEnitySaved = gradStudentOptionalProgramRepository.save(gradEnity);
historyService.createStudentOptionalProgramHistory(gradEnitySaved, USER_CREATE);
graduationStatusRepository.updateGradStudentRecalculationFlags(studentID, "Y", "Y");
return gradStudentOptionalProgramTransformer.transformToDTO(gradEnitySaved);
}

@Transactional
public StudentOptionalProgram updateStudentGradOptionalProgram(UUID studentID, UUID optionalProgramID, StudentOptionalProgram gradStudentOptionalProgram) throws EntityNotFoundException {
validateStudent(getGraduationStatus(studentID));
gradStudentOptionalProgram.setOptionalProgramID(optionalProgramID);
gradStudentOptionalProgram.setStudentID(studentID);
Optional<StudentOptionalProgramEntity> gradStudentOptionalProgramEntityOptional =
gradStudentOptionalProgramRepository.findByStudentIDAndOptionalProgramID(studentID, optionalProgramID);
StudentOptionalProgramEntity sourceObject = gradStudentOptionalProgramTransformer.transformToEntity(gradStudentOptionalProgram);
sourceObject.setUpdateUser(null); //this change is just till idir login is fixed
if (gradStudentOptionalProgramEntityOptional.isPresent()) {
StudentOptionalProgramEntity gradEnity = gradStudentOptionalProgramEntityOptional.get();
logger.debug(" -> Student {} Optional Program Entity is found for ID: {} === Entity ID: {}", studentID, optionalProgramID, gradEnity.getId());
BeanUtils.copyProperties(sourceObject, gradEnity, "id", "studentID", "optionalProgramID", UPDATE_USER, UPDATE_DATE);
gradEnity.setOptionalProgramCompletionDate(sourceObject.getOptionalProgramCompletionDate());
StudentOptionalProgramEntity gradEnitySaved = gradStudentOptionalProgramRepository.save(gradEnity);
historyService.createStudentOptionalProgramHistory(gradEnitySaved,GRAD_ALG);
graduationStatusRepository.updateGradStudentRecalculationFlags(studentID, "Y", "Y");
return gradStudentOptionalProgramTransformer.transformToDTO(gradEnitySaved);
} else {
String msg = "Student %s optional program % was not found";
throw new EntityNotFoundException(String.format(msg, studentID, optionalProgramID));
}
}

@Transactional
public void deleteStudentGradOptionalProgram(UUID studentID, UUID optionalProgramID, String careerProgramID) throws EntityNotFoundException {
validateStudent(getGraduationStatus(studentID));
Optional<StudentOptionalProgramEntity> gradStudentOptionalOptional =
gradStudentOptionalProgramRepository.findByStudentIDAndOptionalProgramID(studentID, optionalProgramID);
logger.debug("Save with payload ==> Student Optional Program ID: {}", optionalProgramID);
if (gradStudentOptionalOptional.isPresent()) {
StudentOptionalProgramEntity gradEnity = gradStudentOptionalOptional.get();
logger.debug(" -> Student Optional Program Entity is found for ID: {} === Entity ID: {}", optionalProgramID, optionalProgramID);
gradStudentOptionalProgramRepository.delete(gradEnity);
historyService.createStudentOptionalProgramHistory(gradEnity, USER_DELETE);
if(StringUtils.isNotBlank(careerProgramID)) {
gradStudentCareerProgramRepository.deleteStudentCareerProgramEntityByStudentIDAndId(studentID, UUID.fromString(careerProgramID));
}
graduationStatusRepository.updateGradStudentRecalculationFlags(studentID, "Y", "Y");
} else {
String msg = "Student Optional Program %s for student %s is not found";
throw new EntityNotFoundException(String.format(msg, optionalProgramID, studentID));
}
}

public StudentOptionalProgram updateStudentGradOptionalProgram(StudentOptionalProgramReq gradStudentOptionalProgramReq,String accessToken) {
public StudentOptionalProgram updateStudentGradOptionalProgram(StudentOptionalProgramReq gradStudentOptionalProgramReq, String accessToken) {
Optional<StudentOptionalProgramEntity> gradStudentOptionalOptional = Optional.empty();
if(gradStudentOptionalProgramReq.getId() != null)
gradStudentOptionalOptional = gradStudentOptionalProgramRepository.findById(gradStudentOptionalProgramReq.getId());
Expand All @@ -797,22 +868,22 @@ public StudentOptionalProgram updateStudentGradOptionalProgram(StudentOptionalPr
sourceObject.setId(gradStudentOptionalProgramReq.getId());
sourceObject.setStudentID(gradStudentOptionalProgramReq.getStudentID());
sourceObject.setOptionalProgramCompletionDate(gradStudentOptionalProgramReq.getOptionalProgramCompletionDate() != null ?Date.valueOf(gradStudentOptionalProgramReq.getOptionalProgramCompletionDate()) : null);
sourceObject.setOptionalProgramID(gradOptionalProgram != null ?gradOptionalProgram.getOptionalProgramID():null);
sourceObject.setOptionalProgramID(gradOptionalProgram != null ? gradOptionalProgram.getOptionalProgramID() : null);
if (gradStudentOptionalOptional.isPresent()) {
StudentOptionalProgramEntity gradEntity = gradStudentOptionalOptional.get();
if(gradEntity.getOptionalProgramID().equals(sourceObject.getOptionalProgramID())) {
sourceObject.setStudentOptionalProgramData(gradEntity.getStudentOptionalProgramData());
}else {
} else {
sourceObject.setStudentOptionalProgramData(null);
}
BeanUtils.copyProperties(sourceObject, gradEntity, CREATE_USER, CREATE_DATE,"id");
BeanUtils.copyProperties(sourceObject, gradEntity, CREATE_USER, CREATE_DATE, "id");
gradEntity.setOptionalProgramCompletionDate(sourceObject.getOptionalProgramCompletionDate());
gradEntity = gradStudentOptionalProgramRepository.save(gradEntity);
historyService.createStudentOptionalProgramHistory(gradEntity, USER_EDIT);
return gradStudentOptionalProgramTransformer.transformToDTO(gradEntity);
} else {
sourceObject = gradStudentOptionalProgramRepository.save(sourceObject);
historyService.createStudentOptionalProgramHistory(sourceObject, USER_EDIT);
historyService.createStudentOptionalProgramHistory(sourceObject, USER_CREATE);
return gradStudentOptionalProgramTransformer.transformToDTO(sourceObject);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public class EducGradStudentApiConstants {
public static final String GRAD_STUDENT_OPTIONAL_PROGRAM_BY_PEN_PROGRAM_OPTIONAL_PROGRAM = "/optionalprogram/{studentID}/{optionalProgramID}";
public static final String SAVE_GRAD_STUDENT_OPTIONAL_PROGRAM = "/optionalprogram";
public static final String UPDATE_GRAD_STUDENT_OPTIONAL_PROGRAM = "/gradstudent/optionalprogram";
public static final String GRAD_STUDENT_OPTIONAL_PROGRAM_DELETE = "/{studentID}/optionalPrograms/{optionalProgramID}/careerPrograms";
public static final String GRAD_STUDENT_OPTIONAL_PROGRAM_UPDATE = "/{studentID}/optionalPrograms/{optionalProgramID}";
public static final String GRAD_STUDENT_OPTIONAL_PROGRAMS = "/{studentID}/optionalPrograms";
public static final String GRAD_STUDENT_RECALCULATE = "/recalculate";
public static final String GRAD_STUDENT_PROJECTED_RUN = "/projected";
public static final String GRAD_STUDENT_BY_STUDENT_ID_FOR_BATCH_RUN = "/batch/gradstudent/studentid/{studentID}";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ca.bc.gov.educ.api.gradstudent.util;

public class GradBusinessRuleException extends RuntimeException{
public class GradBusinessRuleException extends RuntimeException {

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ public void addError(String formattedErrorMessage, Object... args) {

public void addErrorAndStop(String errorMessage) {
errorList.get().add(errorMessage);
throw new ca.bc.gov.educ.api.gradstudent.util.GradBusinessRuleException();
throw new ca.bc.gov.educ.api.gradstudent.util.GradBusinessRuleException(String.join(",\n", errorList.get()));

}

public void addErrorAndStop(String formattedErrorMessage, Object... args) {
errorList.get().add(String.format(formattedErrorMessage, args));
throw new ca.bc.gov.educ.api.gradstudent.util.GradBusinessRuleException();
throw new ca.bc.gov.educ.api.gradstudent.util.GradBusinessRuleException(String.join(",\n", errorList.get()));

}

Expand Down Expand Up @@ -83,7 +83,7 @@ public boolean requiredField(Object requiredValue, String fieldName) {

public void stopOnErrors() {
if (hasErrors()) {
throw new ca.bc.gov.educ.api.gradstudent.util.GradBusinessRuleException();
throw new ca.bc.gov.educ.api.gradstudent.util.GradBusinessRuleException(String.join(",\n", errorList.get()));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public <T> ResponseEntity<T> NOT_FOUND() {
return new ResponseEntity<T>(HttpStatus.NOT_FOUND);
}

public <T> ResponseEntity<T> NOT_FOUND(T body) {
return new ResponseEntity<T>(body, HttpStatus.NOT_FOUND);
}

public <T> ResponseEntity<T> NO_CONTENT() {
return new ResponseEntity<T>(HttpStatus.NO_CONTENT);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
BEGIN
EXECUTE IMMEDIATE '
ALTER TABLE STUDENT_OPTIONAL_PROGRAM
ADD CONSTRAINT STUDENT_OPTIONAL_PROGRAM_UK UNIQUE (
GRADUATION_STUDENT_RECORD_ID
,OPTIONAL_PROGRAM_ID
) ENABLE';
EXCEPTION
WHEN OTHERS THEN
IF SQLCODE != -2261 THEN
RAISE;
END IF;
END;
/

Loading

0 comments on commit 8ed6e1b

Please sign in to comment.