use of easik.database.types.Int in project fql by CategoricalData.
the class RowEntryDialog method verify.
/**
* Called when the user clicks the OK button. If the input for a given field
* is deemed valid, it is added to the input map. If any input is invalid.
* The whole input map is set to null and the method returns false.
*
* @return true if all inputs check out, false otherwise.
*/
@Override
public boolean verify() {
input = new HashSet<>(10);
JComponent inputComponent;
String fieldInput;
String column;
for (final DialogRow row : dialogRows) {
column = row.columnName;
inputComponent = row.input;
// valid and is an INT
if (foreignKeys.containsKey(column)) {
fieldInput = ((JTextField) inputComponent).getText();
input.add(new ColumnEntry(column, fieldInput, new Int()));
} else /*
* else if (inputComponent instanceof JDateChooser) { // grab input
* from date chooser - null -> bad input final String date =
* Long.toString(((JDateChooser)
* inputComponent).getDate().getTime());
*
* if (date == null) { // bad input
* JOptionPane.showMessageDialog(this,
* "<html>Invalid date selected for <b>" + column + "</b>", column +
* ": Invalid input", JOptionPane.ERROR_MESSAGE);
*
* input = null;
*
* return false; }
*
* input.add(new ColumnEntry(column, date, row.type)); }
*/
if (inputComponent instanceof TimeChooser) {
final TimeChooser t = (TimeChooser) inputComponent;
input.add(new ColumnEntry(column, Long.toString(t.getMillis()), row.type));
} else /*
* else if (inputComponent instanceof DateTimeChooser) { final
* DateTimeChooser dtc = (DateTimeChooser) inputComponent; final
* Date date = dtc.dateChooser.getDate();
*
* if (date == null) { // bad input
* JOptionPane.showMessageDialog(this,
* "<html>Invalid date/time for <b>" + column + "</b>", column +
* ": Invalid input", JOptionPane.ERROR_MESSAGE);
*
* input = null;
*
* return false; }
*
* final long dateTime = dtc.getMillis();
*
* input.add(new ColumnEntry(column, Long.toString(dateTime),
* row.type)); }
*/
{
fieldInput = ((JTextField) inputComponent).getText();
// validate the input
if ("".equals(fieldInput) || row.type.verifyInput(fieldInput)) {
input.add(new ColumnEntry(column, fieldInput, row.type));
} else {
// otherwise we have bad input
JOptionPane.showMessageDialog(this, "<html>Invalid input '" + fieldInput + "' for <b>" + column + "</b> (" + row.type + ')', column + ": Invalid input", JOptionPane.ERROR_MESSAGE);
input = null;
return false;
}
}
}
return true;
}
use of easik.database.types.Int in project fql by CategoricalData.
the class JDBCUpdateMonitor method insert.
/**
* Determines if insertion into a given table requires special handling due
* to constraints it may be in. As of now, special cases that may result
* from being in multiple constraints are not supported.
*
* @param table
* The table into which we wish to insert data
* @return Success of the insertion
*/
@Override
public boolean insert(final EntityNode table) {
final DialogOptions dOpts = getDialogOptions(table);
final String lineSep = EasikTools.systemLineSeparator();
// a set of column-value pairs of which we wish to force a specific
// value, leaving the user out
final Set<ColumnEntry> forced = new HashSet<>(10);
// contstraint. Tighten up?
for (final ModelConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> c : table.getConstraints()) {
if (c instanceof SumConstraint) {
// of its foreign key, so remove it from the dialog's selection
for (final ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> sp : c.getPaths()) {
if (sp.getDomain() == table) {
// we force the value 0 to avoid out driver to kick back
// an error for having a null fKey
final String columnName = cn.tableFK(sp.getFirstEdge());
dOpts.fKeys.remove(columnName);
forced.add(new ColumnEntry(columnName, "0", new Int()));
break;
}
}
}
if (c instanceof CommutativeDiagram) {
// commute
if (c.getPaths().get(0).getDomain() == table) {
JOptionPane.showMessageDialog(null, "Be sure that the following paths commute:" + lineSep + EasikTools.join(lineSep, c.getPaths()), "Commutative diagram", JOptionPane.INFORMATION_MESSAGE);
try {
return promptAndInsert(table, dOpts, forced);
} catch (SQLException e) {
/*
* if(e instanceof com.mysql.jdbc.exceptions.jdbc4.
* MySQLIntegrityConstraintViolationException){
* //injective property violated
* JOptionPane.showMessageDialog(null, e.getMessage(),
* "Injective property violation",
* JOptionPane.ERROR_MESSAGE); }else{
*/
JOptionPane.showMessageDialog(null, "Not all of the following paths commute -- insert aborted!" + lineSep + EasikTools.join(lineSep, c.getPaths()), "Commutative diagram failure", JOptionPane.ERROR_MESSAGE);
// }
}
}
}
if (c instanceof PullbackConstraint) {
// happens, we want to let the user update the new record
if (((PullbackConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>) c).getTarget() != table) {
final EntityNode pullback = ((PullbackConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>) c).getSource();
try {
// get row count pre-insert
ResultSet result = dbd.executeQuery("SELECT COUNT(*) FROM " + pullback.getName() + " X");
result.next();
final int preRowCount = result.getInt(1);
if (!promptAndInsert(table, dOpts)) {
return false;
}
// get row count post-insert
result = dbd.executeQuery("SELECT COUNT(*) FROM " + pullback.getName() + " X");
result.next();
final int postRowCount = result.getInt(1);
// new row (the one with the highest primary ID)
if (postRowCount > preRowCount) {
result = dbd.executeQuery("SELECT MAX(" + cn.tablePK(pullback) + ") FROM " + pullback.getName() + " X");
result.next();
final int pk = result.getInt(1);
if (JOptionPane.showConfirmDialog(null, "New record in pullback table '" + pullback.getName() + "'. Enter column data?", "Insert column data?", JOptionPane.YES_NO_OPTION) == 0) {
updateRow(pullback, pk);
}
}
return true;
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Could not execute update. MYSQL Error output:\n" + e.getMessage());
}
}
}
if (c instanceof ProductConstraint) {
// inserting into the product.
for (final ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> sp : c.getPaths()) {
if (sp.getCoDomain() == table) {
final EntityNode product = sp.getDomain();
try {
if (!promptAndInsert(table, dOpts)) {
return false;
}
// get the new records from the product. They are
// any record who's fk to our INSERT factor matches
// the primary id of the last insert
ResultSet result = dbd.executeQuery("SELECT MAX(" + cn.tablePK(table) + ") FROM " + table.getName() + " X");
result.next();
final int newPK = result.getInt(1);
result = dbd.executeQuery("SELECT * FROM " + product.getName() + " WHERE " + cn.tableFK(sp.getFirstEdge()) + " = " + newPK);
// get count of new rows as result of INSERT
result.last();
final int newRows = result.getRow();
result.beforeFirst();
if ((newRows > 0) && (JOptionPane.showConfirmDialog(null, newRows + " new rows in product table '" + product.getName() + "'. Insert column data?", "Insert column data?", JOptionPane.YES_NO_OPTION) == 0)) {
while (result.next()) {
updateRow(product, result.getInt(1));
}
}
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}
return true;
}
}
}
if (c instanceof LimitConstraint) {
// TRIANGLES TODO CF2012 Incomplete
}
}
try {
return promptAndInsert(table, dOpts, forced);
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Could not execute update. MYSQL Error output:\n" + e.getMessage());
System.err.println(e.getMessage());
return false;
}
}
Aggregations