Class UserTransaction

java.lang.Object
com.ecom.winners.configurations.UserTransaction

@Configuration public class UserTransaction extends Object
Configuration for the User transactions job. In this step, user's transactions are streamed from a CSV file from resources as TransactionDTO, transformed into a Transaction entity, and persisted into the database.
See Also:
  • Configuration
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    org.springframework.batch.core.Step
    insertUserTransactions (org.springframework.batch.core.repository.JobRepository jobRepository, TransactionProcessor transactionProcessor, org.springframework.transaction.PlatformTransactionManager transactionManager, org.springframework.batch.item.database.JpaItemWriter<Transaction> transactionWriter, org.springframework.batch.item.file.FlatFileItemReader<TransactionDTO> transactionReader, int chunkSize)
    The User's transaction Step.
    processor (UserRepository userRepository)
    The Transaction processor checks the user_id from TransactionDTO with the help of UserRepository and finds the equivalent user in the database.
    org.springframework.batch.item.file.FlatFileItemReader<TransactionDTO>
    reader (String resourcePath, String names)
    Reader starts with streaming the rows from the CSV file from the resource path, selects headers, and converts rows into TransactionDTO
    org.springframework.batch.item.database.JpaItemWriter<Transaction>
    writer (jakarta.persistence.EntityManagerFactory entityManagerFactory)
    The Transaction writer takes a Transaction entity from the processor, persists into the database where it also manages the required relation between user and it's transactions.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • UserTransaction

      public UserTransaction()
  • Method Details

    • reader

      @Bean(name="transactionReader") public org.springframework.batch.item.file.FlatFileItemReader<TransactionDTO> reader (@Value("${transaction.reader.resource.path}") String resourcePath, @Value("${transaction.reader.resource.headers}") String names)
      Reader starts with streaming the rows from the CSV file from the resource path, selects headers, and converts rows into TransactionDTO
      Parameters:
      resourcePath - path to CSV file
      names - headers of the CSV file
      Returns:
      FlatFileItemReader of type TransactionDTO
    • processor

      @Bean(name="transactionProcessor") public TransactionProcessor processor (UserRepository userRepository)
      The Transaction processor checks the user_id from TransactionDTO with the help of UserRepository and finds the equivalent user in the database. It creates the Transaction entity based on the user entity and amount. Transaction entity generates a creation date for each Transaction.
      Parameters:
      userRepository - repository for managing interactions for User resource in the database
      Returns:
      TransactionProcessor that is an implementation of ItemProcessor
      See Also:
    • writer

      @Bean(name="transactionWriter") public org.springframework.batch.item.database.JpaItemWriter<Transaction> writer (jakarta.persistence.EntityManagerFactory entityManagerFactory)
      The Transaction writer takes a Transaction entity from the processor, persists into the database where it also manages the required relation between user and it's transactions.
      Parameters:
      entityManagerFactory - EntityManagerFactory
      Returns:
      JpaItemWriter of type Transaction entity
      See Also:
      • ItemWriter
      • EntityManagerFactory
    • insertUserTransactions

      @Bean(name="insertUserTransactions") public org.springframework.batch.core.Step insertUserTransactions (org.springframework.batch.core.repository.JobRepository jobRepository, TransactionProcessor transactionProcessor, org.springframework.transaction.PlatformTransactionManager transactionManager, org.springframework.batch.item.database.JpaItemWriter<Transaction> transactionWriter, org.springframework.batch.item.file.FlatFileItemReader<TransactionDTO> transactionReader, @Value("${transaction.reader.chunk.size}") int chunkSize)
      The User's transaction Step. A step that manages the insertion of the user's transactions from a CSV file into the database
      Parameters:
      jobRepository - JobRepository
      transactionProcessor - TransactionProcessor
      transactionManager - PlatformTransactionManager
      transactionWriter - JpaItemWriter for Transactions
      transactionReader - FlatFileItemReader for TransactionDTO
      chunkSize - size of the batch
      Returns:
      Step
      See Also:
      • JobRepository
      • TransactionProcessor
      • PlatformTransactionManager
      • JpaItemWriter
      • FlatFileItemReader
      • Value