Search in sources :

Example 16 with Sketch

use of easik.sketch.Sketch in project fql by CategoricalData.

the class Overview method refreshAll.

/**
 * Refreshes the overview GUI as well as the GUIs of all Sketches and Views.
 */
public void refreshAll() {
    for (SketchNode node : _sketchNodes.values()) {
        Sketch s = node.getFrame().getMModel();
        s.refresh();
        s.updateThumb();
    }
    for (ViewNode node : _viewNodes.values()) {
        View v = node.getMModel();
        v.refresh();
        v.updateThumb();
    }
    refresh();
}
Also used : Sketch(easik.sketch.Sketch) ViewNode(easik.overview.vertex.ViewNode) SketchNode(easik.overview.vertex.SketchNode) View(easik.view.View)

Example 17 with Sketch

use of easik.sketch.Sketch in project fql by CategoricalData.

the class XSDElement method setKeys.

/**
 * Use an entity node to set the keys, including foreign keyrefs and
 * uniques.
 * <p/>
 * Key is set from the primary key. KeyRefs are set from the outgoing edges.
 * Uniques are set by Uniques and by noninclusion injective outgoing edges.
 *
 * @param node
 *            we are working with
 */
@SuppressWarnings("unused")
public void setKeys(final EntityNode node) {
    final List<UniqueKey<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>> uniqueKeyList = node.getUniqueKeys();
    final XSDComplexType myType = (XSDComplexType) getElementType();
    constraints = new ArrayList<XSDAbstractKey>(uniqueKeyList.size() + 3);
    final String idName = Easik.getInstance().getSettings().getProperty("xml_id_name");
    final String keyrefName = Easik.getInstance().getSettings().getProperty("xml_keyref_name");
    final boolean idIsAttribute = Boolean.valueOf(Easik.getInstance().getSettings().getProperty("xml_id_is_attribute"));
    final XSDKey primaryKey = node.createXMLPrimaryKey(this);
    final XSDElement theParent = (XSDElement) getParent();
    theParent.addConstraint(primaryKey);
    for (final SketchEdge edge : node.getOutgoingEdges()) {
        final boolean isInclusion = edge.getTargetEntity().getName().equals(theParent.getName());
        if (edge.isInjective()) {
            if (!isInclusion) {
                constraints.add(new XSDUniqueKey(edge.getForeignKeyName(keyrefName), this, edge.getName()));
            }
        }
        if (!isInclusion) {
            theParent.addConstraint(new XSDKeyRef(this, edge.getTargetEntity().getXMLPrimaryKeyName(), edge.getName()));
            myType.addAtom(new XSDElement(edge.getName(), edge.isPartial(), XSDBaseType.xsInt));
        }
    }
    for (final UniqueKey<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> k : uniqueKeyList) {
        this.addConstraint(new XSDUniqueKey(this, k));
    }
}
Also used : XSDAbstractKey(easik.xml.xsd.nodes.constraints.XSDAbstractKey) XSDKey(easik.xml.xsd.nodes.constraints.XSDKey) XSDKeyRef(easik.xml.xsd.nodes.constraints.XSDKeyRef) SketchGraphModel(easik.sketch.util.graph.SketchGraphModel) EntityNode(easik.sketch.vertex.EntityNode) SketchFrame(easik.ui.SketchFrame) XSDUniqueKey(easik.xml.xsd.nodes.constraints.XSDUniqueKey) SketchEdge(easik.sketch.edge.SketchEdge) XSDUniqueKey(easik.xml.xsd.nodes.constraints.XSDUniqueKey) UniqueKey(easik.model.keys.UniqueKey) Sketch(easik.sketch.Sketch) XSDComplexType(easik.xml.xsd.nodes.types.XSDComplexType)

Example 18 with Sketch

use of easik.sketch.Sketch in project fql by CategoricalData.

the class ModelInfoTreeUI method setPopMenuItems.

/**
 * Sets which of the menu items will be visible
 *
 * @return true if the popup should be displayed, false otherwise
 */
