Skip to content

Commit

Permalink
#96 - deprecated some methods searching subject by providing beginnin…
Browse files Browse the repository at this point in the history
…g of time. can be removed in future. util to compare string values.
  • Loading branch information
petmongrels committed Nov 7, 2023
1 parent 266fdcd commit 9b1fe02
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.*;

@Component
public class AvniSubjectRepository extends BaseAvniRepository {
Expand All @@ -37,6 +35,11 @@ public <T> T getSubjects(Date lastModifiedDateTime, String subjectType, String p
return response;
}

public Subject[] getSubjects(String subjectType, HashMap<String, Object> observationParam) {
return this.getSubjects(getStartingTime(), subjectType, observationParam);
}

@Deprecated // for getting some subjects use without lastModifiedDateTime method
public Subject[] getSubjects(Date lastModifiedDateTime, String subjectType, HashMap<String, Object> concepts) {
String fromTime = FormatAndParseUtil.toISODateTimeString(lastModifiedDateTime);
HashMap<String, String> queryParams = new HashMap<>();
Expand All @@ -50,6 +53,15 @@ public Subject[] getSubjects(Date lastModifiedDateTime, String subjectType, Hash
return Arrays.stream(subjects).filter(subject -> !subject.getVoided()).toArray(Subject[]::new);
}

public Subject getSubject(String subjectType, HashMap<String, Object> concepts) {
return pickAndExpectOne(getSubjects(getStartingTime(), subjectType, concepts));
}

private Date getStartingTime() {
return new GregorianCalendar(1900, Calendar.JANUARY, 1).getTime();
}

@Deprecated // for getting subject use without lastModifiedDateTime method
public Subject getSubject(Date lastModifiedDateTime, String subjectType, HashMap<String, Object> concepts) {
return pickAndExpectOne(getSubjects(lastModifiedDateTime, subjectType, concepts));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,13 @@ public Subject findSubject(OpenMRSPatient openMRSPatient, PatientToSubjectMetaDa
LinkedHashMap<String, Object> subjectCriteria = new LinkedHashMap<>();
String prefix = constants.getValue(ConstantKey.BahmniIdentifierPrefix.name());
subjectCriteria.put(patientToSubjectMetaData.avniIdentifierConcept(), identifier.replace(prefix, ""));
return avniSubjectRepository.getSubject(
new GregorianCalendar(1900, Calendar.JANUARY, 1).getTime(),
constants.getValue(ConstantKey.IntegrationAvniSubjectType.name()),
subjectCriteria
);
return avniSubjectRepository.getSubject(constants.getValue(ConstantKey.IntegrationAvniSubjectType.name()), subjectCriteria);
}

public Subject[] findSubjects(Subject subject, SubjectToPatientMetaData subjectToPatientMetaData, Constants constants) {
LinkedHashMap<String, Object> subjectCriteria = new LinkedHashMap<>();
subjectCriteria.put(subjectToPatientMetaData.avniIdentifierConcept(), subject.getId(subjectToPatientMetaData.avniIdentifierConcept()));
return avniSubjectRepository.getSubjects(
new GregorianCalendar(1900, Calendar.JANUARY, 1).getTime(),
constants.getValue(ConstantKey.IntegrationAvniSubjectType.name()),
subjectCriteria
);
return avniSubjectRepository.getSubjects(constants.getValue(ConstantKey.IntegrationAvniSubjectType.name()), subjectCriteria);
}

public void createRegistrationEncounter(OpenMRSPatient openMRSPatient, Subject subject, PatientToSubjectMetaData patientToSubjectMetaData) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ public interface LahiMappingDbConstants {
String MAPPING_TYPE_OBS = "Observations";
String MAPPING_GROUP_STUDENT = "Student";

String CONTACT_PHONE_NUMBER = "Student contact number";
String ALTERNATE_PHONE_NUMBER = "Alternate (Whatsapp number)";

Map<String,Object> DEFAULT_STUDENT_OBS_VALUE_MAP = Map.ofEntries(
entry("Does student give permission to LAHI to Send Whatsapp/SMS/Call for any career opportunities?","Yes"),
entry("Career options you are interested in", Collections.singletonList("Other")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@
import org.apache.log4j.Logger;
import org.avni_integration_service.avni.domain.Subject;
import org.avni_integration_service.avni.repository.AvniSubjectRepository;
import org.avni_integration_service.util.ObjectUtil;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.stream.Collectors;

@Component
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 = "";

private static final Logger logger = Logger.getLogger(AvniStudentRepository.class);
private final AvniSubjectRepository avniSubjectRepository;

Expand All @@ -22,6 +31,17 @@ public void addSubject(Subject subject) {
}

public List<Subject> findMatchingStudents(Subject subject) {
LinkedHashMap<String, Object> subjectSearchCriteria = new LinkedHashMap<>();
subjectSearchCriteria.put(CONTACT_PHONE_NUMBER, subject.getObservation(CONTACT_PHONE_NUMBER));
Subject[] subjects = avniSubjectRepository.getSubjects(STUDENT_SUBJECT_TYPE, subjectSearchCriteria);
// First Name, Last Name, Father Name, DOB, Gender
List<Subject> duplicateSubjects = Arrays.stream(subjects).filter(x ->
ObjectUtil.nullSafeEqualsIgnoreCase(x.getFirstName(), subject.getFirstName())
&& ObjectUtil.nullSafeEqualsIgnoreCase(x.getLastName(), subject.getLastName())
&& ObjectUtil.nullSafeEqualsIgnoreCase(x.getGender(), subject.getGender())
&& ObjectUtil.nullSafeEqualsIgnoreCase(x.getDateOfBirth(), subject.getDateOfBirth())
&& ObjectUtil.nullSafeEqualsIgnoreCase(x.getObservation(FATHERS_NAME_CONCEPT), subject.getObservation(FATHERS_NAME_CONCEPT))
).collect(Collectors.toList());
return new ArrayList<>();
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.avni_integration_service.lahi.service;

import org.apache.logging.log4j.util.Strings;
import org.avni_integration_service.avni.domain.ObservationHolder;
import org.avni_integration_service.avni.domain.Subject;
import org.avni_integration_service.lahi.config.LahiMappingDbConstants;
import org.avni_integration_service.lahi.domain.LahiStudentConstants;
import org.avni_integration_service.lahi.domain.LahiStudent;
import org.avni_integration_service.lahi.repository.AvniStudentRepository;
import org.avni_integration_service.lahi.util.DateTimeUtil;
import org.avni_integration_service.common.ObservationMapper;
import org.springframework.stereotype.Service;
Expand All @@ -24,18 +24,14 @@ public StudentMapper(ObservationMapper observationMapper) {

public Subject mapToSubject(LahiStudent lahiStudent) {
Subject subject = this.subjectWithoutObservations(lahiStudent);
this.populateObservations(subject, lahiStudent);
observationMapper.mapObservations(subject, lahiStudent.getObservations(), LahiMappingDbConstants.MAPPING_GROUP_STUDENT, LahiMappingDbConstants.MAPPING_TYPE_OBS);
Map<String, Object> observations = subject.getObservations();
LahiMappingDbConstants.DEFAULT_STUDENT_OBS_VALUE_MAP.forEach(observations::put);
setOtherAddress(subject, lahiStudent);
setPhoneNumber(subject, lahiStudent);
return subject;
}

private void populateObservations(ObservationHolder observationHolder, LahiStudent student) {
observationMapper.mapObservations(observationHolder, student.getObservations(), LahiMappingDbConstants.MAPPING_GROUP_STUDENT, LahiMappingDbConstants.MAPPING_TYPE_OBS);
}

private void setOtherAddress(Subject subject, LahiStudent student) {
Map<String, Object> subjectObservations = subject.getObservations();
StringBuilder stringBuilder = new StringBuilder();
Expand All @@ -59,7 +55,7 @@ private void setPhoneNumber(Subject subject, LahiStudent student) {
String contactNumber = student.getContactPhone();
if (contactNumber != null && contactNumber.length() == 12) {
contactPhoneNumber = contactNumber.substring(2);
subjectObservations.put(LahiMappingDbConstants.CONTACT_PHONE_NUMBER, contactPhoneNumber);
subjectObservations.put(AvniStudentRepository.CONTACT_PHONE_NUMBER, contactPhoneNumber);
}
setAlternatePhoneNumber(student, subjectObservations, contactPhoneNumber);
}
Expand All @@ -72,7 +68,7 @@ private void setAlternatePhoneNumber(LahiStudent student, Map<String, Object> su
}
alternatePhoneNumber = Long.parseLong((StringUtils.hasText(alternateNumber) && alternateNumber.length() == 10) ?
alternateNumber : contactPhoneNumber);
subjectObservations.put(LahiMappingDbConstants.ALTERNATE_PHONE_NUMBER, alternatePhoneNumber);
subjectObservations.put(AvniStudentRepository.ALTERNATE_PHONE_NUMBER, alternatePhoneNumber);
}

private Subject subjectWithoutObservations(LahiStudent student) {
Expand All @@ -92,7 +88,7 @@ private Subject subjectWithoutObservations(LahiStudent student) {
subject.setRegistrationDate(registrationDate);
subject.setDateOfBirth(dob);
subject.setGender(gender);
subject.setSubjectType("Student");
subject.setSubjectType(AvniStudentRepository.STUDENT_SUBJECT_TYPE);
return subject;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.avni_integration_service.util;

import org.springframework.util.ObjectUtils;

public class ObjectUtil {
public static boolean nullSafeEqualsIgnoreCase(Object o1, Object o2) {
if (ObjectUtils.nullSafeEquals(o1, o2)) {
return true;
}
if (o1 != null && o2 != null) {
return ObjectUtil.nullSafeEqualsIgnoreCase((String) o1, (String) o2);
}
return false;
}

public static boolean nullSafeEqualsIgnoreCase(String o1, String o2) {
if (ObjectUtils.nullSafeEquals(o1, o2)) {
return true;
}
if (o1 != null && o2 != null) {
return o1.equalsIgnoreCase(o2);
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.avni_integration_service.util;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class ObjectUtilTest {
@Test
public void nullSafeEqualsIgnoreCase() {
assertTrue(ObjectUtil.nullSafeEqualsIgnoreCase("A", "a"));
assertTrue(ObjectUtil.nullSafeEqualsIgnoreCase("A", "A"));
assertTrue(ObjectUtil.nullSafeEqualsIgnoreCase(null, null));
assertFalse(ObjectUtil.nullSafeEqualsIgnoreCase(null, "a"));
assertFalse(ObjectUtil.nullSafeEqualsIgnoreCase("a", "b"));
}
}

0 comments on commit 9b1fe02

Please sign in to comment.