CurriculumsearchApplication.java 3.69 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
				String password = new BCryptPasswordEncoder().encode("test");
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
				String testEmail = "test@test.com";

				// Check if the user with the email already exists before saving
				RRHHUser existingUser = rrhhUserRepository.findByEmail(testEmail);
				if (existingUser == null) {
					RRHHUser testuser = new RRHHUser();
					testuser.setEmail(testEmail);
					testuser.setFirstName("test");
					testuser.setLastName("test");
					testuser.setPassword(password);
					rrhhUserRepository.save(testuser);
					System.out.println("Usuario Test: \nEmail: " + testEmail + "\nPassword: test");
				} else {
					System.out.println("User with email " + testEmail + " already exists.");
				}
64
				
Joel Florentin committed
65
			} catch (IOException e){
66 67 68 69 70 71 72 73 74 75 76
				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
77 78 79 80 81 82 83 84
			}

			
			
			
		};
	}

85
	public static <Q,T extends JpaRepository<Q,Long>> void  guardarJson(T repo,String srcJson, Class<Q> clazz ) throws JsonParseException, JsonMappingException, IOException {
86
		ObjectMapper mapper = new ObjectMapper();
87 88
		TypeFactory t = TypeFactory.defaultInstance();
		//TypeReference<List<Q>> typeReference1 = new TypeReference<List<Q>>(){};
89 90
		InputStream inputStream = TypeReference.class.getResourceAsStream(srcJson);
		List<Q> listaAguardar;
91 92 93 94 95
		listaAguardar = mapper.readValue(inputStream,t.constructCollectionType(ArrayList.class,clazz));
		repo.saveAll(listaAguardar);
		repo.flush();
		System.out.println(srcJson+" Saved!");
		
96 97
	}

98
	
Heti Abhishek Shah committed
99
}