use of easik.sketch.vertex.EntityNode in project fql by CategoricalData.
the class JDBCExporter method createConstraints.
/**
* Generates db constraints from the sketch constraints. This method is not
* generally overridden by drivers: the individual createConstraint(...)
* methods are called for each existing constraint.
*
* @return list of queries to create the constraints.
*/
protected List<String> createConstraints() {
final List<String> constraintSQL = new LinkedList<>();
final List<ModelConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>> constraints = new ArrayList<>(sketch.getConstraints().values());
int id = 0;
for (final ModelConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> c : constraints) {
id++;
if (c instanceof CommutativeDiagram) {
constraintSQL.addAll(createConstraint((CommutativeDiagram<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>) c, String.valueOf(id)));
} else if (c instanceof ProductConstraint) {
constraintSQL.addAll(createConstraint((ProductConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>) c, String.valueOf(id)));
} else if (c instanceof PullbackConstraint) {
constraintSQL.addAll(createConstraint((PullbackConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>) c, String.valueOf(id)));
} else if (c instanceof EqualizerConstraint) {
constraintSQL.addAll(createConstraint((EqualizerConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>) c, String.valueOf(id)));
} else if (c instanceof SumConstraint) {
constraintSQL.addAll(createConstraint((SumConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>) c, String.valueOf(id)));
} else if (c instanceof LimitConstraint) {
constraintSQL.addAll(createConstraint((LimitConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>) c, String.valueOf(id)));
} else {
System.err.println("Unknown constraint type encountered: " + c.getClass());
}
}
return constraintSQL;
}
use of easik.sketch.vertex.EntityNode in project fql by CategoricalData.
the class JDBCExporter method createTables.
/**
* Recursive method to create table definitions. Each iteration exhaustively
* determines and generates all unconnected EntityNodes, then, if any
* EntityNodes remain, arbitrarily removes one (deferring creation of its
* references), and recurses, until all tables have been created.
*
* @param tables
* the sql for the tables
* @param entities
* sketch entities to create
* @param created
* sketch entites already created
*/
private void createTables(final List<String> tables, final Set<EntityNode> entities, final Set<EntityNode> created) {
NON_EMPTY: while (!entities.isEmpty()) {
// entities
ENTITIES: for (final EntityNode n : entities) {
for (final SketchEdge e : n.getOutgoingEdges()) {
final EntityNode target = e.getTargetEntity();
// vertex (for now)
if (!created.contains(target)) {
continue ENTITIES;
}
}
// Also check shadow edges:
/**
* Removing all traces of shadow edges
*
* for (final SketchEdge e : n.getShadowEdges()) { final
* EntityNode target = e.getTargetEntity();
*
* // if an EntityNode target isn't already created, skip this
* vertex (for now) if (!created.contains(target)) { continue
* ENTITIES; } }
*/
// If we got here, all the entity's targets exist, so we can go
// ahead
// with the table creation.
tables.addAll(createTable(n, true));
entities.remove(n);
created.add(n);
// Restart the non-empty loop, since we've
continue NON_EMPTY;
// changed entities.
}
// or (even simpler), a self-reference (A -> A).
break;
}
if (!entities.isEmpty()) {
final EntityNode someNode = entities.iterator().next();
tables.addAll(createTable(someNode, false));
entities.remove(someNode);
created.add(someNode);
// Recurse, creating the rest of the tables
createTables(tables, entities, created);
// When the remaining tables are created, we are able to create the
// reference:
tables.addAll(createReferences(someNode));
}
}
use of easik.sketch.vertex.EntityNode in project fql by CategoricalData.
the class SketchFrame method buildMenu.
/**
* Builds the menu
*/
private void buildMenu() {
final JMenuBar mainMenu;
final JMenu menuFile;
final JMenu menuHelp;
mainMenu = new JMenuBar();
// Make the File Menu
menuFile = new JMenu("File");
mainMenu.add(menuFile);
menuFile.setMnemonic(KeyEvent.VK_F);
final JMenu exportMenu = new JMenu("Export to");
menuFile.add(exportMenu);
// Export to server:
addMenuItem(exportMenu, new JMenuItem(new ExportDatabaseAction(this, _ourSketch)), KeyEvent.VK_D);
// Export to SQL text dump:
addMenuItem(exportMenu, new JMenuItem(new ExportFileAction(this, _ourSketch)), KeyEvent.VK_F);
addMenuItem(exportMenu, new JMenuItem(new ExportImageAction<>(this)), null);
addMenuItem(exportMenu, new JMenuItem(new SketchExportAction(this)), null);
menuFile.addSeparator();
addMenuItem(menuFile, new JMenuItem(new DocumentInfoAction(this)), KeyEvent.VK_I);
menuFile.addSeparator();
addMenuItem(menuFile, new JMenuItem(new FileQuitAction(this)), KeyEvent.VK_W);
// Make the Edit menu for "edit mode"
menuEditEdit = new JMenu("Edit");
mainMenu.add(menuEditEdit);
addMenuItem(menuEditEdit, _UndoItem = new JMenuItem(new UndoAction(_ourSketch)), KeyEvent.VK_Z);
addMenuItem(menuEditEdit, _RedoItem = new JMenuItem(new RedoAction(_ourSketch)), KeyEvent.VK_Y);
menuEditEdit.addSeparator();
menuEditEdit.add(menuEditAdd = new JMenu("Add"));
addMenuItem(menuEditAdd, _AddEntityItem = new JMenuItem(new NewEntityAction<>(null, this)), null);
menuEditAdd.add(menuEditAddEdge = new JMenu("Add edge"));
addMenuItem(menuEditAddEdge, addEdge = new JMenuItem(new NewSketchEdgeAction(this, Edge.NORMAL)), null);
addMenuItem(menuEditAddEdge, addInjEdge = new JMenuItem(new NewSketchEdgeAction(this, Edge.INJECTIVE)), null);
addMenuItem(menuEditAddEdge, addParEdge = new JMenuItem(new NewSketchEdgeAction(this, Edge.PARTIAL)), null);
addMenuItem(menuEditAddEdge, addSelfParEdge = new JMenuItem(new NewSketchEdgeAction(this, Edge.SELF)), null);
addMenuItem(menuEditAdd, addAtt = new JMenuItem(new AddAttributeAction<>(this)), null);
addMenuItem(menuEditAdd, addUK = new JMenuItem(new AddUniqueKeyAction<>(this)), null);
addMenuItem(menuEditEdit, del = new JMenuItem(new DeleteFromSketchAction(this)), null);
addMenuItem(menuEditEdit, rename = new JMenuItem(new RenameInSketchAction(this)), KeyEvent.VK_R);
menuEditEdit.add(new JSeparator());
addMenuItem(menuEditEdit, editEdge = new JMenuItem(new EditSketchEdgeAction(this)), KeyEvent.VK_E);
menuEditEdit.add(new JSeparator());
// If
addMenuItem(menuEditEdit, _ProgramSettingsItem = new JMenuItem("Preferences"), KeyEvent.VK_P);
// you
// change
// this
// title,
// update
// OSX.java
_ProgramSettingsItem.setToolTipText("Set global EASIK preferences");
_ProgramSettingsItem.addActionListener(new ActionListener() {
@Override
public void actionPerformed(final ActionEvent e1) {
final ProgramSettingsUI myUI = new ProgramSettingsUI(SketchFrame.this);
myUI.showDialog();
}
});
// add listener to enable appropriate menu items
menuEditEdit.addMenuListener(new MenuListener() {
@Override
public void menuCanceled(final MenuEvent e) {
}
@Override
public void menuDeselected(final MenuEvent e) {
}
@Override
public void menuSelected(final MenuEvent e) {
final Object[] selection = _ourSketch.getSelectionCells();
final SketchGraphModel model = _ourSketch.getGraphModel();
final boolean undoable = model.canUndo() && !_ourSketch.getDatabase().hasConnection() && !_ourSketch.isSynced();
final boolean redoable = model.canRedo() && !_ourSketch.getDatabase().hasConnection() && !_ourSketch.isSynced();
_UndoItem.setEnabled(undoable);
_RedoItem.setEnabled(redoable);
menuEditAdd.setEnabled(true);
menuEditAddEdge.setEnabled(true);
_AddEntityItem.setEnabled(false);
addEdge.setEnabled(false);
addInjEdge.setEnabled(false);
addParEdge.setEnabled(false);
addSelfParEdge.setEnabled(false);
addAtt.setEnabled(false);
addUK.setEnabled(false);
rename.setEnabled(false);
del.setEnabled(false);
editEdge.setEnabled(false);
if (selection.length == 0) {
_AddEntityItem.setEnabled(true);
menuEditAddEdge.setEnabled(false);
}
if (selection.length >= 1) {
del.setEnabled(true);
if (selection.length == 1) {
if (selection[0] instanceof EntityNode) {
rename.setEnabled(true);
}
if (selection[0] instanceof EntityNode) {
addSelfParEdge.setEnabled(true);
addAtt.setEnabled(true);
addUK.setEnabled(true);
} else if (selection[0] instanceof SketchEdge) {
editEdge.setEnabled(true);
}
}
// if selection contains an edge, dissable edit|add...
for (int i = 0; i < selection.length; i++) {
if (selection[i] instanceof SketchEdge) {
menuEditAdd.setEnabled(false);
}
}
if (selection.length >= 3) {
menuEditAdd.setEnabled(false);
}
}
if ((selection.length == 2) && (selection[0] instanceof EntityNode) && (selection[1] instanceof EntityNode)) {
addEdge.setEnabled(true);
addInjEdge.setEnabled(true);
addParEdge.setEnabled(true);
}
}
});
// Make the Edit menu for "manipulate mode"
@SuppressWarnings("unused") final JMenuItem updateRowMenuItem;
@SuppressWarnings("unused") final JMenuItem viewDataMenuItem;
menuEditManip = new JMenu("Edit");
mainMenu.add(menuEditManip);
menuEditManip.add(addRowMenuItem = new JMenuItem(new AddRowAction(_ourSketch)));
menuEditManip.add(insertQueryMenuItem = new JMenuItem(new ExecPreparedInsertAction(_ourSketch)));
menuEditManip.addSeparator();
menuEditManip.add(updateRowMenuItem = new JMenuItem(new UpdateRowAction(_ourSketch)));
menuEditManip.addSeparator();
menuEditManip.add(deleteRowMenuItem = new JMenuItem(new DeleteRowAction(_ourSketch)));
menuEditManip.add(deleteQueryMenuItem = new JMenuItem(new ExecPreparedDeleteAction(_ourSketch)));
menuEditManip.addSeparator();
menuEditManip.add(viewDataMenuItem = new JMenuItem(new ViewDataAction(_ourSketch)));
menuEditManip.addMenuListener(new MenuListener() {
@Override
public void menuCanceled(final MenuEvent e) {
}
@Override
public void menuDeselected(final MenuEvent e) {
}
@Override
public void menuSelected(final MenuEvent e) {
final Object[] selection = _ourSketch.getSelectionCells();
final boolean enable = (selection.length == 1) && (selection[0] instanceof EntityNode);
for (final Component c : menuEditManip.getMenuComponents()) {
c.setEnabled(enable);
}
if (!enable) {
return;
}
if (!_ourSketch.editable((EntityNode) selection[0])) {
addRowMenuItem.setEnabled(false);
insertQueryMenuItem.setEnabled(false);
deleteRowMenuItem.setEnabled(false);
deleteQueryMenuItem.setEnabled(false);
}
}
});
menuView = new JMenu("View");
mainMenu.add(menuView);
menuView.add(_ShowAttributesToggle = new JCheckBoxMenuItem("Attributes/keys visible"));
_ShowAttributesToggle.setToolTipText("Toggle display of attributes and unique keys");
_ShowAttributesToggle.setState("show".equals(Easik.getInstance().getSettings().getProperty("attrib_display", "show")));
_ShowAttributesToggle.addActionListener(new ActionListener() {
@Override
public void actionPerformed(final ActionEvent e1) {
_ourSketch.refresh();
}
});
menuView.add(_ShowConstraintToggle = new JCheckBoxMenuItem("Constraints visible"));
_ShowConstraintToggle.setToolTipText("Toggle display of constraints");
_ShowConstraintToggle.setState(true);
_ShowConstraintToggle.addActionListener(new ActionListener() {
@Override
public void actionPerformed(final ActionEvent e1) {
ModelConstraint.setAllConstraintsVisible(_ourSketch.getConstraints(), _ShowConstraintToggle.getState());
}
});
// Create constraints menu
menuConstraint = new JMenu("Constraints");
menuConstraint.setMnemonic(KeyEvent.VK_C);
mainMenu.add(menuConstraint);
addMenuItem(menuConstraint, _AddCommutativeDiagramMenuItem = new JMenuItem(new AddCommutativeDiagramAction<>(this)), null);
addMenuItem(menuConstraint, _AddSumMenuItem = new JMenuItem(new AddSumConstraintAction<>(this)), null);
addMenuItem(menuConstraint, _AddProductMenuItem = new JMenuItem(new AddProductConstraintAction<>(this)), null);
addMenuItem(menuConstraint, _AddPullbackMenuItem = new JMenuItem(new AddPullbackConstraintAction<>(this)), null);
addMenuItem(menuConstraint, _AddEqualizerMenuItem = new JMenuItem(new AddEqualizerConstraintAction(this)), null);
// addMenuItem(menuConstraint, _AddLimMenuItem = new JMenuItem(new
// AddLimitConstraintAction<SketchFrame,
// SketchGraphModel,Sketch,EntityNode, SketchEdge>(this)), null);
// Make SQL connection menu
menuSQL = new JMenu("SQL Connection");
menuSQL.setMnemonic(KeyEvent.VK_S);
mainMenu.add(menuSQL);
addMenuItem(menuSQL, new JMenuItem(new DisconnectAction(_ourSketch)), null);
// DTRIG
addMenuItem(menuSQL, new JMenuItem(new OverrideConstraintAction(_ourSketch)), null);
// CF2012
// Create help menu
menuHelp = new JMenu("Help");
mainMenu.add(menuHelp);
menuHelp.setMnemonic(KeyEvent.VK_H);
menuHelp.add(new HelpAction());
menuHelp.add(new AboutAction());
setJMenuBar(mainMenu);
}
use of easik.sketch.vertex.EntityNode in project fql by CategoricalData.
the class ExecPreparedDeleteAction method actionPerformed.
/**
* Create the new entity and set up its name
*
* @param e
* The action event
*/
@Override
public void actionPerformed(ActionEvent e) {
Object[] currentSelection = _theSketch.getSelectionCells();
if (!(currentSelection[0] instanceof EntityNode)) {
return;
}
EntityNode table = (EntityNode) currentSelection[0];
JDBCDriver dbd = null;
dbd = _theSketch.getDatabase().getJDBCDriver();
if (dbd == null) {
// The user hit Cancel
return;
}
FreeQueryDialog afqd;
String text = "DELETE FROM " + table.getName() + "\n WHERE()";
while (true) {
afqd = new FreeQueryDialog(_theSketch.getFrame(), text);
if (!afqd.isAccepted()) {
return;
}
try {
String input = afqd.getInput();
dbd.executeUpdate(input);
return;
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, ex.getMessage());
text = afqd.getInput();
continue;
}
}
}
use of easik.sketch.vertex.EntityNode 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