use of com.kyj.fx.voeditor.visual.component.grid.EditableTableView.ColumnExpression in project Gargoyle by callakrsos.
the class EditableTableViewExam method start.
@Override
public void start(Stage primaryStage) throws Exception {
Button btnExec = new Button("Exec.");
Button btnAdd = new Button("Add ");
Button btnRemove = new Button("Remove");
Button btnSave = new Button("Save");
btnAdd.setDisable(true);
TextField textField = new TextField();
HBox hBox = new HBox(5, textField, btnExec, btnAdd, btnRemove, btnSave);
EditableTableView editableTableView = new EditableTableView(new Supplier<Connection>() {
@Override
public Connection get() {
try {
return DbUtil.getConnection();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
});
editableTableView.tableNameProperty().addListener((oba, oldval, newval) -> {
btnAdd.setDisable(false);
});
// editableTableView.execute("tbm_sm_realtime_search");
btnExec.setOnAction(e -> {
String tableName = textField.getText();
try {
editableTableView.readByTableName("select * from " + tableName, tableName);
} catch (Exception e1) {
e1.printStackTrace();
}
});
btnAdd.setOnAction(ev -> {
editableTableView.getItems().add(new HashMap<>());
editableTableView.getSelectionModel().selectLast();
editableTableView.scrollTo(editableTableView.getItems().size() - 1);
});
btnRemove.setOnAction(ev -> {
editableTableView.getItems().removeAll(editableTableView.getSelectionModel().getSelectedItems());
});
btnSave.setOnAction(ev -> {
try {
editableTableView.save();
} catch (Exception e1) {
e1.printStackTrace();
}
});
editableTableView.setOnMouseClicked(ev -> {
Map<ColumnExpression, ObjectProperty<ValueExpression>> selectedItem = editableTableView.getSelectionModel().getSelectedItem();
System.out.println(selectedItem);
});
BorderPane root = new BorderPane(editableTableView);
root.setTop(hBox);
Scene value = new Scene(root);
// ".table-row{ -fx-background-color: red; }"
// value.getStylesheets().add(EditableTableViewExam.class.getResource("EditableTableViewExam.css").toExternalForm());
primaryStage.setScene(value);
primaryStage.show();
}
use of com.kyj.fx.voeditor.visual.component.grid.EditableTableView.ColumnExpression in project Gargoyle by callakrsos.
the class EditableTableView method getStatements.
private STATUS getStatements(DML dml, BiFunction<STATUS, List<String>, STATUS> function) {
List<String> saveSqlList = new ArrayList<>();
List<Map<ColumnExpression, ObjectProperty<ValueExpression>>> items = Collections.emptyList();
switch(dml) {
case UPDATE:
items = getItems().stream().filter(m -> {
return !ColumnExpression.isContainsNewRowMeata(m);
}).collect(Collectors.toList());
break;
case DELETE:
items = this.removedList;
break;
case INSERT:
items = getItems().stream().filter(m -> {
return ColumnExpression.isContainsNewRowMeata(m);
}).collect(Collectors.toList());
break;
default:
break;
}
if (items.isEmpty())
return STATUS.OK;
int modifiedCnt = 0;
//키값이 존재하지않는 테이블인경우 where조건은 수정되지않는 컬럼으로 사용.
boolean isNotContainsPrimaryTable = false;
//키값도 존재하지않을뿐더러 변경된 컬럼도 없는경우
boolean isNotContainsModifiedTable = false;
if (items != null && !items.isEmpty()) {
long count = items.get(0).values().stream().filter(vo -> vo.getValue().isPrimaryKey).count();
//키값 존재유무에 따른 플래그 update
if (count == 0)
isNotContainsPrimaryTable = true;
//키값이 없는 상태에서 수정된 컬럼도 없는 경우 상태 update
if (count == 0) {
long modifiedCount = items.get(0).values().stream().filter(vo -> vo.getValue().isModified).count();
if (modifiedCount == 0)
isNotContainsModifiedTable = true;
}
}
for (Map<ColumnExpression, ObjectProperty<ValueExpression>> m : items) {
StringBuffer whereStatement = new StringBuffer();
StringBuffer setStatement = new StringBuffer();
StringBuffer columnsStatement = new StringBuffer();
StringBuffer valuesStatement = new StringBuffer();
Iterator<ColumnExpression> iterator = m.keySet().iterator();
modifiedCnt = 0;
while (iterator.hasNext()) {
ColumnExpression colummExp = iterator.next();
if (colummExp.isNewRowMeta())
continue;
if (colummExp.isMetadata)
continue;
// else {
ObjectProperty<ValueExpression> value = m.get(colummExp);
ValueExpression valueExp = value.get();
if (valueExp.isPrimaryKey) {
//기본키값이 빈 경우 메세지.
if (valueExp.getDisplayText() == null) /*|| valueExp.getDisplayText().isEmpty()*/
{
return function.apply(STATUS.PK_VAL_IS_EMPTY, Collections.emptyList());
}
whereStatement.append(String.format("and %s = '%s'", colummExp, valueExp.getRealValue()));
}
if (valueExp.isModified) {
setStatement.append(String.format("%s = %s,", colummExp, getValue(colummExp, valueExp)));
modifiedCnt++;
if (isNotContainsPrimaryTable) {
whereStatement.append(String.format("and %s = '%s'", colummExp, valueExp.getRealValue()));
}
}
if ((!valueExp.isPrimaryKey && !valueExp.isModified) && (isNotContainsPrimaryTable || isNotContainsModifiedTable)) {
whereStatement.append(String.format("and %s = '%s'", colummExp, valueExp.getRealValue()));
}
columnsStatement.append(String.format("%s,", colummExp));
valuesStatement.append(String.format("'%s',", valueExp.getDisplayText()));
// }
}
int setStatementLength = setStatement.length();
if (setStatementLength != 0) {
setStatement.setLength(setStatementLength - 1);
}
int columnsStatementLength = columnsStatement.length();
if (columnsStatementLength != 0) {
columnsStatement.setLength(columnsStatementLength - 1);
}
int valuesStatementLength = valuesStatement.length();
if (valuesStatementLength != 0) {
valuesStatement.setLength(valuesStatementLength - 1);
}
switch(dml) {
case INSERT:
saveSqlList.add(String.format("insert into %s (%s) values (%s)", this.tableName.get(), columnsStatement.toString(), valuesStatement.toString()));
break;
case UPDATE:
if (modifiedCnt > 0) {
saveSqlList.add(String.format("update %s set %s where 1=1 %s", this.tableName.get(), setStatement.toString(), whereStatement.toString()));
}
break;
case DELETE:
saveSqlList.add(String.format("delete from %s where 1=1 %s", this.tableName.get(), whereStatement.toString()));
break;
}
}
return function.apply(STATUS.OK, saveSqlList);
}
use of com.kyj.fx.voeditor.visual.component.grid.EditableTableView.ColumnExpression in project Gargoyle by callakrsos.
the class EditableTableView method readByTableName.
private void readByTableName(String sql, String tableName, boolean appendHist) throws Exception {
LOGGER.debug(sql);
getColumns().clear();
getItems().clear();
removedList.clear();
columnMap.clear();
try (Connection connection = connectionSupplier.get()) {
List<String> pks = DbUtil.pks(connection, tableName);
Map<String, Boolean> columnsToMap = DbUtil.columnsToMap(connection, tableName, rs -> {
try {
return rs.getString(4);
} catch (Exception e) {
return null;
}
}, rs -> {
try {
return Boolean.valueOf("YES".equals(rs.getString(18)));
} catch (Exception e) {
return false;
}
});
LOGGER.debug("nullable columns ? {} ", columnsToMap);
DbUtil.select(connection, sql, FETCH_COUNT, LIMIT_ROW_COUNT, new BiFunction<ResultSetMetaData, ResultSet, List<Map<String, Object>>>() {
@Override
public List<Map<String, Object>> apply(ResultSetMetaData t, ResultSet u) {
try {
int columnCount = t.getColumnCount();
for (int i = 1; i <= columnCount; i++) {
String columnName = t.getColumnName(i);
ColumnExpression columnExp = new ColumnExpression(columnName);
columnExp.isPrimaryColumn = pks.contains(columnName);
columnExp.isNullableColumn = columnsToMap.containsKey(columnName) && (columnsToMap.get(columnName) == true);
columnExp.setColumnType(t.getColumnType(i));
TableColumn<Map<ColumnExpression, ObjectProperty<ValueExpression>>, ValueExpression> e = new TableColumn<>(columnName);
e.setUserData(columnExp);
e.setCellValueFactory(DynamicCallback.fromTableColumn(columnExp));
e.setCellFactory(DEFAULT_CELL_FACTORY);
e.setEditable(true);
columnMap.put(columnName, columnExp);
getColumns().add(e);
}
while (u.next()) {
Map<ColumnExpression, ObjectProperty<ValueExpression>> hashMap = new HashMap<>();
for (int i = 1; i <= columnCount; i++) {
String columnName = t.getColumnName(i);
//new ColumnExpression(columnName);
ColumnExpression columnExp = columnMap.get(columnName);
ValueExpression valueExp = new ValueExpression();
valueExp.displayText.set(u.getString(columnName));
valueExp.isPrimaryKey = pks.contains(columnName);
valueExp.realValue.set(u.getObject(columnName));
valueExp.setColumnExpression(columnExp);
hashMap.put(columnExp, new SimpleObjectProperty<>(valueExp));
}
getItems().removeListener(itemChangeListener);
getItems().add(hashMap);
getItems().addListener(itemChangeListener);
}
} catch (SQLException e) {
LOGGER.error(ValueUtil.toString(e));
}
return null;
}
});
if (appendHist) {
if (history.size() >= HISTORY_LIMITED_SIZE) {
history.removeFirst();
}
history.add(sql);
}
this.tableName.set(tableName);
}
}
Aggregations