From 1c640de416266da4d2e2d50ef0fc4d2625b8a450 Mon Sep 17 00:00:00 2001 From: localhorst Date: Fri, 5 Oct 2018 13:01:42 +0200 Subject: [PATCH] job in DB --- src/application/DBController.java | 52 +++++-- src/application/Job.java | 120 ++++++++++++++-- src/application/Main.java | 2 +- src/application/MainWindowController.java | 159 +++++++++++++++------- 4 files changed, 256 insertions(+), 77 deletions(-) diff --git a/src/application/DBController.java b/src/application/DBController.java index 73f51ab..83d8d05 100644 --- a/src/application/DBController.java +++ b/src/application/DBController.java @@ -75,7 +75,7 @@ class DBController return true; } } - + public String getCategoryNameFromPositionen(int pID) { // System.out.println("getCategoryName: " + pID); @@ -373,7 +373,6 @@ class DBController } } - // table Jobs section // public void erstelleTabelleJobs() { // Erstelle Tabelle mit Reihen @@ -382,28 +381,57 @@ class DBController Statement stmt = connection.createStatement(); stmt.executeUpdate("DROP TABLE IF EXISTS jobs;"); stmt.executeUpdate( - "CREATE TABLE jobs (jobid, time, positionen_name, positionen_value, positionen_cat, state, jobvalue);"); + "CREATE TABLE jobs (jobid, time, positionen_quantity, positionen_name, positionen_value, positionen_cat, state, jobvalue);"); } catch (SQLException e) { System.err.println("Couldn't handle DB-Query"); e.printStackTrace(); } } - - public int getLatestJobNumber_Job() { - + + public int getLatestJobNumber_Job() + { + try { Statement stmt = connection.createStatement(); ResultSet rs = stmt.executeQuery( "SELECT jobid from jobs ORDER BY jobid DESC LIMIT 1;"); return rs.getInt("jobid"); } catch (SQLException e) { - //System.err.println("Couldn't handle DB-Query"); - //e.printStackTrace(); - + // System.err.println("Couldn't handle DB-Query"); + // e.printStackTrace(); + } - System.out.println("XXXXXXXXXXX 1 XXXXXXXXXXX"); - return 1; - + System.out.println("XXXXXXXXXXX 0 XXXXXXXXXXX"); + return 0; + + } + + public void fillJobs_Jobs(int pID, String pTime, String pPositionen_quantity, String pPositionen_name, + String pPositionen_value, String pPositionen_cat, String pState, + String pJobvalue) + { + + System.out.println("Create new Job Entry"); + try { + PreparedStatement ps = connection + .prepareStatement("INSERT INTO jobs VALUES (?, ?, ?, ?, ?, ?, ?, ?);"); + ps.setInt(1, pID); // primary + ps.setString(2, pTime); + ps.setString(3, pPositionen_quantity); + ps.setString(4, pPositionen_name); + ps.setString(5, pPositionen_value); + ps.setString(6, pPositionen_cat); + ps.setString(7, pState); + ps.setString(8, pJobvalue); + ps.addBatch(); + connection.setAutoCommit(false); + ps.executeBatch(); // SQL execution + connection.setAutoCommit(true); + } catch (SQLException e) { + System.err.println("Couldn't handle DB-Query"); + e.printStackTrace(); + } + } } diff --git a/src/application/Job.java b/src/application/Job.java index ec35975..bfabb98 100644 --- a/src/application/Job.java +++ b/src/application/Job.java @@ -7,7 +7,7 @@ public class Job private int jobnumber; - private int jobvalue; + private float jobvalue; private String jobtime; @@ -19,30 +19,49 @@ public class Job private ArrayList positionenCat; - public Job(int pJobnumber) + public Job(int pJobnumber, String pTime) { this.jobnumber = pJobnumber; - System.out.println("Neuer Job: " + this.jobnumber); + this.jobtime = pTime; + + System.out + .println("Neuer Job: " + this.jobnumber + " Time: " + this.jobtime); positionenQuantity = new ArrayList(); positionenName = new ArrayList(); positionenValue = new ArrayList(); positionenCat = new ArrayList(); - //System.out.println("Größe: " + positionenName.size()); - + // System.out.println("Größe: " + positionenName.size()); + + } + + public int getJobnumber() + { + return this.jobnumber; + } + + public String getJobtime() + { + return this.jobtime; + } + + public float getJobValue() + { + + return this.jobvalue; } public void addPosition(String pPositionenName, float pPositionenValue, String pPositionenCat) { - //System.out.println("addName"); + // System.out.println("addName"); for (int i = 0; i < positionenName.size(); i++) { if (positionenName.get(i).equals(pPositionenName)) { // Item is already in list, increase quantity positionenQuantity.set(i, positionenQuantity.get(i) + 1); - //System.out.println("Item exists, increasing quantity"); + // System.out.println("Item exists, increasing quantity"); return; } } @@ -51,6 +70,8 @@ public class Job positionenValue.add(pPositionenValue); positionenCat.add(pPositionenCat); positionenQuantity.add(1); + + calcJobValue(); } public void printJobOnConsole() @@ -60,23 +81,92 @@ public class Job System.out.println("JobNummer: " + jobnumber); System.out.println("---------------------------------------------"); - //System.out.println("Größe: " + positionenName.size()); - + // System.out.println("Größe: " + positionenName.size()); + for (int i = 0; i < positionenName.size(); i++) { System.out.println( positionenQuantity.get(i) + " " + positionenName.get(i) + " " + positionenValue.get(i) + " " + positionenCat.get(i)); -/* - System.out.println("i is: " + i); - System.out.println(positionenName.get(i)); - System.out.println(positionenQuantity.get(i)); - */ - + /* + * System.out.println("i is: " + i); + * System.out.println(positionenName.get(i)); + * System.out.println(positionenQuantity.get(i)); + */ + } System.out.println("---------------------------------------------"); } + public ArrayList getCurrentJobPositionen() + { + + ArrayList jobitems = new ArrayList(); + + for (int i = 0; i < positionenName.size(); i++) { + + tableDataCurrentOrder tmp = new tableDataCurrentOrder( + positionenName.get(i), positionenQuantity.get(i)); + + jobitems.add(tmp); + + } + + return jobitems; + } + + private void calcJobValue() + { + + for (int i = 0; i < positionenValue.size(); i++) { + + jobvalue = jobvalue + + (positionenQuantity.get(i) * positionenValue.get(i)); + + } + + } + + public String createPosQuantityDBString() + { + String tmp = ""; + for (int i = 0; i < positionenName.size(); i++) { + + tmp = tmp + ";" + positionenQuantity.get(i); + } + return tmp; + } + + public String createPosNameDBString() + { + String tmp = ""; + for (int i = 0; i < positionenName.size(); i++) { + + tmp = tmp + ";" + positionenName.get(i); + } + return tmp; + } + + public String createPosValueDBString() + { + String tmp = ""; + for (int i = 0; i < positionenName.size(); i++) { + + tmp = tmp + ";" + positionenValue.get(i); + } + return tmp; + } + + public String createPosCatDBString() + { + String tmp = ""; + for (int i = 0; i < positionenName.size(); i++) { + + tmp = tmp + ";" + positionenCat.get(i); + } + return tmp; + } + } diff --git a/src/application/Main.java b/src/application/Main.java index 52d4ce2..69131b2 100644 --- a/src/application/Main.java +++ b/src/application/Main.java @@ -79,7 +79,7 @@ public class Main extends Application mwc.fillCategory(); mwc.loadGridButtons(); mwc.getSelectedCat(); //Load DB entries in Chois Box - mwc.createFirstJob(); + mwc.createNewJob(); } else { // config.xml NOT found, first start of app System.out.println("keine XML gefunden!"); diff --git a/src/application/MainWindowController.java b/src/application/MainWindowController.java index 790808d..02f53c4 100644 --- a/src/application/MainWindowController.java +++ b/src/application/MainWindowController.java @@ -320,7 +320,7 @@ public class MainWindowController private String databaseName; private boolean lockState = false; - + private boolean isPrintBtnDisabled = true; private Job currentJob = null; @@ -330,7 +330,8 @@ public class MainWindowController new tableDataCurrentOrder("0", 0)); @FXML - TreeItem rootJobs = new TreeItem<>(new tableDataCurrentOrder("0", 0)); + TreeItem rootJobs = new TreeItem<>( + new tableDataCurrentOrder("0", 0)); @FXML TreeItem rootPositionen = new TreeItem<>( @@ -490,16 +491,27 @@ public class MainWindowController @FXML public void btnPrintBillAction(ActionEvent event) { - //System.out.println("Button!"); - + // System.out.println("Button!"); + currentJob.printJobOnConsole(); currentJob = null; - currentJob = new Job(dbc.getLatestJobNumber_Job()); - + createNewJob(); + btnPrintBill.setDisable(true); isPrintBtnDisabled = true; + + rootCurrentJob.getChildren().remove(0, + rootCurrentJob.getChildren().size()); + + dbc.fillJobs_Jobs(currentJob.getJobnumber(), currentJob.getJobtime(), + currentJob.createPosQuantityDBString(), + currentJob.createPosNameDBString(), + currentJob.createPosValueDBString(), + currentJob.createPosCatDBString(), "verbucht", + String.valueOf(currentJob.getJobValue())); + } @FXML @@ -675,12 +687,21 @@ public class MainWindowController public void initUI() { System.out.println("initUI"); + for (int i = 0; i < 25; i++) { + getButtonByID(i).setVisible(false); + } tftNewDBName.setText(getDatabaseName()); tftKat05.setDisable(true); btnPrintBill.setDisable(true); isPrintBtnDisabled = true; initPositionen(); initCurrentOrderTreeTableView(); + + + + + + } @@ -854,8 +875,16 @@ public class MainWindowController { DateFormat dateFormat = new SimpleDateFormat("HH:mm"); Date date = new Date(); - String heutigesDatum = dateFormat.format(date); - return heutigesDatum; + String time = dateFormat.format(date); + return time; + } + + public String getSystemDate() + { + DateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy"); + Date date = new Date(); + String dateStr = dateFormat.format(date); + return dateStr; } public void saveSettings(String databasename) throws Exception @@ -995,17 +1024,17 @@ public class MainWindowController btnCalcStats.setDisable(pState); btnClearEntry.setDisable(pState); btnDeleteSelectedPosition.setDisable(pState); - - if((!isPrintBtnDisabled) && (!pState)) { - // print was not disabled and will be enabled again - btnPrintBill.setDisable(pState); - } - - if(pState) { - // disable allways - btnPrintBill.setDisable(pState); - } - + + if ((!isPrintBtnDisabled) && (!pState)) { + // print was not disabled and will be enabled again + btnPrintBill.setDisable(pState); + } + + if (pState) { + // disable allways + btnPrintBill.setDisable(pState); + } + btnReprintJob.setDisable(pState); btnSaveEntry.setDisable(pState); btnCancelJob.setDisable(pState); @@ -1020,7 +1049,7 @@ public class MainWindowController catChoise.setDisable(pState); tableCurrentOrder.setDisable(pState); - //jobsTreeTable.setDisable(pState); + // jobsTreeTable.setDisable(pState); entryTreeTable.setDisable(pState); labelAllPrize.setVisible(!pState); @@ -1131,48 +1160,80 @@ public class MainWindowController btnPrintBill.setDisable(false); isPrintBtnDisabled = false; - - tableDataCurrentOrder test01 = new tableDataCurrentOrder("Test", 42); - - - rootCurrentJob.getChildren().remove(0, - rootCurrentJob.getChildren().size()); - - rootCurrentJob.getChildren().add(new TreeItem(test01)); - + + rootCurrentJob.getChildren().remove(0, + rootCurrentJob.getChildren().size()); + + for (int i = 0; i < currentJob.getCurrentJobPositionen().size(); i++) { + rootCurrentJob.getChildren().add(new TreeItem( + currentJob.getCurrentJobPositionen().get(i))); + } + } - - private void initCurrentOrderTreeTableView() { - - + + private void initCurrentOrderTreeTableView() + { + columnQuantity.setStyle("-fx-alignment: CENTER;"); columnPosition.setStyle("-fx-alignment: CENTER;"); tableCurrentOrder.setRoot(rootCurrentJob); tableCurrentOrder.setShowRoot(false); tableCurrentOrder.setEditable(false); - - columnQuantity.setCellValueFactory( - cellData -> cellData.getValue().getValue().quantityProperty().asObject()); + + columnQuantity.setCellValueFactory(cellData -> cellData.getValue() + .getValue().quantityProperty().asObject()); columnPosition.setCellValueFactory( cellData -> cellData.getValue().getValue().positionProperty()); - - - - - - - - - + + tableCurrentOrder.getSelectionModel().selectedItemProperty() + .addListener(new ChangeListener() { + @Override + public void changed(ObservableValue observable, Object oldVal, + Object newVal) + { + // last = selected; //for auto-play + int selected = tableCurrentOrder.getSelectionModel() + .getSelectedIndex(); // get selected item + + // String currentOrderPosNamr = + // columnPosition.getCellData(selected); // Ausgewählte + // Spalte + + System.out.println( + "CurrentOrder - Ausgewaehlte Spalte: " + selected); + + try { // Setzt den Inhalt in die Textfelder + + /* + * + * tftNewPosition + * .setText(dbc.getName_Positionen(idPositionen)); + * tftNewValue.setText(dbc.getValue_Positionen(idPositionen + * )); colorChoise.getSelectionModel().select( + * getColorID(dbc.getColor_Positionen(idPositionen))); + * + * catChoise.getSelectionModel().select( + * dbc.getCategoryNameFromPositionen(idPositionen)); + * + */ + + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + }); + } - - - public void createFirstJob() + public void createNewJob() { - currentJob = new Job(dbc.getLatestJobNumber_Job()); + currentJob = new Job(dbc.getLatestJobNumber_Job() + 1, + getSystemTime() + " " + getSystemDate()); + labelJobCounter.setText("Auftragsnummer: " + + String.valueOf(dbc.getLatestJobNumber_Job() + 1)); } public void updateTimeLabel()