src/main/java/ch/bfh/ti/i4mi/mag/mhd/iti65/Iti65RequestConverter.java
/**
	 * ITI-65: process DocumentReference resource from Bundle
	 * @param reference
	 * @param entry
	 */
ITI-65: process DocumentReference resource from Bundle
@param reference
@param entry
ITI - 65 : process DocumentReference resource from Bundle @param reference @param entry
private  void processDocumentReference(DocumentReference reference, DocumentEntry entry) {
        entry.assignEntryUuid();
        reference.setId(entry.getEntryUuid());
        Identifier masterIdentifier = reference.getMasterIdentifier();
        entry.setUniqueId(noPrefix(masterIdentifier.getValue()));
		DocumentReferenceStatus status = reference.getStatus();
		switch (status) {
		case CURRENT:entry.setAvailabilityStatus(AvailabilityStatus.APPROVED);break;
		case SUPERSEDED:entry.setAvailabilityStatus(AvailabilityStatus.DEPRECATED);break;
		default: throw new InvalidRequestException("Unknown document status");
		}
		CodeableConcept type = reference.getType();
		entry.setTypeCode(transform(type));
		List<CodeableConcept> category = reference.getCategory();
		entry.setClassCode(transform(category));
		Reference subject = reference.getSubject();
		entry.setPatientId(transformReferenceToIdentifiable(subject, reference));
		entry.setCreationTime(timestampFromDate(reference.getDateElement()));
		for (Reference authorRef : reference.getAuthor()) {
		   entry.getAuthors().add(transformAuthor(authorRef, reference.getContained(),null));
		}
		if (reference.hasAuthenticator()) {
			 Reference authenticatorRef = reference.getAuthenticator();
			 Resource authenticator = findResource(authenticatorRef, reference.getContained());
			 if (authenticator instanceof Practitioner) {
				 entry.setLegalAuthenticator(transform((Practitioner) authenticator));	
			 } else if (authenticator instanceof PractitionerRole) {
				 Practitioner practitioner = (Practitioner) findResource(((PractitionerRole) authenticator).getPractitioner(), reference.getContained());
				 if (practitioner != null) entry.setLegalAuthenticator(transform(practitioner));
			 } else throw new InvalidRequestException("No authenticator of type Organization supported.");			
		}
		String title = reference.getDescription();
		if (title != null) entry.setTitle(localizedString(title));
		List<CodeableConcept> securityLabels = reference.getSecurityLabel();
		transformCodeableConcepts(securityLabels, entry.getConfidentialityCodes());				       
		DocumentReferenceContentComponent content = reference.getContentFirstRep();		
		if (content==null) throw new InvalidRequestException("Missing content field in DocumentReference");
		Attachment attachment = content.getAttachment();
		if (attachment==null) throw new InvalidRequestException("Missing attachment field in DocumentReference");
		entry.setMimeType(attachment.getContentType());
		entry.setLanguageCode(attachment.getLanguage());
		if (attachment.hasSize()) entry.setSize((long) attachment.getSize());
        byte[] hash = attachment.getHash();
        if (hash != null) entry.setHash(new String(hash));
        String comments = attachment.getTitle();
        if (comments!=null) entry.setComments(localizedString(comments));       
        if (attachment.hasCreation()) {
        	if (entry.getCreationTime() == null) entry.setCreationTime(timestampFromDate(attachment.getCreationElement()));
        	else if (!timestampFromDate(attachment.getCreationElement()).equals(entry.getCreationTime())) throw new InvalidRequestException("DocumentReference.date does not match attachment.creation element");
        }        
        Coding coding = content.getFormat();
        entry.setFormatCode(transform(coding));       
        DocumentReferenceContextComponent context = reference.getContext();
         Instead: referenceIdList -> related.identifier
        for (Reference ref : context.getRelated()) {
        	Identifiable refId = transformReferenceToIdentifiable(ref, reference);
        	if (refId != null) {
        		ReferenceId referenceId = new ReferenceId();
        		referenceId.setAssigningAuthority(new CXiAssigningAuthority(null, refId.getAssigningAuthority().getUniversalId(), refId.getAssigningAuthority().getUniversalIdType()));        	
          	    referenceId.setId(refId.getId());
        		entry.getReferenceIdList().add(referenceId);        	
        	}
        }
         Currently not mapped
        /*for (Reference encounterRef : context.getEncounter()) {
        	ReferenceId referenceId = new ReferenceId();
        	Identifiable id = transformReferenceToIdentifiable(encounterRef, reference);
        	if (id != null) {
        	  referenceId.setIdTypeCode(ReferenceId.ID_TYPE_ENCOUNTER_ID);        	
        	  referenceId.setId(id.getId());
        	  referenceId.setAssigningAuthority(new CXiAid.getAssigningAuthority().getUniversalId());
			  entry.getReferenceIdList().add(referenceId );
        }*/
         eventCodeList -> context.event CodeableConcept [0..*]       
        List<CodeableConcept> events = context.getEvent();
        transformCodeableConcepts(events, entry.getEventCodeList());
         serviceStartTime serviceStopTime -> context.period Period [0..1]
        Period period = context.getPeriod();
        if (period != null) {
        	entry.setServiceStartTime(timestampFromDate(period.getStartElement()));
        	entry.setServiceStopTime(timestampFromDate(period.getEndElement()));
        }
         healthcareFacilityTypeCode -> context.facilityType CodeableConcept
         [0..1]
        entry.setHealthcareFacilityTypeCode(transformCodeableConcept(context.getFacilityType()));
         practiceSettingCode -> context.practiceSetting CodeableConcept [0..1]
        entry.setPracticeSettingCode(transformCodeableConcept(context.getPracticeSetting()));
        Extension originalRole = reference.getExtensionByUrl("http:fhir.ch/ig/ch-epr-mhealth/StructureDefinition/ch-ext-author-authorrole");
        if (originalRole != null) {
        	if (originalRole.getValue() instanceof Coding) {
        		Coding value = (Coding) originalRole.getValue();
        		String system = noPrefix(value.getSystem());
        		String code = value.getCode();
        		entry.setExtraMetadata(Collections.singletonMap("urn:e-health-suisse:2020:originalProviderRole", Collections.singletonList(code+"^^^&"+system+"&ISO")));
        	}
        }
         sourcePatientId and sourcePatientInfo -> context.sourcePatientInfo
         Reference(Patient) [0..1] Contained Patient Resource with
         Patient.identifier.use element set to ‘usual’.
        if (context.hasSourcePatientInfo()) {
          entry.setSourcePatientId(transformReferenceToIdentifiable(context.getSourcePatientInfo(), reference));
          entry.setSourcePatientInfo(transformReferenceToPatientInfo(context.getSourcePatientInfo(), reference));
        }
	}
