As @Jaiwo99 say i understand that there is not a standard for do that. So i decided to make the work with Spring Batch. I realized a batch for load from CSV files Roles and Permissions of my application.
@Configuration
@EnableBatchProcessing
public class BatchConfiguration {
@Autowired
public JobBuilderFactory jobBuilderFactory;
@Autowired
public StepBuilderFactory stepBuilderFactory;
@Autowired
public MongoTemplate mongoTemplate;
@Bean
public PlatformTransactionManager transactionManager() {
return new ResourcelessTransactionManager();
}
@Bean
public Tasklet defaultRolePermissionTasklet() {
return new DefaultRolePermissionTasklet();
}
public <T> FlatFileItemReader<T> readerFile(String fileName,String[] fields,Class<T> type) {
FlatFileItemReader<T> reader = new FlatFileItemReader<T>();
reader.setStrict(false);
reader.setResource(new ClassPathResource(fileName));
reader.setLineMapper(new DefaultLineMapper<T>() {
{
setLineTokenizer(new DelimitedLineTokenizer() {
{
setNames(fields);
setStrict(false);
}
});
setFieldSetMapper(new BeanWrapperFieldSetMapper<T>() {
{
setTargetType(type);
}
});
}
});
return reader;
}
@Bean
public PermissionItemProcessor permissionProcessor() {
return new PermissionItemProcessor();
}
@Bean
public RoleItemProcessor roleProcessor() {
return new RoleItemProcessor();
}
@Bean
public Job initAuthenticationData(JobCompletionNotificationListener listener) {
return jobBuilderFactory.get("initAuthenticationData").incrementer(new RunIdIncrementer()).listener(listener)
.start(stepPermission())
.next(stepRole())
.next(stepDefaultRolePermissions())
.build();
}
@Bean
public Step stepDefaultRolePermissions() {
return stepBuilderFactory.get("stepDefaultRolePermissions").tasklet(defaultRolePermissionTasklet()).build();
}
@Bean
public Step stepPermission() {
MongoItemWriter<Permission> writer = new MongoItemWriter<Permission>();
writer.setTemplate(mongoTemplate);
return stepBuilderFactory.get("stepPermission").<Permission, Permission>chunk(20)
.reader(readerFile("permission-data.csv",new String[] {"name","description"},Permission.class))
.processor(permissionProcessor())
.writer(writer)
.build();
}
@Bean
public Step stepRole() {
MongoItemWriter<Role> writer = new MongoItemWriter<Role>();
writer.setTemplate(mongoTemplate);
return stepBuilderFactory.get("stepRole").<Role, Role>chunk(20)
.reader(readerFile("role-data.csv",new String[] {"name","description"},Role.class))
.processor(roleProcessor())
.writer(writer)
.build();
}
}