Search in sources :

Example 1 with SketchFrame

use of easik.ui.SketchFrame 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 2 with SketchFrame

use of easik.ui.SketchFrame 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 3 with SketchFrame

use of easik.ui.SketchFrame 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 4 with SketchFrame

use of easik.ui.SketchFrame in project fql by CategoricalData.

the class SketchExportAction method actionPerformed.

/**
 * Display a dialog prompting the user for the name under which to save the
 * current sketch.
 *
 * @param e
 *            The action event
 */
@Override
public void actionPerformed(final ActionEvent e) {
    final Sketch sketch;
    if (_theFrame instanceof SketchFrame) {
        sketch = ((SketchFrame) _theFrame).getMModel();
    } else {
        final Object[] currentSelection = ((ApplicationFrame) _theFrame).getOverview().getSelectionCells();
        if (!((currentSelection.length == 1) && (currentSelection[0] instanceof SketchNode))) {
            System.err.println("Sketch export via overview popup should only be enabled when " + "selection size is 1, and it is a sketch node");
            return;
        }
        sketch = ((SketchNode) currentSelection[0]).getFrame().getMModel();
    }
    final File selFile = FileChooser.saveFile("Save EASIK Sketch", FileFilter.EASIK_SKETCH, "sketch");
    if (selFile != null) {
        sketch.getDocInfo().updateModificationDate();
        sketch.saveToXML(selFile);
    }
}
Also used : SketchFrame(easik.ui.SketchFrame) Sketch(easik.sketch.Sketch) SketchNode(easik.overview.vertex.SketchNode) File(java.io.File)

Example 5 with SketchFrame

use of easik.ui.SketchFrame in project fql by CategoricalData.

the class ImportSketchAction method actionPerformed0.

public void actionPerformed0(File selFile) {
    Point p = (_newPoint != null) ? _newPoint : _theOverview.getNewSketchPosition(10);
    _theOverview.getFrame().getInfoTreeUI().storeExpansion();
    SketchNode newNode = _theOverview.addNewSketch(_theOverview.getNewName("ImportedSketch0"), p.getX(), p.getY());
    _theOverview.getFrame().getInfoTreeUI().storeExpansion();
    SketchFrame newFrame = newNode.getFrame();
    Sketch sk = newFrame.getMModel();
    if (!sk.loadFromXML(selFile)) {
        return;
    }
    newNode.updateName();
    sk.updateThumb();
    _theOverview.getFrame().getInfoTreeUI().revertExpansion();
    _theOverview.setDirty(true);
    // Updates gui
    _theOverview.refresh();
}
Also used : SketchFrame(easik.ui.SketchFrame) Sketch(easik.sketch.Sketch) Point(java.awt.Point) SketchNode(easik.overview.vertex.SketchNode)

Aggregations

SketchFrame (easik.ui.SketchFrame)35 SketchEdge (easik.sketch.edge.SketchEdge)31 Sketch (easik.sketch.Sketch)30 SketchGraphModel (easik.sketch.util.graph.SketchGraphModel)29 EntityNode (easik.sketch.vertex.EntityNode)29 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)12 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 SketchNode (easik.overview.vertex.SketchNode)4