PostulanteController.java 4.77 KB
Newer Older
1 2 3
package com.roshka.controller;


4 5
import java.util.List;

6
import javax.validation.ConstraintViolationException;
7

8
import com.roshka.modelo.*;
9 10
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
11
import com.roshka.modelo.Disponibilidad;
12 13
import com.roshka.modelo.EstadoCivil;
import com.roshka.modelo.Nacionalidad;
14
import com.roshka.modelo.Postulante;
15 16 17
import com.roshka.modelo.TipoExperiencia;
import com.roshka.repositorio.CiudadRepository;
import com.roshka.repositorio.DepartamentoRepository;
18
import com.roshka.repositorio.ExperienciaRepository;
19
import com.roshka.repositorio.InstitucionRepository;
20
import com.roshka.repositorio.PostulanteRepository;
21
import com.roshka.repositorio.TecnologiaRepository;
22 23

import org.springframework.beans.factory.annotation.Autowired;
24 25
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
26
import org.springframework.stereotype.Controller;
27
import org.springframework.ui.Model;
28 29
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;
30

31 32


33 34
@Controller
public class PostulanteController {
35
    PostulanteRepository post;
36
    TecnologiaRepository tecRepo;
37
    ExperienciaRepository expRepo;
38
    InstitucionRepository institucionRepository;
39
    DepartamentoRepository depRepo;
40
    CiudadRepository ciuRepo;
41

42
    @Autowired
43
    public PostulanteController(PostulanteRepository post, TecnologiaRepository tecRepo, ExperienciaRepository expRepo, InstitucionRepository institucionRepository, DepartamentoRepository depRepo, CiudadRepository ciuRepo) {
44 45 46 47
        this.post = post;
        this.tecRepo = tecRepo;
        this.expRepo = expRepo;
        this.institucionRepository = institucionRepository;
48 49
        this.depRepo = depRepo;
        this.ciuRepo = ciuRepo;
50
    }
51

52

53
    @RequestMapping("/")
54
    public String index() {
55
      
56

57
      return "index";
58 59
    }

Joel Florentin committed
60
    @RequestMapping("/postulantes")
Joel Florentin committed
61 62
    public String postulantes(Model model,
                            @RequestParam(required = false,name = "tec")Long tecnologidaId) {
Joel Florentin committed
63
        model.addAttribute("tecnologias", tecRepo.findAll());
Joel Florentin committed
64 65
        if(tecnologidaId==null) model.addAttribute("postulantes", post.findAll());
        else model.addAttribute("postulantes", post.buscarPostulantesPorTecnologia(tecnologidaId));
Joel Florentin committed
66 67 68
        return "postulantes";
    }

69
    @RequestMapping("/postulante")
70 71
    public String getFormPostulante(Model model){
        model.addAttribute("tecnologias", tecRepo.findAll());
72
        model.addAttribute("disponibilidades", Disponibilidad.values());
73 74
        model.addAttribute("tiposDeEstudio", TipoDeEstudio.values());
        model.addAttribute("estadosEstudio", EstadoEstudio.values());
75 76 77 78 79 80 81 82 83 84
        model.addAttribute("estadosCiviles", EstadoCivil.values());
        model.addAttribute("nacionalidades", Nacionalidad.values());
        model.addAttribute("tiposExperencia", TipoExperiencia.values());
        try {
            model.addAttribute("ciudades", new ObjectMapper().writeValueAsString(ciuRepo.findAll()));
        } catch (JsonProcessingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        model.addAttribute("departamentos", depRepo.findAll());
85

86 87
        return "postulante-form";
    }
88

89 90
    @PostMapping(value = "/postulante",consumes = "application/json")
    public String guardarPostulante(@RequestBody Postulante postulante){
91
        System.out.println("hola");
92 93 94 95 96
        postulante.getTecnologias().stream().filter(
                    tec -> tec.getTecnologia().getId() != 0 
            ).forEach(
                    tec -> tec.setTecnologia(tecRepo.getById(tec.getTecnologia().getId()))
                    );
97
        for(Estudio estudio: postulante.getEstudios()){
98 99
            Institucion institucion = institucionRepository.findByNombre(estudio.getInstitucion().getNombre());
            if(institucion==null){
100
                institucionRepository.save(estudio.getInstitucion());
101 102
            }else{
                estudio.setInstitucion(institucion);
103 104 105
            }
        }
        System.out.println("hola");
106
        post.save(postulante);
107
        return "redirect:/";
108 109
    }

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler({MethodArgumentNotValidException.class})
    public ResponseEntity<String> handleValidationExceptions(
            MethodArgumentNotValidException ex) {
        return ResponseEntity.status(HttpStatus.BAD_REQUEST)
                .body(ex.getMessage());
    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler({ConstraintViolationException.class})
    public ResponseEntity<String> handleValidationExceptions2(
            ConstraintViolationException ex) {
        return ResponseEntity.status(HttpStatus.BAD_REQUEST)
                .body(ex.getMessage());
    }
125 126
    

127

128

129
}