Advertisement
Guest User

Untitled

a guest
Nov 24th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.38 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package fr.epsi.montpellier.w;
  7.  
  8. import javax.xml.bind.annotation.XmlRootElement;
  9.  
  10. @XmlRootElement(name = "booktrain")
  11. public class BookTrain {
  12.  
  13.     private String bookNumber;
  14.     private Train currentTrain;
  15.     private int numberPlaces;
  16.  
  17.     public String getBookNumber() {
  18.         return bookNumber;
  19.     }
  20.  
  21.     public void setBookNumber(String bookNumber) {
  22.         this.bookNumber = bookNumber;
  23.     }
  24.  
  25.     public Train getCurrentTrain() {
  26.         return currentTrain;
  27.     }
  28.  
  29.     /**
  30.      * @param currentTrain the currentTrain to set
  31.      */
  32.     public void setCurrentTrain(Train currentTrain) {
  33.         this.currentTrain = currentTrain;
  34.     }
  35.  
  36.     /**
  37.      * @return the numberPlaces
  38.      */
  39.     public int getNumberPlaces() {
  40.         return numberPlaces;
  41.     }
  42.  
  43.     /**
  44.      * @param numberPlaces the numberPlaces to set
  45.      */
  46.     public void setNumberPlaces(int numberPlaces) {
  47.         this.numberPlaces = numberPlaces;
  48.     }
  49.  
  50. }
  51. /*
  52.  * To change this license header, choose License Headers in Project Properties.
  53.  * To change this template file, choose Tools | Templates
  54.  * and open the template in the editor.
  55.  */
  56. package fr.epsi.montpellier.w;
  57.  
  58. import java.util.List;
  59. import javax.ws.rs.DELETE;
  60. import javax.ws.rs.GET;
  61. import javax.ws.rs.POST;
  62. import javax.ws.rs.Path;
  63. import javax.ws.rs.PathParam;
  64. import javax.ws.rs.core.Response;
  65. import javax.ws.rs.core.Response.Status;
  66.  
  67. /**
  68.  *
  69.  * @author M34LMAR }
  70.  */
  71. public class BookTrainResource {
  72.  
  73.     @Path("/booktrains")   //   Chemin   suivant   /trains/booktrains   pour   invoquer   cette   méthode          
  74.     public BookTrainResource getBookTrainResource() {
  75.         return new BookTrainResource();
  76.     }
  77.  
  78.     @POST   //   Méthode   HTTP   utilisée   pour   déclencher   cette   méthode
  79.     public Response createBookTrain(String numTrain, int numberPlaces) {
  80.         Train currentTrain = null;
  81.         for (Train current : BookTrainBD.getTrains()) {
  82.             if (current.getNumTrain().equals(numTrain)) {
  83.                 currentTrain = current;
  84.             }
  85.         }
  86.         if (currentTrain == null) {
  87.             return Response
  88.                     .status(Status.NO_CONTENT)
  89.                     .build();
  90.         }
  91.         BookTrain newBookTrain = new BookTrain();
  92.         newBookTrain.setNumberPlaces(numberPlaces);
  93.         newBookTrain.setCurrentTrain(currentTrain);
  94.         newBookTrain.setBookNumber(Long.toString(System.currentTimeMillis()));
  95.         BookTrainBD.getBookTrains().add(newBookTrain);
  96.         return Response
  97.                 .status(Status.OK)
  98.                 .entity(newBookTrain.getBookNumber()).build();
  99.     }
  100.  
  101.     @GET   //   Méthode   HTTP   utilisée   pour   déclencher   cette   méthode          
  102.     public Response getBookTrains() {
  103.         System.out.println("getBookTrains");
  104.         return Response
  105.                 .status(Status.OK)
  106.                 .entity(BookTrainBD.getBookTrains()).build();
  107.     }
  108.  
  109.     @GET   //   Méthode   HTTP   utilisée   pour   déclencher   cette   méthode
  110.     @Path("{id}")   //   Chemin   de   façon   à   intégrer   un   template   parameter   (id)
  111.     public Response getBookTrain(@PathParam("id") String bookNumber) {
  112.         List<BookTrain> bookTrains = BookTrainBD.getBookTrains();
  113.         for (BookTrain current : bookTrains) {
  114.             if (current.getBookNumber().equals(bookNumber)) {
  115.                 return Response
  116.                         .status(Status.OK)
  117.                         .entity(current)
  118.                         .build();
  119.             }
  120.         }
  121.         return Response
  122.                 .status(Status.NO_CONTENT).build();
  123.     }
  124.  
  125.     @DELETE   //   Méthode   HTTP   utilisée   pour   déclencher   cette   méthode
  126.     @Path("{id}")   //   Chemin   de   façon   à   intégrer   un   template   parameter   (id)
  127.     public Response removeBookTrain(@PathParam("id") String bookNumber) {
  128.         BookTrain currentBookTrain = null;
  129.         for (BookTrain current : BookTrainBD.getBookTrains()) {
  130.             if (current.getBookNumber().equals(bookNumber)) {
  131.                 currentBookTrain = current;
  132.             }
  133.         }
  134.         return Response.status(Status.ACCEPTED).build();
  135.     }
  136.  
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement