Skip to content

Commit

Permalink
Ajouter vol
Browse files Browse the repository at this point in the history
  • Loading branch information
e.elhjoujy committed Oct 21, 2023
1 parent 9394511 commit 9b2f68f
Show file tree
Hide file tree
Showing 17 changed files with 564 additions and 41 deletions.
4 changes: 2 additions & 2 deletions src/main/java/ma/yc/airafraik/Helper/FakeEntitesHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,9 @@ public static VolEntity getVolEntity() {
vol.setVilleDepart("SAFI");
vol.setVilleArrivee("RABAT");
vol.setNomberDePlaces(33);
vol.setDateDepart("2023-05-12");
vol.setDateDepart(new Date());
vol.setHeureDepart("12:00");
vol.setDateArrive("2023-05-12");
vol.setDateArrive(new Date());
vol.setHeureArrivee("14:00");
vol.setVilleDepart("Casablanca");
vol.setVilleArrivee("Rabat");
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/ma/yc/airafraik/core/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ public static VolEntity getVolEntity() {
vol.setCode("VOL-"+Util.generatedLong().toString());
vol.setPrix(1000);
vol.setNomberDePlaces(33);
vol.setDateDepart("2021-05-12");
vol.setDateDepart(new Date());
vol.setHeureDepart("12:00");
vol.setDateArrive("2021-05-12");
vol.setDateArrive(new Date());
vol.setHeureArrivee("14:00");
vol.setVilleDepart("Casablanca");
vol.setVilleArrivee("Rabat");
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/ma/yc/airafraik/dao/AvionDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ma.yc.airafraik.dao;

import ma.yc.airafraik.entities.AvionEntity;

import java.util.Collection;

public interface AvionDao {

public boolean ajouterAvion(AvionEntity avion);
public boolean supprimerAvion(String id);
public boolean modifierAvion(AvionEntity avion);
public AvionEntity consulterAvion(String id);
public Collection<AvionEntity> consulterAvions();




}
68 changes: 68 additions & 0 deletions src/main/java/ma/yc/airafraik/dao/Impl/AvionDaoImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package ma.yc.airafraik.dao.Impl;

import ma.yc.airafraik.connection.HyperJpa;
import ma.yc.airafraik.dao.AvionDao;
import ma.yc.airafraik.entities.AvionEntity;

import java.util.Collection;

public class AvionDaoImpl implements AvionDao {
HyperJpa hyperJpa ;

public AvionDaoImpl() {
this.hyperJpa = HyperJpa.getInstance();
}

@Override
public boolean ajouterAvion(AvionEntity avion) {
return false;
}

@Override
public boolean supprimerAvion(String id) {
return false;
}

@Override
public boolean modifierAvion(AvionEntity avion) {
return false;
}

@Override
public AvionEntity consulterAvion(String id) {
Integer idInt = Integer.parseInt(id);
try{
this.hyperJpa.beginTransaction();
AvionEntity avionEntity = this.hyperJpa.getEntityManager().
createQuery("select a from AvionEntity a where a.id = :id", AvionEntity.class).
setParameter("id", idInt).getSingleResult();

this.hyperJpa.commitTransaction();
return avionEntity;
}catch (Exception e){
e.printStackTrace();
this.hyperJpa.rollbackTransaction();
}
return null;
}

@Override
public Collection<AvionEntity> consulterAvions() {
try{
this.hyperJpa.beginTransaction();
Collection<AvionEntity> avionEntities = this.hyperJpa.getEntityManager().
createQuery("select a from AvionEntity a", AvionEntity.class).getResultList();

this.hyperJpa.commitTransaction();
return avionEntities;

}catch (Exception e){
e.printStackTrace();
this.hyperJpa.rollbackTransaction();
}
finally {
// this.hyperJpa.close();
}
return null;
}
}
19 changes: 14 additions & 5 deletions src/main/java/ma/yc/airafraik/dao/Impl/VolDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,25 @@ public boolean ajouterVol(VolEntity vol) {
try {
hyperJpa.beginTransaction();

this.entityManager.persist(vol.getSocieteAerienneEntity());

// this.entityManager.persist(vol.getSocieteAerienneEntity());


this.entityManager.persist(vol.getAvion());

this.entityManager.persist(vol);
this.transaction.commit();

hyperJpa.commitTransaction();
return true;

} finally {
hyperJpa.rollbackTransaction();
hyperJpa.close();
}catch (Exception e){
if (transaction != null && transaction.isActive()) {
transaction.rollback(); // Rollback the transaction in case of an exception
}
else {
e.printStackTrace();
transaction.rollback();
}
}

return false;
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/ma/yc/airafraik/entities/VolEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;

@AllArgsConstructor
@NoArgsConstructor
Expand All @@ -26,9 +27,9 @@ public class VolEntity {
private String heureArrivee;

@Column(name = "date_depart")
private String dateDepart ;
private Date dateDepart ;
@Column(name = "date_arrivee")
private String dateArrive ;
private Date dateArrive ;
@Column(name = "ville_depart")
private String villeDepart ;
@Column(name = "ville_arrivee")
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/ma/yc/airafraik/service/AvionService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ma.yc.airafraik.service;

import ma.yc.airafraik.entities.AvionEntity;

import java.util.Collection;

public interface AvionService {

public Collection<AvionEntity> consulterAvions();
public AvionEntity consulterAvion(String id);

public void ajouterAvion(AvionEntity avionEntity);
}
9 changes: 9 additions & 0 deletions src/main/java/ma/yc/airafraik/service/VolService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ma.yc.airafraik.service;

import ma.yc.airafraik.entities.VolEntity;

public interface VolService {
public boolean ajouterVol(VolEntity vol);

boolean supprimerVol(String id);
}
33 changes: 33 additions & 0 deletions src/main/java/ma/yc/airafraik/service/impl/AvionServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package ma.yc.airafraik.service.impl;

import ma.yc.airafraik.dao.AvionDao;
import ma.yc.airafraik.dao.Impl.AvionDaoImpl;
import ma.yc.airafraik.entities.AvionEntity;
import ma.yc.airafraik.service.AvionService;

import java.util.Collection;

public class AvionServiceImpl implements AvionService {

private AvionDao avionDao;

public AvionServiceImpl() {
this.avionDao = new AvionDaoImpl();
}

@Override
public Collection<AvionEntity> consulterAvions() {
return this.avionDao.consulterAvions();
}

@Override
public AvionEntity consulterAvion(String id) {
if (id == null) return null;
return this.avionDao.consulterAvion(id);
}

@Override
public void ajouterAvion(AvionEntity avionEntity) {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ public class PaypalPaiementServiceImpl implements PaiementService{
double fakeAccountSolde = 1000;
@Override
public boolean payer(double montant) {
System.out.println("Paiement Paypal de "+montant+" effectué avec succès");
System.out.println("Solde restant : "+(fakeAccountSolde-montant));
// System.out.println("Paiement Paypal de "+montant+" effectué avec succès");
// System.out.println("Solde restant : "+(fakeAccountSolde-montant));
return true;

}
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/ma/yc/airafraik/service/impl/VolServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package ma.yc.airafraik.service.impl;

import ma.yc.airafraik.dao.Impl.VolDaoImpl;
import ma.yc.airafraik.dao.VolDao;
import ma.yc.airafraik.entities.VolEntity;
import ma.yc.airafraik.service.SearchVolsService;
import ma.yc.airafraik.service.VolService;

public class VolServiceImpl implements VolService {

private VolDao volDao;
private SearchVolsService searchVolsService;


public VolServiceImpl() {
this.volDao = new VolDaoImpl();
this.searchVolsService = new SearchVolsServiceImpl();
}

@Override
public boolean ajouterVol(VolEntity vol) {
if (vol == null) return false;
return this.volDao.ajouterVol(vol);
}

@Override
public boolean supprimerVol(String id) {
VolEntity vol = this.searchVolsService.searchVolParId(id);
return this.volDao.supprimerVol(vol);
}
}
81 changes: 81 additions & 0 deletions src/main/java/ma/yc/airafraik/web/Admin/AvionController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package ma.yc.airafraik.web.Admin;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import ma.yc.airafraik.core.Print;
import ma.yc.airafraik.entities.AvionEntity;
import ma.yc.airafraik.entities.VolEntity;
import ma.yc.airafraik.service.AvionService;
import ma.yc.airafraik.service.SearchVolsService;
import ma.yc.airafraik.service.VolService;
import ma.yc.airafraik.service.impl.VolServiceImpl;

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

@WebServlet(name = "AvionController", value = "/admin-avion")
public class AvionController extends HttpServlet {

private VolService volService ;
private AvionService avionService;
private String message;
@Override
public void init() throws ServletException {
this.volService = new VolServiceImpl();
super.init();
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {
//TODO : AJOUTER NOUVEAU AVION AND GO BACK TO ADMIN DASHBOARD
String code = request.getParameter("code");
String heureDepart = request.getParameter("heure_depart");
String heureArrivee = request.getParameter("heure_arrivee");
String dateDepartStr = request.getParameter("date_depart");
String dateArriveeStr = request.getParameter("date_arrivee");
String villeDepart = request.getParameter("ville_depart");
String villeArrivee = request.getParameter("ville_arrivee");
int nombreDePlaces = Integer.parseInt(request.getParameter("nombre_de_places"));
double prix = Double.parseDouble(request.getParameter("prix"));
Integer avionId = Integer.parseInt(request.getParameter("avion"));

// Parse date strings to Date objects
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date dateDepart = null;
Date dateArrivee = null;
try {
dateDepart = dateFormat.parse(dateDepartStr);
dateArrivee = dateFormat.parse(dateArriveeStr);
} catch (java.text.ParseException e) {
e.printStackTrace();
}

// Create a new VolEntity object and set the data
VolEntity vol = new VolEntity();
vol.setCode(code);
vol.setHeureDepart(heureDepart);
vol.setHeureArrivee(heureArrivee);
vol.setDateDepart(dateDepart);
vol.setDateArrive(dateArrivee);
vol.setVilleDepart(villeDepart);
vol.setVilleArrivee(villeArrivee);
vol.setNomberDePlaces(nombreDePlaces);
AvionEntity avion = new AvionEntity();
vol.setAvion(this.avionService.consulterAvion(avionId.toString()));
// You need to retrieve the AvionEntity object based on avionId and set it to vol

vol.setPrix(prix);

if (this.volService.ajouterVol(vol))
request.setAttribute("message","Avion ajouté avec succès");
else
request.setAttribute("message","Erreur lors de l'ajout de l'avion");

// req.getRequestDispatcher("views/admin/dashboard.jsp").forward(req,resp);
resp.sendRedirect("admin-dashboard");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import jakarta.servlet.http.HttpServletResponse;
import ma.yc.airafraik.entities.VolEntity;
import ma.yc.airafraik.service.AccountService;
import ma.yc.airafraik.service.AvionService;
import ma.yc.airafraik.service.SearchVolsService;
import ma.yc.airafraik.service.impl.AccountAdminServiceImpl;
import ma.yc.airafraik.service.impl.AvionServiceImpl;
import ma.yc.airafraik.service.impl.SearchVolsServiceImpl;

import java.io.IOException;
Expand All @@ -21,12 +23,14 @@ public class DashboardController extends HttpServlet {
private boolean isAccountValid = true;
private String message;
private SearchVolsService searchVolsService;
private AvionService avionService;


@Override
public void init() throws ServletException {
this.accountService = new AccountAdminServiceImpl();
this.searchVolsService = new SearchVolsServiceImpl();
this.avionService = new AvionServiceImpl();
}

@Override
Expand All @@ -35,8 +39,10 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se
req.setAttribute("message",message);
Collection<VolEntity> volEntities = this.searchVolsService.consulterVols();
req.setAttribute("vols",volEntities);
req.setAttribute("avions",this.avionService.consulterAvions());
req.getRequestDispatcher("views/admin/dashboard.jsp").forward(req,resp);


}

private void verifyAccount(HttpServletRequest req, HttpServletResponse resp) {
Expand Down
Loading

0 comments on commit 9b2f68f

Please sign in to comment.