public boolean setPopMenuItems() {
    // If there is nothing seleceted then just do nothing
    if (_theFrame.getInfoTreeUI().getInfoTree().isSelectionEmpty()) {
        return false;
    } else // sketch
    if (_theFrame.getMode() == F.Mode.MANIPULATE) {
        return false;
    }
    // Get currently selected object
    DefaultMutableTreeNode curSelected = (DefaultMutableTreeNode) _theFrame.getInfoTreeUI().getInfoTree().getSelectionPath().getLastPathComponent();
    // Hide all elements
    for (Component c : _popupMenu.getComponents()) {
        c.setVisible(false);
    }
    // Check what is currently selected
    if (curSelected instanceof ModelVertex) {
        _addAttributeItem.setVisible(true);
        _addUniqueKeyItem.setVisible(true);
        _renameEntityItem.setVisible(true);
        _deleteEntityItem.setVisible(true);
    } else if (curSelected instanceof EntityAttribute) {
        if (_theFrame.getMModel() instanceof Sketch) {
            _editAttributeItem.setVisible(true);
            _deleteAttributeItem.setVisible(true);
        }
    } else if (curSelected instanceof UniqueKey) {
        if (_theFrame.getMModel() instanceof Sketch) {
            _editUniqueKeyItem.setVisible(true);
            _deleteUniqueKeyItem.setVisible(true);
        }
    } else if (curSelected instanceof ModelConstraint) {
        if (_theFrame.getMModel() instanceof Sketch) {
            if ((curSelected instanceof SumConstraint) || (curSelected instanceof ProductConstraint) || (curSelected instanceof CommutativeDiagram)) {
                _addPathItem.setVisible(true);
            }
            _deleteConstraintItem.setVisible(true);
        }
    } else if (curSelected instanceof ModelPath) {
        Object myConst = curSelected.getParent();
        if ((myConst instanceof SumConstraint) || (myConst instanceof ProductConstraint) || (myConst instanceof CommutativeDiagram)) {
            _deletePathItem.setVisible(true);
        }
    } else if (curSelected == _tree_entities) {
        _addEntityItem.setVisible(true);
    } else if (curSelected == _tree_constraints) {
        _addCommutativeItem.setVisible(true);
        _addProductItem.setVisible(true);
        _addPullbackItem.setVisible(true);
        _addSumItem.setVisible(true);
    // _addLimItem.setVisible(true);
    } else if (curSelected == _tree_constraints_commutative) {
        _addCommutativeItem.setVisible(true);
    } else if (curSelected == _tree_constraints_product) {
        _addProductItem.setVisible(true);
    } else if (curSelected == _tree_constraints_pullback) {
        _addPullbackItem.setVisible(true);
    } else if (curSelected == _tree_constraints_equalizer) {
        _addEqualizerItem.setVisible(true);
    } else if (curSelected == _tree_constraints_sum) {
        _addSumItem.setVisible(true);
    } else if (curSelected == _tree_constraints_limit) {
    // _addLimItem.setVisible(true);
    } else {
        return false;
    }
    return true;
}
Also used : ProductConstraint(easik.model.constraint.ProductConstraint) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) EntityAttribute(easik.model.attribute.EntityAttribute) ModelConstraint(easik.model.constraint.ModelConstraint) UniqueKey(easik.model.keys.UniqueKey) ModelPath(easik.model.path.ModelPath) ModelVertex(easik.model.vertex.ModelVertex) Sketch(easik.sketch.Sketch) Component(java.awt.Component) SumConstraint(easik.model.constraint.SumConstraint) CommutativeDiagram(easik.model.constraint.CommutativeDiagram)

Example 19 with Sketch

use of easik.sketch.Sketch in project fql by CategoricalData.

the class DeleteAttributeAction method actionPerformed.

/**
 * Deletes the currently selected attribute
 *
 * @param e
 *            The action event
 */
