Search in sources :

Example 1 with SketchGraphModel

use of easik.sketch.util.graph.SketchGraphModel in project fql by CategoricalData.

the class SketchFrame method closeWindow.

/**
 * Trys to close window as long it is not dirty.
 */
@Override
public void closeWindow() {
    // We're exiting, so update the width/height of the main window, and
    // save the settings
    _settings.setProperty("sketch_display_width", String.valueOf(getWidth()));
    _settings.setProperty("sketch_display_height", String.valueOf(getHeight()));
    _settings.setProperty("sketch_divider_position", String.valueOf(_mainSplitPane.getDividerLocation()));
    _settings.setProperty("sketch_frame_location_x", String.valueOf(getX()));
    _settings.setProperty("sketch_frame_location_y", String.valueOf(getY()));
    _settings.store();
    // Refresh displayed thumbnail
    _ourSketch.clearSelection();
    _ourSketch.getOverview().refreshAll();
    final ModelStateManager<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> stateManager = _ourSketch.getStateManager();
    final SketchGraphModel sgm = _ourSketch.getGraphModel();
    while (sgm.inInsignificantUpdate()) {
        sgm.cancelInsignificantUpdate();
    }
    while (!(stateManager.peekState() instanceof BasicEditingState)) {
        stateManager.popState();
    }
    setVisible(false);
}
Also used : SketchEdge(easik.sketch.edge.SketchEdge) BasicEditingState(easik.model.states.BasicEditingState) Sketch(easik.sketch.Sketch) SketchGraphModel(easik.sketch.util.graph.SketchGraphModel) EntityNode(easik.sketch.vertex.EntityNode)

Example 2 with SketchGraphModel

use of easik.sketch.util.graph.SketchGraphModel in project fql by CategoricalData.

the class JDBCUpdateMonitor method getDialogOptions.

/**
 * Gets the options needed for open a dialog for row insertion: map of
 * attribute names to their type, and a map of foreign key names to a JTable
 * of data they point to.
 *
 * @param table
 *            The node representing the table who's information we want
 * @return Wrapper class holding the maps needed for insertion dialog
 */
private DialogOptions getDialogOptions(final EntityNode table) {
    @SuppressWarnings("unused") final HashSet<ModelConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>> constraints = new HashSet<ModelConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>>(table.getConstraints());
    final HashMap<String, EasikType> attToType = new HashMap<>(25);
    final LinkedHashMap<String, EntityNode> fKeys = new LinkedHashMap<>(10);
    // find attributes, and map to their EasikType
    for (final EntityAttribute<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> ea : table.getEntityAttributes()) {
        attToType.put(ea.getName(), ea.getType());
    }
    // find all foreign keys, and add to foreign key set
    for (final SketchEdge ske : table.getOutgoingEdges()) {
        fKeys.put(cn.tableFK(ske), ske.getTargetEntity());
    }
    return new DialogOptions(attToType, fKeys);
}
Also used : ModelConstraint(easik.model.constraint.ModelConstraint) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SketchGraphModel(easik.sketch.util.graph.SketchGraphModel) EntityNode(easik.sketch.vertex.EntityNode) LinkedHashMap(java.util.LinkedHashMap) SketchFrame(easik.ui.SketchFrame) SketchEdge(easik.sketch.edge.SketchEdge) Sketch(easik.sketch.Sketch) EasikType(easik.database.types.EasikType) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 3 with SketchGraphModel

use of easik.sketch.util.graph.SketchGraphModel in project fql by CategoricalData.

the class JDBCUpdateMonitor method deleteFrom.

/**
 * Trys to delete from a given table. If the action will break a constraint,
 * it is aborted and the user is notified.
 *
 * @param table
 *            The table from which we will attempt the delete
 * @return The success of the delete
 */
