77import java .nio .charset .StandardCharsets ;
88import java .nio .file .Files ;
99import java .nio .file .Paths ;
10- import java .util .ArrayList ;
1110import java .util .Arrays ;
1211import java .util .List ;
1312import java .util .stream .Collectors ;
4443@ ConditionalOnProperty (
4544 prefix = "command.line.runner" ,
4645 value = "enabled" ,
47- havingValue = "true" ,
48- matchIfMissing = false )
46+ havingValue = "true" )
4947@ Component
5048public class TransformJsonToXml implements CommandLineRunner {
5149 private static final String JSON_FILE_INPUT_PATH =
@@ -67,39 +65,23 @@ public void run(String... args) {
6765 getFiles ().forEach (file -> {
6866 String xmlResult = mapJsonToXml (file .getJsonFileInput ());
6967 writeToFile (xmlResult , file .getJsonFileName ());
70- //ehrExtractMapper.validateXmlAgainstSchema(xmlResult);
7168 LOGGER .info ("Successfully validated XML for file: {}" , file .getJsonFileName ());
7269 });
7370 } catch (NHSNumberNotFound | UnreadableJsonFileException | NoJsonFileFound | Hl7TranslatedResponseError e ) {
74- LOGGER .error ("error: " + e .getMessage ());
71+ LOGGER .error ("error: {}" , e .getMessage ());
7572 }
7673 LOGGER .info ("end" );
7774 }
7875
7976 private List <InputFile > getFiles () throws UnreadableJsonFileException , NoJsonFileFound {
8077 File [] files = new File (JSON_FILE_INPUT_PATH ).listFiles ();
81- List <String > jsonStringInputs = new ArrayList <>();
82- List <String > fileNames = new ArrayList <>();
8378
8479 if (files == null || files .length == 0 ) {
8580 throw new NoJsonFileFound ("No json files found" );
8681 }
8782
88- LOGGER .info ("Processing " + files . length + " files from location: " + JSON_FILE_INPUT_PATH );
83+ LOGGER .info ("Processing {} files from location: {}" , files . length , JSON_FILE_INPUT_PATH );
8984
90- Arrays .stream (files )
91- .peek (file -> LOGGER .info ("Parsing file: {}" , file .getName ()))
92- .filter (file -> FilenameUtils .getExtension (file .getName ()).equalsIgnoreCase ("json" ))
93- .forEach (file -> {
94- String jsonAsString ;
95- try {
96- jsonAsString = readJsonFileAsString (JSON_FILE_INPUT_PATH + file .getName ());
97- } catch (Exception e ) {
98- throw new UnreadableJsonFileException ("Cant read file " + file .getName ());
99- }
100- jsonStringInputs .add (jsonAsString );
101- fileNames .add (file .getName ());
102- });
10385 return Arrays .stream (files )
10486 .filter (file -> FilenameUtils .getExtension (file .getName ()).equalsIgnoreCase ("json" ))
10587 .map (file -> {
@@ -109,7 +91,6 @@ private List<InputFile> getFiles() throws UnreadableJsonFileException, NoJsonFil
10991 } catch (IOException e ) {
11092 throw new UnreadableJsonFileException ("Cannot read Json File as String: " + file .getName ());
11193 }
112-
11394 }).collect (Collectors .toList ());
11495
11596 }
@@ -171,15 +152,16 @@ private String extractNhsNumber(String json) throws NHSNumberNotFound {
171152 .filter (resource -> ResourceType .Patient .equals (resource .getResourceType ()))
172153 .map (Patient .class ::cast )
173154 .map (resource -> getNhsNumberIdentifier (nhsNumberSystem , resource ))
174- .map (Identifier .class ::cast )
175155 .findFirst ()
176156 .orElseThrow (() -> new NHSNumberNotFound ("No Patient identifier was found" ))
177157 .getValue ();
178158 }
179159
180160 private Identifier getNhsNumberIdentifier (String nhsNumberSystem , Patient resource ) {
181- return resource .getIdentifier ()
182- .stream ().filter (identifier -> identifier .getSystem ().equals (nhsNumberSystem )).findFirst ().get ();
161+ return resource .getIdentifier ().stream ()
162+ .filter (identifier -> identifier .getSystem ().equals (nhsNumberSystem ))
163+ .findFirst ()
164+ .orElseThrow (() -> new NHSNumberNotFound ("No Patient identifier was found" ));
183165 }
184166
185167 @ Data
0 commit comments