private void processDocumentReference ( DocumentReference reference , DocumentEntry entry ) { entry . assignEntryUuid ( ) ; reference . setId ( entry . getEntryUuid ( ) ) ; Identifier masterIdentifier = reference . getMasterIdentifier ( ) ; entry . setUniqueId ( noPrefix ( masterIdentifier . getValue ( ) ) ) ; DocumentReferenceStatus status = reference . getStatus ( ) ; switch ( status ) { case CURRENT : entry . setAvailabilityStatus ( AvailabilityStatus . APPROVED ) ; break ; case SUPERSEDED : entry . setAvailabilityStatus ( AvailabilityStatus . DEPRECATED ) ; break ; default : throw new InvalidRequestException ( "Unknown document status" ) ; } CodeableConcept type = reference . getType ( ) ; entry . setTypeCode ( transform ( type ) ) ; List < CodeableConcept > category = reference . getCategory ( ) ; entry . setClassCode ( transform ( category ) ) ; Reference subject = reference . getSubject ( ) ; entry . setPatientId ( transformReferenceToIdentifiable ( subject , reference ) ) ; entry . setCreationTime ( timestampFromDate ( reference . getDateElement ( ) ) ) ; for ( Reference authorRef : reference . getAuthor ( ) ) { entry . getAuthors ( ) . add ( transformAuthor ( authorRef , reference . getContained ( ) , null ) ) ; } if ( reference . hasAuthenticator ( ) ) { Reference authenticatorRef = reference . getAuthenticator ( ) ; Resource authenticator = findResource ( authenticatorRef , reference . getContained ( ) ) ; if ( authenticator instanceof Practitioner ) { entry . setLegalAuthenticator ( transform ( ( Practitioner ) authenticator ) ) ; } else if ( authenticator instanceof PractitionerRole ) { Practitioner practitioner = ( Practitioner ) findResource ( ( ( PractitionerRole ) authenticator ) . getPractitioner ( ) , reference . getContained ( ) ) ; if ( practitioner != null ) entry . setLegalAuthenticator ( transform ( practitioner ) ) ; } else throw new InvalidRequestException ( "No authenticator of type Organization supported." ) ; } String title = reference . getDescription ( ) ; if ( title != null ) entry . setTitle ( localizedString ( title ) ) ; List < CodeableConcept > securityLabels = reference . getSecurityLabel ( ) ; transformCodeableConcepts ( securityLabels , entry . getConfidentialityCodes ( ) ) ; DocumentReferenceContentComponent content = reference . getContentFirstRep ( ) ; if ( content == null ) throw new InvalidRequestException ( "Missing content field in DocumentReference" ) ; Attachment attachment = content . getAttachment ( ) ; if ( attachment == null ) throw new InvalidRequestException ( "Missing attachment field in DocumentReference" ) ; entry . setMimeType ( attachment . getContentType ( ) ) ; entry . setLanguageCode ( attachment . getLanguage ( ) ) ; if ( attachment . hasSize ( ) ) entry . setSize ( ( long ) attachment . getSize ( ) ) ; byte [ ] hash = attachment . getHash ( ) ; if ( hash != null ) entry . setHash ( new String ( hash ) ) ; String comments = attachment . getTitle ( ) ; if ( comments != null ) entry . setComments ( localizedString ( comments ) ) ; if ( attachment . hasCreation ( ) ) { if ( entry . getCreationTime ( ) == null ) entry . setCreationTime ( timestampFromDate ( attachment . getCreationElement ( ) ) ) ; else if ( ! timestampFromDate ( attachment . getCreationElement ( ) ) . equals ( entry . getCreationTime ( ) ) ) throw new InvalidRequestException ( "DocumentReference.date does not match attachment.creation element" ) ; } Coding coding = content . getFormat ( ) ; entry . setFormatCode ( transform ( coding ) ) ; DocumentReferenceContextComponent context = reference . getContext ( ) ; for ( Reference ref : context . getRelated ( ) ) { Identifiable refId = transformReferenceToIdentifiable ( ref , reference ) ; if ( refId != null ) { ReferenceId referenceId = new ReferenceId ( ) ; referenceId . setAssigningAuthority ( new CXiAssigningAuthority ( null , refId . getAssigningAuthority ( ) . getUniversalId ( ) , refId . getAssigningAuthority ( ) . getUniversalIdType ( ) ) ) ; referenceId . setId ( refId . getId ( ) ) ; entry . getReferenceIdList ( ) . add ( referenceId ) ; } } /*for (Reference encounterRef : context.getEncounter()) {
        	ReferenceId referenceId = new ReferenceId();
        	Identifiable id = transformReferenceToIdentifiable(encounterRef, reference);
        	if (id != null) {
        	  referenceId.setIdTypeCode(ReferenceId.ID_TYPE_ENCOUNTER_ID);        	
        	  referenceId.setId(id.getId());
        	  //referenceId.setAssigningAuthority(new CXiAid.getAssigningAuthority().getUniversalId());
			  entry.getReferenceIdList().add(referenceId );
        }*/ List < CodeableConcept > events = context . getEvent ( ) ; transformCodeableConcepts ( events , entry . getEventCodeList ( ) ) ; Period period = context . getPeriod ( ) ; if ( period != null ) { entry . setServiceStartTime ( timestampFromDate ( period . getStartElement ( ) ) ) ; entry . setServiceStopTime ( timestampFromDate ( period . getEndElement ( ) ) ) ; } entry . setHealthcareFacilityTypeCode ( transformCodeableConcept ( context . getFacilityType ( ) ) ) ; entry . setPracticeSettingCode ( transformCodeableConcept ( context . getPracticeSetting ( ) ) ) ; Extension originalRole = reference . getExtensionByUrl ( "http://fhir.ch/ig/ch-epr-mhealth/StructureDefinition/ch-ext-author-authorrole" ) ; if ( originalRole != null ) { if ( originalRole . getValue ( ) instanceof Coding ) { Coding value = ( Coding ) originalRole . getValue ( ) ; String system = noPrefix ( value . getSystem ( ) ) ; String code = value . getCode ( ) ; entry . setExtraMetadata ( Collections . singletonMap ( "urn:e-health-suisse:2020:originalProviderRole" , Collections . singletonList ( code + "^^^&" + system + "&ISO" ) ) ) ; } } if ( context . hasSourcePatientInfo ( ) ) { entry . setSourcePatientId ( transformReferenceToIdentifiable ( context . getSourcePatientInfo ( ) , reference ) ) ; entry . setSourcePatientInfo ( transformReferenceToPatientInfo ( context . getSourcePatientInfo ( ) , reference ) ) ; } }
