ConvocatoriaController.java 3.91 KB
Newer Older
1 2 3 4 5
package com.roshka.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

6
import com.roshka.modelo.Cargo;
7 8 9 10
import com.roshka.modelo.ConvocatoriaCargo;
import com.roshka.repositorio.CargoRepository;
import com.roshka.repositorio.ConvocatoriaRepository;

11 12
import org.apache.jasper.tagplugins.jstl.core.ForEach;
import org.dom4j.Branch;
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
import org.hibernate.jpa.TypedParameterValue;
import org.hibernate.type.IntegerType;
import org.hibernate.type.LongType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class ConvocatoriaController {

    CargoRepository cargoRepo;
    ConvocatoriaRepository convoRepo;

    @Autowired
    public ConvocatoriaController(CargoRepository cargoRepo, ConvocatoriaRepository convoRepo) {
        this.cargoRepo = cargoRepo;
        this.convoRepo = convoRepo;
    }

    @RequestMapping("/convocatorias")
    public String menuConvocatorias(Model model,
                            @RequestParam(required = false) Long cargoId,
                            @RequestParam(required = false) Integer isOpen//1: true, 0: false
                            ) {

        model.addAttribute("cargos", cargoRepo.findAll());
        
        model.addAttribute("convocatorias", convoRepo.f1ndByCargoAndEstado(new TypedParameterValue(LongType.INSTANCE, cargoId), new Date(), new TypedParameterValue(IntegerType.INSTANCE, isOpen)));
        //model.addAttribute("convocatorias",cargoId==null? convoRepo.findAll() : convoRepo.findByCargoId(cargoId));
        return "convocatorias";
    }


    @RequestMapping(path = {"/convocatoria","/convocatoria/{id}"})
    public String formConvocatoria(Model model,@PathVariable(required = false) Long id) {
        model.addAttribute("cargos", cargoRepo.findAll());
55 56 57 58 59
        if(id == null){
             model.addAttribute("convocatoria", new ConvocatoriaCargo());
             model.addAttribute("listaConvocatoria", convoRepo.findAll());
            
        }
60 61 62 63 64 65
        else {
            ConvocatoriaCargo cc = convoRepo.getById(id);
            cc.setFechaFinS(new SimpleDateFormat("yyyy-MM-dd").format((cc.getFechaFin())));
            cc.setFechaInicioS(new SimpleDateFormat("yyyy-MM-dd").format((cc.getFechaInicio())));
            
            model.addAttribute("convocatoria", cc);
66
            model.addAttribute("listaConvocatoria", convoRepo.findAll());
67 68 69 70 71 72
        }
        
        return "convocatoria-form";
    }

    @PostMapping(path = {"/convocatoria","/convocatoria/{id}"})
73
    public String guardarConvocatoria(@ModelAttribute ConvocatoriaCargo convocatoria, BindingResult result, @PathVariable(required = false) Long id,Model model) {
74 75
        if(result.hasErrors()); 
        if(id != null) convocatoria.setId(id);
76 77 78 79
        //System.out.println(convoRepo.filtrarConvocatoriasPorCargo(convocatoria.getCargoId()));
        for(ConvocatoriaCargo c: convoRepo.filtrarConvocatoriasPorCargo(convocatoria.getCargoId())){
            
           
80 81
            if(result.hasErrors() || c.getCargoId()==convocatoria.getCargoId() &&   c.getFechaFin().after(convocatoria.getFechaInicio()) )
            {           
82
                
83 84 85
                model.addAttribute("existeFecha", true);
                System.out.println("no debe");
                return "convocatoria-form";
86 87 88 89 90 91 92 93 94 95 96
                             
            }
            else{
                convoRepo.save(convocatoria);
                System.out.println("si anda");
                break;

            }
        }

        
97 98 99
        return "redirect:/convocatorias";
    }
}