Skip to content

Commit

Permalink
#96 - introduced three different checked exceptions meant for handled…
Browse files Browse the repository at this point in the history
… by callers of integration process. removed unmapped fields. metadata data mapping.
  • Loading branch information
petmongrels committed Nov 9, 2023
1 parent 07e1b3a commit 681a91c
Show file tree
Hide file tree
Showing 14 changed files with 109 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ public class FlowResult {
private final Map<String, Object> fields;
private final Map<String, Object> results;

public static String STUDENT_CONTACT_NUMBER = "contact_phone";

public FlowResult(Map<String, Object> fields) {
this.fields = fields;
results = readValue((String)this.fields.get("results"), Map.class);
}

public String getContactPhone() {
return (String) fields.get("contact_phone");
return (String) fields.get(STUDENT_CONTACT_NUMBER);
}

public String getInsertedAt() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import com.google.cloud.bigquery.LegacySQLTypeName;
import com.google.cloud.bigquery.Schema;

import static org.avni_integration_service.glific.bigQuery.domain.FlowResult.STUDENT_CONTACT_NUMBER;

public class SchemaBuilder {

public Schema flowResultSchema() {
return Schema.of(Field.of("contact_phone", LegacySQLTypeName.STRING),
return Schema.of(Field.of(STUDENT_CONTACT_NUMBER, LegacySQLTypeName.STRING),
Field.of("flowresult_id", LegacySQLTypeName.STRING),
Field.of("inserted_at", LegacySQLTypeName.DATETIME),
Field.of("results", LegacySQLTypeName.STRING),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.avni_integration_service.common;

/**
* Use this exception to indicate that the current message cannot be processed due to problems with this message
*/
public class MessageUnprocessableException extends Exception {
public MessageUnprocessableException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.avni_integration_service.common;

/**
* Use this exception to indicate that integration service module cannot process any messages due to problems in configuration of integration service, integration system, or avni.
*/
public class PlatformException extends Exception {
public PlatformException(String message) {
super(message);
}

public PlatformException(Throwable cause) {
super(cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.avni_integration_service.common;

/**
* Use this to indicate the source of problem in processing is unknown
*/
public class UnknownException extends Exception {
public UnknownException(Throwable cause) {
super(cause);
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,7 @@
package org.avni_integration_service.integration_data.domain.framework;

public class MappingException extends RuntimeException {
public MappingException() {
}

public MappingException(String message) {
super(message);
}

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

public MappingException(Throwable cause) {
super(cause);
}

public MappingException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package org.avni_integration_service.lahi.domain;

import java.util.Arrays;
import java.util.List;

public interface LahiStudentConstants {
String FIRST_NAME = "avni_first_name";
String LAST_NAME = "avni_last_name";
Expand All @@ -13,7 +10,6 @@ public interface LahiStudentConstants {
String DISTRICT = "avni_district_name";
String CITY_NAME = "avni_city_name";
String SCHOOL = "avni_school_name";
String STUDENT_CONTACT_NUMBER = "contact_phone";
String ALTERNATE_NUMBER = "avni_alternate_contact";
String EMAIL = "avni_email";
String HIGHEST_QUALIFICATION = "avni_highest_qualification";
Expand All @@ -27,19 +23,4 @@ public interface LahiStudentConstants {
String STREAM = "avni_stream";
String QUALIFICATION_STREAM = "avni_other_qualification_stream";
String FLOWRESULT_ID = "flowresult_id";

List<String> MandatoryFields =
Arrays.asList(
FIRST_NAME,
LAST_NAME,
DATE_OF_BIRTH,
GENDER,
FATHER_NAME,
STUDENT_CONTACT_NUMBER,
HIGHEST_QUALIFICATION,
ACADEMIC_YEAR,
QUALIFICATION_STREAM,
OTHER_QUALIFICATION,
VOCATIONAL
);
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
package org.avni_integration_service.lahi.domain;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import org.apache.log4j.Logger;
import org.avni_integration_service.common.PlatformException;
import org.avni_integration_service.common.MessageUnprocessableException;
import org.avni_integration_service.glific.bigQuery.domain.FlowResult;
import org.avni_integration_service.lahi.util.DateTimeUtil;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.time.Period;
import java.util.*;
import java.util.stream.Collectors;

@JsonIgnoreProperties(ignoreUnknown = true)
public class Student implements LahiStudentConstants {
private static final Logger logger = Logger.getLogger(Student.class);
private static final List<String> Core_Fields = Arrays.asList(FIRST_NAME,LAST_NAME,DATE_OF_BIRTH, GENDER);
private static final List<String> PRIMITIVE_OBS_FIELDS = Arrays.asList(OTHER_STATE, DISTRICT, CITY_NAME, SCHOOL,
// STUDENT_CONTACT_NUMBER, ALTERNATE_NUMBER,
EMAIL, OTHER_QUALIFICATION, FATHER_NAME, QUALIFICATION_STREAM);
private static final List<String> CODED_OBS_FIELDS = Arrays.asList(STATE, HIGHEST_QUALIFICATION,
private static final List<String> MandatoryFields =
Arrays.asList(
FIRST_NAME,
LAST_NAME,
DATE_OF_BIRTH,
GENDER
);

private static final List<String> OtherPrimitiveFields = Arrays.asList(EMAIL, OTHER_QUALIFICATION, FATHER_NAME, QUALIFICATION_STREAM);
private static final List<String> OtherCodedFields = Arrays.asList(HIGHEST_QUALIFICATION,
QUALIFICATION_STATUS, ACADEMIC_YEAR, VOCATIONAL, TRADE, STREAM);
private static final List<String> Genders = Arrays.asList("Male", "Female", "Other");

private FlowResult flowResult;
private final FlowResult flowResult;

public Student(FlowResult flowResult) {
this.flowResult = flowResult;
Expand Down Expand Up @@ -73,9 +84,20 @@ public String getFlowResultId() {
public Map<String, Object> getObservations() {
HashMap<String, Object> observations = new HashMap<>();

PRIMITIVE_OBS_FIELDS.forEach(fieldName -> observations.put(fieldName, getInput(fieldName)));
CODED_OBS_FIELDS.forEach(fieldName -> observations.put(fieldName, getCategory(fieldName)));
OtherPrimitiveFields.forEach(fieldName -> observations.put(fieldName, getInput(fieldName)));
OtherCodedFields.forEach(fieldName -> observations.put(fieldName, getCategory(fieldName)));

return observations;
}

public void validate() throws PlatformException, MessageUnprocessableException {
List<String> missingFields = MandatoryFields.stream().filter(field -> !StringUtils.hasLength(getInput(field))).collect(Collectors.toList());
if (missingFields.size() > 0)
throw new PlatformException(String.format("Missing fields: %s", String.join(",", missingFields)));
if (!Genders.contains(getGender()))
throw new PlatformException(String.format("Gender value is wrong: %s", getGender()));
boolean ageLessThan14 = Period.between(Objects.requireNonNull(DateTimeUtil.toLocalDate(getDateOfBirth(), DateTimeUtil.DD_MM_YYYY)), LocalDate.now()).getYears() < 14;
if (ageLessThan14)
throw new MessageUnprocessableException(String.format("Age is less than 14: %s", getDateOfBirth()));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class AvniStudentRepository {
public static final String STUDENT_SUBJECT_TYPE = "Student";
public static final String CONTACT_PHONE_NUMBER = "Student contact number";
public static final String ALTERNATE_PHONE_NUMBER = "Alternate (Whatsapp number)";
public static final String FATHERS_NAME_CONCEPT = "";
public static final String FATHERS_NAME_CONCEPT = "Father's name";

private static final Logger logger = Logger.getLogger(AvniStudentRepository.class);
private final AvniSubjectRepository avniSubjectRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.apache.log4j.Logger;
import org.avni_integration_service.avni.domain.Subject;
import org.avni_integration_service.common.MessageUnprocessableException;
import org.avni_integration_service.common.UnknownException;
import org.avni_integration_service.lahi.repository.AvniStudentRepository;
import org.springframework.stereotype.Service;

Expand All @@ -16,13 +18,17 @@ public AvniStudentService(AvniStudentRepository avniStudentRepository) {
this.avniStudentRepository = avniStudentRepository;
}

public void saveStudent(Subject subject) {
public void saveStudent(Subject subject) throws MessageUnprocessableException, UnknownException {
List<Subject> matchingStudents = avniStudentRepository.findMatchingStudents(subject);
if (matchingStudents.size() > 0) {
logger.warn(String.format("Found %d students with matching details, skipping", matchingStudents.size()));
return;
throw new MessageUnprocessableException("Avni has at least one such existing student");
}

avniStudentRepository.addSubject(subject);
try {
avniStudentRepository.addSubject(subject);
} catch (Exception e) {
throw new UnknownException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import org.apache.logging.log4j.util.Strings;
import org.avni_integration_service.avni.domain.Subject;
import org.avni_integration_service.common.PlatformException;
import org.avni_integration_service.integration_data.domain.framework.MappingException;
import org.avni_integration_service.lahi.config.LahiMappingDbConstants;
import org.avni_integration_service.lahi.domain.LahiStudentConstants;
import org.avni_integration_service.lahi.domain.Student;
Expand All @@ -22,9 +24,14 @@ public StudentMapper(ObservationMapper observationMapper) {
this.observationMapper = observationMapper;
}

public Subject mapToSubject(Student lahiStudent) {
public Subject mapToSubject(Student lahiStudent) throws PlatformException {
Subject subject = this.subjectWithoutObservations(lahiStudent);
observationMapper.mapObservations(subject, lahiStudent.getObservations(), LahiMappingDbConstants.MAPPING_GROUP_STUDENT, LahiMappingDbConstants.MAPPING_TYPE_OBS);
try {
observationMapper.mapObservations(subject, lahiStudent.getObservations(), LahiMappingDbConstants.MAPPING_GROUP_STUDENT, LahiMappingDbConstants.MAPPING_TYPE_OBS);
} catch (MappingException mappingException) {
throw new PlatformException(mappingException);
}

Map<String, Object> observations = subject.getObservations();
LahiMappingDbConstants.DEFAULT_STUDENT_OBS_VALUE_MAP.forEach(observations::put);
setOtherAddress(subject, lahiStudent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import org.apache.log4j.Logger;
import org.avni_integration_service.avni.domain.Subject;
import org.avni_integration_service.common.MessageUnprocessableException;
import org.avni_integration_service.common.PlatformException;
import org.avni_integration_service.common.UnknownException;
import org.avni_integration_service.lahi.domain.Student;
import org.avni_integration_service.lahi.domain.Students;
import org.avni_integration_service.lahi.service.*;
Expand Down Expand Up @@ -32,9 +35,14 @@ public void processStudents() {
Subject subject = studentMapper.mapToSubject(student);
avniStudentService.saveStudent(subject);
lahiIntegrationDataService.studentProcessed(student);
} catch (Throwable t) {
logger.error("Error while process glific student", t);
studentErrorService.studentProcessingError(student, t);
} catch (PlatformException e) {
logger.error("Platform level issue. Stop processing.", e);
throw new RuntimeException(e);
} catch (MessageUnprocessableException e) {
logger.error("Problem with message. Continue processing.", e);
} catch (UnknownException e) {
logger.error("Unknown error. Adding to error record.", e);
studentErrorService.studentProcessingError(student, e);
}
}
}
Expand Down
Loading

0 comments on commit 681a91c

Please sign in to comment.