@Override
public void actionPerformed(ActionEvent e) {
    // If there is nothing seleceted then just do nothing
    if (_theFrame.getInfoTreeUI().getInfoTree().isSelectionEmpty()) {
        return;
    }
    // cancel operation
    if (_theFrame.getMModel().isSynced()) {
        int choice = JOptionPane.showConfirmDialog(_theFrame, "Warning: this sketch is currently synced with a db; continue and break synchronization?", "Warning!", JOptionPane.OK_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
        if (choice == JOptionPane.CANCEL_OPTION) {
            return;
        }
    }
    // Get currently selected object
    DefaultMutableTreeNode curSelected = (DefaultMutableTreeNode) _theFrame.getInfoTreeUI().getInfoTree().getSelectionPath().getLastPathComponent();
    // Selection is an attribute
    if (curSelected instanceof EntityAttribute) {
        @SuppressWarnings("unchecked") EntityAttribute<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> curAttribute = (EntityAttribute<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>) curSelected;
        EntityNode parentEntity = curAttribute.getEntity();
        // Show a confirmation dialog box for the deletion
        if (JOptionPane.showConfirmDialog(_theFrame, "Are you sure you want to delete the '" + curAttribute.getName() + "' attribute from the '" + parentEntity + "' entity?", "Confirm Delete", JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION) {
            // Delete the attribute from entity
            parentEntity.removeEntityAttribute(curAttribute);
            _theFrame.getMModel().setDirty();
            _theFrame.getMModel().setSynced(false);
        }
    } else // Selection is not an attribute
    {
        JOptionPane.showMessageDialog(_theFrame, "You don't have an attribute selected. \nPlease select an attribute and try again.", "No Attribute Selected", JOptionPane.ERROR_MESSAGE);
        return;
    }
    _theFrame.getInfoTreeUI().revertExpansion();
}
Also used : SketchFrame(easik.ui.SketchFrame) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) EntityAttribute(easik.model.attribute.EntityAttribute) SketchEdge(easik.sketch.edge.SketchEdge) Sketch(easik.sketch.Sketch) SketchGraphModel(easik.sketch.util.graph.SketchGraphModel) EntityNode(easik.sketch.vertex.EntityNode)

Example 20 with Sketch

use of easik.sketch.Sketch in project fql by CategoricalData.

the class MySQLExporter method createConstraint.

/**
 * @param constraint
 * @param id
 *
 * @return
 */
@Override
public List<String> createConstraint(final ProductConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> constraint, final String id) {
    final List<String> sql = new LinkedList<>();
    final String delConName = "productConstraint" + id + "Delete";
    StringBuilder proc = new StringBuilder(500);
    proc.append("CREATE PROCEDURE ").append(quoteId(delConName)).append("(id ").append(pkType()).append(") BEGIN").append(lineSep);
    EntityNode begin = null;
    int j = 0;
    // c.b_id = b.id JOIN d ON d.c_id = c.id...> WHERE <begin> = id;
    for (final ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> p : constraint.getPaths()) {
        if (begin == null) {
            begin = p.getDomain();
        }
        proc.append("   DELETE ").append(quoteId(p.getCoDomain())).append(" FROM ").append(joinPath(p)).append(lineSep).append("           WHERE ").append(qualifiedPK(begin)).append(" = id;").append(lineSep);
        // Federico Mora
        StringBuilder proc1 = new StringBuilder(500);
        StringBuilder body = new StringBuilder(50);
        proc1.append("CREATE PROCEDURE ").append("Update" + constraint.getID() + "Proj" + j++).append("() BEGIN").append(lineSep);
        for (final ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> q : constraint.getPaths()) {
            if (p == q) {
            // do nothing, cascade will take care of it
            } else {
                // making the update string which is used by all the delete
                // procedures.
                final LinkedList<SketchEdge> edges = q.getEdges();
                if (edges.size() >= 2) {
                    Iterator<SketchEdge> iter = edges.iterator();
                    SketchEdge e = iter.next();
                    body.append("   UPDATE ").append(leftJoinPath(q)).append(lineSep);
                    body.append("   SET");
                    while (iter.hasNext()) {
                        SketchEdge ske = iter.next();
                        body.append(" " + quoteId(ske.getSourceEntity()) + ".BC" + constraint.getID() + " = " + false + ",");
                    }
                    body.delete(body.length() - 1, body.length());
                    body.append(" WHERE " + qualifiedFK(e) + " IS NULL;" + lineSep);
                }
            }
        }
        if (body.length() > 0) {
            proc1.append(body);
            proc1.append("END");
            sql.add(proc1.toString());
            // Now create the triggers to call the new procedure
            addTrigger(p.getCoDomain(), "AFTER DELETE", "CALL " + quoteId("Update" + constraint.getID() + "Proj" + (j - 1)) + "()");
        }
    // end Federico Mora
    }
    // Select B.id From A Join B On A.f1 = B.id Join D On A.f3 = D.id Where
    // D.id = 1
    proc.append("END");
    sql.add(proc.toString());
    // Now create the trigger to call the new procedure
    addTrigger(begin, "BEFORE DELETE", "CALL " + quoteId(delConName) + "(OLD." + quoteId(tablePK(begin)) + ')');
    for (final ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> p : constraint.getPaths()) {
        final EntityNode dest = p.getCoDomain();
        final String conName = "productConstraint" + id + "Insert" + cleanId(dest);
        final Collection<String> args = new LinkedList<>();
        final Collection<String> params = new LinkedList<>();
        args.add("id " + pkType());
        params.add("NEW." + quoteId(tablePK(dest)));
        // commented out by Sarah van der Laan -- caused error in generated
        // SQL file (invalid foreign keys)
        /**
         * for (final SketchEdge shadow : dest.getShadowEdges()) {
         * args.add(quoteId("NEW_shadow_" + tableFK(shadow)) + ' ' +
         * pkType()); params.add("NEW." + quoteId(tableFK(shadow))); }
         */
        proc = new StringBuilder(500);
        proc.append("CREATE PROCEDURE ").append(quoteId(conName)).append('(').append(EasikTools.join(", ", args)).append(") BEGIN").append(lineSep).append(" DECLARE _lastId ").append(pkType()).append(';').append(lineSep);
        final StringBuilder createIntermediates = new StringBuilder(250);
        final Collection<String> clauses = new LinkedList<>();
        for (final ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> q : constraint.getPaths()) {
            if (p == q) {
                continue;
            }
            // Older versions were doing "SELECT COUNT(*) FROM TABLE", but
            // that has a big
            // performance hit for very large, busy InnoDB tables, when all
            // we really care
            // about is non-empty:
            clauses.add("(SELECT 1 FROM " + quoteId(q.getCoDomain()) + " LIMIT 1) = 1");
            // If we end up putting anything in createIntermediate, we'll
            // use these
            // insertions when we insert the first row into one of the path
            // targets
            final LinkedList<SketchEdge> sketchEdgeLinkedList = q.getEdges();
            final SketchEdge[] edges = sketchEdgeLinkedList.toArray(new SketchEdge[sketchEdgeLinkedList.size()]);
            for (int i = edges.length - 1; i > 0; i--) {
                final SketchEdge e = edges[i];
                final EntityNode source = e.getSourceEntity();
                final EntityNode target = e.getTargetEntity();
                // after source there used to be dest.getShadowEdges();
                createIntermediates.append("                        ").append(insertInto(true, source, qualifiedPK(target), quoteId(target), Collections.singletonList(quoteId(tableFK(e))), null));
            }
        }
        // In words: If the tables forming the domains of the other paths
        // contain items.
        proc.append("       IF ").append(EasikTools.join(" AND ", clauses)).append(" THEN").append(lineSep);
        if (createIntermediates.length() > 0) {
            // If we just inserted the first row, we're going to have to
            // build a path of intermediate tables
            // for the other paths, which we built in createIntermediate.
            proc.append("           IF (SELECT COUNT(*) FROM (SELECT 1 FROM ").append(quoteId(dest)).append(" LIMIT 2) a) = 1 THEN").append(lineSep).append(createIntermediates).append("               END IF;").append(lineSep).append("").append(lineSep);
        }
        // Produce the intermediate path insertion strings
        final LinkedList<SketchEdge> sketchEdgeLinkedList = p.getEdges();
        final SketchEdge[] edges = sketchEdgeLinkedList.toArray(new SketchEdge[sketchEdgeLinkedList.size()]);
        if (edges.length > 1) {
            for (int i = edges.length - 1; i > 0; i--) {
                final SketchEdge e = edges[i];
                final EntityNode source = e.getSourceEntity();
                @SuppressWarnings("unused") final EntityNode target = e.getTargetEntity();
                // after source there used to be dest.getShadowEdges();
                proc.append("               ").append(insertInto(true, source, null, null, Collections.singletonList(quoteId(tableFK(e))), Collections.singletonList((i == edges.length - 1) ? "id" : "LAST_INSERT_ID()")));
            }
            proc.append("           SET _lastId = LAST_INSERT_ID();").append(lineSep);
        } else {
            proc.append("           SET _lastId = id;").append(lineSep);
        }
        // Now the proper insertion
        final List<String> columns = new LinkedList<>();
        final Collection<String> values = new LinkedList<>();
        final Collection<String> from = new LinkedList<>();
        EntityNode thisTarget = null;
        for (final ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> q : constraint.getPaths()) {
            final EntityNode target = q.getFirstEdge().getTargetEntity();
            columns.add(quoteId(tableFK(q.getFirstEdge())));
            values.add(qualifiedPK(target));
            from.add(quoteId(target));
            if (q == p) {
                thisTarget = target;
            }
        }
        // after begin there used to be dest.getShadowEdges();
        proc.append("               ").append(insertInto(true, begin, EasikTools.join(", ", values), EasikTools.join(" CROSS JOIN ", from) + " WHERE " + qualifiedPK(thisTarget) + " = _lastId", columns, null)).append("   END IF;").append(lineSep).append("END");
        sql.add(proc.toString());
        addTrigger(dest, "AFTER INSERT", "CALL " + quoteId(conName) + '(' + EasikTools.join(", ", params) + ')');
    }
    return delimit("$$", sql);
}
Also used : LinkedList(java.util.LinkedList) LimitConstraint(easik.model.constraint.LimitConstraint) EqualizerConstraint(easik.model.constraint.EqualizerConstraint) ProductConstraint(easik.model.constraint.ProductConstraint) SumConstraint(easik.model.constraint.SumConstraint) PullbackConstraint(easik.model.constraint.PullbackConstraint) EntityNode(easik.sketch.vertex.EntityNode) SketchGraphModel(easik.sketch.util.graph.SketchGraphModel) SketchFrame(easik.ui.SketchFrame) SketchEdge(easik.sketch.edge.SketchEdge) Sketch(easik.sketch.Sketch)

Aggregations

Sketch (easik.sketch.Sketch)40 SketchEdge (easik.sketch.edge.SketchEdge)32 EntityNode (easik.sketch.vertex.EntityNode)30 SketchFrame (easik.ui.SketchFrame)30 SketchGraphModel (easik.sketch.util.graph.SketchGraphModel)27 LimitConstraint (easik.model.constraint.LimitConstraint)16 ProductConstraint (easik.model.constraint.ProductConstraint)16 SumConstraint (easik.model.constraint.SumConstraint)16 PullbackConstraint (easik.model.constraint.PullbackConstraint)15 LinkedList (java.util.LinkedList)15 ModelConstraint (easik.model.constraint.ModelConstraint)13 EqualizerConstraint (easik.model.constraint.EqualizerConstraint)12 ModelPath (easik.model.path.ModelPath)9 ArrayList (java.util.ArrayList)7 LinkedHashSet (java.util.LinkedHashSet)7 InjectiveEdge (easik.sketch.edge.InjectiveEdge)5 XSDAnnotation (easik.xml.xsd.nodes.XSDAnnotation)5 XSDType (easik.xml.xsd.nodes.types.XSDType)5 EasikType (easik.database.types.EasikType)4 EntityAttribute (easik.model.attribute.EntityAttribute)4