88import gov .cdc .usds .simplereport .db .model .ApiUser ;
99import gov .cdc .usds .simplereport .db .model .DeviceType ;
1010import gov .cdc .usds .simplereport .db .model .Facility ;
11+ import gov .cdc .usds .simplereport .db .model .Organization ;
1112import gov .cdc .usds .simplereport .db .model .Person ;
1213import gov .cdc .usds .simplereport .db .model .PhoneNumber ;
1314import gov .cdc .usds .simplereport .db .model .SupportedDisease ;
@@ -55,6 +56,7 @@ public class FacilityCsvExportService {
5556 private final FacilityRepository facilityRepository ;
5657 private static final int BATCH_SIZE = 10000 ;
5758 private final PersonRepository personRepository ;
59+ private final OrganizationService organizationService ;
5860
5961 public record FacilityExportParameters (
6062 UUID facilityId ,
@@ -126,12 +128,22 @@ public void streamFacilityPatientsAsCsv(OutputStream outputStream, UUID facility
126128 long facilityPatientsCount = getFacilityPatientsCount (facilityId );
127129 int totalPages = (int ) Math .ceil ((double ) facilityPatientsCount / BATCH_SIZE );
128130
131+ // Organization currentOrg = organizationService.getOrganizationById(organizationId);
132+ // String facilityList = facilityRepository
133+ // .findAllByOrganizationAndDeleted(currentOrg, false)
134+ // .stream()
135+ // .map(Facility::getFacilityName)
136+ // .collect(Collectors.joining(";"));
137+
129138 log .info (
130139 "Starting facility CSV patient export for facilityId={}: {} patient records in {} batches" ,
131140 facilityId ,
132141 facilityPatientsCount ,
133142 totalPages );
134143
144+ // fetch facility name and all facilities in the org here? would cut down on querying the
145+ // facility
146+ // and org tables whenever we access those values in the writePatientCsvRow methode
135147 for (int currentPage = 0 ; currentPage < totalPages ; currentPage ++) {
136148 Pageable pageable = PageRequest .of (currentPage , BATCH_SIZE );
137149 List <Person > facilityPatients = fetchFacilityPatients (facilityId , pageable );
@@ -143,9 +155,9 @@ public void streamFacilityPatientsAsCsv(OutputStream outputStream, UUID facility
143155 BATCH_SIZE ,
144156 facilityPatients .size (),
145157 (currentPage * BATCH_SIZE ) + facilityPatients .size ());
146- for (Person patient : facilityPatients ) {
147- writePatientCsvRow (csvPrinter , patient );
148- }
158+ // for (Person patient : facilityPatients) {
159+ // writePatientCsvRow(csvPrinter, patient);
160+ // }
149161
150162 csvPrinter .flush ();
151163 log .debug ("Processed batch {}/{} for facility CSV export" , (currentPage + 1 ), totalPages );
@@ -158,6 +170,8 @@ public void streamFacilityPatientsAsCsv(OutputStream outputStream, UUID facility
158170 }
159171 }
160172
173+ // fetch all facilities in the org here? would cut down on querying the facility
174+ // and org tables whenever we access those values in the writePatientCsvRow methode
161175 @ Transactional (readOnly = true )
162176 public void streamOrganizationPatientsAsCsv (OutputStream outputStream , UUID organizationId ) {
163177
@@ -167,6 +181,12 @@ public void streamOrganizationPatientsAsCsv(OutputStream outputStream, UUID orga
167181 long organizationPatientsCount = getOrganizationPatientsCount (organizationId );
168182 int totalPages = (int ) Math .ceil ((double ) organizationPatientsCount / BATCH_SIZE );
169183
184+ Organization currentOrg = organizationService .getOrganizationById (organizationId );
185+ String facilityList =
186+ facilityRepository .findAllByOrganizationAndDeleted (currentOrg , false ).stream ()
187+ .map (Facility ::getFacilityName )
188+ .collect (Collectors .joining (";" ));
189+
170190 log .info (
171191 "Starting organization CSV patient export for organizationId={}: {} patient records in {} batches" ,
172192 organizationId ,
@@ -185,7 +205,7 @@ public void streamOrganizationPatientsAsCsv(OutputStream outputStream, UUID orga
185205 organizationPatients .size (),
186206 (currentPage * BATCH_SIZE ) + organizationPatients .size ());
187207 for (Person patient : organizationPatients ) {
188- writePatientCsvRow (csvPrinter , patient );
208+ writePatientCsvRow (csvPrinter , patient , facilityList );
189209 }
190210
191211 csvPrinter .flush ();
@@ -494,7 +514,8 @@ private void writeCsvRow(CSVPrinter csvPrinter, TestResultsListItem item) throws
494514 patient != null ? patient .getEmployedInHealthcare () : "" );
495515 }
496516
497- private void writePatientCsvRow (CSVPrinter csvPrinter , Person patient ) throws IOException {
517+ private void writePatientCsvRow (CSVPrinter csvPrinter , Person patient , String facilityList )
518+ throws IOException {
498519 // "First Name",
499520 // "Middle Name",
500521 // "Last Name",
@@ -545,13 +566,7 @@ private void writePatientCsvRow(CSVPrinter csvPrinter, Person patient) throws IO
545566 trimToEmpty (patient .getGender ()),
546567 trimToEmpty (patient .getEthnicity ()),
547568 trimToEmpty (patient .getRole ().toString ()),
548- patient .getFacility () != null
549- ? patient .getFacility ().getFacilityName ()
550- : facilityRepository
551- .findAllByOrganizationAndDeleted (patient .getOrganization (), false )
552- .stream ()
553- .map (Facility ::getFacilityName )
554- .collect (Collectors .joining (";" )),
569+ patient .getFacility () != null ? patient .getFacility ().getFacilityName () : facilityList ,
555570 patient .getEmployedInHealthcare () != null ? patient .getEmployedInHealthcare () : "" ,
556571 patient .getResidentCongregateSetting () != null
557572 ? patient .getResidentCongregateSetting ()
0 commit comments