From 6ac94a45724db511eee604d6951fb05ea85ed925 Mon Sep 17 00:00:00 2001 From: sknobl2s <sebastian.knoblauch@smail.inf.h-brs.de> Date: Mon, 19 Jun 2017 18:44:54 +0200 Subject: [PATCH] Transaction.java : Verkaeufer von String zu Int TransactionControl.java: Logik erstellt --- .../java/org/s4s/modell/dto/Transaction.java | 8 +-- .../process/control/TransactionControl.java | 50 ++++++++++++++++--- 2 files changed, 46 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/s4s/modell/dto/Transaction.java b/src/main/java/org/s4s/modell/dto/Transaction.java index 3233cc0..ddfb783 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 f708ad1..51c16ea 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); + } } } -- GitLab