Skip to content
Commits on Source (3)
......@@ -52,6 +52,7 @@ public class BootstrapData implements CommandLineRunner {
pilote1.setAge(100);
pilote1.setRace(RacePilote.CHALACTEENS);
pilote1.setType(TypePilote.APPRENTI);
pilote1.setEtat(EtatPilote.DISPONIBLE);
Pilote pilote2 = new Pilote();
pilote2.setNom("WOW");
......@@ -98,6 +99,15 @@ public class BootstrapData implements CommandLineRunner {
pilote6.setGrade(GradePilote.OFFICIER);
pilote6.setEtat(EtatPilote.BLESSE);
Pilote pilote7 = new Pilote();
pilote7.setNom("DUREL");
pilote7.setPrenom("Johad2");
pilote7.setAge(22);
pilote7.setRace(RacePilote.HUMAINS);
pilote7.setType(TypePilote.APPRENTI);
pilote7.setGrade(GradePilote.OFFICIER);
pilote7.setEtat(EtatPilote.DISPONIBLE);
piloteRepository.save(pilote1);
piloteRepository.save(pilote2);
piloteRepository.save(pilote3);
......@@ -120,10 +130,15 @@ public class BootstrapData implements CommandLineRunner {
chasseur4.setType(TypeChasseur.X_WING);
chasseur4.setEtat(EtatChasseur.DETRUIT);
Chasseur chasseur5 = new Chasseur();
chasseur5.setType(TypeChasseur.Y_WING);
chasseur5.setEtat(EtatChasseur.OPERATIONNEL);
chasseurRepository.save(chasseur1);
chasseurRepository.save(chasseur2);
chasseurRepository.save(chasseur3);
chasseurRepository.save(chasseur4);
chasseurRepository.save(chasseur5);
Mission mission1 = new Mission();
mission1.setTitre("Mission 1");
......@@ -155,6 +170,17 @@ public class BootstrapData implements CommandLineRunner {
affectation2.setPilote(pilote5);
affectation2.setChasseur(chasseur4);
affectationRepository.save(affectation1);
affectationRepository.save(affectation2);
mission2.getAffectation().add(affectation2);
pilote5.getAffectation().add(affectation2);
chasseur4.getAffectation().add(affectation2);
mission3.getAffectation().add(affectation1);
pilote3.getAffectation().add(affectation1);
chasseur2.getAffectation().add(affectation1);
List list = piloteRepository.findByEtat(EtatPilote.DISPONIBLE);
for (var pilote : list) {
System.out.println("CHECK : " + pilote);
......
......@@ -5,7 +5,9 @@
package fr.ldnr.starWars.controllers;
import fr.ldnr.starWars.domains.Pilote;
import fr.ldnr.starWars.enumerations.EtatPilote;
import fr.ldnr.starWars.enumerations.RacePilote;
import fr.ldnr.starWars.enumerations.TypePilote;
import fr.ldnr.starWars.services.PiloteService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
......@@ -49,6 +51,9 @@ public class InscriptionPiloteController {
pilote.setPrenom(prenom);
pilote.setRace(race2);
pilote.setAge(age2);
pilote.setEtat(EtatPilote.DISPONIBLE);
pilote.setType(TypePilote.APPRENTI);
System.out.println("Check pilote : " + pilote);
piloteService.save(pilote);
return "menu";
......
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package fr.ldnr.starWars.controllers;
import fr.ldnr.starWars.enumerations.EtatPilote;
import fr.ldnr.starWars.enumerations.TypePilote;
import fr.ldnr.starWars.services.PiloteService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
/**
*
* @author stag
*/
@Controller
public class ListePiloteDisponible {
private final PiloteService piloteService;
public ListePiloteDisponible(PiloteService piloteService) {
this.piloteService = piloteService;
}
@GetMapping("/listePiloteDisponible")
public String listePilote(Model model) {
//model.addAttribute("etats", piloteService.listEtat());
model.addAttribute("pilotes", piloteService.findByEtatAndType(EtatPilote.DISPONIBLE, TypePilote.COMBATTANT));
return "listePiloteDisponible";
}
}
......@@ -6,7 +6,9 @@ package fr.ldnr.starWars.controllers;
import fr.ldnr.starWars.enumerations.EtatPilote;
import fr.ldnr.starWars.enumerations.GradePilote;
import fr.ldnr.starWars.enumerations.TypePilote;
import fr.ldnr.starWars.services.PiloteService;
import java.util.ArrayList;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
......@@ -29,7 +31,6 @@ public class ListePiloteParEtat {
@GetMapping("/listePiloteParEtat")
public String listePilote(Model model) {
model.addAttribute("etats", piloteService.listEtat());
return "listePiloteParEtat";
}
......
......@@ -4,10 +4,26 @@
*/
package fr.ldnr.starWars.controllers;
import fr.ldnr.starWars.domains.Affectation;
import fr.ldnr.starWars.domains.Chasseur;
import fr.ldnr.starWars.domains.Mission;
import fr.ldnr.starWars.domains.Pilote;
import fr.ldnr.starWars.enumerations.EtatChasseur;
import fr.ldnr.starWars.enumerations.EtatPilote;
import fr.ldnr.starWars.enumerations.StatutMission;
import fr.ldnr.starWars.enumerations.TypeMission;
import fr.ldnr.starWars.services.AffectationService;
import fr.ldnr.starWars.services.ChasseurService;
import fr.ldnr.starWars.services.MissionService;
import fr.ldnr.starWars.services.PiloteService;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
/**
*
......@@ -16,16 +32,123 @@ import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class NouvelleMissionController {
@Autowired
private final MissionService missionService;
@Autowired
private final PiloteService piloteService;
@Autowired
private final ChasseurService chasseurService;
@Autowired
private final AffectationService affectationService;
// pour stocker les pilotes et les chasseurs temporairement
ArrayList<Pilote> listPiloteTemp = new ArrayList<>();
ArrayList<Chasseur> listChasseurTemp = new ArrayList<>();
Integer idMission;
public NouvelleMissionController(MissionService missionService) {
public NouvelleMissionController(MissionService missionService, PiloteService piloteService, ChasseurService chasseurService, AffectationService affectationService) {
this.missionService = missionService;
this.piloteService = piloteService;
this.chasseurService = chasseurService;
this.affectationService = affectationService;
}
@RequestMapping("/nouvelleMission")
public String getMission(Model model) {
model.addAttribute("typeMissions", missionService.listMission());
model.addAttribute("mission", new Mission());
model.addAttribute("affectationFormVisible", false); // Initially hidden
// Update the table with Pilot and Plane info
List<Pilote> listPilotes = piloteService.findByEtat(EtatPilote.DISPONIBLE);
List<Chasseur> listChasseurs = chasseurService.findByEtat(EtatChasseur.OPERATIONNEL);
model.addAttribute("pilotesDispo", listPilotes);
model.addAttribute("chasseursDispo", listChasseurs);
model.addAttribute("pilotesDispo", listPilotes);
model.addAttribute("chasseursDispo", listChasseurs);
return "nouvelleMission";
}
@PostMapping("/nouvelleMission")
public String nouvelleMissionSubmit(Model model,
@RequestParam(value = "titre") String titre,
@RequestParam(value = "typeMissions") String typeMissions) {
// Save the mission to the database
Mission mission = new Mission();
mission.setTitre(titre);
mission.setType(TypeMission.valueOf(typeMissions));
mission.setStatut(StatutMission.EN_COUR);
System.out.println("CHECK : " + mission);
model.addAttribute("typeMissions", missionService.listMission());
List<Pilote> listPilotes = piloteService.findByEtat(EtatPilote.DISPONIBLE);
List<Chasseur> listChasseurs = chasseurService.findByEtat(EtatChasseur.OPERATIONNEL);
model.addAttribute("pilotesDispo", listPilotes);
model.addAttribute("chasseursDispo", listChasseurs);
missionService.save(mission);
idMission = mission.getIdMission();
//System.out.println("CHECK2 : " + idMission);
return "nouvelleMission";
}
@PostMapping("/affectation")
// Handle Affectation form submission
//@PostMapping("/affectation/{idMission}")
// public String affectationSubmit(@PathVariable Long missionId, @ModelAttribute Affectation affectation, Model model) {
public String affectationSubmit(Model model,
@RequestParam(value = "piloteSelect") String piloteSelect,
@RequestParam(value = "chasseurSelect") String chasseurSelect) {
// Update the table with Pilot and Plane info
model.addAttribute("typeMissions", missionService.listMission());
List<Pilote> listPilotes = piloteService.findByEtat(EtatPilote.DISPONIBLE);
List<Chasseur> listChasseurs = chasseurService.findByEtat(EtatChasseur.OPERATIONNEL);
model.addAttribute("pilotesDispo", listPilotes);
model.addAttribute("chasseursDispo", listChasseurs);
System.out.println("CHECK " + piloteSelect);
// recuperate Pilote from db
Integer idPilote = (Integer.valueOf(piloteSelect));
Pilote piloteRecupere = piloteService.findById(idPilote);
System.out.println("CHECK PILOTE RECUPEREE : " + piloteRecupere);
// recuperate Chasseur from db
Integer idChasseur = (Integer.valueOf(chasseurSelect));
//System.out.println("CHECK chasseurSelect : " + chasseurSelect);
Chasseur chasseurRecupere = chasseurService.findById(idChasseur);
System.out.println("CHECK Chasseur RECUPEREE : " + chasseurRecupere);
// recuperate Chasseur from db
Mission missionRecupere = missionService.findById(idMission);
System.out.println("CHECK Mission RECUPEREE : " + missionRecupere);
//System.out.println("id Mission : " + idMission);
// create et remplir affectation
Affectation affectation = new Affectation();
affectation.setChasseur(chasseurRecupere);
affectation.setPilote(piloteRecupere);
affectation.setMission(missionRecupere);
affectationService.save(affectation);
System.out.println("affectation : " + affectation);
piloteRecupere.getAffectation().add(affectation);
chasseurRecupere.getAffectation().add(affectation);
missionRecupere.getAffectation().add(affectation);
piloteService.save(piloteRecupere);
chasseurService.save(chasseurRecupere);
missionService.save(missionRecupere);
listPiloteTemp.add(piloteRecupere);
listChasseurTemp.add(chasseurRecupere);
// System.out.println("");
//
// Affectation a = new Affectation();
// a.setChasseur(chasseurRecupere);
// a.setPilote(piloteRecupere);
// a.setMission(missionRecupere);
// piloteRecupere.getAffectation().add(a);
// System.out.println("apres ajouter affectation : " + piloteRecupere.getAffectation().toString());
//
return "nouvelleMission";
}
......
......@@ -6,6 +6,9 @@ package fr.ldnr.starWars.domains;
import jakarta.persistence.EmbeddedId;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.MapsId;
......@@ -18,30 +21,25 @@ import java.util.Objects;
@Entity
public class Affectation {
@EmbeddedId // to mark the primary key
private AffectationId affectationId;
@Id // to mark the primary key
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer idAffectation;;
@ManyToOne
@MapsId("idPilote") // to tie to to part of the key
@JoinColumn
private Pilote pilote;
@ManyToOne
@MapsId("idChasseur")
@JoinColumn
private Chasseur chasseur;
@ManyToOne
@MapsId("idMission")
@JoinColumn
private Mission mission;
public AffectationId getAffectationId() {
return affectationId;
public Integer getIdAffectation() {
return idAffectation;
}
public void setAffectationId(AffectationId affectationId) {
this.affectationId = affectationId;
public void setIdAffectation(Integer idAffectation) {
this.idAffectation = idAffectation;
}
public Pilote getPilote() {
......@@ -70,8 +68,8 @@ public class Affectation {
@Override
public int hashCode() {
int hash = 5;
hash = 79 * hash + Objects.hashCode(this.affectationId);
int hash = 3;
hash = 59 * hash + Objects.hashCode(this.idAffectation);
return hash;
}
......@@ -87,13 +85,13 @@ public class Affectation {
return false;
}
final Affectation other = (Affectation) obj;
return Objects.equals(this.affectationId, other.affectationId);
return Objects.equals(this.idAffectation, other.idAffectation);
}
@Override
public String toString() {
return "Affectation{" + "affectationId=" + affectationId + ", pilote=" + pilote + ", chasseur=" + chasseur + ", mission=" + mission + '}';
}
// @Override
// public String toString() {
// return "Affectation{" + "idAffectation=" + idAffectation + ", pilote=" + pilote + ", chasseur=" + chasseur + ", mission=" + mission + '}';
// }
}
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package fr.ldnr.starWars.domains;
import jakarta.persistence.Column;
import jakarta.persistence.Embeddable;
import java.io.Serializable;
import java.util.Objects;
/**
*
* @author stag
*/
@Embeddable
public class AffectationId implements Serializable {
@Column(name = "idPilote")
private Integer idPilote;
@Column(name = "idChasseur")
private Integer idChasseur;
@Column(name = "idMission")
private Integer idMission;
@Override
public int hashCode() {
int hash = 7;
hash = 17 * hash + Objects.hashCode(this.idPilote);
hash = 17 * hash + Objects.hashCode(this.idChasseur);
hash = 17 * hash + Objects.hashCode(this.idMission);
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final AffectationId other = (AffectationId) obj;
if (!Objects.equals(this.idPilote, other.idPilote)) {
return false;
}
if (!Objects.equals(this.idChasseur, other.idChasseur)) {
return false;
}
return Objects.equals(this.idMission, other.idMission);
}
}
......@@ -11,6 +11,7 @@ import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
......@@ -28,7 +29,7 @@ public class Chasseur {
private EtatChasseur etat;
@OneToMany(mappedBy = "chasseur")
Set<Affectation> affectation;
Set<Affectation> affectation = new HashSet<>();
public Integer getIdChasseur() {
return idChasseur;
......
......@@ -5,6 +5,7 @@
package fr.ldnr.starWars.domains;
import fr.ldnr.starWars.enumerations.ResultatMission;
import fr.ldnr.starWars.enumerations.StatutMission;
import fr.ldnr.starWars.enumerations.TypeMission;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
......@@ -12,6 +13,7 @@ import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import java.util.Date;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
......@@ -29,9 +31,10 @@ public class Mission {
private TypeMission type;
private Integer nbHeure;
private ResultatMission resultat;
private StatutMission statut;
@OneToMany(mappedBy = "mission")
Set<Affectation> affectation;
Set<Affectation> affectation = new HashSet<>();
public Integer getIdMission() {
return idMission;
......@@ -65,6 +68,16 @@ public class Mission {
this.nbHeure = nbHeure;
}
public StatutMission getStatut() {
return statut;
}
public void setStatut(StatutMission statut) {
this.statut = statut;
}
public Set<Affectation> getAffectation() {
return affectation;
}
......
......@@ -9,10 +9,12 @@ import fr.ldnr.starWars.enumerations.GradePilote;
import fr.ldnr.starWars.enumerations.RacePilote;
import fr.ldnr.starWars.enumerations.TypePilote;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
......@@ -34,8 +36,8 @@ public class Pilote {
private EtatPilote etat;
private GradePilote grade;
@OneToMany(mappedBy = "pilote")
Set<Affectation> affectation;
@OneToMany(mappedBy = "pilote", fetch = FetchType.EAGER)
Set<Affectation> affectation = new HashSet<>();
public Integer getIdPilote() {
return idPilote;
......@@ -133,16 +135,8 @@ public class Pilote {
@Override
public String toString() {
return "Pilote{" + "idPilote=" + idPilote
+ ", nom=" + nom + ", prenom=" + prenom
+ ", age_inscription=" + age
+ ", race=" + race + ", type_pilote=" + type
+ ", etat=" + etat
+ ", grade=" + grade
+ ", affectation=" + "TBD}";
return "Pilote{" + "idPilote=" + idPilote + ", nom=" + nom + ", prenom=" + prenom + ", age=" + age + ", race=" + race + ", type=" + type + ", etat=" + etat + ", grade=" + grade + ", affectation=" + affectation + '}';
}
}
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Interface.java to edit this template
*/
package fr.ldnr.starWars.enumerations;
/**
*
* @author stag
*/
public enum StatutMission {
EN_COUR, TERMINE
}
......@@ -5,6 +5,10 @@
package fr.ldnr.starWars.repositories;
import fr.ldnr.starWars.domains.Chasseur;
import fr.ldnr.starWars.domains.Pilote;
import fr.ldnr.starWars.enumerations.EtatChasseur;
import fr.ldnr.starWars.enumerations.EtatPilote;
import java.util.List;
import org.springframework.data.repository.CrudRepository;
/**
......@@ -13,4 +17,6 @@ import org.springframework.data.repository.CrudRepository;
*/
public interface ChasseurRepository extends CrudRepository<Chasseur, Long> {
List<Chasseur> findByEtat(EtatChasseur etat);
}
......@@ -29,4 +29,9 @@ public interface PiloteRepository extends CrudRepository<Pilote, Long> {
List<Pilote> findByType(TypePilote type_pilote);
List<Pilote> findByRace(RacePilote race);
// to test
//List<Pilote> findByEtatInAndTypeIn(List<EtatPilote> etats, List<TypePilote> types);
List<Pilote> findByEtatAndType(EtatPilote etat, TypePilote type);
}
......@@ -6,7 +6,6 @@ package fr.ldnr.starWars.services;
import fr.ldnr.starWars.domains.Affectation;
import fr.ldnr.starWars.repositories.AffectationRepository;
import fr.ldnr.starWars.repositories.MissionRepository;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import org.springframework.stereotype.Service;
......
......@@ -7,6 +7,7 @@ package fr.ldnr.starWars.services;
import fr.ldnr.starWars.domains.Chasseur;
import fr.ldnr.starWars.enumerations.EtatChasseur;
import fr.ldnr.starWars.enumerations.TypeChasseur;
import java.util.List;
/**
*
......@@ -18,6 +19,8 @@ public interface ChasseurService {
Chasseur save(Chasseur chasseur);
public List<Chasseur> findByEtat(EtatChasseur etat);
public Iterable<Chasseur> trouverParId(Long id_chasseur);
public Iterable<Chasseur> trouverParEtat(EtatChasseur etat);
......@@ -25,4 +28,7 @@ public interface ChasseurService {
public Iterable<Chasseur> trouverParType(TypeChasseur type);
public Iterable<TypeChasseur> listeTypeChasseur();
public Chasseur findById(Integer idChasseur);
}
......@@ -65,7 +65,6 @@ public class ChasseurServiceImpl implements ChasseurService {
return list;
}
@Override
public Iterable<Chasseur> trouverParType(TypeChasseur type) {
......@@ -76,5 +75,17 @@ public class ChasseurServiceImpl implements ChasseurService {
return list;
}
@Override
public List<Chasseur> findByEtat(EtatChasseur etat) {
return chasseurRepository.findByEtat(etat);
}
@Override
public Chasseur findById(Integer idChasseur) {
Chasseur chasseur = new Chasseur();
chasseur = (Chasseur) em.createQuery("SELECT c FROM Chasseur c WHERE c.idChasseur= :id")
.setParameter("id", idChasseur).getSingleResult();
return chasseur;
}
}
......@@ -13,14 +13,15 @@ import fr.ldnr.starWars.enumerations.TypeMission;
* @author Vincent
*/
public interface MissionService {
Iterable<Mission> findAll();
Mission save(Mission mission);
public Iterable<TypeMission> listMission();
public Iterable<ResultatMission> resultatMission();
public Mission findById(Integer idMission);
}
......@@ -4,7 +4,9 @@
*/
package fr.ldnr.starWars.services;
import fr.ldnr.starWars.domains.Chasseur;
import fr.ldnr.starWars.domains.Mission;
import fr.ldnr.starWars.domains.Pilote;
import fr.ldnr.starWars.enumerations.ResultatMission;
import fr.ldnr.starWars.enumerations.TypeMission;
import fr.ldnr.starWars.repositories.MissionRepository;
......@@ -39,6 +41,7 @@ public class MissionServiceImpl implements MissionService {
List<TypeMission> list = Arrays.asList(TypeMission.values());
return list;
}
@Override
public Iterable<ResultatMission> resultatMission() {
List<ResultatMission> resultat = Arrays.asList(ResultatMission.values());
......@@ -48,6 +51,14 @@ public class MissionServiceImpl implements MissionService {
@Override
public Mission save(Mission mission) {
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
return missionRepository.save(mission);
}
@Override
public Mission findById(Integer idMission) {
Mission mission = new Mission();
mission = (Mission) entityManager.createQuery("SELECT m FROM Mission m WHERE m.idMission = :id")
.setParameter("id", idMission).getSingleResult();
return mission;
}
}
......@@ -29,6 +29,8 @@ public interface PiloteService {
public List<Pilote> findByRace(RacePilote race);
public List<Pilote> findByEtatAndType(EtatPilote etat, TypePilote type);
public Iterable<RacePilote> listRace();
public Iterable<TypePilote> listType();
......
......@@ -62,15 +62,14 @@ public class PiloteServiceImpl implements PiloteService {
@Override
public Pilote save(Pilote pilote) {
pilote.setType(TypePilote.APPRENTI);
return piloteRepository.save(pilote);
}
@Override
public Pilote findById(Integer id_pilote) {
public Pilote findById(Integer idPilote) {
Pilote pilote = new Pilote();
pilote = (Pilote) entityManager.createQuery("SELECT p FROM Pilote p WHERE p.idPilote = :id")
.setParameter("id", id_pilote).getSingleResult();
.setParameter("id", idPilote).getSingleResult();
return pilote;
}
......@@ -99,4 +98,9 @@ public class PiloteServiceImpl implements PiloteService {
return piloteRepository.findByRace(race);
}
@Override
public List<Pilote> findByEtatAndType(EtatPilote etat, TypePilote type) {
return piloteRepository.findByEtatAndType(etat, type);
}
}