@Override
public boolean deleteFrom(final EntityNode table) {
    final int[] selectedPKs = DatabaseUtil.selectRowPKs(table.getMModel().getFrame(), table);
    // if there was a selection
    if (selectedPKs.length > 0) {
        final String PKcolumn = cn.tablePK(table);
        final StringBuilder sb = new StringBuilder("DELETE FROM " + dbd.quoteId(table.getName()) + " WHERE ");
        // populate input set for prepared statement while adding column
        // names
        final Set<ColumnEntry> input = new LinkedHashSet<>(selectedPKs.length);
        for (final int pk : selectedPKs) {
            for (EntityAttribute<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> ea : table.getHiddenEntityAttributes()) {
                try {
                    ResultSet result = dbd.executeQuery("SELECT * FROM " + table.getName() + " Where id=" + pk + " AND " + ea.getName() + "= 1");
                    // we do this by .isBeforeFirst()
                    if (result.isBeforeFirst()) {
                        JOptionPane.showMessageDialog(null, "Unable to execute DELETE: Deleting row will cause constraint inconsistency");
                        return false;
                    }
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            sb.append(PKcolumn).append("=? OR ");
            input.add(new ColumnEntry(PKcolumn, Integer.toString(pk), new Int()));
        }
        // remove last ',OR '
        sb.delete(sb.length() - 4, sb.length());
        try {
            dbd.executePreparedUpdate(sb.toString(), input);
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, "Unable to execute DELETE: " + e.getMessage());
        }
    }
    return true;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) SQLException(java.sql.SQLException) ColumnEntry(easik.ui.datamanip.ColumnEntry) LimitConstraint(easik.model.constraint.LimitConstraint) ProductConstraint(easik.model.constraint.ProductConstraint) SumConstraint(easik.model.constraint.SumConstraint) PullbackConstraint(easik.model.constraint.PullbackConstraint) ModelConstraint(easik.model.constraint.ModelConstraint) Int(easik.database.types.Int) SketchGraphModel(easik.sketch.util.graph.SketchGraphModel) EntityNode(easik.sketch.vertex.EntityNode) SketchFrame(easik.ui.SketchFrame) SketchEdge(easik.sketch.edge.SketchEdge) ResultSet(java.sql.ResultSet) Sketch(easik.sketch.Sketch)

Example 4 with SketchGraphModel

use of easik.sketch.util.graph.SketchGraphModel in project fql by CategoricalData.

the class JDBCViewUpdateMonitor 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) {
                    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: " + 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: " + e.getMessage());
        return false;
    }
}
Also used : LimitConstraint(easik.model.constraint.LimitConstraint) SQLException(java.sql.SQLException) ColumnEntry(easik.ui.datamanip.ColumnEntry) PullbackConstraint(easik.model.constraint.PullbackConstraint) SumConstraint(easik.model.constraint.SumConstraint) Int(easik.database.types.Int) LimitConstraint(easik.model.constraint.LimitConstraint) ProductConstraint(easik.model.constraint.ProductConstraint) SumConstraint(easik.model.constraint.SumConstraint) PullbackConstraint(easik.model.constraint.PullbackConstraint) ModelConstraint(easik.model.constraint.ModelConstraint) SketchGraphModel(easik.sketch.util.graph.SketchGraphModel) EntityNode(easik.sketch.vertex.EntityNode) SketchFrame(easik.ui.SketchFrame) ProductConstraint(easik.model.constraint.ProductConstraint) SketchEdge(easik.sketch.edge.SketchEdge) ResultSet(java.sql.ResultSet) Sketch(easik.sketch.Sketch) CommutativeDiagram(easik.model.constraint.CommutativeDiagram) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 5 with SketchGraphModel

use of easik.sketch.util.graph.SketchGraphModel in project fql by CategoricalData.

the class Sketch method removeEdge.

/**
 * Removes an edge, also cascades to remove all constraints using it.
 *
 * @param toRemove
 *            The edge about to be removed
 */
public void removeEdge(final SketchEdge toRemove) {
    model.beginUpdate();
    // Check for constraints that need these edges
    for (final ModelConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> c : _constraints.values()) {
        if (c.hasEdge(toRemove)) {
            removeConstraint(c);
        }
    }
    if (toRemove instanceof UniqueIndexable) {
        final EntityNode source = toRemove.getSourceEntity();
        boolean needCleanup = false;
        for (final UniqueKey<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> q : source.getUniqueKeys()) {
            if (q.removeElement((UniqueIndexable) toRemove)) {
                needCleanup = true;
            }
        }
        if (needCleanup) {
            source.cleanup();
        }
    // FIXME: undo: unique key cleanup stuff
    }
    _edges.remove(toRemove.getName());
    toRemove.getSourceEntity().removeDepend(toRemove.getTargetEntity());
    model.postEdit(new AbstractUndoableEdit() {

        /**
         */
        private static final long serialVersionUID = 711022413236293938L;

        @Override
        public void undo() {
            super.undo();
            _edges.put(toRemove.getName(), toRemove);
            toRemove.getSourceEntity().addDepend(toRemove.getTargetEntity());
        }

        @Override
        public void redo() {
            super.redo();
            _edges.remove(toRemove.getName());
            toRemove.getSourceEntity().removeDepend(toRemove.getTargetEntity());
        }
    });
    getGraphLayoutCache().remove(new Object[] { toRemove });
    model.endUpdate();
    KosarajuSCC s = new KosarajuSCC(this);
    strongConnected = s.getSCC();
}
Also used : KosarajuSCC(easik.model.util.graph.KosarajuSCC) SketchGraphModel(easik.sketch.util.graph.SketchGraphModel) EntityNode(easik.sketch.vertex.EntityNode) SketchFrame(easik.ui.SketchFrame) AbstractUndoableEdit(javax.swing.undo.AbstractUndoableEdit) SketchEdge(easik.sketch.edge.SketchEdge) UniqueIndexable(easik.model.keys.UniqueIndexable)

Aggregations

SketchGraphModel (easik.sketch.util.graph.SketchGraphModel)32 SketchEdge (easik.sketch.edge.SketchEdge)31 EntityNode (easik.sketch.vertex.EntityNode)30 SketchFrame (easik.ui.SketchFrame)29 Sketch (easik.sketch.Sketch)27 LimitConstraint (easik.model.constraint.LimitConstraint)16 ProductConstraint (easik.model.constraint.ProductConstraint)15 PullbackConstraint (easik.model.constraint.PullbackConstraint)15 SumConstraint (easik.model.constraint.SumConstraint)15 LinkedList (java.util.LinkedList)15 EqualizerConstraint (easik.model.constraint.EqualizerConstraint)12 ModelConstraint (easik.model.constraint.ModelConstraint)11 ModelPath (easik.model.path.ModelPath)9 ArrayList (java.util.ArrayList)8 LinkedHashSet (java.util.LinkedHashSet)7 XSDAnnotation (easik.xml.xsd.nodes.XSDAnnotation)5 XSDType (easik.xml.xsd.nodes.types.XSDType)5 EasikType (easik.database.types.EasikType)4 UniqueIndexable (easik.model.keys.UniqueIndexable)4 ColumnEntry (easik.ui.datamanip.ColumnEntry)4