diff --git a/src/main/java/org/s4s/gui/views/ArtikelerstellungsView.java b/src/main/java/org/s4s/gui/views/ArtikelerstellungsView.java new file mode 100644 index 0000000000000000000000000000000000000000..fec1a4aa4dd26119f0b59f2b36af57815fb7dfa3 --- /dev/null +++ b/src/main/java/org/s4s/gui/views/ArtikelerstellungsView.java @@ -0,0 +1,232 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package org.s4s.gui.views; + +import com.vaadin.navigator.View; +import com.vaadin.navigator.ViewChangeListener; +import com.vaadin.server.FileResource; +import com.vaadin.server.FontAwesome; +import com.vaadin.server.Page; +import com.vaadin.ui.Button; +import com.vaadin.ui.DateField; +import com.vaadin.ui.FormLayout; +import com.vaadin.ui.HorizontalLayout; +import com.vaadin.ui.Image; +import com.vaadin.ui.Label; +import com.vaadin.ui.Layout; +import com.vaadin.ui.NativeSelect; +import com.vaadin.ui.Notification; +import com.vaadin.ui.Panel; +import com.vaadin.ui.TextArea; +import com.vaadin.ui.TextField; +import com.vaadin.ui.UI; +import com.vaadin.ui.Upload; +import com.vaadin.ui.Upload.Receiver; +import com.vaadin.ui.Upload.StartedEvent; +import com.vaadin.ui.Upload.StartedListener; +import com.vaadin.ui.Upload.SucceededEvent; +import com.vaadin.ui.Upload.SucceededListener; +import com.vaadin.ui.VerticalLayout; +import java.io.File; +import java.io.FileOutputStream; +import java.io.OutputStream; +import java.util.Date; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.s4s.exceptions.DAOException; +import org.s4s.modell.dto.User; +import org.s4s.process.control.ArtikelerstellungsControl; +import org.s4s.process.control.ShopControl; +import org.s4s.services.util.Roles; +import org.s4s.services.util.Views; + +/** + * + * @author synot + */ +public class ArtikelerstellungsView extends TemplateView implements View { + + @Override + public void enter(ViewChangeListener.ViewChangeEvent event) { + setUp(); + } + + public void setUp() { + + final TextField tfName = new TextField("Artikel Name:"); + final TextArea taBeschreibung = new TextArea("Artikel Beschreibung:"); + final Label lMaxLength = new Label("Max. 250 Zeichen"); + final NativeSelect nsKategorie = new NativeSelect("Kategorie:"); + final NativeSelect nsZustand = new NativeSelect("Gebrauchszustand:"); + final TextField tfPreis = new TextField("Preis pro Artikel:"); + final TextField tfAnzahl = new TextField("Anzahl der Artikel:"); + final Button bErstellen = new Button("Erstellen", FontAwesome.CHECK); + final Button bAbbrechen = new Button("Abbrechen", FontAwesome.STOP_CIRCLE); + final DateField dfVon = new DateField("Von:"); + final DateField dfBis = new DateField("Bis:"); + final NativeSelect nsModul = new NativeSelect("Modul:"); + + // Artikel Bild Upload Area + // Show Uploaded Image in this placeholder + final Image image = new Image("Gespeichertes Bild:"); + + // Implement both receiver that saves upload in a file and + // listener for successful upload + class ImageUploader implements Receiver, SucceededListener { + + public File file; + + @Override + public OutputStream receiveUpload(String filename, String mimeType) { + // Create File Output Stream + FileOutputStream fos; // Stream to write to + try { + // Open the file for writing + file = new File("/temp/uploads/" + filename); + fos = new FileOutputStream(file); + } catch (final java.io.FileNotFoundException e) { + new Notification("Das Bild konnte nicht geladen werden\n", e.getMessage(), Notification.Type.ERROR_MESSAGE).show(Page.getCurrent()); + return null; + } + return fos; // Return the output stream to write to + } + + @Override + public void uploadSucceeded(SucceededEvent event) { + // Show the uploaded file in the image viewer + image.setVisible(true); + image.setSource(new FileResource(file)); + } + } + + ImageUploader receiver = new ImageUploader(); + + // Create the Upload with a caption and set receiver later + Upload upload = new Upload("Hier das Bild zum Artikel Speichern:", receiver); + upload.addSucceededListener(receiver); + + // Prevent too big Uploads + final long UPLOAD_LIMIT = 1000000l; + upload.addStartedListener(new StartedListener() { + private static final long serialVersionUID = 4728847902678459488L; + + @Override + public void uploadStarted(StartedEvent event) { + if (event.getContentLength() > UPLOAD_LIMIT) { + Notification.show("Die Datei ist zu groß!", + Notification.Type.ERROR_MESSAGE); + upload.interruptUpload(); + } + } + }); + + // Put the components in a panel + Layout layoutUpload = new VerticalLayout(); + layoutUpload.addComponents(upload, image); + + // Create uploads directory + File uploads = new File("/tmp/uploads"); + if (!uploads.exists() && !uploads.mkdir()) { + layoutUpload.addComponent(new Label("ERROR: Could not create upload dir")); + } + + tfName.setSizeFull(); + taBeschreibung.setMaxLength(250); + taBeschreibung.setSizeFull(); + + nsZustand.addItems("Neu", "Gebraucht"); + nsZustand.setNullSelectionAllowed(false); + + nsKategorie.addItems("Dienstleistung", "Buch", "Multimedia"); + nsModul.setNullSelectionAllowed(false); + + HorizontalLayout hKommand = new HorizontalLayout(bAbbrechen, bErstellen); + HorizontalLayout hVerfuegbarkeit = new HorizontalLayout(dfVon, dfBis); + HorizontalLayout hKriterien = new HorizontalLayout(nsKategorie, nsModul); + hVerfuegbarkeit.setCaption("Verfügbarkeit:"); + hKriterien.setCaption("Kriterien:"); + + Panel panel = new Panel("Artikel Erstellung"); + panel.setSizeUndefined(); + + FormLayout flArtikel = new FormLayout(layoutUpload, tfName, taBeschreibung, lMaxLength, hKriterien, tfAnzahl, nsZustand, hVerfuegbarkeit, tfPreis, hKommand); + flArtikel.setMargin(true); + + panel.setContent(flArtikel); + + ArtikelerstellungsControl aCont = new ArtikelerstellungsControl(); + ShopControl sCont = new ShopControl(); + + try { + nsModul.addItems(aCont.getModul()); + } catch (Exception e) { + } + nsModul.setNullSelectionAllowed(false); + User u = (User) UI.getCurrent().getSession().getAttribute(Roles.CURRENTUSER); + System.out.println(u.getUserId()); + bErstellen.addClickListener((Button.ClickEvent event) -> { + + if (tfName.isEmpty()) { + Notification.show("Bitte einen Namen eingeben!", Notification.Type.ERROR_MESSAGE); + } else if(taBeschreibung.isEmpty()) { + Notification.show("Bitte eine Beschreibung eingeben!", Notification.Type.ERROR_MESSAGE); + } else if(tfPreis.isEmpty()) { + Notification.show("Bitte einen Preis eingeben!", Notification.Type.ERROR_MESSAGE); + } else if(nsKategorie.isEmpty()) { + Notification.show("Bitte wählen Sie eine Kategorie aus!", Notification.Type.ERROR_MESSAGE); + } else { + try { + + int sId = sCont.getShopIdByUserId(u.getUserId()); + System.out.println(sId); + + int mId = aCont.getModulByName(nsModul.getValue().toString()); + System.out.println(mId); + int cId = 0; + if (nsKategorie.getValue().equals("Multimedia")) { + cId = 1; + } else { + cId = 2; + } + + String name = tfName.getValue(); + System.out.println(name); + String beschreibung = taBeschreibung.getValue(); + System.out.println(beschreibung); + Date dVon = dfVon.getValue(); + System.out.println(dVon); + Date dBis = dfBis.getValue(); + System.out.println(dBis); + double preis = Double.parseDouble(tfPreis.getValue()); + System.out.println(preis); + boolean result = false; + + if (cId == 2 && dVon == null && dBis == null) { + Notification.show("Bei einer Dienstleistung muss eine Verfügbarkeit angegeben sein!", Notification.Type.ERROR_MESSAGE); + return; + } else { + if (cId == 2) { + result = aCont.addProduct(name, beschreibung, dVon, dBis, preis, image, sId, mId, cId); + } else { + result = aCont.addProduct(name, beschreibung, preis, image, sId, mId, cId); + } + } + + if (result) { + Notification.show("Artikel wurde erfolgreich gespeichert."); + UI.getCurrent().getNavigator().navigateTo(Views.SHOPVIEW); + } else { + Notification.show("Artikel konnte nicht gespeichert werden!", Notification.Type.ERROR_MESSAGE); + } + } catch (DAOException ex) { + Logger.getLogger(ArtikelerstellungsView.class.getName()).log(Level.SEVERE, null, ex); + } + } + }); + + super.setUpFooterAndHeader(panel); + } +} diff --git a/src/main/java/org/s4s/process/control/ArtikelerstellungsControl.java b/src/main/java/org/s4s/process/control/ArtikelerstellungsControl.java new file mode 100644 index 0000000000000000000000000000000000000000..ff6a29c04464e1b27d06bd72d075b468306c732c --- /dev/null +++ b/src/main/java/org/s4s/process/control/ArtikelerstellungsControl.java @@ -0,0 +1,143 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package org.s4s.process.control; + +import com.vaadin.ui.Image; +import com.vaadin.ui.UI; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; +import org.s4s.dao.ProductDAO; +import org.s4s.dao.impl.AbstractDatabaseClass; +import org.s4s.exceptions.DAOException; +import org.s4s.modell.dto.Product; +import org.s4s.modell.dto.Shop; +import org.s4s.modell.dto.User; +import org.s4s.services.db.JDBCConnection; +import org.s4s.services.util.Roles; + +/** + * + * @author synot + */ +public class ArtikelerstellungsControl extends AbstractDatabaseClass implements ProductDAO { + + private static final User user = (User) UI.getCurrent().getSession().getAttribute(Roles.CURRENTUSER); + private final Statement statement = JDBCConnection.getInstance().getStatement(); + + //DB-Block + Statement st; + String sql_select; + String sql_update; + + public ArrayList getModul() throws DAOException { + ArrayList moduls = new ArrayList(); + ResultSet result; + + try { + sql_select = "SELECT * FROM \"ERR\".\"modul\""; + result = JDBCConnection.getInstance().getStatement().executeQuery(sql_select); + + while (result.next()) { + moduls.add(result.getString("modul_name")); + } + + } catch (SQLException e) { + throw new DAOException(e); + } + + return moduls; + } + + public int getModulByName(String modul) throws DAOException { + int mId = 0; + ResultSet result; + + try { + sql_select = "SELECT * FROM \"ERR\".\"modul\"" + + "WHERE modul_name = \'" + modul + "\'"; + result = JDBCConnection.getInstance().getStatement().executeQuery(sql_select); + + while (result.next()) { + mId = result.getInt("modulid"); + } + + } catch (SQLException e) { + throw new DAOException(e); + } + + return mId; + } + + @Override + public List<Product> getProductByDescription(String typ) throws DAOException { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + // Product - Attribute: Multimedia -> no Timespan required + public boolean addProduct(String name, String beschreibung, double preis, Image pic, int shopID, int modulID, int cat) throws DAOException { + boolean done = false; + + try { + sql_update = "INSERT INTO \"ERR\".\"artikel\"(name, beschreibung, preis, foto, shopid, modulid, kategorie) " + + "VALUES(\'" + name + "\', \'" + beschreibung + "\', \'" + preis + "\', \'" + pic + "\', \'" + shopID + "\', \'" + modulID + "\', \'" + cat + "\')"; + int exe = JDBCConnection.getInstance().getStatement().executeUpdate(sql_update); + + + if (exe < 0) { + throw new DAOException("Wrong Input!"); + } + done = true; + + } catch (Exception e) { + e.printStackTrace(); + System.out.println("Exception occur: " + e); + } + + return done; + } + + // 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 { + boolean done = false; + + try { + sql_update = "INSERT INTO \"ERR\".\"artikel\"(name, beschreibung, datum_von, preis, datum_bis, foto, shopid, modulid, kategorie) " + + "VALUES(\'" + name + "\', \'" + beschreibung + "\', \'" + dVon + "\', \'" + preis + "\', \'" + dBis + "\', \'" + pic + "\', \'" + shopID + "\', \'" + modulID + "\', \'" + cat + "\')"; + int exe = JDBCConnection.getInstance().getStatement().executeUpdate(sql_update); + + + if (exe < 0) { + throw new DAOException("Wrong Input!"); + } + done = true; + + } catch (Exception e) { + e.printStackTrace(); + System.out.println("Exception occur: " + e); + } + + return done; + } + + @Override + public List<Product> getProductByName(String typ) throws DAOException { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public Product getProductById(int id) throws DAOException { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } + + @Override + public Shop getProductOwner(Product product) throws DAOException { + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + } +}