Skip to content

Commit

Permalink
GRAD2-2139: task is completed. (#571)
Browse files Browse the repository at this point in the history
* GRAD2-2139: task is completed.

GRAD2-2139: task is completed.

* Removed the duplicate codes by reusing the common method.

Removed the duplicate codes by reusing the common method.

* Cleaned up the code.

* Improved the loggings.

Improved the loggings.
  • Loading branch information
infstar authored Aug 24, 2023
1 parent ad904b0 commit 2fc058a
Showing 1 changed file with 56 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,46 +18,45 @@
import org.springframework.stereotype.Component;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.*;


@Component
public class GraduationStatusTransformer {

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

private static final String JSON_PARSING_ERROR = "Parsing Error: {}";

@Autowired
ModelMapper modelMapper;

@Autowired
GradValidation validation;

public GraduationStudentRecord transformToDTO (GraduationStudentRecordEntity gradStatusEntity) {
GraduationStudentRecord gradStatus = modelMapper.map(gradStatusEntity, GraduationStudentRecord.class);
GraduationStudentRecord gradStatus = modelMapper.map(gradStatusEntity, GraduationStudentRecord.class);
gradStatus.setProgramCompletionDate(EducGradStudentApiUtils.parseDateFromString(gradStatusEntity.getProgramCompletionDate() != null ?
EducGradStudentApiUtils.formatDate(gradStatusEntity.getProgramCompletionDate()) : null));
return gradStatus;
return gradStatus;
}

public GraduationStudentRecord transformToDTO ( Optional<GraduationStudentRecordEntity> gradStatusEntity ) {
GraduationStudentRecordEntity cae = new GraduationStudentRecordEntity();
GraduationStudentRecordEntity cae = new GraduationStudentRecordEntity();
if (gradStatusEntity.isPresent())
cae = gradStatusEntity.get();

GraduationStudentRecord gradStatus = modelMapper.map(cae, GraduationStudentRecord.class);
gradStatus.setProgramCompletionDate(EducGradStudentApiUtils.parseTraxDate(gradStatus.getProgramCompletionDate() != null ? gradStatus.getProgramCompletionDate():null));
return gradStatus;
}

public List<GraduationStudentRecord> transformToDTO (Iterable<GraduationStudentRecordEntity> gradStatusEntities ) {
List<GraduationStudentRecord> gradStatusList = new ArrayList<>();
public List<GraduationStudentRecord> transformToDTO (Iterable<GraduationStudentRecordEntity> gradStatusEntities ) {
List<GraduationStudentRecord> gradStatusList = new ArrayList<>();
for (GraduationStudentRecordEntity gradStatusEntity : gradStatusEntities) {
GraduationStudentRecord gradStatus = modelMapper.map(gradStatusEntity, GraduationStudentRecord.class);
gradStatus.setProgramCompletionDate(EducGradStudentApiUtils.parseTraxDate(gradStatusEntity.getProgramCompletionDate() != null ? gradStatusEntity.getProgramCompletionDate().toString():null));
gradStatusList.add(gradStatus);
gradStatus.setProgramCompletionDate(EducGradStudentApiUtils.parseTraxDate(gradStatusEntity.getProgramCompletionDate() != null ? gradStatusEntity.getProgramCompletionDate().toString():null));
gradStatusList.add(gradStatus);
}
return gradStatusList;
}
Expand All @@ -78,15 +77,15 @@ public GraduationStudentRecordEntity transformToEntity(GraduationStudentRecord g
GraduationStudentRecordEntity gradStatusEntity = modelMapper.map(gradStatus, GraduationStudentRecordEntity.class);
Date programCompletionDate = null;
try {
if(gradStatus.getProgramCompletionDate() != null) {
String pDate = gradStatus.getProgramCompletionDate();
if(gradStatus.getProgramCompletionDate().length() <= 7) {
pDate = EducGradStudentApiUtils.parsingTraxDate(gradStatus.getProgramCompletionDate());
}
programCompletionDate= Date.valueOf(pDate);
}
if(gradStatus.getProgramCompletionDate() != null) {
String pDate = gradStatus.getProgramCompletionDate();
if(gradStatus.getProgramCompletionDate().length() <= 7) {
pDate = EducGradStudentApiUtils.parsingTraxDate(gradStatus.getProgramCompletionDate());
}
programCompletionDate= Date.valueOf(pDate);
}
}catch(Exception e) {
validation.addErrorAndStop("Invalid Date");
validation.addErrorAndStop("Invalid Date");
}
gradStatusEntity.setProgramCompletionDate(programCompletionDate);
return gradStatusEntity;
Expand All @@ -106,7 +105,7 @@ public GraduationStudentRecordDistribution tToDForDistribution(GraduationStudent
try {
existingData = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false).readValue(ent.getStudentGradData(), GraduationData.class);
} catch (JsonProcessingException e) {
e.printStackTrace();
logger.error(JSON_PARSING_ERROR, e.getMessage());
}
if(existingData != null) {
distObj.setPen(existingData.getGradStudent().getPen());
Expand All @@ -127,17 +126,7 @@ public List<GraduationStudentRecord> tToDForBatch(Iterable<GraduationStudentReco
logger.debug("GraduationStudentRecordEntity {} with database program completion date {}", gradStatusEntity.getPen(), gradStatusEntity.getProgramCompletionDate());
gradStatus.setProgramCompletionDate(EducGradStudentApiUtils.formatDate(gradStatusEntity.getProgramCompletionDate(), "yyyy/MM"));
logger.debug("GraduationStudentRecord {} with trax program completion date {}", gradStatus.getPen(), gradStatus.getProgramCompletionDate());
if(gradStatus.getStudentGradData() != null) {
GraduationData existingData = null;
try {
existingData = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false).readValue(gradStatus.getStudentGradData(), GraduationData.class);
gradStatus.setPen(existingData.getGradStudent().getPen());
gradStatus.setLegalFirstName(existingData.getGradStudent().getLegalFirstName());
gradStatus.setLegalMiddleNames(existingData.getGradStudent().getLegalMiddleNames());
gradStatus.setLegalLastName(existingData.getGradStudent().getLegalLastName());
gradStatus.setNonGradReasons(existingData.getNonGradReasons());
} catch (JsonProcessingException e) {e.getMessage();}
}
populatePenAndLegalNamesAndNonGradReasons(gradStatus);
gradStatus.setStudentCitizenship(gradStatusEntity.getStudentCitizenship());
gradStatus.setStudentGradData(null);
gradStatus.setCreateDate(DateUtils.toLocalDateTime(gradStatusEntity.getCreateDate()));
Expand All @@ -148,20 +137,49 @@ public List<GraduationStudentRecord> tToDForBatch(Iterable<GraduationStudentReco
}

public List<UUID> tToDForAmalgamation(Iterable<GraduationStudentRecordEntity> gradStatusEntities, String type) {
List<UUID> studentList = new ArrayList<>();
List<GraduationStudentRecord> results = new ArrayList<>();
for (GraduationStudentRecordEntity gradStatusEntity : gradStatusEntities) {
GraduationStudentRecord gradStatus = modelMapper.map(gradStatusEntity, GraduationStudentRecord.class);
gradStatus.setProgramCompletionDate(EducGradStudentApiUtils.parseTraxDate(gradStatusEntity.getProgramCompletionDate() != null ? gradStatusEntity.getProgramCompletionDate().toString():null));
populatePenAndLegalNamesAndNonGradReasons(gradStatus);
if(gradStatus.getStudentProjectedGradData() != null) {
ProjectedRunClob existingData = null;
try {
existingData = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false).readValue(gradStatus.getStudentProjectedGradData(), ProjectedRunClob.class);
ProjectedRunClob existingData = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false).readValue(gradStatus.getStudentProjectedGradData(), ProjectedRunClob.class);
if((!existingData.isGraduated() && type.equalsIgnoreCase("TVRNONGRAD")) || (existingData.isGraduated() && type.equalsIgnoreCase("TVRGRAD"))) {
studentList.add(gradStatusEntity.getStudentID());
results.add(gradStatus);
}
} catch (JsonProcessingException e) {e.getMessage();}
} catch (JsonProcessingException e) {
logger.error(JSON_PARSING_ERROR ,e.getMessage());
}
}

}
if (results.isEmpty()) {
return new ArrayList<>();
}

// sort by names
results.sort(Comparator.comparing(GraduationStudentRecord::getLegalLastName, Comparator.nullsLast(Comparator.naturalOrder()))
.thenComparing(GraduationStudentRecord::getLegalFirstName, Comparator.nullsLast(Comparator.naturalOrder()))
.thenComparing(GraduationStudentRecord::getLegalMiddleNames, Comparator.nullsLast(Comparator.naturalOrder())));
return results.stream().map(GraduationStudentRecord::getStudentID).toList();
}

private void populatePenAndLegalNamesAndNonGradReasons(GraduationStudentRecord gradStatus) {
if(gradStatus.getStudentGradData() != null) {
GraduationData existingData = null;
try {
existingData = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false).readValue(gradStatus.getStudentGradData(), GraduationData.class);
} catch (JsonProcessingException e) {
logger.error(JSON_PARSING_ERROR, e.getMessage());
}
if (existingData != null) {
gradStatus.setPen(existingData.getGradStudent().getPen());
gradStatus.setLegalFirstName(existingData.getGradStudent().getLegalFirstName());
gradStatus.setLegalMiddleNames(existingData.getGradStudent().getLegalMiddleNames());
gradStatus.setLegalLastName(existingData.getGradStudent().getLegalLastName());
gradStatus.setNonGradReasons(existingData.getNonGradReasons());
}
}
return studentList;
}
}

0 comments on commit 2fc058a

Please sign in to comment.