@@ -80,75 +80,34 @@ public List<? extends AbstractResourceEntity> findAll(String facilityId, List<Pa
8080 return mongoOperations .find (query , entityType );
8181 }
8282
83- public List <SharedResource > findSharedResources (String facilityId , List <PatientReportingEvaluationStatus .Resource > resources ) {
84- List <SharedResource > sharedResources = new ArrayList <>();
85- if (resources == null || resources .isEmpty ()) {
86- return sharedResources ;
87- }
88-
89- // Batch the searches so that it doesn't exceed mongo's query limitations
90- List <PatientReportingEvaluationStatus .Resource > remainingResources = new ArrayList <>(resources );
91- while (!remainingResources .isEmpty ()) {
92- int characterCount = 0 ;
93- List <Criteria > batchCriteria = new ArrayList <>();
94-
95- for (int i = remainingResources .size () - 1 ; i >= 0 ; i --) {
96- PatientReportingEvaluationStatus .Resource resource = remainingResources .get (i );
97- int resourceCharLength = resource .getResourceType ().toString ().length () + resource .getResourceId ().length ();
98-
99- if (characterCount + resourceCharLength >= 10000 ) {
100- break ;
101- }
102-
103- characterCount += resourceCharLength ;
104- batchCriteria .add (Criteria .where ("facilityId" ).is (facilityId )
105- .and ("resourceType" ).is (resource .getResourceType ())
106- .and ("resourceId" ).is (resource .getResourceId ()));
107- remainingResources .remove (i );
108- }
109-
110- if (!batchCriteria .isEmpty ()) {
111- Criteria combinedCriteria = new Criteria ().orOperator (batchCriteria .toArray (new Criteria [0 ]));
112- Query query = new Query (combinedCriteria );
113- sharedResources .addAll (mongoOperations .find (query , SharedResource .class ));
114- }
115- }
116-
117- return sharedResources ;
118- }
119-
120- public List <PatientResource > findPatientResources (String facilityId , String patientId , List <String > reportIds ) {
83+ public List <PatientResource > findResources (boolean isShared , String facilityId , String correlationId ) {
12184 List <AggregationOperation > pipeline = new ArrayList <>();
12285
12386 pipeline .add (Aggregation .match (Criteria .where ("facilityId" ).is (facilityId )
124- .and ("patientId" ).is (patientId )
125- .and ("reports.reportTrackingId" ).in (reportIds )));
87+ .and ("correlationId" ).is (correlationId )));
12688 pipeline .add (Aggregation .unwind ("resources" ));
127- pipeline .add (Aggregation .match (Criteria .where ("resources.normalizationStatus" ).is ("NORMALIZED" )));
89+ pipeline .add (Aggregation .replaceRoot ("resources" ));
90+ pipeline .add (Aggregation .match (Criteria .where ("isPatientResource" ).is (!isShared )
91+ .and ("normalizationStatus" ).is ("NORMALIZED" )));
12892
12993 Document lookupStage = new Document ("$lookup" ,
130- new Document ("from" , "patientResource" )
131- .append ("let" , new Document ("localResourceId" , "$resources.resourceId" )
132- .append ("localResourceType" , "$resources.resourceType" ))
133- .append ("pipeline" , List .of (
134- new Document ("$match" ,
135- new Document ("$expr" ,
136- new Document ("$and" , List .of (
137- new Document ("$eq" , List .of ("$resourceId" , "$$localResourceId" )),
138- new Document ("$eq" , List .of ("$resourceType" , "$$localResourceType" )),
139- new Document ("$eq" , List .of ("$facilityId" , facilityId ))
140- ))
141- )
142- )
143- ))
144- .append ("as" , "matchedResources" )
145- );
94+ new Document ("from" , isShared ? "sharedResource" : "patientResource" )
95+ .append ("localField" , "resourceId" )
96+ .append ("foreignField" , "resourceId" )
97+ .append ("as" , "patientResources" ));
14698
14799 pipeline .add (new CustomAggregationOperation (lookupStage ));
100+ pipeline .add (Aggregation .unwind ("patientResources" ));
101+
102+ Document matchExpr = new Document ("$match" ,
103+ new Document ("$expr" ,
104+ new Document ("$and" , List .of (
105+ new Document ("$eq" , List .of ("$patientResources.facilityId" , facilityId )),
106+ new Document ("$eq" , List .of ("$patientResources.resourceType" , "$resourceType" ))
107+ ))));
108+ pipeline .add (new CustomAggregationOperation (matchExpr ));
148109
149- pipeline .add (Aggregation .unwind ("matchedResources" ));
150- pipeline .add (Aggregation .match (Criteria .where ("matchedResources.facilityId" ).is (facilityId )));
151- pipeline .add (Aggregation .replaceRoot ("matchedResources" ));
110+ pipeline .add (Aggregation .replaceRoot ("patientResources" ));
152111
153112 Aggregation aggregation = Aggregation .newAggregation (pipeline );
154113 return mongoOperations .aggregate (aggregation , "patientReportingEvaluationStatus" , PatientResource .class ).getMappedResults ();
0 commit comments