Skip to content

Commit

Permalink
Finished table controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Björn committed Apr 7, 2018
1 parent 623e36e commit af9c548
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/main/java/controllers/StartController.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void initialize(URL location, ResourceBundle resources) {
if (database == null) {
try {
// TODO change database back to information_schema
database = new Database("slim_test", "localhost", 3306, "root", "");
database = new Database("bikehaendler", "localhost", 3306, "root", "");
} catch (SQLException e) {
this.error.setText("Initialization failed. Local database not found. Code 4");
e.printStackTrace();
Expand Down
95 changes: 82 additions & 13 deletions src/main/java/controllers/TableController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.sun.javafx.collections.ObservableListWrapper;
import exception.DatabaseNotFoundException;
import exception.QueryFailedException;
import exception.TableNotFoundException;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
Expand All @@ -28,6 +29,7 @@
import java.net.URL;
import java.util.*;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
* Class TableController
Expand Down Expand Up @@ -117,6 +119,11 @@ public void saveChanges() {
}
}

/**
* Insert values.
* @param values
* @return
*/
boolean insert(HashMap<String, String> values) {
Table t = getTable();
StringBuilder keys = new StringBuilder();
Expand Down Expand Up @@ -178,23 +185,38 @@ ValidationContext validate() {
*/
@FXML
public void deleteRecord() {
// TODO continue here
Object row = this.tableView.getSelectionModel().getSelectedItem();
ObservableListWrapper row = (ObservableListWrapper) this.tableView.getSelectionModel().getSelectedItem();
HashMap<String, String> selectors = new HashMap<>();
try {
this.getTable().getColumns().forEach((Column col) ->{
AtomicInteger counter = new AtomicInteger();
ArrayList<Column> columns = this.getTable().getColumns();
columns.forEach((Column col) -> {
if (col.isPrimary()) {
String name = col.getName();
String value = "NULL";
if (row.size() > counter.get()) {
value = (String) row.get(counter.get());
}

this.tableView.getColumns().forEach(o -> {
TableColumn column = (TableColumn)o;
String name = column.getText();
if (name.equalsIgnoreCase(col.getName())) {
selectors.put(name, value);
}
counter.getAndIncrement();
});

}
});
StringBuilder query = new StringBuilder(String.format("DELETE FROM %s WHERE ",this.getTable().getName()));
selectors.forEach((String rowName, String value)->{
if (value.equalsIgnoreCase("NULL")) {
query.append(String.format("%s IS NULL", rowName));
} else {
query.append(String.format("%s = %s AND", rowName, value));
}
});
} catch (QueryFailedException e) {
query.delete(query.length() - 4, query.length());
CRUDTable table = new CRUDTable();
table.update(query.toString());
query.delete(0, query.length());
this.initTableGUI();
} catch (QueryFailedException | TableNotFoundException | IOException e) {
e.printStackTrace();
}
}
Expand Down Expand Up @@ -243,6 +265,9 @@ public ObservableValue<String> call(TableColumn.CellDataFeatures<ObservableList,
// TODO this may cause some bugs because of the key (column name) => value may differ
if (j < rowValues.size()) {
cellValue = rowValues.get(j);
if (cellValue == null) {
cellValue = "NULL";
}
} else {
cellValue = "NULL";
}
Expand All @@ -258,6 +283,10 @@ public ObservableValue<String> call(TableColumn.CellDataFeatures<ObservableList,
populate(tableReference);
}

/**
* Populate table fields with the table reference object
* @param tableReference
*/
public void populate(Table tableReference) {
// get all data by row
ArrayList<Row> rows = null;
Expand Down Expand Up @@ -288,14 +317,20 @@ public void populate(Table tableReference) {
}

/**
* From http://java-buddy.blogspot.ch/2013/03/javafx-editable-tableview-with-dynamic.html
* Seen @ http://java-buddy.blogspot.ch/2013/03/javafx-editable-tableview-with-dynamic.html
*/
class EditingCell extends TableCell<XYChart.Data, String> {
private TextField textField;

/**
* Editing Cell event
*/
public EditingCell() {
}

/**
* Function to be called when starting to edit a cell
*/
@Override
public void startEdit() {
// Get the container to get all columns from the table reference
Expand Down Expand Up @@ -331,6 +366,9 @@ public void startEdit() {
textField.selectAll();
}

/**
* Method to call when editing a cell is cancelled.
*/
@Override
public void cancelEdit() {
super.cancelEdit();
Expand All @@ -339,6 +377,11 @@ public void cancelEdit() {
setContentDisplay(ContentDisplay.TEXT_ONLY);
}

/**
* Update an item (fully copied from reference)
* @param item
* @param empty
*/
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);

Expand All @@ -359,12 +402,18 @@ public void updateItem(String item, boolean empty) {
}
}

/**
* Create the textfield to edit (fully copied from reference)
*/
private void createTextField() {
textField = new TextField(getString());
textField.setMinWidth(this.getWidth() - this.getGraphicTextGap() * 2);
textField.setOnKeyPressed(new EventHandler<KeyEvent>() {


/**
* Handle incoming key event
* @param t
*/
@Override
public void handle(KeyEvent t) {
if (t.getCode() == KeyCode.ENTER && textField.getText() != null) {
Expand All @@ -374,6 +423,10 @@ public void handle(KeyEvent t) {
}
}

/**
* Save edited value.
* @param value
*/
void commitEdit(String value) {
int rowId = getTableRow().getIndex();

Expand All @@ -395,11 +448,19 @@ void commitEdit(String value) {
EditingCell.super.commitEdit(value);
}

/**
* Add value to insert queue (if type of action is an INSERT)
* @param key
* @param value
*/
void addForInsert(String key, String value) {
insertValues.put(key.toLowerCase(), value);
}


/**
* Create and execute update query for database.
* @param value
*/
void update(String value) {
HashMap<String, String> where = new HashMap<>();
int rowId = getTableRow().getIndex();
Expand Down Expand Up @@ -441,6 +502,10 @@ void update(String value) {
updateQuery.append(";");
}

/**
* Get all primary keys of the table.
* @return ArrayList<String> a list of the primary keys.
*/
ArrayList<String> getPrimaryKeys() {
final ArrayList<String> primaryKeys = new ArrayList<>();
try {
Expand All @@ -463,6 +528,10 @@ Table getTable() {
});
}

/**
* Get Item as string
* @return String
*/
private String getString() {
return getItem() == null ? "" : getItem().toString();
}
Expand Down

0 comments on commit af9c548

Please sign in to comment.