Use Codification Systems
Codifications like SNOMED CT, ICD-10, or LOINC are used to express medical concepts in a formal and unambiguous way.
In Cardinal, codifications are represented through the Code entity, which can also be used to represent internal
codification systems:
- Kotlin
- Python
- Typescript
- Dart
sdk.code.createCode(Code(
	id = "INTERNAL|ANALYSIS|1",
	type = "INTERNAL",
	code = "ANALYSIS",
	version = "1",
	label = mapOf("en" to "Internal analysis code")
))
sdk.code.createCodes(listOf(
	Code(
		id = "SNOMED|45007003|1",
		type = "SNOMED",
		code = "45007003",
		version = "1",
		label = mapOf("en" to "Low blood pressure")
	),
	Code(
		id = "SNOMED|38341003|1",
		type = "SNOMED",
		code = "38341003",
		version = "1",
		label = mapOf("en" to "High blood pressure")
	),
	Code(
		id = "SNOMED|2004005|1",
		type = "SNOMED",
		code = "2004005",
		version = "1",
		label = mapOf("en" to "Normal blood pressure")
	)
))
internal_code = sdk.code.create_code_blocking(
	Code(
		id="INTERNAL|ANALYSIS|1",
		type="INTERNAL",
		code="ANALYSIS",
		version="1",
		label={"en": "Internal analysis code"}
	)
)
sdk.code.create_codes_blocking(
	Code(
		id="SNOMED|45007003|1",
		type="SNOMED",
		code="45007003",
		version="1",
		label={"en": "Low blood pressure"}
	),
	Code(
		id="SNOMED|38341003|1",
		type="SNOMED",
		code="38341003",
		version="1",
		label={"en": "High blood pressure"}
	),
	Code(
		id="SNOMED|2004005|1",
		type="SNOMED",
		code="2004005",
		version="1",
		label={"en": "Normal blood pressure"}
	)
)
const internalCode = await sdk.code.createCode(new Code({
	id: "INTERNAL|ANALYSIS|1",
	type: "INTERNAL",
	code: "ANALYSIS",
	version: "1",
	label: {"en": "Internal analysis code"}
}))
await sdk.code.createCodes([
	new Code({
		id: "SNOMED|45007003|1",
		type: "SNOMED",
		code: "45007003",
		version: "1",
		label: {"en": "Low blood pressure"}
	}),
	new Code({
		id: "SNOMED|38341003|1",
		type: "SNOMED",
		code: "38341003",
		version: "1",
		label: {"en": "High blood pressure"}
	}),
	new Code({
		id: "SNOMED|2004005|1",
		type: "SNOMED",
		code: "2004005",
		version: "1",
		label: {"en": "Normal blood pressure"}
	})
])
await sdk.code.createCode(Code(
	"INTERNAL|ANALYSIS|1",
	type: "INTERNAL",
	code: "ANALYSIS",
	version: "1",
	label: {"en": "Internal analysis code"}
));
await sdk.code.createCodes([
	Code(
		"SNOMED|45007003|1",
		type: "SNOMED",
		code: "45007003",
		version: "1",
		label: {"en": "Low blood pressure"}
	),
	Code(
		"SNOMED|38341003|1",
		type: "SNOMED",
		code: "38341003",
		version: "1",
		label: {"en": "High blood pressure"}
	),
	Code(
		"SNOMED|2004005|1",
		type: "SNOMED",
		code: "2004005",
		version: "1",
		label: {"en": "Normal blood pressure"}
	)
]);
The codification systems are supported, but the codes themselves are not present by default in the cloud.
A Code is defined by three properties:
- type: Represents the codification system the code belongs to.
- code: Represents the unique ID of the code within the codification system.
- version: Allows versioning of the code, enabling the maintenance of both old and new versions when something in the- Codeis updated (such as the label or metadata). In this case, a new- Codeentity is created with the updated content and a new version.
A Code is uniquely identified by the type, code, version triple, so the ID of a Code is not a UUID but rather
the string type|code|version.
A Code can be used to add additional context to other entities. In the following example, it is used to add a tag to
a Service.
Like other entities, it is possible to search code to facilitate the retrieval: for example, it is possible to retrieve codes of a certain type searching for a word that is present in the label for a specific language:
- Kotlin
- Python
- Typescript
- Dart
val codeIterator = sdk.code.filterCodesBy(
	CodeFilters.byLanguageTypeLabelRegion(
		language = "en",
		label = "blood",
		type = "SNOMED"
	)
)
var selectedCode: Code? = null
while (codeIterator.hasNext() && selectedCode == null) {
	val code = codeIterator.next(1).first()
	prettyPrint(code)
	print("Use this code? [y/N]: ")
	val use = readln().trim().lowercase() == "y"
	if (use) {
		selectedCode = code
	}
}
val patient = sdk.patient.createPatient(
	DecryptedPatient(
		id = UUID.randomUUID().toString(),
		firstName = "Annabelle",
		lastName = "Hall",
	).let { sdk.patient.withEncryptionMetadata(it) }
)
val formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss")
val contact = DecryptedContact(
	id = UUID.randomUUID().toString(),
	descr = "Blood pressure measurement",
	openingDate = LocalDateTime.now().format(formatter).toLong(),
	services = setOf(
		DecryptedService(
			id = UUID.randomUUID().toString(),
			label = "Blood pressure",
			content = mapOf(
				"en" to DecryptedContent(
					measureValue = Measure(
						value = Random.nextInt(80, 120).toDouble(),
						unit = "mmHg"
					)
				)
			),
			tags = setOf(
				CodeStub(
					id = selectedCode.id,
					type = selectedCode.type,
					code = selectedCode.code,
					version = selectedCode.version
				)
			)
		)
	)
)
val createdContact = sdk.contact.createContact(
	sdk.contact.withEncryptionMetadata(contact, patient)
)
code_iterator = sdk.code.filter_codes_by_blocking(
	CodeFilters.by_language_type_label_region(
		language="en",
		label="blood",
		type="SNOMED"
	)
)
selected_code = None
while selected_code is None and code_iterator.has_next_blocking():
	code = code_iterator.next_blocking(1)[0]
	pretty_print_code(code)
	choice = input("Use this code [y/N]: ")
	if choice.lower() == "y":
		selected_code = code
		break
