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

Joel Florentin committed
3 4
import java.io.IOException;
import java.io.InputStream;
5
import java.util.ArrayList;
Joel Florentin committed
6 7
import java.util.List;

8
import com.fasterxml.jackson.core.JsonParseException;
Joel Florentin committed
9
import com.fasterxml.jackson.core.type.TypeReference;
10
import com.fasterxml.jackson.databind.JsonMappingException;
Joel Florentin committed
11
import com.fasterxml.jackson.databind.ObjectMapper;
12
import com.fasterxml.jackson.databind.type.TypeFactory;
13 14
import com.roshka.modelo.*;
import com.roshka.repositorio.*;
Joel Florentin committed
15

16
import org.hibernate.PersistentObjectException;
Joel Florentin committed
17
import org.springframework.boot.CommandLineRunner;
Heti Abhishek Shah committed
18 19
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
willgonzz committed
20
import org.springframework.boot.autoconfigure.domain.EntityScan;
Joel Florentin committed
21
import org.springframework.context.annotation.Bean;
22
import org.springframework.data.jpa.repository.JpaRepository;
willgonzz committed
23
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
24
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
Heti Abhishek Shah committed
25 26

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

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

Joel Florentin committed
35
	@Bean
36
	CommandLineRunner runner(PostulanteRepository postRepo, TecnologiaRepository tecRepo, DepartamentoRepository depR,
37
							 CiudadRepository ciudR, RRHHUserRepository rrhhUserRepository, CargoRepository cargoR, ConvocatoriaRepository convR) {
Joel Florentin committed
38 39
		return args -> {
			try {
40
				// read json and write to db
41 42 43 44
				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);
45
				guardarJson(tecRepo,"/json/tecnologia.json",Tecnologia.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
}