From 7298d9273c3f653b1365127fbf9991591f4cee60 Mon Sep 17 00:00:00 2001 From: Joel Date: Thu, 11 Nov 2021 13:19:40 -0300 Subject: [PATCH] funcion para poblar el json --- curriculumsearch/src/main/java/com/roshka/CurriculumsearchApplication.java | 70 +++++++++++++++++----------------------------------------------------- 1 file changed, 17 insertions(+), 53 deletions(-) diff --git a/curriculumsearch/src/main/java/com/roshka/CurriculumsearchApplication.java b/curriculumsearch/src/main/java/com/roshka/CurriculumsearchApplication.java index d9c5103..173a835 100644 --- a/curriculumsearch/src/main/java/com/roshka/CurriculumsearchApplication.java +++ b/curriculumsearch/src/main/java/com/roshka/CurriculumsearchApplication.java @@ -5,8 +5,11 @@ import java.io.InputStream; import java.util.ArrayList; import java.util.List; +import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.type.TypeFactory; import com.roshka.modelo.*; import com.roshka.repositorio.*; @@ -35,48 +38,12 @@ public class CurriculumsearchApplication { return args -> { try { // read json and write to db - ObjectMapper mapper = new ObjectMapper(); - TypeReference> typeReference1 = new TypeReference>(){}; - InputStream inputStream = TypeReference.class.getResourceAsStream("/json/Departamento.json"); - List departamento= mapper.readValue(inputStream,typeReference1); - depR.saveAll(departamento); - System.out.println("Departamentos Saved!"); - TypeReference> typeReference2 = new TypeReference>(){}; - inputStream = TypeReference.class.getResourceAsStream("/json/Ciudad.json"); - List ciudades= mapper.readValue(inputStream,typeReference2); - ciudR.saveAll(ciudades); - System.out.println("Cuidad Saved!"); - TypeReference> typeReference3 = new TypeReference>(){}; - inputStream = TypeReference.class.getResourceAsStream("/json/cargo.json"); - List cargos= mapper.readValue(inputStream,typeReference3); - cargoR.saveAll(cargos); - cargoR.flush(); - System.out.println("Cargos Saved!"); - /* TypeReference> typeReference5 = new TypeReference>(){}; - inputStream = TypeReference.class.getResourceAsStream("/json/tecnologia.json"); - List tecnologias= mapper.readValue(inputStream,typeReference5); - tecRepo.saveAll(tecnologias); - tecRepo.flush(); - System.out.println("Cargos Saved!"); */ - TypeReference> typeReference4 = new TypeReference>(){}; - inputStream = TypeReference.class.getResourceAsStream("/json/convocatoria.json"); - List convocatorias= mapper.readValue(inputStream,typeReference4); - convocatorias = convR.saveAll(convocatorias); - convR.flush(); - System.out.println("convocatorias Saved!"); - TypeReference> typeReference = new TypeReference>(){}; - inputStream = TypeReference.class.getResourceAsStream("/json/postulante.json"); - List postulantes = mapper.readValue(inputStream,typeReference); - /* for (Postulante postulante : postulantes) { - for (int i = 0; i < postulante.getPostulaciones().size(); i++) { - - postulante.getPostulaciones().set(i, convR.getById(postulante.getPostulaciones().get(i).getId())); - - - } - } */ - postRepo.saveAll(postulantes); - System.out.println("postulantes Saved!"); + guardarJson(cargoR,"/json/cargo.json",Cargo.class); + guardarJson(convR,"/json/convocatoria.json",ConvocatoriaCargo.class); + guardarJson(depR,"/json/Departamento.json",Departamento.class); + guardarJson(ciudR,"/json/Ciudad.json",Ciudad.class); + guardarJson(postRepo,"/json/postulante.json",Postulante.class); + String password = new BCryptPasswordEncoder().encode("test"); RRHHUser testuser = new RRHHUser(); testuser.setEmail("test@test.com"); @@ -106,20 +73,17 @@ public class CurriculumsearchApplication { }; } - public static > void guardarJson(T repo,String srcJson ) { + public static > void guardarJson(T repo,String srcJson, Class clazz ) throws JsonParseException, JsonMappingException, IOException { ObjectMapper mapper = new ObjectMapper(); - TypeReference> typeReference1 = new TypeReference>(){}; + TypeFactory t = TypeFactory.defaultInstance(); + //TypeReference> typeReference1 = new TypeReference>(){}; InputStream inputStream = TypeReference.class.getResourceAsStream(srcJson); List listaAguardar; - try { - listaAguardar = mapper.readValue(inputStream,typeReference1); - repo.saveAll(listaAguardar); - repo.flush(); - System.out.println(srcJson+" Saved!"); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } + listaAguardar = mapper.readValue(inputStream,t.constructCollectionType(ArrayList.class,clazz)); + repo.saveAll(listaAguardar); + repo.flush(); + System.out.println(srcJson+" Saved!"); + } -- libgit2 0.26.0