0
  1. I have to read the data from a DB table and converts it into a XML file.

  2. Also, the fields(columns of the table) that that needs to converted as XML should be configuration driven(property file or configuration table) .

I have completed the first part using Spring Batach(Spring Boot App). Please provide the guidance to complete the second part.

<code>
    @Bean("dbToXmlJob")
    public Job dbToXmlJob(@Qualifier("dbToXmlStep") Step step) throws Exception {
        return this.jobBuilderFactory
            .get(Constants.JOB_NAME_DB_TO_XML)
            //.validator(dbToXmlJobValidator())
            .start(step)
            .build();
    }

    @Bean("dbToXmlStep")
    public Step dbToXmlStep(@Qualifier("jpaReader") ItemReader<PatientEntity> itemReader,
                     @Qualifier("dbToXmlProcessor")Function<PatientEntity, PatientRecord> processor,
                     @Qualifier("xmlWriter")ItemWriter<PatientRecord> writer) throws Exception {
        return this.stepBuilderFactory
            .get(Constants.STEP_NAME_DB_TO_XML)
            .<PatientEntity, PatientRecord>chunk(2)
            .reader(itemReader)
            .processor(processor)
            .writer(writer)
            .build();
    }

    @Bean("jpaReader")
    @StepScope
    public JpaPagingItemReader<PatientEntity> dbReader() throws Exception {
        String jpqlQuery = "SELECT p from PatientEntity p";

        JpaPagingItemReader<PatientEntity> reader = new JpaPagingItemReader<>();
        reader.setQueryString(jpqlQuery);
        reader.setEntityManagerFactory(batchEntityManagerFactory);
        reader.setPageSize(100);
        reader.afterPropertiesSet();
        reader.setSaveState(true);

        return reader;
    }

    @Bean("dbToXmlProcessor")
    @StepScope
    public Function<PatientEntity, PatientRecord> xmlProcessor() {
        return (patientRecord) ->  {
            return new PatientRecord(
                patientRecord.getSourceId(),
                patientRecord.getFirstName(),
                patientRecord.getMiddleInitial(),
                patientRecord.getLastName(),
                patientRecord.getEmailAddress(),
                patientRecord.getPhoneNumber(),
                patientRecord.getStreet(),
                patientRecord.getCity(),
                patientRecord.getState(),
                patientRecord.getZipCode(),
                patientRecord.getBirthDate().toString(),
                patientRecord.getSocialSecurityNumber());
        };
    }

    @Bean("xmlWriter")
    @StepScope
    public StaxEventItemWriter<PatientRecord> xmlWriter() {

        StaxEventItemWriter<PatientRecord> xmlFileWriter = new StaxEventItemWriter<>();

        String exportFilePath = "file:xml/patients.xml";
        Path file = Paths.get(applicationProperties.getBatch().getInputPath() + 
                File.separator + "output.xml");
        xmlFileWriter.setResource(new FileSystemResource(file));
        xmlFileWriter.setRootTagName("employees");

        Jaxb2Marshaller empMarshaller = new Jaxb2Marshaller();
        empMarshaller.setClassesToBeBound(PatientRecord.class);
        xmlFileWriter.setMarshaller(empMarshaller);
        System.out.println("marshalling");;
        return xmlFileWriter;

    }
</code>

Thanks, Balachandar

user1346346
  • 195
  • 2
  • 16

1 Answers1

0

Please provide the guidance to complete the second part.

  1. Also, the fields(columns of the table) that that needs to converted as XML should be configuration driven(property file or configuration table) .

You can put the columns you want in a properties file, for example application.properties:

columns=column1,column5,column42

and configure your reader with them:

@Configuration
@EnableBatchProcessing
@PropertySource("classpath:application.properties")
class MyConfigClass {

   @StepScope
   public JpaPagingItemReader<PatientEntity> dbReader(@Value("${columns}") String columns) throws Exception {
      String[] columnsArray = columns.split(",");
      // use columns to dynamically create your query
   }

   // the rest of your config

}
Community
  • 1
  • 1
Mahmoud Ben Hassine
  • 28,519
  • 3
  • 32
  • 50