comparison Samples/FHIR/src/main/java/Toolbox.java @ 11:8d876a4f541b

added sample FHIR server
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sat, 21 Oct 2023 09:53:25 +0200
parents
children 0dc05fe76bd5
comparison
equal deleted inserted replaced
10:6b9433432ee0 11:8d876a4f541b
1 /**
2 * SPDX-FileCopyrightText: 2023 Sebastien Jodogne, UCLouvain, Belgium
3 * SPDX-License-Identifier: GPL-3.0-or-later
4 */
5
6 /**
7 * Java plugin for Orthanc
8 * Copyright (C) 2023 Sebastien Jodogne, UCLouvain, Belgium
9 *
10 * This program is free software: you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation, either version 3 of the
13 * License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 **/
23
24
25 import org.hl7.fhir.r5.model.CodeableConcept;
26 import org.hl7.fhir.r5.model.Reference;
27
28 import java.time.LocalDate;
29 import java.util.Calendar;
30 import java.util.Date;
31 import java.util.GregorianCalendar;
32 import java.util.regex.Matcher;
33 import java.util.regex.Pattern;
34
35 public class Toolbox {
36 public static String TAG_PATIENT_NAME = "0010,0010";
37 public static String TAG_PATIENT_ID = "0010,0020";
38 public static String TAG_PATIENT_BIRTH_DATE = "0010,0030";
39 public static String TAG_PATIENT_SEX = "0010,0040";
40 public static String TAG_STUDY_INSTANCE_UID = "0020,000d";
41 public static String TAG_STUDY_DATE = "0008,0020";
42 public static String TAG_SERIES_DESCRIPTION = "0008,103e";
43 public static String TAG_SERIES_INSTANCE_UID = "0020,000e";
44 public static String TAG_MODALITY = "0008,0060";
45 public static String TAG_SERIES_NUMBER = "0020,0011";
46 public static String TAG_SOP_INSTANCE_UID = "0008,0018";
47 public static String TAG_INSTANCE_NUMBER = "0020,0013";
48
49 public static Date parseDicomDate(String date) {
50 Pattern pattern = Pattern.compile("^([0-9]{4})([0-9]{2})([0-9]{2})$");
51 Matcher matcher = pattern.matcher(date);
52 if (matcher.matches()) {
53 GregorianCalendar calendar = new GregorianCalendar(
54 Integer.parseInt(matcher.group(1)),
55 Integer.parseInt(matcher.group(2)) - 1, // Month is 1-based in DICOM
56 Integer.parseInt(matcher.group(3)));
57 return calendar.getTime();
58 } else {
59 throw new IllegalArgumentException("Badly formatted DICOM date: " + date);
60 }
61 }
62
63 public static Date parseFhirDate(String date) {
64 Pattern pattern = Pattern.compile("^([0-9]{4})-([0-9]{2})-([0-9]{2})$");
65 Matcher matcher = pattern.matcher(date);
66 if (matcher.matches()) {
67 Calendar c = Calendar.getInstance();
68 c.set(Integer.parseInt(matcher.group(1)),
69 Integer.parseInt(matcher.group(2)) - 1, // Month is 1-based in FHIR
70 Integer.parseInt(matcher.group(3)));
71 return c.getTime();
72 } else {
73 throw new IllegalArgumentException("Badly formatted FHIR date: " + date);
74 }
75 }
76
77 public static String formatDicomDate(Date date) {
78 Calendar calendar = new GregorianCalendar();
79 calendar.setTime(date);
80 return String.format("%04d%02d%02d", calendar.get(Calendar.YEAR),
81 calendar.get(Calendar.MONTH) + 1, // Month is 1-based in DICOM
82 calendar.get(Calendar.DAY_OF_MONTH));
83 }
84
85 public static String formatFhirDate(Date date) {
86 Calendar calendar = new GregorianCalendar();
87 calendar.setTime(date);
88 return String.format("%04d-%02d-%02d", calendar.get(Calendar.YEAR),
89 calendar.get(Calendar.MONTH) + 1, // Month is 1-based in FHIR
90 calendar.get(Calendar.DAY_OF_MONTH));
91 }
92
93 public static CodeableConcept createCodeableConcept(String system,
94 String code) {
95 CodeableConcept codeable = new CodeableConcept();
96 codeable.addCoding();
97 codeable.getCoding().get(0).setSystem(system);
98 codeable.getCoding().get(0).setCode(code);
99 return codeable;
100 }
101
102 public static CodeableConcept createDicomCodeableConcept(String code) {
103 return createCodeableConcept("http://dicom.nema.org/resources/ontology/DCM", code);
104 }
105
106 public static Reference createLocalReference(String type,
107 String id) {
108 Reference reference = new Reference();
109 reference.setReference(type + "/" + id);
110 return reference;
111 }
112 }