Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Date parsing error #479

Merged
merged 1 commit into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import lombok.EqualsAndHashCode;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.LocalDate;
import java.util.List;
import java.util.UUID;

Expand Down Expand Up @@ -38,12 +38,12 @@ public class GraduationStudentRecord extends BaseModel {
private String consumerEducationRequirementMet;
private String studentCitizenship;
private ExceptionMessage exception;
private LocalDateTime adultStartDate;
private LocalDate adultStartDate;

private List<GradRequirement> nonGradReasons;
private List<StudentCareerProgram> careerPrograms;

public LocalDateTime getAdultStartDate() {
return adultStartDate == null ? LocalDateTime.now() : adultStartDate;
public LocalDate getAdultStartDate() {
return adultStartDate == null ? LocalDate.now() : adultStartDate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public GraduationStudentRecord prepareGraduationStatusObj(GraduationData graduat

@SneakyThrows
public void prepareGraduationStatusData(GraduationStudentRecord obj, GraduationData graduationDataStatus) {
obj.setStudentGradData(new ObjectMapper().writeValueAsString(graduationDataStatus));
obj.setStudentGradData(objectMapper.writeValueAsString(graduationDataStatus));
}

public GraduationStudentRecord saveStudentGradStatus(String studentID,Long batchId,String accessToken, GraduationStudentRecord toBeSaved, ExceptionMessage exception) {
Expand Down Expand Up @@ -106,7 +106,7 @@ public GraduationStudentRecord saveStudentRecordProjectedRun(ProjectedRunClob pr
@SneakyThrows
public GraduationStudentRecord processProjectedResults(GraduationStudentRecord gradResponse, GraduationData graduationDataStatus) {

gradResponse.setStudentGradData(new ObjectMapper().writeValueAsString(graduationDataStatus));
gradResponse.setStudentGradData(objectMapper.writeValueAsString(graduationDataStatus));
gradResponse.setProgramCompletionDate(graduationDataStatus.getGradStatus().getProgramCompletionDate());
gradResponse.setGpa(graduationDataStatus.getGradStatus().getGpa());
gradResponse.setHonoursStanding(graduationDataStatus.getGradStatus().getHonoursStanding());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ public class GraduationService {
RESTService restService;
EducGraduationApiConstants educGraduationApiConstants;
SchoolYearDates schoolYearDates;
ObjectMapper objectMapper;

@Autowired
public GraduationService(WebClient webClient, AlgorithmProcessFactory algorithmProcessFactory, GradStatusService gradStatusService, SchoolService schoolService, ReportService reportService, TokenUtils tokenUtils, RESTService restService, EducGraduationApiConstants educGraduationApiConstants, SchoolYearDates schoolYearDates) {
public GraduationService(WebClient webClient, AlgorithmProcessFactory algorithmProcessFactory, GradStatusService gradStatusService, SchoolService schoolService, ReportService reportService, TokenUtils tokenUtils, RESTService restService, EducGraduationApiConstants educGraduationApiConstants, SchoolYearDates schoolYearDates, ObjectMapper objectMapper) {
this.webClient = webClient;
this.algorithmProcessFactory = algorithmProcessFactory;
this.gradStatusService = gradStatusService;
Expand All @@ -63,6 +64,7 @@ public GraduationService(WebClient webClient, AlgorithmProcessFactory algorithmP
this.restService = restService;
this.educGraduationApiConstants = educGraduationApiConstants;
this.schoolYearDates = schoolYearDates;
this.objectMapper = objectMapper;
}

public AlgorithmResponse graduateStudent(String studentID, Long batchId, String accessToken, String projectedType) {
Expand Down Expand Up @@ -366,7 +368,7 @@ private List<Student> processStudentList(List<GraduationStudentRecord> gradStudL
} else {
std.setGraduationData(new ca.bc.gov.educ.api.graduation.model.report.GraduationData());
if (gsr.getStudentProjectedGradData() != null) {
ProjectedRunClob projectedClob = new ObjectMapper().readValue(gsr.getStudentProjectedGradData(), ProjectedRunClob.class);
ProjectedRunClob projectedClob = objectMapper.readValue(gsr.getStudentProjectedGradData(), ProjectedRunClob.class);
std.setNonGradReasons(getNonGradReasons(projectedClob.getNonGradReasons()));
if (!projectedClob.isGraduated())
stdPrjList.add(std);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
import ca.bc.gov.educ.api.graduation.model.dto.GraduationData;
import ca.bc.gov.educ.api.graduation.model.dto.StudentOptionalProgram;
import ca.bc.gov.educ.api.graduation.util.EducGraduationApiConstants;
import ca.bc.gov.educ.api.graduation.util.JsonTransformer;
import ca.bc.gov.educ.api.graduation.util.ThreadLocalStateUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.stereotype.Service;
Expand All @@ -23,6 +22,9 @@ public class OptionalProgramService {

@Autowired
WebClient webClient;

@Autowired
JsonTransformer jsonTransformer;

@Autowired
EducGraduationApiConstants educGraduationApiConstants;
Expand All @@ -49,12 +51,7 @@ public List<StudentOptionalProgram> saveAndLogOptionalPrograms(GraduationData gr
gradOptionalProgram.setOptionalProgramID(optionalPrograms.getOptionalProgramID());
gradOptionalProgram.setStudentID(optionalPrograms.getStudentID());
gradOptionalProgram.setOptionalProgramCompletionDate(optionalPrograms.getOptionalProgramCompletionDate());
try {
gradOptionalProgram.setStudentOptionalProgramData(new ObjectMapper().writeValueAsString(optionalPrograms));
} catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
gradOptionalProgram.setStudentOptionalProgramData(jsonTransformer.marshall(optionalPrograms));
optionalProgramCode.setCode(gradOptionalProgram.getOptionalProgramCode());
optionalProgramCode.setName(gradOptionalProgram.getOptionalProgramName());
//Save Optional Grad Status
Expand Down Expand Up @@ -85,11 +82,7 @@ public List<StudentOptionalProgram> projectedOptionalPrograms(GraduationData gra
graduationDataStatus.setDualDogwood(true);
}
optionalProgramProjectedObj.setOptionalProgramCompletionDate(optionalPrograms.getOptionalProgramCompletionDate());
try {
optionalProgramProjectedObj.setStudentOptionalProgramData(new ObjectMapper().writeValueAsString(optionalPrograms));
} catch (JsonProcessingException e) {
e.printStackTrace();
}
optionalProgramProjectedObj.setStudentOptionalProgramData(jsonTransformer.marshall(optionalPrograms));
optionalProgramProjectedObj.setOptionalProgramID(gradOptionalProgram.getOptionalProgramID());
optionalProgramProjectedObj.setStudentID(gradOptionalProgram.getStudentID());
optionalProgramProjectedObj.setId(gradOptionalProgram.getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import ca.bc.gov.educ.api.graduation.model.report.School;
import ca.bc.gov.educ.api.graduation.model.report.*;
import ca.bc.gov.educ.api.graduation.util.*;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -1128,12 +1126,14 @@ public ReportData prepareAchievementReportData(ca.bc.gov.educ.api.graduation.mod
}

public Pair<GraduationStudentRecord, ca.bc.gov.educ.api.graduation.model.dto.GraduationData> getGraduationStudentRecordAndGraduationData(String pen, String accessToken) {
String graduationDataJson = "{}";
try {
GraduationStudentRecord graduationStudentRecord = getGraduationStudentRecordByPen(pen, accessToken);
ca.bc.gov.educ.api.graduation.model.dto.GraduationData graduationData = (ca.bc.gov.educ.api.graduation.model.dto.GraduationData) jsonTransformer.unmarshall(graduationStudentRecord.getStudentGradData(), ca.bc.gov.educ.api.graduation.model.dto.GraduationData.class);
graduationDataJson = graduationStudentRecord.getStudentGradData();
ca.bc.gov.educ.api.graduation.model.dto.GraduationData graduationData = (ca.bc.gov.educ.api.graduation.model.dto.GraduationData) jsonTransformer.unmarshall(graduationDataJson, ca.bc.gov.educ.api.graduation.model.dto.GraduationData.class);
return Pair.of(graduationStudentRecord, graduationData);
} catch (Exception e) {
log.error("model.dto.GraduationData unmarshal error for student {}: {}", pen, e.getLocalizedMessage());
log.error("GraduationData {} unmarshal error for student {}: {}", graduationDataJson, pen, e.getLocalizedMessage());
return null;
}
}
Expand All @@ -1157,12 +1157,7 @@ private List<OptionalProgram> getOptionalProgramAchvReport(String gradProgramCod
op.setOptionalProgramName(sPO.getOptionalProgramName());
op.setProgramCompletionDate(sPO.getOptionalProgramCompletionDate());

GradAlgorithmOptionalStudentProgram existingData = null;
try {
existingData = new ObjectMapper().readValue(sPO.getStudentOptionalProgramData(), GradAlgorithmOptionalStudentProgram.class);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
GradAlgorithmOptionalStudentProgram existingData = (GradAlgorithmOptionalStudentProgram)jsonTransformer.unmarshall(sPO.getStudentOptionalProgramData(), GradAlgorithmOptionalStudentProgram.class);
if (existingData != null && existingData.getOptionalNonGradReasons() != null) {
op.setNonGradReasons(getNonGradReasons(gradProgramCode, existingData.getOptionalNonGradReasons(), false, null, false));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.JsonDeserializer;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.time.Instant;
Expand All @@ -14,11 +16,9 @@

import static ca.bc.gov.educ.api.graduation.util.EducGraduationApiConstants.SECOND_DEFAULT_DATE_FORMAT;

public class GradLocalDateDeserializer extends StdDeserializer<LocalDate> {
public class GradLocalDateDeserializer extends JsonDeserializer<LocalDate> {

public GradLocalDateDeserializer() {
super(LocalDate.class);
}
private static final Logger logger = LoggerFactory.getLogger(GradLocalDateTimeDeserializer.class);

@Override
public LocalDate deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
package ca.bc.gov.educ.api.graduation.util;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;

import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class GradLocalDateSerializer extends StdSerializer<LocalDate> {

public GradLocalDateSerializer() {
super(LocalDate.class);
}
public class GradLocalDateSerializer extends JsonSerializer<LocalDate> {

@Override
public void serialize(LocalDate localDate, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
import com.fasterxml.jackson.databind.JsonDeserializer;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.time.Instant;
Expand All @@ -15,11 +17,9 @@

import static ca.bc.gov.educ.api.graduation.util.EducGraduationApiConstants.*;

public class GradLocalDateTimeDeserializer extends StdDeserializer<LocalDateTime> {
public class GradLocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {

public GradLocalDateTimeDeserializer() {
super(LocalDateTime.class);
}
private static final Logger logger = LoggerFactory.getLogger(GradLocalDateTimeDeserializer.class);

@Override
public LocalDateTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
package ca.bc.gov.educ.api.graduation.util;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class GradLocalDateTimeSerializer extends StdSerializer<LocalDateTime> {

public GradLocalDateTimeSerializer() {
super(LocalDateTime.class);
}
public class GradLocalDateTimeSerializer extends JsonSerializer<LocalDateTime> {

@Override
public void serialize(LocalDateTime localDate, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,47 +1,25 @@
package ca.bc.gov.educ.api.graduation.util;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.module.SimpleModule;
import jakarta.annotation.PostConstruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.TimeZone;

@Component
public class JsonTransformer implements Transformer {

private static final Logger log = LoggerFactory.getLogger(JsonTransformer.class);

final ObjectMapper objectMapper;

@Autowired
ObjectMapper objectMapper;

@PostConstruct
public void init() {
SimpleModule simpleModule = new SimpleModule();
simpleModule.addSerializer(LocalDate.class, new GradLocalDateSerializer());
simpleModule.addSerializer(LocalDateTime.class, new GradLocalDateTimeSerializer());
simpleModule.addDeserializer(LocalDate.class, new GradLocalDateDeserializer());
simpleModule.addDeserializer(LocalDateTime.class, new GradLocalDateTimeDeserializer());
objectMapper
.findAndRegisterModules()
.registerModule(simpleModule)
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
.disable(SerializationFeature.INDENT_OUTPUT)
.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING)
.enable(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN)
.enable(JsonGenerator.Feature.ESCAPE_NON_ASCII)
.setTimeZone(TimeZone.getDefault())
// .enable(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS)
;
public JsonTransformer(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}

@Override
Expand Down
Loading