diff --git a/src/main/java/org/s4s/modell/dto/Transaction.java b/src/main/java/org/s4s/modell/dto/Transaction.java index 3233cc0adfdd2c4d47a81e6bd50c506980f2dd51..ddfb78350217c5fc4c2245435aa755fe6fbb9902 100644 --- a/src/main/java/org/s4s/modell/dto/Transaction.java +++ b/src/main/java/org/s4s/modell/dto/Transaction.java @@ -11,21 +11,21 @@ package org.s4s.modell.dto; */ public class Transaction { - private String verkaeufer; + private Integer verkaeufer; private User kaeufer; private Product produkt; - public Transaction(String verkaeufer, User kaeufer, Product produkt) { + public Transaction(Integer verkaeufer, User kaeufer, Product produkt) { this.verkaeufer = verkaeufer; this.kaeufer = kaeufer; this.produkt = produkt; } - public String getVerkaeufer() { + public Integer getVerkaeufer() { return verkaeufer; } - public void setVerkaeufer(String verkaeufer) { + public void setVerkaeufer(Integer verkaeufer) { this.verkaeufer = verkaeufer; } diff --git a/src/main/java/org/s4s/process/control/TransactionControl.java b/src/main/java/org/s4s/process/control/TransactionControl.java index f708ad1168da90bd561f8da838e47fbcc36816a5..51c16ea0a249b34d3a15a993fc70b216ef14d02f 100644 --- a/src/main/java/org/s4s/process/control/TransactionControl.java +++ b/src/main/java/org/s4s/process/control/TransactionControl.java @@ -3,10 +3,14 @@ package org.s4s.process.control; import com.vaadin.ui.UI; import java.sql.ResultSet; import java.sql.SQLException; +import java.sql.Statement; import org.s4s.dao.impl.AbstractDatabaseClass; import org.s4s.exceptions.DAOException; import org.s4s.modell.dto.Product; +import org.s4s.modell.dto.Transaction; import org.s4s.modell.dto.User; +import org.s4s.modell.dto.Warenkorb; +import org.s4s.services.db.JDBCConnection; import org.s4s.services.util.Roles; /** @@ -15,23 +19,53 @@ import org.s4s.services.util.Roles; */ public class TransactionControl extends AbstractDatabaseClass { - String verkaeufer; - User kaeufer; - Product produkt; - //Warenkorb warenkorb = Warenkorb.getInstance(); + Transaction transaktion; + private final Statement statement = JDBCConnection.getInstance().getStatement(); - String kaufer = (String) UI.getCurrent().getSession().getAttribute(Roles.CURRENTUSER); + //String kaeuferString = (String) UI.getCurrent().getSession().getAttribute(Roles.CURRENTUSER); + public void createTransactions() throws DAOException, SQLException { + for (Product p : Warenkorb.getInstance().inhalt()) { + transaktion.setKaeufer((User) UI.getCurrent().getSession().getAttribute(Roles.CURRENTUSER)); + transaktion.setVerkaeufer(kaeuferinDB(p.getId())); + transaktion.setProdukt(p); + createTransactionInDB(transaktion); + updateGekauftStatusInArtikel(transaktion); + } + } - public void createTransaction() throws DAOException, SQLException { + public Integer kaeuferinDB(int produktid) throws DAOException, SQLException { + Integer verkaeufer; try (ResultSet rs = executeQuery("SELECT u.\"besitzer\"" + " FROM \"ERR\".\"shop\" u , \"ERR\".\"artikel\" s" + " WHERE u.shopid = s.shopid AND " - + "s.artikelid'" + produkt.getId() + "';");) { + + "s.artikelid'" + transaktion.getProdukt().getId() + "';");) { - verkaeufer = rs.getString("besitzer"); + verkaeufer = rs.getInt("besitzer"); } catch (SQLException ex) { throw new DAOException(ex); } + return verkaeufer; + } + public void createTransactionInDB(Transaction transaktion) throws DAOException, SQLException { + try { + statement.execute("INSERT INTO \"ERR\".\"transaktion\" (verkaeufer, kaeufer, artikelid) VALUES(" + + transaktion.getVerkaeufer() + "," + + transaktion.getKaeufer().getUserId() + "," + + transaktion.getProdukt().getId()); + } catch (SQLException ex) { + throw new DAOException(ex); + } + } + + public void updateGekauftStatusInArtikel(Transaction transaktion) throws DAOException, SQLException { + try { + statement.execute("UPDATE INTO \"ERR\".\"artikel\"" + + "SET gekauft = true" + + "WHERE artikelid = " + + transaktion.getProdukt().getId()); + } catch (SQLException ex) { + throw new DAOException(ex); + } } }