patient = sdk.patient.create_patient_blocking(
	sdk.patient.with_encryption_metadata_blocking(
		DecryptedPatient(
			id=str(uuid.uuid4()),
			first_name="Annabelle",
			last_name="Hall"
		)
	)
)
contact = DecryptedContact(
	id=str(uuid.uuid4()),
	descr="Blood pressure measurement",
	opening_date=int(datetime.now().strftime("%Y%m%d%H%M%S")),
	services=[
		DecryptedService(
			id=str(uuid.uuid4()),
			label="Blood pressure",
			content={
				"en": DecryptedContent(
					measure_value=Measure(
						value=float(random.randint(80, 120)),
						unit="mmHg"
					)
				)
			},
			tags=[
				CodeStub(
					id=selected_code.id,
					type=selected_code.type,
					code=selected_code.code,
					version=selected_code.version
				)
			]
		)
	]
)
created_contact = sdk.contact.create_contact_blocking(
	sdk.contact.with_encryption_metadata_blocking(contact, patient)
)
const codeIterator = await sdk.code.filterCodesBy(
	CodeFilters.byLanguageTypeLabelRegion(
		"en",
		"SNOMED",
		{ label: "blood" }
	)
)
let selectedCode: Code | null = null
while ((await codeIterator.hasNext()) && selectedCode == null) {
	const code = (await codeIterator.next(1))[0]
	prettyPrintCode(code)
	const use = (await readLn("Use this code? [y/N]: ")).trim().toLowerCase() === "y"
	if (use) {
		selectedCode = code
	}
}
if (selectedCode == null) {
	console.log("No code was selected")
	return
}
const patient = await sdk.patient.createPatient(
	await sdk.patient.withEncryptionMetadata(
		new DecryptedPatient({
			id: uuid(),
			firstName: "Annabelle",
			lastName: "Hall",
		})
	)
)
const contact = new DecryptedContact({
	id: uuid(),
	descr: "Blood pressure measurement",
	openingDate: currentFuzzyDate(),
	services: [
		new DecryptedService({
			id: uuid(),
			label: "Blood pressure",
			content: {
				"en": new DecryptedContent({
					measureValue: new Measure({
						value: random(80, 120),
						unit: "mmHg"
					})
				})
			}
		})
	],
	tags: [
		new CodeStub({
			id: selectedCode.id,
			type: selectedCode.type,
			code: selectedCode.code,
			version: selectedCode.version
		})
	]
})
const createdContact = await sdk.contact.createContact(
	await sdk.contact.withEncryptionMetadata(contact, patient)
)
final codeIterator = await sdk.code.filterCodesBy(
	await CodeFilters.byLanguageTypeLabelRegion(
		"en",
		"SNOMED",
		label: "blood",
	)
);
Code? selectedCode;
while ((await codeIterator.hasNext()) && selectedCode == null) {
	final code = (await codeIterator.next(1)).first;
	displayCodeOnTheUI(code);
	final use = await checkForUserConfirmation();
	if (use) {
		selectedCode = code;
	}
}
final patient = await sdk.patient.createPatient(
	await sdk.patient.withEncryptionMetadata(
		DecryptedPatient(
			generateUuid(),
			firstName: "Annabelle",
			lastName: "Hall",
		)
	)
);
final contact = DecryptedContact(
	generateUuid(),
	descr: "Blood pressure measurement",
	openingDate: currentDateAsYYYYMMddHHmmSS(),
	services: {
		DecryptedService(
			generateUuid(),
			label: "Blood pressure",
			content: {
				"en": DecryptedContent(
					measureValue: Measure(
						value: (80 + Random().nextInt(41)).toDouble(),
						unit: "mmHg"
					)
				)
			},
			tags: {
				CodeStub(
					id: selectedCode.id,
					type: selectedCode.type,
					code: selectedCode.code,
					version: selectedCode.version
				)
			}
		)
	}
);
final createdContact = await sdk.contact.createContact(
	await sdk.contact.withEncryptionMetadata(contact, patient)
);
In this snippet, the user can choose a Code from all the SNOMED codes that contain the word "blood"  in their label
in English. Then, a Contact is created that contains a Service for a blood pressure measurement, and a tag is added
for the SNOMED code of high, low, or normal blood pressure as a CodeStub, a reference to a Code that
includes only the type, code, and version.
Codes can also be used to filter entities:
- Kotlin
- Python
- Typescript
- Dart
val serviceIterator = sdk.contact.filterServicesBy(
	ServiceFilters.byTagAndValueDateForSelf(
		tagType = selectedCode.type,
		tagCode = selectedCode.code
	)
)
println("Result of searching by code: \${selectedCode.id}")
while (serviceIterator.hasNext()) {
	val service = serviceIterator.next(1).first()
	prettyPrint(service)
}
service_iterator = sdk.contact.filter_services_by_blocking(
	ServiceFilters.by_tag_and_value_date_for_self(
		tag_type=selected_code.type,
		tag_code=selected_code.code
	)
)
print(f"Result of searching Services by code: {selected_code.id}")
while service_iterator.has_next_blocking():
	service = service_iterator.next_blocking(1)[0]
	pretty_print_service(service)
const serviceIterator = await sdk.contact.filterServicesBy(
	ServiceFilters.byTagAndValueDateForSelf(
		selectedCode.type,
		{ tagCode: selectedCode.code }
	)
)
console.log(`Result of searching Services by code: ${selectedCode.id}`)
while (await serviceIterator.hasNext()) {
	const service = (await serviceIterator.next(1))[0]
	prettyPrintService(service)
}
final serviceIterator = await sdk.contact.filterServicesBy(
	await ServiceFilters.byTagAndValueDateForSelf(
		selectedCode.type!,
		tagCode: selectedCode.code
	)
);
while (await serviceIterator.hasNext()) {
	final service = (await serviceIterator.next(1)).first;
	displayServiceToUI(service);
}
This example shows how to retrieve all the Services that have a tag with the specified type and code and are
shared with the current user.