@Test public void testReadDiagnosFile() throws Exception { DiagnosRepositoryImpl diagnosRepository = new DiagnosRepositoryImpl(); factory.populateRepoFromDiagnosisCodeFile(FILE_3, diagnosRepository, StandardCharsets.ISO_8859_1); diagnosRepository.openLuceneIndexReader(); assertEquals(980, diagnosRepository.nbrOfDiagosis()); assertNotNull(diagnosRepository.getDiagnosesByCode("A00-")); }
public void populateRepoFromDiagnosisCodeFile(String fileUrl, DiagnosRepositoryImpl diagnosRepository, Charset fileEncoding) { if (Strings.nullToEmpty(fileUrl).trim().isEmpty()) { return; } final String location = ResourceUtils.isUrl(fileUrl) ? fileUrl : "file:" + fileUrl; LOG.debug("Loading diagnosis from: '{}'", location); try { Resource resource = resourceLoader.getResource(location); if (!resource.exists()) { LOG.error("Could not load diagnosis file since '{}' does not exists", location); return; } int count = 0; IndexWriterConfig idxWriterConfig = new IndexWriterConfig(new StandardAnalyzer()); try (IndexWriter idxWriter = new IndexWriter(diagnosRepository.getLuceneIndex(), idxWriterConfig); BufferedReader reader = new BufferedReader(new InputStreamReader(resource.getInputStream(), fileEncoding))) { while (reader.ready()) { String line = reader.readLine(); if (line != null) { Diagnos diagnos = createDiagnosFromString(line, count == 0); if (diagnos != null) { Document doc = new Document(); doc.add(new StringField(DiagnosRepository.CODE, diagnos.getKod(), Field.Store.YES)); doc.add(new TextField(DiagnosRepository.DESC, diagnos.getBeskrivning(), Field.Store.YES)); idxWriter.addDocument(doc); count++; } } } } LOG.info("Loaded {} codes from file {}", count, fileUrl); } catch (IOException ioe) { LOG.error("IOException occured when loading diagnosis file '{}'", fileUrl); throw new RuntimeException("Error occured when loading diagnosis file", ioe); } }
DiagnosRepositoryFactory { public void populateRepoFromDiagnosisCodeFile(String fileUrl, DiagnosRepositoryImpl diagnosRepository, Charset fileEncoding) { if (Strings.nullToEmpty(fileUrl).trim().isEmpty()) { return; } final String location = ResourceUtils.isUrl(fileUrl) ? fileUrl : "file:" + fileUrl; LOG.debug("Loading diagnosis from: '{}'", location); try { Resource resource = resourceLoader.getResource(location); if (!resource.exists()) { LOG.error("Could not load diagnosis file since '{}' does not exists", location); return; } int count = 0; IndexWriterConfig idxWriterConfig = new IndexWriterConfig(new StandardAnalyzer()); try (IndexWriter idxWriter = new IndexWriter(diagnosRepository.getLuceneIndex(), idxWriterConfig); BufferedReader reader = new BufferedReader(new InputStreamReader(resource.getInputStream(), fileEncoding))) { while (reader.ready()) { String line = reader.readLine(); if (line != null) { Diagnos diagnos = createDiagnosFromString(line, count == 0); if (diagnos != null) { Document doc = new Document(); doc.add(new StringField(DiagnosRepository.CODE, diagnos.getKod(), Field.Store.YES)); doc.add(new TextField(DiagnosRepository.DESC, diagnos.getBeskrivning(), Field.Store.YES)); idxWriter.addDocument(doc); count++; } } } } LOG.info("Loaded {} codes from file {}", count, fileUrl); } catch (IOException ioe) { LOG.error("IOException occured when loading diagnosis file '{}'", fileUrl); throw new RuntimeException("Error occured when loading diagnosis file", ioe); } } }
DiagnosRepositoryFactory { public void populateRepoFromDiagnosisCodeFile(String fileUrl, DiagnosRepositoryImpl diagnosRepository, Charset fileEncoding) { if (Strings.nullToEmpty(fileUrl).trim().isEmpty()) { return; } final String location = ResourceUtils.isUrl(fileUrl) ? fileUrl : "file:" + fileUrl; LOG.debug("Loading diagnosis from: '{}'", location); try { Resource resource = resourceLoader.getResource(location); if (!resource.exists()) { LOG.error("Could not load diagnosis file since '{}' does not exists", location); return; } int count = 0; IndexWriterConfig idxWriterConfig = new IndexWriterConfig(new StandardAnalyzer()); try (IndexWriter idxWriter = new IndexWriter(diagnosRepository.getLuceneIndex(), idxWriterConfig); BufferedReader reader = new BufferedReader(new InputStreamReader(resource.getInputStream(), fileEncoding))) { while (reader.ready()) { String line = reader.readLine(); if (line != null) { Diagnos diagnos = createDiagnosFromString(line, count == 0); if (diagnos != null) { Document doc = new Document(); doc.add(new StringField(DiagnosRepository.CODE, diagnos.getKod(), Field.Store.YES)); doc.add(new TextField(DiagnosRepository.DESC, diagnos.getBeskrivning(), Field.Store.YES)); idxWriter.addDocument(doc); count++; } } } } LOG.info("Loaded {} codes from file {}", count, fileUrl); } catch (IOException ioe) { LOG.error("IOException occured when loading diagnosis file '{}'", fileUrl); throw new RuntimeException("Error occured when loading diagnosis file", ioe); } }  }
DiagnosRepositoryFactory { public void populateRepoFromDiagnosisCodeFile(String fileUrl, DiagnosRepositoryImpl diagnosRepository, Charset fileEncoding) { if (Strings.nullToEmpty(fileUrl).trim().isEmpty()) { return; } final String location = ResourceUtils.isUrl(fileUrl) ? fileUrl : "file:" + fileUrl; LOG.debug("Loading diagnosis from: '{}'", location); try { Resource resource = resourceLoader.getResource(location); if (!resource.exists()) { LOG.error("Could not load diagnosis file since '{}' does not exists", location); return; } int count = 0; IndexWriterConfig idxWriterConfig = new IndexWriterConfig(new StandardAnalyzer()); try (IndexWriter idxWriter = new IndexWriter(diagnosRepository.getLuceneIndex(), idxWriterConfig); BufferedReader reader = new BufferedReader(new InputStreamReader(resource.getInputStream(), fileEncoding))) { while (reader.ready()) { String line = reader.readLine(); if (line != null) { Diagnos diagnos = createDiagnosFromString(line, count == 0); if (diagnos != null) { Document doc = new Document(); doc.add(new StringField(DiagnosRepository.CODE, diagnos.getKod(), Field.Store.YES)); doc.add(new TextField(DiagnosRepository.DESC, diagnos.getBeskrivning(), Field.Store.YES)); idxWriter.addDocument(doc); count++; } } } } LOG.info("Loaded {} codes from file {}", count, fileUrl); } catch (IOException ioe) { LOG.error("IOException occured when loading diagnosis file '{}'", fileUrl); throw new RuntimeException("Error occured when loading diagnosis file", ioe); } }  DiagnosRepository createAndInitDiagnosRepository(List<String> filesList, Charset fileEncoding); void populateRepoFromDiagnosisCodeFile(String fileUrl, DiagnosRepositoryImpl diagnosRepository,
                                                  Charset fileEncoding); Diagnos createDiagnosFromString(String line, boolean firstLineInFile); }
