CurriculumsearchApplication.java 3.39 KB
Newer Older
Heti Abhishek Shah committed
1 2
package com.roshka;

3
import com.fasterxml.jackson.core.JsonParseException;
Joel Florentin committed
4
import com.fasterxml.jackson.core.type.TypeReference;
5
import com.fasterxml.jackson.databind.JsonMappingException;
Joel Florentin committed
6
import com.fasterxml.jackson.databind.ObjectMapper;
7
import com.fasterxml.jackson.databind.type.TypeFactory;
8 9
import com.roshka.modelo.*;
import com.roshka.repositorio.*;
10
import org.hibernate.PersistentObjectException;
Joel Florentin committed
11
import org.springframework.boot.CommandLineRunner;
Heti Abhishek Shah committed
12 13
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
willgonzz committed
14
import org.springframework.boot.autoconfigure.domain.EntityScan;
Joel Florentin committed
15
import org.springframework.context.annotation.Bean;
16
import org.springframework.data.jpa.repository.JpaRepository;
willgonzz committed
17
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
18
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
Heti Abhishek Shah committed
19

20 21 22 23 24
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

Heti Abhishek Shah committed
25
@SpringBootApplication
willgonzz committed
26 27
@EnableJpaRepositories("com.roshka.repositorio")
@EntityScan("com.roshka.modelo")
Heti Abhishek Shah committed
28 29 30 31 32 33
public class CurriculumsearchApplication {

	public static void main(String[] args) {
		SpringApplication.run(CurriculumsearchApplication.class, args);
	}

Joel Florentin committed
34
	@Bean
35
	CommandLineRunner runner(PostulanteRepository postRepo, TecnologiaRepository tecRepo, DepartamentoRepository depR,
36
							 CiudadRepository ciudR, RRHHUserRepository rrhhUserRepository, CargoRepository cargoR, ConvocatoriaRepository convR, InstitucionRepository insR) {
Joel Florentin committed
37 38
		return args -> {
			try {
39
				// read json and write to db
40 41 42 43
				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);
44
				guardarJson(tecRepo,"/json/tecnologia.json",Tecnologia.class);
45
				guardarJson(insR,"/json/institucion.json",Institucion.class);
46 47
				guardarJson(postRepo,"/json/postulante.json",Postulante.class);
				
48 49 50 51 52 53 54 55
				String password = new BCryptPasswordEncoder().encode("test");
				RRHHUser testuser = new RRHHUser();
				testuser.setEmail("test@test.com");
				testuser.setFirstName("test");
				testuser.setLastName("test");
				testuser.setPassword(password);
				rrhhUserRepository.save(testuser);
				System.out.println("Usuario Test: \nEmail: test@test.com\nPassword: test");
56
				
Joel Florentin committed
57
			} catch (IOException e){
58 59 60 61 62 63 64 65 66 67 68
				System.out.println("Unable to save: " + e.getMessage());
			}
			catch(PersistentObjectException ex){
				System.out.println("Unable to save: " + ex.getMessage());
				
				ex.printStackTrace();
			}
			catch(Exception ex){
				System.out.println("Unable to save: " + ex.getMessage());
				
				ex.printStackTrace();
Joel Florentin committed
69 70 71 72 73 74 75 76
			}

			
			
			
		};
	}

77
	public static <Q,T extends JpaRepository<Q,Long>> void  guardarJson(T repo,String srcJson, Class<Q> clazz ) throws JsonParseException, JsonMappingException, IOException {
78
		ObjectMapper mapper = new ObjectMapper();
79 80
		TypeFactory t = TypeFactory.defaultInstance();
		//TypeReference<List<Q>> typeReference1 = new TypeReference<List<Q>>(){};
81 82
		InputStream inputStream = TypeReference.class.getResourceAsStream(srcJson);
		List<Q> listaAguardar;
83 84 85 86 87
		listaAguardar = mapper.readValue(inputStream,t.constructCollectionType(ArrayList.class,clazz));
		repo.saveAll(listaAguardar);
		repo.flush();
		System.out.println(srcJson+" Saved!");
		
88 89
	}

90
	
Heti Abhishek Shah committed
91
}