CurriculumsearchApplication.java 5.03 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 8 9
import java.util.List;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
10 11
import com.roshka.modelo.*;
import com.roshka.repositorio.*;
Joel Florentin committed
12

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

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

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

Joel Florentin committed
32
	@Bean
33
	CommandLineRunner runner(PostulanteRepository postRepo, TecnologiaRepository tecRepo, DepartamentoRepository depR,
34
							 CiudadRepository ciudR, RRHHUserRepository rrhhUserRepository, CargoRepository cargoR, ConvocatoriaRepository convR) {
Joel Florentin committed
35 36
		return args -> {
			try {
37 38 39 40 41 42 43 44 45 46 47 48
				// read json and write to db
				ObjectMapper mapper = new ObjectMapper();
				TypeReference<List<Departamento>> typeReference1 = new TypeReference<List<Departamento>>(){};
				InputStream inputStream = TypeReference.class.getResourceAsStream("/json/Departamento.json");
				List<Departamento> departamento= mapper.readValue(inputStream,typeReference1);
				depR.saveAll(departamento);
				System.out.println("Departamentos Saved!");
				TypeReference<List<Ciudad>> typeReference2 = new TypeReference<List<Ciudad>>(){};
				inputStream = TypeReference.class.getResourceAsStream("/json/Ciudad.json");
				List<Ciudad> ciudades= mapper.readValue(inputStream,typeReference2);
				ciudR.saveAll(ciudades);
				System.out.println("Cuidad Saved!");
49 50 51 52 53 54
				TypeReference<List<Cargo>> typeReference3 = new TypeReference<List<Cargo>>(){};
				inputStream = TypeReference.class.getResourceAsStream("/json/cargo.json");
				List<Cargo> cargos= mapper.readValue(inputStream,typeReference3);
				cargoR.saveAll(cargos);
				cargoR.flush();
				System.out.println("Cargos Saved!");
55 56 57 58 59 60
				/* TypeReference<List<Tecnologia>> typeReference5 = new TypeReference<List<Tecnologia>>(){};
				inputStream = TypeReference.class.getResourceAsStream("/json/tecnologia.json");
				List<Tecnologia> tecnologias= mapper.readValue(inputStream,typeReference5);
				tecRepo.saveAll(tecnologias);
				tecRepo.flush();
				System.out.println("Cargos Saved!"); */
61 62 63
				TypeReference<List<ConvocatoriaCargo>> typeReference4 = new TypeReference<List<ConvocatoriaCargo>>(){};
				inputStream = TypeReference.class.getResourceAsStream("/json/convocatoria.json");
				List<ConvocatoriaCargo> convocatorias= mapper.readValue(inputStream,typeReference4);
64
				convocatorias = convR.saveAll(convocatorias);
65 66
				convR.flush();
				System.out.println("convocatorias Saved!");
67 68
				TypeReference<List<Postulante>> typeReference = new TypeReference<List<Postulante>>(){};
				inputStream = TypeReference.class.getResourceAsStream("/json/postulante.json");
Joel Florentin committed
69
				List<Postulante> postulantes = mapper.readValue(inputStream,typeReference);
70
				/*  for (Postulante postulante : postulantes) {
71
					for (int i = 0; i < postulante.getPostulaciones().size(); i++) {
72
						
73
						postulante.getPostulaciones().set(i, convR.getById(postulante.getPostulaciones().get(i).getId()));
74
						
75 76
						
					}
77
				}  */
Joel Florentin committed
78 79
				postRepo.saveAll(postulantes);
				System.out.println("postulantes Saved!");
80 81 82 83 84 85 86 87
				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");
88
				
Joel Florentin committed
89
			} catch (IOException e){
90 91 92 93 94 95 96 97 98 99 100
				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
101 102 103 104 105 106 107 108
			}

			
			
			
		};
	}

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
	public static <Q,T extends JpaRepository<Q,Long>> void  guardarJson(T repo,String srcJson ) {
		ObjectMapper mapper = new ObjectMapper();
		TypeReference<List<Q>> typeReference1 = new TypeReference<List<Q>>(){};
		InputStream inputStream = TypeReference.class.getResourceAsStream(srcJson);
		List<Q> 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();
		}
	}

125
	
Heti Abhishek Shah committed
126
}