DiagnosRepositoryFactory { public void populateRepoFromDiagnosisCodeFile(String fileUrl, DiagnosRepositoryImpl diagnosRepository, Charset fileEncoding) { if (Strings.nullToEmpty(fileUrl).trim().isEmpty()) { return; } final String location = ResourceUtils.isUrl(fileUrl) ? fileUrl : "file:" + fileUrl; LOG.debug("Loading diagnosis from: '{}'", location); try { Resource resource = resourceLoader.getResource(location); if (!resource.exists()) { LOG.error("Could not load diagnosis file since '{}' does not exists", location); return; } int count = 0; IndexWriterConfig idxWriterConfig = new IndexWriterConfig(new StandardAnalyzer()); try (IndexWriter idxWriter = new IndexWriter(diagnosRepository.getLuceneIndex(), idxWriterConfig); BufferedReader reader = new BufferedReader(new InputStreamReader(resource.getInputStream(), fileEncoding))) { while (reader.ready()) { String line = reader.readLine(); if (line != null) { Diagnos diagnos = createDiagnosFromString(line, count == 0); if (diagnos != null) { Document doc = new Document(); doc.add(new StringField(DiagnosRepository.CODE, diagnos.getKod(), Field.Store.YES)); doc.add(new TextField(DiagnosRepository.DESC, diagnos.getBeskrivning(), Field.Store.YES)); idxWriter.addDocument(doc); count++; } } } } LOG.info("Loaded {} codes from file {}", count, fileUrl); } catch (IOException ioe) { LOG.error("IOException occured when loading diagnosis file '{}'", fileUrl); throw new RuntimeException("Error occured when loading diagnosis file", ioe); } }  DiagnosRepository createAndInitDiagnosRepository(List<String> filesList, Charset fileEncoding); void populateRepoFromDiagnosisCodeFile(String fileUrl, DiagnosRepositoryImpl diagnosRepository,
                                                  Charset fileEncoding); Diagnos createDiagnosFromString(String line, boolean firstLineInFile);  }