ITI-65: process DocumentReference resource from Bundle
 @param reference
 @param entry
ITI - 65 : process DocumentReference resource from Bundle @param reference @param entry
// limitedMetadata -> meta.profile canonical [0..*]  // No action // availabilityStatus -> status code {DocumentReferenceStatus} [1..1] // approved -> status=current // deprecated -> status=superseded // Other status values are allowed but are not defined in this mapping to XDS. // contentTypeCode -> type CodeableConcept [0..1] // classCode -> category CodeableConcept [0..*] // patientId -> subject Reference(Patient| Practitioner| Group| Device) [0..1],        // creationTime -> date instant [0..1]      // authorPerson, authorInstitution, authorPerson, authorRole, // authorSpeciality, authorTelecommunication -> author Reference(Practitioner| // PractitionerRole| Organization| Device| Patient| RelatedPerson) [0..*]   		 // legalAuthenticator -> authenticator Note 1 // title -> description string [0..1] // confidentialityCode -> securityLabel CodeableConcept [0..*] Note: This // is NOT the DocumentReference.meta, as that holds the meta tags for the // DocumentReference itself.		 // mimeType -> content.attachment.contentType [1..1] code [0..1] // languageCode -> content.attachment.language code [0..1] // size -> content.attachment.size integer [0..1] The size is calculated // on the data prior to base64 encoding, if the data is base64 encoded. // hash -> content.attachment.hash string [0..1] // comments -> content.attachment.title string [0..1] // creationTime -> content.attachment.creation dateTime [0..1] // formatCode -> content.format Coding [0..1] // referenceIdList -> context.encounter Reference(Encounter) [0..*] When // referenceIdList contains an encounter, and a FHIR Encounter is available, it // may be referenced. // We do not support this // // Instead: referenceIdList -> related.identifier // Currently not mapped // eventCodeList -> context.event CodeableConcept [0..*]        // serviceStartTime serviceStopTime -> context.period Period [0..1] // healthcareFacilityTypeCode -> context.facilityType CodeableConcept // [0..1] // practiceSettingCode -> context.practiceSetting CodeableConcept [0..1] // sourcePatientId and sourcePatientInfo -> context.sourcePatientInfo // Reference(Patient) [0..1] Contained Patient Resource with // Patient.identifier.use element set to ‘usual’.