Advertisement
Guest User

Untitled

a guest
May 28th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.32 KB | None | 0 0
  1. package dst3.managedBeans;
  2.  
  3. import java.util.List;
  4. import java.util.ResourceBundle;
  5.  
  6. import javax.ejb.EJB;
  7. import javax.faces.application.FacesMessage;
  8. import javax.faces.bean.ManagedBean;
  9. import javax.faces.bean.SessionScoped;
  10. import javax.faces.context.FacesContext;
  11.  
  12. import dst3.dto.FlightInfo;
  13. import dst3.dto.ShoppingCartDto;
  14. import dst3.ejb.BookingManagerBean;
  15. import dst3.ejb.exception.LoginException;
  16. import dst3.ejb.exception.NotEnoughSeatsException;
  17. import dst3.ejb.exception.UnknownFlightException;
  18.  
  19. @ManagedBean(name = "shoppingCartBean")
  20. @SessionScoped
  21. public class ShoppingCartBean {
  22.     private final ResourceBundle rb = ResourceBundle.getBundle("messages");
  23.  
  24.     @EJB
  25.     private BookingManagerBean bookingManagerBean;
  26.  
  27.     private FlightInfo currentFlight;
  28.     private Double totalPrice = new Double(0);
  29.  
  30.     private List<ShoppingCartDto> shoppingCart;
  31.  
  32.     private boolean loggedin = false;
  33.     private String username;
  34.     private String password;
  35.  
  36.     public boolean isLoggedin() {
  37.         return loggedin;
  38.     }
  39.  
  40.     public void logout() {
  41.         loggedin = false;
  42.         FacesContext ctx = FacesContext.getCurrentInstance();
  43.         ctx.addMessage("home:notification", new FacesMessage(rb
  44.                 .getString("logoutMessage")));
  45.     }
  46.  
  47.     public boolean login() {
  48.         boolean success = true;
  49.         FacesContext ctx;
  50.  
  51.         try {
  52.             bookingManagerBean.login(username, password);
  53.         } catch (LoginException e) {
  54.             ctx = FacesContext.getCurrentInstance();
  55.             ctx.addMessage("login:notification", new FacesMessage(rb
  56.                     .getString("loginError")));
  57.             success = false;
  58.         }
  59.  
  60.         if (success) {
  61.             ctx = FacesContext.getCurrentInstance();
  62.             ctx.addMessage("home:notification", new FacesMessage(rb
  63.                     .getString("loginMessage")));
  64.         }
  65.         loggedin = true;
  66.         return success;
  67.     }
  68.  
  69.     public boolean addTickets() {
  70.         boolean success = true;
  71.         try {
  72.             bookingManagerBean.addTickets(currentFlight.getId(), Integer
  73.                     .parseInt(currentFlight.getNrOfTickets()));
  74.         } catch (NumberFormatException e) {
  75.             FacesContext ctx = FacesContext.getCurrentInstance();
  76.             ctx.addMessage("shoppingCart:notification", new FacesMessage(rb
  77.                     .getString("numberFormatException")));
  78.             success = false;
  79.         } catch (UnknownFlightException e) {
  80.             FacesContext ctx = FacesContext.getCurrentInstance();
  81.             ctx.addMessage("shoppingCart:notification", new FacesMessage(rb
  82.                     .getString("unknownFlightException")));
  83.             success = false;
  84.         } catch (NotEnoughSeatsException e) {
  85.             FacesContext ctx = FacesContext.getCurrentInstance();
  86.             ctx.addMessage("shoppingCart:notification", new FacesMessage(rb
  87.                     .getString("notEnoughSeatsException")));
  88.             success = false;
  89.         }
  90.  
  91.         // TODO msg entfernen.
  92.         FacesContext ctx = FacesContext.getCurrentInstance();
  93.         ctx.addMessage("shoppingCart:notification", new FacesMessage(
  94.                 currentFlight.getId() + " " + currentFlight.getNrOfTickets()));
  95.  
  96.         return success;
  97.     }
  98.  
  99.     public boolean removeFlight() {
  100.         boolean success = true;
  101.         bookingManagerBean.removeTickets(currentFlight.getId());
  102.         return success;
  103.     }
  104.  
  105.     public boolean buyTickets() {
  106.         boolean success = true;
  107.         if (!loggedin) {
  108.             FacesContext ctx = FacesContext.getCurrentInstance();
  109.             ctx.addMessage("shoppingCart:notification", new FacesMessage(rb
  110.                     .getString("notLoggedIn")));
  111.             success = false;
  112.         } else {
  113.             FacesContext ctx = FacesContext.getCurrentInstance();
  114.             ctx.addMessage("shoppingCart:notification", new FacesMessage(rb
  115.                     .getString("ticketsBooked")));
  116.             // TODO buchen
  117.         }
  118.         return success;
  119.     }
  120.  
  121.     public void emptyShoopingCartOnLogout() {
  122.         loggedin = false;
  123.     }
  124.  
  125.     public List<ShoppingCartDto> getShoppingCart() {
  126.         shoppingCart = bookingManagerBean.getShoppingCartData();
  127.         return shoppingCart;
  128.     }
  129.  
  130.     public Double getTotalPrice() {
  131.         totalPrice = bookingManagerBean.getPrice().doubleValue();
  132.         return totalPrice;
  133.     }
  134.  
  135.     public void setTotalPrice(Double totalPrice) {
  136.         this.totalPrice = totalPrice;
  137.     }
  138.  
  139.     public String getUsername() {
  140.         return username;
  141.     }
  142.  
  143.     public void setUsername(String username) {
  144.         this.username = username;
  145.     }
  146.  
  147.     public String getPassword() {
  148.         return password;
  149.     }
  150.  
  151.     public void setPassword(String password) {
  152.         this.password = password;
  153.     }
  154.  
  155.     public FlightInfo getCurrentFlight() {
  156.         return currentFlight;
  157.     }
  158.  
  159.     public void setCurrentFlight(FlightInfo currentFlight) {
  160.         this.currentFlight = currentFlight;
  161.     }
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement