
JavaServer.CdsDataLoader.mustache Maven / Gradle / Ivy
package {{basePackage}}.util;
import au.org.consumerdatastandards.holder.model.BankingProductDetail;
import au.org.consumerdatastandards.holder.repository.BankingProductDetailRepository;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
@Component
public class CdsDataLoader {
private static final Logger LOG = LogManager.getLogger(CdsDataLoader.class);
private BankingProductDetailRepository productDetailRepository;
@Autowired
public CdsDataLoader(BankingProductDetailRepository productDetailRepository) {
this.productDetailRepository = productDetailRepository;
}
public void loadProducts(String fileOrFolder) throws IOException {
File file = new File(fileOrFolder);
if (file.isDirectory()) {
File[] files = file.listFiles();
for (File oneFile : files) {
loadProducts(oneFile.getAbsolutePath());
}
} else {
LOG.info("Loading Product data from {}", file.getAbsolutePath());
byte[] jsonData;
try {
jsonData = Files.readAllBytes(Paths.get(file.getCanonicalPath()));
ObjectMapper objectMapper = new ObjectMapper()
.registerModule(new ParameterNamesModule())
.registerModule(new Jdk8Module())
.registerModule(new JavaTimeModule());
BankingProductDetail productDetail = objectMapper.readValue(jsonData, BankingProductDetail.class);
productDetailRepository.save(productDetail);
LOG.info("Product Data loader saved the following to the database: \n{}", new String(jsonData));
} catch (IOException e) {
LOG.error("Product Data loader was unable to read {}, assuming this is fatal and throwing exception", file.getName());
throw e;
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy