@@ -89,7 +89,7 @@ public class SyncFHIRRecord {
8989
9090 SyncFhirCase syncFhirCase = null ;
9191
92- private SyncFhirProfile profile ;
92+ private SyncFhirProfile profile = null ;
9393 private List <PatientProgram > patientPrograms ;
9494
9595 Map <String , Object > anyOtherObject = new HashMap <>();
@@ -728,7 +728,7 @@ private Collection<String> groupInBundles(String resourceType, Collection<IBaseR
728728 return resourceBundles ;
729729 }
730730
731- private Collection <String > groupInCaseBundle (String resourceType , Collection <IBaseResource > iBaseResources , String identifierTypeName ) {
731+ public Collection <String > groupInCaseBundle (String resourceType , Collection <IBaseResource > iBaseResources , String identifierTypeName ) {
732732
733733 Collection <String > resourceBundles = new ArrayList <>();
734734
@@ -752,7 +752,7 @@ private String encodeResourceToString(String resourceType, String identifierType
752752
753753 if (resourceType .equals ("Patient" ) || resourceType .equals ("Practitioner" )) {
754754
755- if (resourceType .equals ("Patient" ) && profile .getKeepProfileIdentifierOnly ()) {
755+ if (resourceType .equals ("Patient" ) && profile != null && profile .getKeepProfileIdentifierOnly ()) {
756756 try {
757757 jsonString = removeIdentifierExceptProfileId (jsonString , "identifier" );
758758 jsonString = addCodingToIdentifier (jsonString , "identifier" );
@@ -803,14 +803,19 @@ private String addUseOfficialToName(String payload, String attributeName) {
803803 }
804804
805805 private String removeIdentifierExceptProfileId (String payload , String attributeName ) {
806- JSONObject jsonObject = new JSONObject (payload );
807- int objectCount = 0 ;
808- for (int i = 0 ; i < jsonObject .getJSONArray (attributeName ).length (); i ++) {
809- if (!jsonObject .getJSONArray ("identifier" ).getJSONObject (i ).getJSONObject ("type" ).getJSONArray ("coding" ).getJSONObject (0 ).get ("code" ).toString ().equals (profile .getPatientIdentifierType ().getUuid ())) {
810- jsonObject .getJSONArray ("identifier" ).remove (i );
806+ if (profile != null ) {
807+ JSONObject jsonObject = new JSONObject (payload );
808+ int objectCount = 0 ;
809+ for (int i = 0 ; i < jsonObject .getJSONArray (attributeName ).length (); i ++) {
810+ if (!jsonObject .getJSONArray ("identifier" ).getJSONObject (i ).getJSONObject ("type" ).getJSONArray ("coding" ).getJSONObject (0 ).get ("code" ).toString ().equals (profile .getPatientIdentifierType ().getUuid ())) {
811+ jsonObject .getJSONArray ("identifier" ).remove (i );
812+ }
811813 }
814+
815+ return jsonObject .toString ();
816+ } else {
817+ return payload ;
812818 }
813- return jsonObject .toString ();
814819 }
815820
816821 private String removeAttribute (String payload , String attributeName ) {
@@ -963,19 +968,21 @@ private List<Person> getPersonsFromEncounterList(List<Encounter> encounters) {
963968 }
964969
965970
966- private Collection <IBaseResource > getPatientResourceBundle (SyncFhirProfile syncFhirProfile , List <PatientIdentifier > patientIdentifiers , SyncFhirCase syncFhirCase ) {
971+ public Collection <IBaseResource > getPatientResourceBundle (SyncFhirProfile syncFhirProfile , List <PatientIdentifier > patientIdentifiers , SyncFhirCase syncFhirCase ) {
967972
968973 DateRangeParam lastUpdated = new DateRangeParam ();
969974
970- if (syncFhirProfile .getIsCaseBasedProfile ()) {
971- if (syncFhirCase != null && syncFhirCase .getLastUpdateDate () != null ) {
972- lastUpdated = new DateRangeParam ().setUpperBoundInclusive (new Date ()).setLowerBoundInclusive (syncFhirCase .getLastUpdateDate ());
973- } else if (syncFhirCase != null ) {
974- lastUpdated = new DateRangeParam ().setUpperBoundInclusive (new Date ()).setLowerBoundInclusive (getDefaultLastSyncDate ());
975- }
976- } else {
977- lastUpdated = new DateRangeParam ().setUpperBoundInclusive (new Date ()).setLowerBoundInclusive (getLastSyncDate (syncFhirProfile , "Patient" ));
975+ if (syncFhirProfile != null ) {
976+ if (syncFhirProfile .getIsCaseBasedProfile ()) {
977+ if (syncFhirCase != null && syncFhirCase .getLastUpdateDate () != null ) {
978+ lastUpdated = new DateRangeParam ().setUpperBoundInclusive (new Date ()).setLowerBoundInclusive (syncFhirCase .getLastUpdateDate ());
979+ } else if (syncFhirCase != null ) {
980+ lastUpdated = new DateRangeParam ().setUpperBoundInclusive (new Date ()).setLowerBoundInclusive (getDefaultLastSyncDate ());
981+ }
982+ } else {
983+ lastUpdated = new DateRangeParam ().setUpperBoundInclusive (new Date ()).setLowerBoundInclusive (getLastSyncDate (syncFhirProfile , "Patient" ));
978984
985+ }
979986 }
980987
981988
@@ -992,12 +999,14 @@ private Collection<IBaseResource> getPatientResourceBundle(SyncFhirProfile syncF
992999 return getApplicationContext ().getBean (FhirPatientService .class ).searchForPatients (patientSearchParams ).getResources (0 , Integer .MAX_VALUE );
9931000 }
9941001
995- private Collection <IBaseResource > getPractitionerResourceBundle (SyncFhirProfile syncFhirProfile , List <Encounter > encounterList , List <Order > orders ) {
1002+ public Collection <IBaseResource > getPractitionerResourceBundle (SyncFhirProfile syncFhirProfile , List <Encounter > encounterList , List <Order > orders ) {
9961003 PractitionerSearchParams practitionerSearchParams = new PractitionerSearchParams ();
9971004
998- if (!syncFhirProfile .getIsCaseBasedProfile ()) {
999- DateRangeParam lastUpdated = new DateRangeParam ().setUpperBoundInclusive (new Date ()).setLowerBoundInclusive (getLastSyncDate (syncFhirProfile , "Practitioner" ));
1000- practitionerSearchParams .setLastUpdated (lastUpdated );
1005+ if (syncFhirProfile != null ) {
1006+ if (!syncFhirProfile .getIsCaseBasedProfile ()) {
1007+ DateRangeParam lastUpdated = new DateRangeParam ().setUpperBoundInclusive (new Date ()).setLowerBoundInclusive (getLastSyncDate (syncFhirProfile , "Practitioner" ));
1008+ practitionerSearchParams .setLastUpdated (lastUpdated );
1009+ }
10011010 }
10021011 TokenAndListParam providerReference = new TokenAndListParam ();
10031012 for (Encounter encounter : encounterList ) {
@@ -1011,28 +1020,36 @@ private Collection<IBaseResource> getPractitionerResourceBundle(SyncFhirProfile
10111020 providerReference .addAnd (new TokenParam (order .getOrderer ().getUuid ()));
10121021 }
10131022
1014- if (providerReference != null ) {
1023+ if (providerReference . size ()> 0 ) {
10151024 practitionerSearchParams .setIdentifier (providerReference );
10161025 }
10171026
1018- return getApplicationContext ().getBean (FhirPractitionerService .class ).searchForPractitioners (practitionerSearchParams ).getResources (0 , Integer .MAX_VALUE );
1027+ List <IBaseResource > iBaseResources = new ArrayList <>();
1028+
1029+ if (providerReference .size ()>0 ) {
1030+ iBaseResources = getApplicationContext ().getBean (FhirPractitionerService .class ).searchForPractitioners (practitionerSearchParams ).getResources (0 , Integer .MAX_VALUE );
1031+ }
1032+
1033+ return iBaseResources ;
10191034
10201035 }
10211036
1022- private Collection <IBaseResource > getPersonResourceBundle (SyncFhirProfile syncFhirProfile , List <Person > personList , SyncFhirCase syncFhirCase ) {
1037+ public Collection <IBaseResource > getPersonResourceBundle (SyncFhirProfile syncFhirProfile , List <Person > personList , SyncFhirCase syncFhirCase ) {
10231038
10241039
10251040 DateRangeParam lastUpdated = new DateRangeParam ();
10261041
1027- if (syncFhirProfile .getIsCaseBasedProfile ()) {
1028- if (syncFhirCase != null && syncFhirCase .getLastUpdateDate () != null ) {
1029- lastUpdated = new DateRangeParam ().setUpperBoundInclusive (new Date ()).setLowerBoundInclusive (syncFhirCase .getLastUpdateDate ());
1042+ if (syncFhirProfile != null ) {
1043+ if (syncFhirProfile .getIsCaseBasedProfile ()) {
1044+ if (syncFhirCase != null && syncFhirCase .getLastUpdateDate () != null ) {
1045+ lastUpdated = new DateRangeParam ().setUpperBoundInclusive (new Date ()).setLowerBoundInclusive (syncFhirCase .getLastUpdateDate ());
1046+ } else {
1047+ lastUpdated = new DateRangeParam ().setUpperBoundInclusive (new Date ()).setLowerBoundInclusive (getDefaultLastSyncDate ());
1048+ }
10301049 } else {
1031- lastUpdated = new DateRangeParam ().setUpperBoundInclusive (new Date ()).setLowerBoundInclusive (getDefaultLastSyncDate ());
1032- }
1033- } else {
1034- lastUpdated = new DateRangeParam ().setUpperBoundInclusive (new Date ()).setLowerBoundInclusive (getLastSyncDate (syncFhirProfile , "Patient" ));
1050+ lastUpdated = new DateRangeParam ().setUpperBoundInclusive (new Date ()).setLowerBoundInclusive (getLastSyncDate (syncFhirProfile , "Patient" ));
10351051
1052+ }
10361053 }
10371054
10381055 PersonSearchParams personSearchParams = new PersonSearchParams ();
@@ -1052,7 +1069,7 @@ private Collection<IBaseResource> getPersonResourceBundle(SyncFhirProfile syncFh
10521069 }
10531070
10541071
1055- private Collection <IBaseResource > getEncounterResourceBundle (List <Encounter > encounters ) {
1072+ public Collection <IBaseResource > getEncounterResourceBundle (List <Encounter > encounters ) {
10561073
10571074
10581075 Collection <String > encounterUUIDS = new ArrayList <>();
@@ -1072,39 +1089,41 @@ private Collection<IBaseResource> getEncounterResourceBundle(List<Encounter> enc
10721089 return iBaseResources ;
10731090 }
10741091
1075- private Collection <IBaseResource > getObservationResourceBundle (SyncFhirProfile syncFhirProfile , List <Encounter > encounterList , List <Person > personList ) {
1076-
1077- JSONObject searchParams = getSearchParametersInJsonObject ("Observation" , syncFhirProfile .getResourceSearchParameter ());
1092+ public Collection <IBaseResource > getObservationResourceBundle (SyncFhirProfile syncFhirProfile , List <Encounter > encounterList , List <Person > personList ) {
10781093
1079- JSONArray codes = searchParams .getJSONArray ("code" );
10801094
10811095 List <Concept > conceptQuestionList = new ArrayList <>();
10821096
1083- for (Object conceptUID : codes ) {
1084- try {
1097+ DateRangeParam lastUpdated = new DateRangeParam ();
10851098
1086- Concept concept = Context .getConceptService ().getConcept (conceptUID .toString ());
1087- if (concept != null ) {
1088- conceptQuestionList .add (concept );
1089- }
1099+ if (syncFhirProfile != null ) {
1100+ JSONObject searchParams = getSearchParametersInJsonObject ("Observation" , syncFhirProfile .getResourceSearchParameter ());
10901101
1091- } catch (Exception e ) {
1092- log .error ("Error while adding concept with uuid " + conceptUID , e );
1093- }
1102+ JSONArray codes = searchParams .getJSONArray ("code" );
10941103
1095- }
1104+ for (Object conceptUID : codes ) {
1105+ try {
10961106
1097- DateRangeParam lastUpdated = new DateRangeParam ();
1107+ Concept concept = Context .getConceptService ().getConcept (conceptUID .toString ());
1108+ if (concept != null ) {
1109+ conceptQuestionList .add (concept );
1110+ }
1111+
1112+ } catch (Exception e ) {
1113+ log .error ("Error while adding concept with uuid " + conceptUID , e );
1114+ }
10981115
1099- if (syncFhirProfile .getIsCaseBasedProfile ()) {
1100- if (syncFhirCase != null && syncFhirCase .getLastUpdateDate () != null ) {
1101- lastUpdated = new DateRangeParam ().setUpperBoundInclusive (new Date ()).setLowerBoundInclusive (syncFhirCase .getLastUpdateDate ());
1102- } else if (syncFhirCase != null ) {
1103- lastUpdated = new DateRangeParam ().setUpperBoundInclusive (new Date ()).setLowerBoundInclusive (getDefaultLastSyncDate ());
11041116 }
1105- } else {
1106- lastUpdated = new DateRangeParam ().setUpperBoundInclusive (new Date ()).setLowerBoundInclusive (getLastSyncDate (syncFhirProfile , "Observation" ));
1117+ if (syncFhirProfile .getIsCaseBasedProfile ()) {
1118+ if (syncFhirCase != null && syncFhirCase .getLastUpdateDate () != null ) {
1119+ lastUpdated = new DateRangeParam ().setUpperBoundInclusive (new Date ()).setLowerBoundInclusive (syncFhirCase .getLastUpdateDate ());
1120+ } else if (syncFhirCase != null ) {
1121+ lastUpdated = new DateRangeParam ().setUpperBoundInclusive (new Date ()).setLowerBoundInclusive (getDefaultLastSyncDate ());
1122+ }
1123+ } else {
1124+ lastUpdated = new DateRangeParam ().setUpperBoundInclusive (new Date ()).setLowerBoundInclusive (getLastSyncDate (syncFhirProfile , "Observation" ));
11071125
1126+ }
11081127 }
11091128
11101129 List <Obs > observationList = Context .getObsService ().getObservations (personList , encounterList , conceptQuestionList , null , null , null , null , null , null , getLastSyncDate (syncFhirProfile , "Observation" ), new Date (), false );
@@ -1121,7 +1140,7 @@ private Collection<IBaseResource> getObservationResourceBundle(SyncFhirProfile s
11211140 }
11221141
11231142
1124- private Collection <IBaseResource > getServiceRequestResourceBundle (List <Order > testOrders ) {
1143+ public Collection <IBaseResource > getServiceRequestResourceBundle (List <Order > testOrders ) {
11251144
11261145
11271146 Collection <String > testOrdersUUIDS = new ArrayList <>();
0 commit comments