145 lines
4.5 KiB
Java
145 lines
4.5 KiB
Java
package de.hsel.itech.jsf;
|
|
|
|
import de.hsel.itech.db.Database;
|
|
import de.hsel.itech.db.pojo.*;
|
|
import lombok.Setter;
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
import javax.faces.annotation.ManagedProperty;
|
|
import javax.faces.application.FacesMessage;
|
|
import javax.faces.bean.ManagedBean;
|
|
import javax.faces.bean.SessionScoped;
|
|
import javax.faces.context.FacesContext;
|
|
import javax.faces.event.PhaseId;
|
|
import javax.faces.event.ValueChangeEvent;
|
|
import java.sql.Date;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
@ManagedBean
|
|
@SessionScoped
|
|
public class CartBean {
|
|
|
|
@Setter
|
|
@ManagedProperty("#{userBean.user}")
|
|
private User user;
|
|
|
|
public User getUser() {
|
|
FacesContext context = FacesContext.getCurrentInstance();
|
|
return (User) context.getApplication().evaluateExpressionGet(context, "#{userBean.user}", User.class);
|
|
}
|
|
|
|
public Payment getPayment () {
|
|
FacesContext context = FacesContext.getCurrentInstance();
|
|
return (Payment) context.getApplication().evaluateExpressionGet(context, "#{paymentBean.selected}", Payment.class);
|
|
}
|
|
|
|
public Address getAddress() {
|
|
FacesContext context = FacesContext.getCurrentInstance();
|
|
return (Address) context.getApplication().evaluateExpressionGet(context, "#{addressBean.selected}", Address.class);
|
|
}
|
|
|
|
@Setter
|
|
private List<ShoppingCartItem> items;
|
|
|
|
public String add(Book book) {
|
|
if (getUser() != null && getUser().getId() != 0) {
|
|
for (ShoppingCartItem item : getItems()) {
|
|
if (item.getArticle().getId() == book.getId()) {
|
|
if(item.getCount() >= 127) {
|
|
items = getFreshItems();
|
|
FacesContext.getCurrentInstance().addMessage("count:count", new FacesMessage("Es können maximal 127 eines Artikels auf einmal gekauft werden."));
|
|
return "cart.jsf";
|
|
}
|
|
Database.getInstance().shoppingCart().delete(item);
|
|
item.setCount(item.getCount() + 1);
|
|
Database.getInstance().shoppingCart().insert(item);
|
|
items = getFreshItems();
|
|
return "cart.jsf";
|
|
}
|
|
}
|
|
Database.getInstance().shoppingCart().insert(new ShoppingCartItem(getUser(), book, 1));
|
|
}
|
|
items = getFreshItems();
|
|
return "cart.jsf";
|
|
}
|
|
|
|
public String remove(ShoppingCartItem item) {
|
|
if (getUser() != null) {
|
|
Database.getInstance().shoppingCart().delete(item);
|
|
items = getFreshItems();
|
|
}
|
|
return "cart.jsf";
|
|
}
|
|
|
|
public List<ShoppingCartItem> getItems() {
|
|
if(items == null)
|
|
items = getFreshItems();
|
|
return items;
|
|
}
|
|
|
|
private List<ShoppingCartItem> getFreshItems() {
|
|
Database db = Database.getInstance();
|
|
if (getUser() != null) {
|
|
items = db.shoppingCart().get(getUser());
|
|
} else {
|
|
items = new ArrayList<>();
|
|
}
|
|
return items;
|
|
}
|
|
|
|
public void change(@NotNull ValueChangeEvent event) {
|
|
if (event.getPhaseId() != PhaseId.INVOKE_APPLICATION) {
|
|
event.setPhaseId(PhaseId.INVOKE_APPLICATION);
|
|
event.queue();
|
|
return;
|
|
}
|
|
|
|
for(ShoppingCartItem item : getItems()) {
|
|
Database.getInstance().shoppingCart().updateItem(item);
|
|
}
|
|
}
|
|
|
|
public String buy() {
|
|
ArrayList<OrderItem> orderList = new ArrayList<>();
|
|
for (ShoppingCartItem item : getFreshItems()) {
|
|
orderList.add(new OrderItem(item.getArticle(), item.getCount()));
|
|
}
|
|
Database.getInstance().order().insert(new Order(getUser(), orderList, new Date(System.currentTimeMillis()), sum(), getPayment(), getAddress()));
|
|
for (ShoppingCartItem item : getFreshItems()) {
|
|
Database.getInstance().shoppingCart().delete(item);
|
|
}
|
|
items = getFreshItems();
|
|
return "thanks.jsf";
|
|
}
|
|
|
|
public int getItemCount() {
|
|
int count = 0;
|
|
for(ShoppingCartItem item : getItems()) {
|
|
count += item.getCount();
|
|
}
|
|
return count;
|
|
}
|
|
|
|
public long sum() {
|
|
int sum = 0;
|
|
for (ShoppingCartItem item : getItems()) {
|
|
sum += item.getCount() * item.getArticle().getPrice();
|
|
}
|
|
return sum;
|
|
}
|
|
|
|
public String getSum() {
|
|
long sum = sum();
|
|
String res = "";
|
|
res += sum / 100;
|
|
res += ",";
|
|
if (sum % 100 < 10) {
|
|
res += "0";
|
|
}
|
|
res += sum % 100;
|
|
return res;
|
|
}
|
|
|
|
}
|