diff --git a/pom.xml b/pom.xml index a02a065930b1d30ee95308855144cff2e9f19f89..094c50b8be020a576ae2a6d78792175c02db0cca 100644 --- a/pom.xml +++ b/pom.xml @@ -98,6 +98,15 @@ <version>1.3</version> <scope>test</scope> </dependency> + <dependency> + <groupId>org.postgresql</groupId> + <artifactId>postgresql</artifactId> + <version>42.0.0</version> +</dependency> + + + + </dependencies> diff --git a/src/main/java/org/s4s/MyUI.java b/src/main/java/org/s4s/MyUI.java index 091ea082d55b3e3de850c3d6be9fbf12c9f34ce4..5f62bdb2b16ad099febaa0c6de0b41634b3c110f 100644 --- a/src/main/java/org/s4s/MyUI.java +++ b/src/main/java/org/s4s/MyUI.java @@ -35,7 +35,7 @@ import org.s4s.gui.views.VerkaeuferbewertungView; import org.s4s.gui.views.WelcomeView; import org.s4s.gui.views.WarenkorbView; import org.s4s.modell.objects.dto.Product; -import org.s4s.process.controll.ProductSearch; +import org.s4s.dao.impl.ProductSearch; import org.s4s.services.util.Views; /** diff --git a/src/main/java/org/s4s/dao/DAOException.java b/src/main/java/org/s4s/dao/DAOException.java new file mode 100644 index 0000000000000000000000000000000000000000..72bda56ce64a9af1942737736553593f6be6dcc8 --- /dev/null +++ b/src/main/java/org/s4s/dao/DAOException.java @@ -0,0 +1,29 @@ +/* + * 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.dao; + +/** + * + * @author Holger + */ +public class DAOException extends Exception { + + public DAOException() { + super(); + } + + public DAOException(String message) { + super(message); + } + + public DAOException(String message, Throwable cause) { + super(message, cause); + } + + public DAOException(Throwable cause) { + super(cause); + } +} diff --git a/src/main/java/org/s4s/dao/KundenDAO.java b/src/main/java/org/s4s/dao/KundenDAO.java new file mode 100644 index 0000000000000000000000000000000000000000..6f56bfcb3e70057dc807ab7d8c0873a92860edd9 --- /dev/null +++ b/src/main/java/org/s4s/dao/KundenDAO.java @@ -0,0 +1,17 @@ +/* + * 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.dao; + +import org.s4s.modell.objects.dto.Kunde; + +/** + * + * @author Holger + */ +public interface KundenDAO { + + Kunde getKundeByCredentials(String username, String password) throws NoSuchUserOrPassword, DAOException; +} diff --git a/src/main/java/org/s4s/dao/NoSuchUserOrPassword.java b/src/main/java/org/s4s/dao/NoSuchUserOrPassword.java new file mode 100644 index 0000000000000000000000000000000000000000..29c799772f6431ba852890805f84940f3022c51a --- /dev/null +++ b/src/main/java/org/s4s/dao/NoSuchUserOrPassword.java @@ -0,0 +1,29 @@ +/* + * 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.dao; + +/** + * + * @author Holger + */ +public class NoSuchUserOrPassword extends Exception { + + public NoSuchUserOrPassword() { + super(); + } + + public NoSuchUserOrPassword(String message) { + super(message); + } + + public NoSuchUserOrPassword(String message, Throwable cause) { + super(message, cause); + } + + public NoSuchUserOrPassword(Throwable cause) { + super(cause); + } +} diff --git a/src/main/java/org/s4s/dao/ProductDAO.java b/src/main/java/org/s4s/dao/ProductDAO.java new file mode 100644 index 0000000000000000000000000000000000000000..ee5a43dd2ab6119e2ced75826ac9da9985e94bfb --- /dev/null +++ b/src/main/java/org/s4s/dao/ProductDAO.java @@ -0,0 +1,18 @@ +/* + * 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.dao; + +import java.util.List; +import org.s4s.modell.objects.dto.Product; + +/** + * + * @author Holger + */ +public interface ProductDAO { + List<Product> getProductByTyp(String typ); + List<Product> getProductByDescription(String typ); +} diff --git a/src/main/java/org/s4s/dao/impl/AbstractDatabaseClass.java b/src/main/java/org/s4s/dao/impl/AbstractDatabaseClass.java new file mode 100644 index 0000000000000000000000000000000000000000..919f1f565803e678d022d4d90b3f4a8d4e9bb6dc --- /dev/null +++ b/src/main/java/org/s4s/dao/impl/AbstractDatabaseClass.java @@ -0,0 +1,27 @@ +/* + * 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.dao.impl; + +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import org.s4s.services.db.JDBCConnection; + +/** + * + * @author Holger + */ +public abstract class AbstractDatabaseClass { + + private JDBCConnection conn = JDBCConnection.getInstance(); + + protected ResultSet executeQuery(String sql) throws SQLException { + Statement stat = conn.getStatement(); + ResultSet set = stat.executeQuery(sql); + stat.closeOnCompletion(); + return set; + } +} diff --git a/src/main/java/org/s4s/dao/impl/LoginControl.java b/src/main/java/org/s4s/dao/impl/LoginControl.java new file mode 100644 index 0000000000000000000000000000000000000000..7eab3810ec3a24c4975329cf98a179a5f9891a51 --- /dev/null +++ b/src/main/java/org/s4s/dao/impl/LoginControl.java @@ -0,0 +1,37 @@ +/* + * 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.dao.impl; + +import java.sql.*; +import org.s4s.dao.DAOException; +import org.s4s.dao.KundenDAO; +import org.s4s.dao.NoSuchUserOrPassword; +import org.s4s.modell.objects.dto.Kunde; + +/** + * + * @author Holger + */ +public class LoginControl extends AbstractDatabaseClass implements KundenDAO { + + @Override + public Kunde getKundeByCredentials(String username, String password) throws NoSuchUserOrPassword, DAOException { + try (ResultSet set = executeQuery("SELECT username, vorname, nachname FROM realm.user WHERE login = " + username + + " AND password = " + password + ";")) { + if (!set.next()) { + throw new NoSuchUserOrPassword(); + } + String vname = set.getString("vorname"); + String nname = set.getString("nachname"); + String uname = set.getString("username"); + + return new Kunde(vname, nname, uname); + + } catch (SQLException ex) { + throw new DAOException(ex); + } + } +} diff --git a/src/main/java/org/s4s/dao/impl/ProductSearch.java b/src/main/java/org/s4s/dao/impl/ProductSearch.java new file mode 100644 index 0000000000000000000000000000000000000000..6b5315df6a451e095f3447690808c050ebdd577b --- /dev/null +++ b/src/main/java/org/s4s/dao/impl/ProductSearch.java @@ -0,0 +1,68 @@ +/* + * 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.dao.impl; + +import java.util.ArrayList; +import java.util.List; +import org.s4s.dao.ProductDAO; +import org.s4s.modell.objects.dto.Product; + +/** + * + * @author Holger + */ +public class ProductSearch extends AbstractDatabaseClass implements ProductDAO { + + Product p1 = new Product("Nachhilfe Algebra", 1, "Nachhilfe", 2.00, "Biete Nachhilfe in Algebra an!"); + Product p2 = new Product("F&I", 2, "Mitschriften", 5.00, "Mitschriften aus dem WS16/17 in F&I!"); + Product p3 = new Product("Übungen Eidip", 3, "Lösungen", 10.50, "Lösungen zu alten Eidip Aufgaben!"); + + private ProductSearch() { + + } + + public static ProductSearch search = null; + + public static ProductSearch getInstance() { + if (search == null) { + search = new ProductSearch(); + } + return search; + } + + @Override + public List<Product> getProductByTyp(String typ) { + List<Product> liste = new ArrayList<>(); + String lowerTyp = typ.toLowerCase(); + if (lowerTyp.contains("nachhilfe")) { + liste.add(p1); + } + if (lowerTyp.contains("mitschrift")) { + liste.add(p2); + } + if (lowerTyp.contains("lösungen")) { + liste.add(p3); + } + + return liste; + } + + @Override + public List<Product> getProductByDescription(String typ) { + List<Product> liste = new ArrayList<>(); + String lowerTyp = typ.toLowerCase(); + if (lowerTyp.contains("algebra")) { + liste.add(p1); + } + if (lowerTyp.contains("f&i")) { + liste.add(p2); + } + if (lowerTyp.contains("eidip")) { + liste.add(p3); + } + return liste; + } +} diff --git a/src/main/java/org/s4s/gui/views/LoginView.java b/src/main/java/org/s4s/gui/views/LoginView.java index 360dfc2095d6fbe4b29cc638a8c4ff11979997bd..d38ddee945516ab8d3dbfa0245dffafff2ae6dd3 100644 --- a/src/main/java/org/s4s/gui/views/LoginView.java +++ b/src/main/java/org/s4s/gui/views/LoginView.java @@ -7,50 +7,74 @@ package org.s4s.gui.views; import com.vaadin.navigator.View; import com.vaadin.navigator.ViewChangeListener; -import com.vaadin.server.Resource; -import com.vaadin.ui.Alignment; +import com.vaadin.server.FontAwesome; +import com.vaadin.shared.ui.label.ContentMode; import com.vaadin.ui.Button; -import com.vaadin.ui.Component; -import com.vaadin.ui.HorizontalLayout; +import com.vaadin.ui.Label; +import com.vaadin.ui.Notification; import com.vaadin.ui.PasswordField; +import com.vaadin.ui.TextField; import com.vaadin.ui.VerticalLayout; -import java.awt.TextField; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.s4s.dao.DAOException; +import org.s4s.dao.KundenDAO; +import org.s4s.dao.NoSuchUserOrPassword; +import org.s4s.dao.impl.LoginControl; /** * * @author Holger */ -public class LoginView extends VerticalLayout implements View{ +public class LoginView extends VerticalLayout implements View { + + private final KundenDAO kundenDAO = new LoginControl(); - @Override public void enter(ViewChangeListener.ViewChangeEvent event) { this.setUp(); } - - public void setUp(){ + + public void setUp() { setSizeFull(); - setMargin(true); - final HorizontalLayout horizontalLayout = new HorizontalLayout(); - final TextField user = new TextField("Benutzername: "); - final PasswordField password = null; - final Button loginButton = new Button("Einloggen"); - - - - horizontalLayout.setSpacing(true); - horizontalLayout.addComponent(loginButton); - horizontalLayout.setComponentAlignment(loginButton, Alignment.TOP_RIGHT); - horizontalLayout.addComponent(password); - horizontalLayout.setComponentAlignment(password, Alignment.TOP_RIGHT); - horizontalLayout.addComponent((Component) user); - horizontalLayout.setComponentAlignment((Component) user, Alignment.TOP_RIGHT); - - - addComponent(horizontalLayout); - setComponentAlignment(horizontalLayout,Alignment.TOP_RIGHT); - + + final TextField userLogin = new TextField(); + userLogin.setCaption("Login-Name: "); + final PasswordField passwordField = new PasswordField(); + passwordField.setCaption("Passwort: "); + + VerticalLayout layout = new VerticalLayout(); + + layout.addComponent(userLogin); + layout.addComponent(passwordField); + + Label label = new Label(" ", ContentMode.HTML); + layout.addComponent(label); + + Button button = new Button("Login", FontAwesome.BEER); + layout.addComponent(button); + + this.addComponent(layout); + + button.addClickListener(new Button.ClickListener() { + @Override + public void buttonClick(Button.ClickEvent event) { + String login = userLogin.getValue(); + String password = passwordField.getValue(); + + try { + kundenDAO.getKundeByCredentials(login, password); + + } catch (NoSuchUserOrPassword ex) { + Notification.show("Fehler", "Login oder Passwort falsch", Notification.Type.ERROR_MESSAGE); + userLogin.setValue(""); + passwordField.setValue(""); + } catch (DAOException ex) { + Notification.show("Fehler", "interner Fehler", Notification.Type.ERROR_MESSAGE); + Logger.getLogger(LoginView.class.getName()).log(Level.SEVERE, null, ex); + } + } + }); } - - + } diff --git a/src/main/java/org/s4s/gui/views/ShopView.java b/src/main/java/org/s4s/gui/views/ShopView.java index a6e1ed6323de79a98ec76a351eeea37724e8f965..ed3b47e5744bf2fef547878cd0ce0c765c238410 100644 --- a/src/main/java/org/s4s/gui/views/ShopView.java +++ b/src/main/java/org/s4s/gui/views/ShopView.java @@ -17,9 +17,12 @@ import com.vaadin.ui.Notification; import com.vaadin.ui.Table; import com.vaadin.ui.TextField; import com.vaadin.ui.VerticalLayout; +import java.util.HashSet; import java.util.List; +import java.util.Set; +import org.s4s.dao.ProductDAO; import org.s4s.modell.objects.dto.Product; -import org.s4s.process.controll.ProductSearch; +import org.s4s.dao.impl.ProductSearch; /** * @@ -27,6 +30,8 @@ import org.s4s.process.controll.ProductSearch; */ public class ShopView extends VerticalLayout implements View{ + private final ProductDAO products = ProductSearch.getInstance(); + @Override public void enter(ViewChangeListener.ViewChangeEvent event) { this.setUp(); @@ -71,11 +76,13 @@ public class ShopView extends VerticalLayout implements View{ } else{ addComponent(table); - List<Product> liste = ProductSearch.getInstance().getProductByTyp(str); + Set<Product> liste = new HashSet<>(); + liste.addAll(products.getProductByTyp(str)); + liste.addAll(products.getProductByDescription(str)); data.removeAllItems(); data.addAll(liste); table.setPageLength(table.size()); } }); } -} +} \ No newline at end of file diff --git a/src/main/java/org/s4s/modell/objects/dto/Kunde.java b/src/main/java/org/s4s/modell/objects/dto/Kunde.java index aca558f3c36317eb6d7a593ea5f6e91deaeefb58..6b0609540a9e37019894d86fc63a521ce739f536 100644 --- a/src/main/java/org/s4s/modell/objects/dto/Kunde.java +++ b/src/main/java/org/s4s/modell/objects/dto/Kunde.java @@ -10,21 +10,27 @@ package org.s4s.modell.objects.dto; * @author Holger */ public class Kunde { + private String vorname; private String nachname; - private int userID; + private String username; + + public Kunde(String vorname, String nachname, String username) { + this.vorname = vorname; + this.nachname = nachname; + this.username = username; + } - - - public String getVorname(){ + public String getVorname() { return vorname; } - - public String getNachname(){ + + public String getNachname() { return nachname; } - - public int getUserID(){ - return userID; + + public String getUsername() { + return username; } + } diff --git a/src/main/java/org/s4s/process/controll/ProductSearch.java b/src/main/java/org/s4s/process/controll/ProductSearch.java deleted file mode 100644 index 2133119d6084452336e4fa9628eae6fc12c04eeb..0000000000000000000000000000000000000000 --- a/src/main/java/org/s4s/process/controll/ProductSearch.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * 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.controll; - -import java.util.ArrayList; -import java.util.List; -import org.s4s.modell.objects.dto.Product; - -/** - * - * @author Holger - */ -public class ProductSearch { - - Product p1 = new Product("Nachhilfe Algebra", 1, "Nachhilfe", 2.00, "Biete Nachhilfe in Algebra an!"); - Product p2 = new Product("F&I", 2, "Mitschriften", 5.00, "Mitschriften aus dem WS16/17 in F&I!"); - Product p3 = new Product("Übungen Eidip", 3,"Lösungen" , 10.50, "Lösungen zu alten Eidip Aufgaben!"); - - private ProductSearch(){ - - } - - public static ProductSearch search = null; - - public static ProductSearch getInstance(){ - if(search== null){ - search = new ProductSearch(); - } - return search; - } - - public List<Product> getProductByTyp(String typ){ - ArrayList<Product> liste = new ArrayList<Product> (); - if(typ.equals("Nachhilfe")){ - liste.add(p1); - } - if(typ.equals("Mitschriften")){ - liste.add(p2); - } - if(typ.equals("Lösungen")){ - liste.add(p3); - } - - return liste; - } -} diff --git a/src/main/java/org/s4s/services/db/JDBCConnection.java b/src/main/java/org/s4s/services/db/JDBCConnection.java new file mode 100644 index 0000000000000000000000000000000000000000..f4b335a5216c82e5dc1659bfd4ce80217f490572 --- /dev/null +++ b/src/main/java/org/s4s/services/db/JDBCConnection.java @@ -0,0 +1,80 @@ +/* + * 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.services.db; + +import java.sql.*; +import java.util.*; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * + * @author Holger + */ +public class JDBCConnection { + + private static JDBCConnection connection = null; + + private String url = ""; + private Connection conn; + + public static JDBCConnection getInstance() { + if (connection == null) { + connection = new JDBCConnection(); + } + return connection; + } + + private JDBCConnection() { + this.initConnection(); + } + + private void initConnection() { + try { + DriverManager.registerDriver(new org.postgresql.Driver()); + this.openConnection(); + } catch (SQLException ex) { + Logger.getLogger(JDBCConnection.class.getName()).log(Level.SEVERE, null, ex); + } + } + + private void openConnection() { + try { + + if (!this.conn.isClosed()) { + return; + } + + Properties props = new Properties(); + props.setProperty("userLogin", ""); + props.setProperty("password", ""); + this.conn = DriverManager.getConnection(this.url, props); + + } catch (SQLException ex) { + Logger.getLogger(JDBCConnection.class.getName()).log(Level.SEVERE, null, ex); + } + } + + public Statement getStatement() { + try { + if (this.conn.isClosed()) { + this.openConnection(); + } + return this.conn.createStatement(); + } catch (SQLException ex) { + Logger.getLogger(JDBCConnection.class.getName()).log(Level.SEVERE, null, ex); + return null; + } + } + + public void closeConnection() { + try { + conn.close(); + } catch (SQLException ex) { + Logger.getLogger(JDBCConnection.class.getName()).log(Level.SEVERE, null, ex); + } + } +} diff --git a/target/classes/org/s4s/MyUI.class b/target/classes/org/s4s/MyUI.class index b7c2a3afa945bd0a41c50b09c320e030d3531a00..5ff9f4de69e653b2c20be79ae6ebc5b343f275ef 100644 Binary files a/target/classes/org/s4s/MyUI.class and b/target/classes/org/s4s/MyUI.class differ diff --git a/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst b/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst index adffa0e826a286a372eeef5956fa74bafd699f45..0b959c4e04f86fb9b7ae5ed03a66f0cd6f4ee914 100644 --- a/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst +++ b/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst @@ -1,19 +1,20 @@ org\s4s\gui\views\KaufbestaetigungView.class org\s4s\gui\views\PasswortView.class +org\s4s\modell\objects\dto\Kunde.class +org\s4s\modell\objects\dto\Product.class +org\s4s\process\controll\exceptions\NoSuchUserOrPassword.class +org\s4s\process\controll\ProductSearch.class +org\s4s\gui\views\WarenkorbView.class org\s4s\gui\views\VerkaeuferbewertungView.class org\s4s\gui\views\CheckoutView.class org\s4s\gui\views\WelcomeView.class org\s4s\modell\objects\dto\Warenkorb.class org\s4s\gui\views\ShopView.class org\s4s\MyUI.class -org\s4s\modell\objects\dto\Kunde.class org\s4s\gui\views\BenutzerkontoView.class -org\s4s\modell\objects\dto\Product.class org\s4s\gui\views\ImpressumView.class -org\s4s\process\controll\ProductSearch.class -org\s4s\gui\views\LoginView.class org\s4s\gui\views\FaqView.class -org\s4s\gui\views\WarenkorbView.class +org\s4s\process\controll\LoginControl.class org\s4s\services\util\Views.class org\s4s\gui\views\ArtikeldetailsView.class org\s4s\MyUI$MyUIServlet.class diff --git a/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst b/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst index c744e91c15a8dac8494420ce08bd409a9ff57046..071d902842dace9a103069594bccda8549e5eb4d 100644 --- a/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst +++ b/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst @@ -1,11 +1,16 @@ C:\Users\Holger\Documents\NetBeansProjects\s4s_MeineTests\src\main\java\org\s4s\gui\views\WelcomeView.java +C:\Users\Holger\Documents\NetBeansProjects\s4s_MeineTests\src\main\java\org\s4s\dao\ProductDAO.java C:\Users\Holger\Documents\NetBeansProjects\s4s_MeineTests\src\main\java\org\s4s\gui\views\PasswortView.java +C:\Users\Holger\Documents\NetBeansProjects\s4s_MeineTests\src\main\java\org\s4s\services\db\JDBCConnection.java C:\Users\Holger\Documents\NetBeansProjects\s4s_MeineTests\src\main\java\org\s4s\gui\views\ArtikeldetailsView.java C:\Users\Holger\Documents\NetBeansProjects\s4s_MeineTests\src\main\java\org\s4s\gui\views\BenutzerkontoView.java C:\Users\Holger\Documents\NetBeansProjects\s4s_MeineTests\src\main\java\org\s4s\gui\views\CheckoutView.java C:\Users\Holger\Documents\NetBeansProjects\s4s_MeineTests\src\main\java\org\s4s\gui\views\FaqView.java C:\Users\Holger\Documents\NetBeansProjects\s4s_MeineTests\src\main\java\org\s4s\gui\views\WarenkorbView.java C:\Users\Holger\Documents\NetBeansProjects\s4s_MeineTests\src\main\java\org\s4s\process\controll\ProductSearch.java +C:\Users\Holger\Documents\NetBeansProjects\s4s_MeineTests\src\main\java\org\s4s\dao\KundenDAO.java +C:\Users\Holger\Documents\NetBeansProjects\s4s_MeineTests\src\main\java\org\s4s\process\controll\LoginControl.java +C:\Users\Holger\Documents\NetBeansProjects\s4s_MeineTests\src\main\java\org\s4s\process\controll\exceptions\NoSuchUserOrPassword.java C:\Users\Holger\Documents\NetBeansProjects\s4s_MeineTests\src\main\java\org\s4s\MyUI.java C:\Users\Holger\Documents\NetBeansProjects\s4s_MeineTests\src\main\java\org\s4s\modell\objects\dto\Product.java C:\Users\Holger\Documents\NetBeansProjects\s4s_MeineTests\src\main\java\org\s4s\gui\views\ImpressumView.java