<commit_before>package uk.gov.hscic.patient.resource.provider;

import ca.uhn.fhir.model.api.IDatatype;
import ca.uhn.fhir.model.dstu2.composite.CodeableConceptDt;
import ca.uhn.fhir.model.dstu2.composite.CodingDt;
import ca.uhn.fhir.model.dstu2.composite.IdentifierDt;
import ca.uhn.fhir.model.dstu2.composite.NarrativeDt;
import ca.uhn.fhir.model.dstu2.composite.ResourceReferenceDt;
import ca.uhn.fhir.model.dstu2.resource.Bundle;
import ca.uhn.fhir.model.dstu2.resource.Bundle.Entry;
import ca.uhn.fhir.model.dstu2.resource.Composition;
import ca.uhn.fhir.model.dstu2.resource.Composition.Section;
import ca.uhn.fhir.model.dstu2.resource.Parameters;
import ca.uhn.fhir.model.dstu2.resource.Parameters.Parameter;
import ca.uhn.fhir.model.dstu2.resource.Patient;
import ca.uhn.fhir.model.dstu2.valueset.BundleTypeEnum;
import ca.uhn.fhir.model.dstu2.valueset.CompositionStatusEnum;
import ca.uhn.fhir.model.dstu2.valueset.NarrativeStatusEnum;
import ca.uhn.fhir.model.primitive.DateTimeDt;
import ca.uhn.fhir.model.primitive.StringDt;
import ca.uhn.fhir.rest.annotation.Operation;
import ca.uhn.fhir.rest.annotation.ResourceParam;
import ca.uhn.fhir.rest.server.IResourceProvider;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;
import org.springframework.context.ApplicationContext;
import uk.gov.hscic.common.types.RepoSource;
import uk.gov.hscic.common.types.RepoSourceType;
import uk.gov.hscic.patient.patientsummary.model.PatientSummaryListHTML;
import uk.gov.hscic.patient.patientsummary.search.PatientSummarySearch;
import uk.gov.hscic.patient.patientsummary.search.PatientSummarySearchFactory;

public class PatientResourceProvider implements IResourceProvider {
    
    @Override
    public Class<Patient> getResourceType() {
        return Patient.class;
    }
    
    ApplicationContext applicationContext;
    
    public void setApplicationContext(ApplicationContext applicationContext){
        this.applicationContext = applicationContext;
    }
            
