diff --git a/src/main/java/org/s4s/dao/ProductDAO.java b/src/main/java/org/s4s/dao/ProductDAO.java index c1831b8003dd97cd4c925e47652e1103239d07bc..c9f4a77b6b20a121f42423d11635dc01718c4c0e 100644 --- a/src/main/java/org/s4s/dao/ProductDAO.java +++ b/src/main/java/org/s4s/dao/ProductDAO.java @@ -30,4 +30,6 @@ public interface ProductDAO { // Product - Attribute: Dienstleistung -> Timespan required public boolean addProduct(String name, String beschreibung, Date dVon, Date dBis, double preis, Image pic, int shopID, int modulID, int cat) throws DAOException; + + public void deleteProductById(int pId) throws DAOException; } diff --git a/src/main/java/org/s4s/dao/impl/ProductDAOimpl.java b/src/main/java/org/s4s/dao/impl/ProductDAOimpl.java index abeb00c68f99f65ed40491e184b4bc201b36e165..3d7b5b72256f72d05df5b2ccd473e5e4097d7df7 100644 --- a/src/main/java/org/s4s/dao/impl/ProductDAOimpl.java +++ b/src/main/java/org/s4s/dao/impl/ProductDAOimpl.java @@ -224,4 +224,17 @@ public class ProductDAOimpl extends AbstractDatabaseClass implements ProductDAO return done; } + + @Override + public void deleteProductById(int pId) throws DAOException { + + try { + sql_update = "DELETE FROM \"ERR\".\"artikel\" " + + "WHERE \"artikelid\" = \'" + pId + "\';"; + JDBCConnection.getInstance().getStatement().execute(sql_update); + } catch (Exception e) { + e.printStackTrace(); + System.out.println("Exception occur: " + e); + } + } } diff --git a/src/main/java/org/s4s/gui/views/ShopView.java b/src/main/java/org/s4s/gui/views/ShopView.java index d95b719a1506413a770fc86004df6f93239ae31f..241215a765f66181a5a9464eb5c72bcaf5089789 100644 --- a/src/main/java/org/s4s/gui/views/ShopView.java +++ b/src/main/java/org/s4s/gui/views/ShopView.java @@ -1,6 +1,9 @@ package org.s4s.gui.views; +import com.sun.jmx.snmp.SnmpDefinitions; import com.vaadin.data.util.BeanContainer; +import com.vaadin.data.util.BeanItem; +import com.vaadin.event.ItemClickEvent; import com.vaadin.navigator.ViewChangeListener; import com.vaadin.shared.ui.label.ContentMode; import com.vaadin.ui.*; @@ -9,6 +12,7 @@ import java.util.HashSet; import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; +import org.s4s.dao.impl.ProductDAOimpl; import org.s4s.exceptions.DAOException; import org.s4s.modell.dto.*; import org.s4s.process.control.ProductControl; @@ -28,6 +32,8 @@ public class ShopView extends TemplateView { private final BeanContainer<Integer, Product> dataProducts = new BeanContainer<>(Product.class); private final Table tableProducts = new Table("Products", dataProducts); + private Product selectedProduct = null; + @Override public void enter(ViewChangeListener.ViewChangeEvent event) { if (session.getAttribute(Roles.CURRENTUSER) == null) { @@ -81,6 +87,7 @@ public class ShopView extends TemplateView { table.setVisibleColumns(new Object[]{"name", "price"}); table.setColumnHeader("name", "Name"); table.setColumnHeader("price", "Preis in €"); + table.setSelectable(true); verticalLayout.addComponent(table); table.setSizeFull(); } @@ -102,7 +109,7 @@ public class ShopView extends TemplateView { verticalLayout.addComponent(label_ShopDetails); verticalLayout.setComponentAlignment(label_ShopDetails, Alignment.MIDDLE_CENTER); - Panel panel_ProductQuantity = new Panel("Anzahl Produkte: "); + Panel panel_ProductQuantity = new Panel("Anzahl Produkte: " + table.size()); Panel panel_Rating = new Panel("Bewertung: " + shop.getBewertung()); Panel panel_RatingNumber = new Panel("Bewertungsanzahl: " + shop.getBewertungsanzahl()); Panel panel_ShopName = new Panel("Anbieter: " + shop.getName()); @@ -121,6 +128,32 @@ public class ShopView extends TemplateView { setUpFooterAndHeader(verticalLayout); + button_deleteProduct.addClickListener((Button.ClickEvent event) -> { + + if (!(selectedProduct == null)) { + try { + ProductDAOimpl.getInstance().deleteProductById(selectedProduct.getId()); + if (ProductDAOimpl.getInstance().getProductById(selectedProduct.getId()).getName() != null) { + Notification.show("Das Produkt\n" + selectedProduct.getName() + "\nkonnte nicht gelöscht werden!", Notification.Type.ERROR_MESSAGE); + } + UI.getCurrent().getNavigator().navigateTo(Views.SHOPVIEW + "/" + shop.getShopId()); + } catch (DAOException ex) { + Logger.getLogger(ShopView.class.getName()).log(Level.SEVERE, null, ex); + } + } else { + Notification.show("Das Produkt\n" + selectedProduct.getName() + "\nkonnte nicht geladen werden!", Notification.Type.ERROR_MESSAGE); + } + }); + + table.addItemClickListener((ItemClickEvent event) -> { + BeanItem<Product> productBean = dataProducts.getItem(event.getItemId()); + selectedProduct = productBean.getBean(); + System.err.println(selectedProduct.getId() + "; " + selectedProduct.getName()); + if (event.isDoubleClick()) { + UI.getCurrent().getNavigator().navigateTo(Views.ARTIKELDETAILS + "/" + selectedProduct.getId()); + } + }); + } }