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


4
import java.io.IOException;
willgonzz committed
5
import java.util.Date;
6 7


8
import javax.validation.ConstraintViolationException;
9

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

27

28
import org.hibernate.jpa.TypedParameterValue;
willgonzz committed
29 30
import org.hibernate.type.IntegerType;
import org.hibernate.type.LongType;
31
import org.springframework.beans.factory.annotation.Autowired;
32 33
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
34
import org.springframework.stereotype.Controller;
35
import org.springframework.ui.Model;
36
import org.springframework.util.StringUtils;
37 38
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.*;
39
import org.springframework.web.multipart.MultipartFile;
40

41 42
@Controller
public class PostulanteController {
43
    PostulanteRepository post;
44
    TecnologiaRepository tecRepo;
45
    ExperienciaRepository expRepo;
46
    InstitucionRepository institucionRepository;
47
    DepartamentoRepository depRepo;
48
    CiudadRepository ciuRepo;
49 50
    EstudioRepository estudioRepository;
    PostulanteTecnologiaRepository postulanteTecnologiaRepository;
51
    ConvocatoriaRepository convoRepo;
willgonzz committed
52
    CargoRepository carRepo;
53

54
    @Autowired
55 56 57
    public PostulanteController(
            PostulanteRepository post, TecnologiaRepository tecRepo, ExperienciaRepository expRepo,
            InstitucionRepository institucionRepository, DepartamentoRepository depRepo,
58
            CiudadRepository ciuRepo, EstudioRepository estudioRepository,
59
            PostulanteTecnologiaRepository postulanteTecnologiaRepository,
60
            ConvocatoriaRepository convoRepo, CargoRepository carRepo) {
61 62 63 64
        this.post = post;
        this.tecRepo = tecRepo;
        this.expRepo = expRepo;
        this.institucionRepository = institucionRepository;
65 66
        this.depRepo = depRepo;
        this.ciuRepo = ciuRepo;
67 68
        this.estudioRepository = estudioRepository;
        this.postulanteTecnologiaRepository = postulanteTecnologiaRepository;
69
        this.convoRepo =convoRepo;
willgonzz committed
70
        this.carRepo=carRepo;
71
    }
72

73
    
74
    @RequestMapping(value = "/work-with-us",method = RequestMethod.GET)
75 76
    public String getFormPostulante(Model model){
        model.addAttribute("tecnologias", tecRepo.findAll());
77
        model.addAttribute("disponibilidades", Disponibilidad.values());
78 79
        model.addAttribute("tiposDeEstudio", TipoDeEstudio.values());
        model.addAttribute("estadosEstudio", EstadoEstudio.values());
80 81 82
        model.addAttribute("estadosCiviles", EstadoCivil.values());
        model.addAttribute("nacionalidades", Nacionalidad.values());
        model.addAttribute("tiposExperencia", TipoExperiencia.values());
83
        model.addAttribute("CargosDisponibles", convoRepo.f1ndByCargoAndEstado(new TypedParameterValue(LongType.INSTANCE, null), new Date(), new TypedParameterValue(IntegerType.INSTANCE, 1)));
84 85
        try {
            model.addAttribute("ciudades", new ObjectMapper().writeValueAsString(ciuRepo.findAll()));
willgonzz committed
86
        } catch (JsonProcessingException er) {
87
            // TODO Auto-generated catch block
willgonzz committed
88
            er.printStackTrace();
89 90
        }
        model.addAttribute("departamentos", depRepo.findAll());
91
        
92 93
        return "postulante-form";
    }
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127

 /*    @PostMapping("/uploadCVPostulante")
    public String uploadFile(@RequestParam("file") MultipartFile file) {
        dbFileRepository.save(storeFile(file));
        DBFile dbFile = dbFileRepository.save(storeFile(file));
        return "{\"id\": "+dbFile.getId()+"}";
    } */

    private DBFile createFile(MultipartFile file) {
        // Normalize file name
        String fileName = StringUtils.cleanPath(file.getOriginalFilename());

        try {
            // Check if the file's name contains invalid characters
            if(fileName.contains("..")) {
                throw new Exception("Sorry! Filename contains invalid path sequence " + fileName);
            }
            if(file.getSize()==0)  throw new Exception("Sorry! File cant be void");;

            DBFile dbFile = new DBFile(fileName, file.getContentType(), file.getBytes());

            return dbFile;
        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }
        catch(Exception ex){
            ex.printStackTrace();
            return null;
        }
    }

    @PostMapping(value = "/work-with-us",consumes = "multipart/form-data")
    public String guardarPostulante(@RequestPart(name = "file",required = false) MultipartFile file,@RequestPart("postulante") Postulante postulante){
128
        //Codigo encargado de modificar postulacion si se envia mismo CI
129
        Postulante postulantex = post.findByNroDocument(postulante.getNroDocument());
130 131 132 133 134 135
        if(postulantex != null){
            estudioRepository.findByPostulante(postulantex).forEach(x -> estudioRepository.delete(x));
            expRepo.findByPostulante(postulantex).forEach(x -> expRepo.delete(x));
            postulanteTecnologiaRepository.findByPostulante(postulantex).forEach(x -> postulanteTecnologiaRepository.delete(x));
            postulante.setId(postulantex.getId());
        }
136 137 138 139 140
        if(file!=null){
            DBFile cv = createFile(file);
            if(cv!=null) cv.setPostulante(postulante);
            postulante.setCvFile(cv);
        }
141
        postulante.getTecnologias().stream().filter(
142
            tec -> tec.getTecnologia().getId() != 0 
143
            ).forEach(
144 145 146
                tec -> tec.setTecnologia(tecRepo.getById(tec.getTecnologia().getId()))
                );
                
147 148 149 150 151 152 153 154
        for(Estudio estudio: postulante.getEstudios()){
            String nombreIns = "";
            nombreIns = estudio.getInstitucion().getNombre().toLowerCase();
            Institucion institucion = institucionRepository.findByNombre(nombreIns);
            if(institucion==null){
                institucionRepository.save(estudio.getInstitucion());
            }else{
                estudio.setInstitucion(institucion);
155
            }
156 157 158 159
        }
        post.save(postulante);
        return "redirect:/work-with-us/postulacion-correcta";
    }
160

161 162 163
    
    
    @GetMapping("/work-with-us/postulacion-correcta")
Joel Florentin committed
164
    public String successPostulation(Model model){
165 166 167
        model.addAttribute("mensaje1", "Tu informacion se ha recibido correctamente!");
        model.addAttribute("mensaje2", " espera por que nos pongamos en contacto!");
        return "exitoRegistro";
168 169
    }

170 171 172
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler({MethodArgumentNotValidException.class})
    public ResponseEntity<String> handleValidationExceptions(
173
        MethodArgumentNotValidException ex) {
174 175
        return ResponseEntity.status(HttpStatus.BAD_REQUEST)
                .body(ex.getMessage());
176
    }
177
            
178 179 180 181 182 183 184 185 186 187
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler({ConstraintViolationException.class})
    public ResponseEntity<String> handleValidationExceptions2(
        ConstraintViolationException ex) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST)
            .body(ex.getMessage());
        }



188
    
189
}
190