    @Operation(name="$getcarerecord")
    public Bundle getPatientCareRecord(@ResourceParam Parameters params){
                
        String nhsNumber = null;
        ArrayList<String> sectionsParamList = new ArrayList();
        
        // Extract the parameters
        for(Parameter param : params.getParameter()){
            
            String paramName = param.getName();
            IDatatype value = param.getValue();
            
            if(value instanceof IdentifierDt){
                nhsNumber = ((IdentifierDt)value).getValue();
            }else if(value instanceof StringDt){
                sectionsParamList.add(((StringDt)value).getValue());
            }
        }
        
        
        // Build
        Bundle bundle = new Bundle();
        bundle.setType(BundleTypeEnum.SEARCH_RESULTS);  // This type is possibly wrong but the type "searchset" is not available in the API enum.
        Entry entry = new Entry();
        
        
        // Validate request fields
        if(nhsNumber == null || nhsNumber.isEmpty()){
            // Build the OperationOutcome response
            
            
        } else {
            //Build the Composition
            Composition careRecordComposition = new Composition();
            
            
            // Set Composition Mandatory Fields
            careRecordComposition.setDate(new DateTimeDt(Calendar.getInstance().getTime()));
            
            CodingDt coding = new CodingDt();
            coding.setSystem("http://fhir.nhs.net/ValueSet/document-type-codes-1-0");
            coding.setCode("CAR");
            coding.setDisplay("Care Record");
            CodeableConceptDt codableConcept = new CodeableConceptDt();
            codableConcept.addCoding(coding);
            codableConcept.setText("Care Record");
            careRecordComposition.setType(codableConcept);
            
            careRecordComposition.setTitle("Patient Care Record");
            
            careRecordComposition.setStatus(CompositionStatusEnum.FINAL);
            
            ResourceReferenceDt patientReference = new ResourceReferenceDt("/Patient/" + nhsNumber);
            careRecordComposition.setSubject(patientReference);
            
            ResourceReferenceDt clinicianReference = new ResourceReferenceDt("/123456");
            careRecordComposition.setAuthor(Collections.singletonList(clinicianReference));
            
            
            // Build requested sections
            if(sectionsParamList.size() > 0){
                ArrayList<Section> sectionsList = new ArrayList();
                
                RepoSource sourceType = RepoSourceType.fromString(null);
                
                for(String sectionName : sectionsParamList){
                    switch(sectionName){
                        case "Summary" :
                                PatientSummarySearch patientSummarySearch = applicationContext.getBean(PatientSummarySearchFactory.class).select(sourceType);
                                List<PatientSummaryListHTML> patientSummaryList = patientSummarySearch.findAllPatientSummaryHTMLTables(nhsNumber);
                                if(patientSummaryList.size() > 0){
                                    //We have a result so build section
                                    Section section = new Section();
                                    section.setTitle("Summary");
                                    CodingDt summaryCoding = new CodingDt();
                                    summaryCoding.setSystem("http://fhir.nhs.net/ValueSet/gpconnect-record-section-1-0");
                                    summaryCoding.setCode("SUM");<commit_msg>GPC-66 Improved handling on no data for section in database.<commit_after>package uk.gov.hscic.patient.resource.provider;

import ca.uhn.fhir.model.api.IDatatype;
import ca.uhn.fhir.model.dstu2.composite.CodeableConceptDt;
import ca.uhn.fhir.model.dstu2.composite.CodingDt;
import ca.uhn.fhir.model.dstu2.composite.IdentifierDt;
import ca.uhn.fhir.model.dstu2.composite.NarrativeDt;
import ca.uhn.fhir.model.dstu2.composite.ResourceReferenceDt;
import ca.uhn.fhir.model.dstu2.resource.Bundle;
import ca.uhn.fhir.model.dstu2.resource.Bundle.Entry;
import ca.uhn.fhir.model.dstu2.resource.Composition;
import ca.uhn.fhir.model.dstu2.resource.Composition.Section;
import ca.uhn.fhir.model.dstu2.resource.Parameters;
import ca.uhn.fhir.model.dstu2.resource.Parameters.Parameter;
import ca.uhn.fhir.model.dstu2.resource.Patient;
import ca.uhn.fhir.model.dstu2.valueset.BundleTypeEnum;
import ca.uhn.fhir.model.dstu2.valueset.CompositionStatusEnum;
import ca.uhn.fhir.model.dstu2.valueset.NarrativeStatusEnum;
import ca.uhn.fhir.model.primitive.DateTimeDt;
import ca.uhn.fhir.model.primitive.StringDt;
import ca.uhn.fhir.rest.annotation.Operation;
import ca.uhn.fhir.rest.annotation.ResourceParam;
import ca.uhn.fhir.rest.server.IResourceProvider;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;
import org.springframework.context.ApplicationContext;
import uk.gov.hscic.common.types.RepoSource;
import uk.gov.hscic.common.types.RepoSourceType;
import uk.gov.hscic.patient.patientsummary.model.PatientSummaryListHTML;
import uk.gov.hscic.patient.patientsummary.search.PatientSummarySearch;
import uk.gov.hscic.patient.patientsummary.search.PatientSummarySearchFactory;

public class PatientResourceProvider implements IResourceProvider {
    
    @Override
    public Class<Patient> getResourceType() {
        return Patient.class;
    }
    
    ApplicationContext applicationContext;
    
    public void setApplicationContext(ApplicationContext applicationContext){
        this.applicationContext = applicationContext;
    }
            
    @Operation(name="$getcarerecord")
    public Bundle getPatientCareRecord(@ResourceParam Parameters params){
                
        String nhsNumber = null;
        ArrayList<String> sectionsParamList = new ArrayList();
        
        // Extract the parameters
        for(Parameter param : params.getParameter()){
            
            String paramName = param.getName();
            IDatatype value = param.getValue();
            
            if(value instanceof IdentifierDt){
                nhsNumber = ((IdentifierDt)value).getValue();
            }else if(value instanceof StringDt){
                sectionsParamList.add(((StringDt)value).getValue());
            }
        }
        
        
        // Build
        Bundle bundle = new Bundle();
        bundle.setType(BundleTypeEnum.SEARCH_RESULTS);  // This type is possibly wrong but the type "searchset" is not available in the API enum.
        Entry entry = new Entry();
        
        
        // Validate request fields
        if(nhsNumber == null || nhsNumber.isEmpty()){
            // Build the OperationOutcome response
            
            
        } else {
            //Build the Composition
            Composition careRecordComposition = new Composition();
            
            
            // Set Composition Mandatory Fields
            careRecordComposition.setDate(new DateTimeDt(Calendar.getInstance().getTime()));
            
            CodingDt coding = new CodingDt();
            coding.setSystem("http://fhir.nhs.net/ValueSet/document-type-codes-1-0");
            coding.setCode("CAR");
            coding.setDisplay("Care Record");
            CodeableConceptDt codableConcept = new CodeableConceptDt();
            codableConcept.addCoding(coding);
            codableConcept.setText("Care Record");
            careRecordComposition.setType(codableConcept);
            
            careRecordComposition.setTitle("Patient Care Record");
            
            careRecordComposition.setStatus(CompositionStatusEnum.FINAL);
            
            ResourceReferenceDt patientReference = new ResourceReferenceDt("/Patient/" + nhsNumber);
            careRecordComposition.setSubject(patientReference);
            
            ResourceReferenceDt clinicianReference = new ResourceReferenceDt("/123456");
            careRecordComposition.setAuthor(Collections.singletonList(clinicianReference));
            
            
            // Build requested sections
            if(sectionsParamList.size() > 0){
                ArrayList<Section> sectionsList = new ArrayList();
                
                RepoSource sourceType = RepoSourceType.fromString(null);
                
                for(String sectionName : sectionsParamList){
                    switch(sectionName){
                        case "Summary" :
                                PatientSummarySearch patientSummarySearch = applicationContext.getBean(PatientSummarySearchFactory.class).select(sourceType);
                                List<PatientSummaryListHTML> patientSummaryList = patientSummarySearch.findAllPatientSummaryHTMLTables(nhsNumber);
                                if(patientSummaryList != null && patientSummaryList.size() > 0){
                                    //We have a result so build section
                                    Section section = new Section();
                                    section.setTitle("Summary");
                                    CodingDt summaryCoding = new CodingDt();
                                    summaryCoding.setSystem("http://fhir.nhs.net/ValueSet/gpconnect-record-section-1-0");
                                    summaryCoding.setCode("SUM");
{'fraction_non_alphanumeric': 0.09129493287137289, 'fraction_numerical': 0.004850584668687744, 'mean_word_length': 2.3466666666666667, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2503656', 'n_tokens_mistral': 3119, 'n_tokens_neox': 3027, 'n_words': 496}