Search in sources :

Example 6 with SketchFrame

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

the class OverviewHandler method startElement.

/**
 * Overloaded method that is called any time the start of an element is
 * found
 *
 * @param namespace
 * 			@see org.xml.sax.helpers.DefaultHandler
 * @param localName
 * 			@see org.xml.sax.helpers.DefaultHandler
 * @param qName
 * 			@see org.xml.sax.helpers.DefaultHandler
 * @param atts
 * 			@see org.xml.sax.helpers.DefaultHandler
 */
@Override
public void startElement(String namespace, String localName, String qName, Attributes atts) {
    _currNode = qName;
    // call
    if (_sketchParser != null) {
        _sketchParser.startElement(namespace, localName, qName, atts);
    } else // Initialize a sketch handler, and note this sketch's attributes
    if (qName.equals("easketch")) {
        String sketchName = atts.getValue("name");
        int sketchX = Integer.parseInt(atts.getValue("x"));
        int sketchY = Integer.parseInt(atts.getValue("y"));
        String cascade = atts.getValue("cascade"), pCascade = atts.getValue("partial-cascade");
        if (cascade == null) {
            cascade = Easik.getInstance().getSettings().getProperty("sql_cascade", "restrict");
        }
        if (pCascade == null) {
            pCascade = Easik.getInstance().getSettings().getProperty("sql_cascade_partial", "set_null");
        }
        Cascade c = cascade.equals("cascade") ? Cascade.CASCADE : Cascade.RESTRICT;
        Cascade cp = pCascade.equals("cascade") ? Cascade.CASCADE : pCascade.equals("restrict") ? Cascade.RESTRICT : Cascade.SET_NULL;
        _sketchParser = SketchFileIO.getNewSketchHandler(_theFrame.getOverview());
        SketchFrame frame = _sketchParser.getFrame();
        Sketch sketch = frame.getMModel();
        sketch.setDefaultCascading(c);
        sketch.setDefaultPartialCascading(cp);
        _newSketchNode = new SketchNode(sketchName, sketchX, sketchY, frame);
        _sketchNodes.put(sketchName, _newSketchNode);
    } else // know not to override the overview's
    if (qName.equals("view")) {
        _parsingView = true;
        _queryNodes = new HashMap<>();
        String viewName = atts.getValue("name");
        int x = Integer.parseInt(atts.getValue("x"));
        int y = Integer.parseInt(atts.getValue("y"));
        String edgeLabel = atts.getValue("viewDefinitionEdge");
        String sketchName = atts.getValue("on_sketch");
        ViewFrame viewFrame = new ViewFrame(_theFrame.getOverview(), _sketchNodes.get(sketchName).getFrame().getMModel());
        _newViewNode = new ViewNode(viewName, x, y, viewFrame);
        _viewDefEdge.put(edgeLabel, new ViewDefinitionEdge(_newViewNode, _sketchNodes.get(sketchName), edgeLabel));
        _viewDocInfo = new DocumentInfo(viewFrame);
        _viewNodes.put(viewName, _newViewNode);
        _sketchNodes.get(sketchName).getFrame().getMModel().addView(_newViewNode);
    } else if (qName.equals("queryNode")) {
        String name = atts.getValue("name");
        int x = Integer.parseInt(atts.getValue("x"));
        int y = Integer.parseInt(atts.getValue("y"));
        String query = atts.getValue("query");
        // exception so they can't be saved
        try {
            _queryNodes.put(name, new QueryNode(name, x, y, _newViewNode.getFrame().getMModel(), query));
        } catch (QueryException e) {
            e.printStackTrace();
        }
    } else if (qName.equals("View_Edge")) {
        View_Edge newEdge;
        String edgeType = atts.getValue("type");
        QueryNode source = _queryNodes.get(atts.getValue("source"));
        QueryNode target = _queryNodes.get(atts.getValue("target"));
        String id = atts.getValue("id");
        String cascadeAtt = atts.getValue("cascade");
        if (cascadeAtt == null) {
            // This is from an export before Easik had per-edge cascading
            // (in other words, before r583)
            // We use the global preferences for cascading
            String key = "sql_cascade", def = "restrict";
            if (edgeType.equals("partial")) {
                key = "sql_cascade_partial";
                def = "set_null";
            }
            cascadeAtt = Easik.getInstance().getSettings().getProperty(key, def);
        }
        @SuppressWarnings("unused") SketchEdge.Cascade cascade = cascadeAtt.equals("set_null") ? SketchEdge.Cascade.SET_NULL : cascadeAtt.equals("cascade") ? SketchEdge.Cascade.CASCADE : SketchEdge.Cascade.RESTRICT;
        if (edgeType.equals("injective")) {
            newEdge = new InjectiveViewEdge(source, target, id);
        } else if (edgeType.equals("partial")) {
            newEdge = new PartialViewEdge(source, target, id);
        } else {
            newEdge = new NormalViewEdge(source, target, id);
        }
        _viewEdges.put(id, newEdge);
    }
}
Also used : NormalViewEdge(easik.view.edge.NormalViewEdge) ViewFrame(easik.ui.ViewFrame) SketchNode(easik.overview.vertex.SketchNode) ViewDefinitionEdge(easik.overview.edge.ViewDefinitionEdge) View_Edge(easik.view.edge.View_Edge) InjectiveViewEdge(easik.view.edge.InjectiveViewEdge) PartialViewEdge(easik.view.edge.PartialViewEdge) SketchFrame(easik.ui.SketchFrame) QueryException(easik.view.util.QueryException) SketchEdge(easik.sketch.edge.SketchEdge) QueryNode(easik.view.vertex.QueryNode) Sketch(easik.sketch.Sketch) ViewNode(easik.overview.vertex.ViewNode) Cascade(easik.model.edge.ModelEdge.Cascade) DocumentInfo(easik.DocumentInfo)

Example 7 with SketchFrame

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

Example 8 with SketchFrame

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

the class Sketch method multPathWarning.

/**
 * Method that checks if there are paths with the same domain and codomain
 * and then sees if they mismatch. We only care about when one of the paths
 * is all cascade and the other path contains an edge which is not cascade.
 * In this case we will warn the user.
 *
 * @return String warning
 * @author Federico Mora
 */
private String multPathWarning() {
    boolean cascade = false;
    boolean other = false;
    String warning = "";
    // want to look at paths from every node to every other node
    for (EntityNode s : _nodes.values()) {
        for (EntityNode t : _nodes.values()) {
            if (s != t) {
                cascade = false;
                other = false;
                // System.out.println("Finding paths from " + s.getName() +
                // " to " + t.getName());
                ArrayList<SketchEdge> startEdges = new ArrayList<>();
                ArrayList<SketchEdge> endEdges = new ArrayList<>();
                for (SketchEdge edge : _edges.values()) {
                    if (edge.getSourceEntity() == s) {
                        startEdges.add(edge);
                    }
                    if (edge.getTargetEntity() == t) {
                        endEdges.add(edge);
                    }
                }
                // care
                if (startEdges.size() < 2 || endEdges.size() < 2) {
                    // + t.getName());
                    continue;
                }
                ArrayList<ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>> paths = new ArrayList<>();
                ArrayList<LinkedList<SketchEdge>> temp = buildpath(s, t);
                for (LinkedList<SketchEdge> q : temp) {
                    if (q.peekLast().getTargetEntity() != t) {
                        q.clear();
                    } else {
                        paths.add(new ModelPath<>(q));
                    }
                }
                if (paths.size() > 1) {
                    for (ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> p : paths) {
                        if (p.isCompositeCascade()) {
                            cascade = true;
                        } else {
                            other = true;
                        }
                    }
                    if (cascade && other) {
                        warning += this.getName() + " contains multiple paths from " + s.getName() + " to " + t.getName() + "\n";
                        for (ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> p : paths) {
                            if (p.isCompositeCascade()) {
                                warning += "        " + p.toString() + " is Aggregate Cascade\n";
                            } else {
                                warning += "        " + p.toString() + " is Aggregate Other\n";
                            }
                        }
                    }
                }
            }
        }
    }
    return warning;
}
Also used : ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) EntityNode(easik.sketch.vertex.EntityNode) SketchGraphModel(easik.sketch.util.graph.SketchGraphModel) SketchFrame(easik.ui.SketchFrame) SketchEdge(easik.sketch.edge.SketchEdge) ModelPath(easik.model.path.ModelPath)

Example 9 with SketchFrame

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

the class SketchFileIO method sketchToElement.

/**
 * Converts a sketch to an Element.
 *
 * @param document
 *            The Document in which our information is being placed.
 * @param sketch
 * @return All of the information needed to rebuild our sketch containted in
 *         an Element. Returns null in the event that the element could not
 *         be created.
 */
public static Element sketchToElement(Document document, Sketch sketch) {
    try {
        Element rootElement = document.createElement("easketch");
        Element header = document.createElement("header");
        // Add Header info to document
        DocumentInfo d = sketch.getDocInfo();
        Element name = document.createElement("title");
        name.appendChild(document.createTextNode(d.getName()));
        header.appendChild(name);
        for (String aut : d.getAuthors()) {
            Element author = document.createElement("author");
            author.appendChild(document.createTextNode(aut));
            header.appendChild(author);
        }
        Element desc = document.createElement("description");
        desc.appendChild(document.createTextNode(d.getDesc()));
        header.appendChild(desc);
        Element creationDate = document.createElement("creationDate");
        creationDate.appendChild(document.createTextNode(EasikConstants.XML_DATETIME.format(d.getCreationDate())));
        header.appendChild(creationDate);
        Element modDate = document.createElement("lastModificationDate");
        modDate.appendChild(document.createTextNode(EasikConstants.XML_DATETIME.format(d.getModificationDate())));
        header.appendChild(modDate);
        Map<String, String> connParams = sketch.getConnectionParams();
        for (String key : connParams.keySet()) {
            Element connParam = document.createElement("connectionParam");
            connParam.setAttribute("name", key);
            connParam.setAttribute("value", connParams.get(key));
            header.appendChild(connParam);
        }
        if (sketch.isSynced()) {
            header.appendChild(document.createElement("synchronized"));
        }
        rootElement.appendChild(header);
        Element entities = document.createElement("entities");
        // Loop through entities, add them to the document
        for (EntityNode currentEntity : sketch.getEntities()) {
            if (currentEntity == null) {
                continue;
            }
            Element thisEntity = document.createElement("entity");
            thisEntity.setAttribute("name", currentEntity.toString());
            thisEntity.setAttribute("x", currentEntity.getX() + "");
            thisEntity.setAttribute("y", currentEntity.getY() + "");
            entities.appendChild(thisEntity);
            // Loop through attributes, add them to the document
            for (EntityAttribute<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> curAttribute : currentEntity.getEntityAttributes()) {
                Element attributeElmt = document.createElement("attribute");
                attributeElmt.setAttribute("name", curAttribute.getName());
                EasikType attType = curAttribute.getType();
                attributeElmt.setAttribute("attributeTypeClass", attType.getClass().getName());
                Map<String, String> typeAttribs = attType.attributes();
                for (String key : typeAttribs.keySet()) {
                    attributeElmt.setAttribute(key, typeAttribs.get(key));
                }
                thisEntity.appendChild(attributeElmt);
            }
        // We can't go through unique keys yet: they have to come
        // *after* edges
        }
        rootElement.appendChild(entities);
        Element edges = document.createElement("edges");
        for (SketchEdge currentEdge : sketch.getEdges().values()) {
            Element thisEdge = document.createElement("edge");
            thisEdge.setAttribute("id", currentEdge.getName());
            thisEdge.setAttribute("source", currentEdge.getSourceEntity().getName());
            thisEdge.setAttribute("target", currentEdge.getTargetEntity().getName());
            thisEdge.setAttribute("type", (currentEdge instanceof PartialEdge) ? "partial" : (currentEdge instanceof InjectiveEdge) ? "injective" : "normal");
            thisEdge.setAttribute("cascade", (currentEdge.getCascading() == SketchEdge.Cascade.SET_NULL) ? "set_null" : (currentEdge.getCascading() == SketchEdge.Cascade.CASCADE) ? "cascade" : "restrict");
            edges.appendChild(thisEdge);
        }
        rootElement.appendChild(edges);
        Element keys = document.createElement("keys");
        // Loop through unique keys for every node, add them to the document
        for (EntityNode currentEntity : sketch.getEntities()) {
            for (UniqueKey<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> curKey : currentEntity.getUniqueKeys()) {
                Element uniqueKeyElmt = document.createElement("uniqueKey");
                uniqueKeyElmt.setAttribute("name", curKey.getKeyName());
                uniqueKeyElmt.setAttribute("noderef", currentEntity.toString());
                keys.appendChild(uniqueKeyElmt);
                for (UniqueIndexable curElem : curKey.getElements()) {
                    if (curElem instanceof EntityAttribute) {
                        Element attributeElmt = document.createElement("attref");
                        attributeElmt.setAttribute("name", curElem.getName());
                        uniqueKeyElmt.appendChild(attributeElmt);
                    } else if (curElem instanceof SketchEdge) {
                        Element edgeElmt = document.createElement("edgekeyref");
                        edgeElmt.setAttribute("id", curElem.getName());
                        uniqueKeyElmt.appendChild(edgeElmt);
                    } else {
                        System.err.println("Unknown unique key item encountered: element '" + curElem.getName() + "' is neither EntityAttribute nor SketchEdge");
                    }
                }
            }
        }
        rootElement.appendChild(keys);
        Element constraints = document.createElement("constraints");
        // Now add the constraints
        for (ModelConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> curConstraint : sketch.getConstraints().values()) {
            Element thisConstraint = document.createElement(curConstraint.getType());
            thisConstraint.setAttribute("x", curConstraint.getX() + "");
            thisConstraint.setAttribute("y", curConstraint.getY() + "");
            thisConstraint.setAttribute("isVisible", curConstraint.isVisible() ? "true" : "false");
            if (curConstraint instanceof LimitConstraint) {
                LimitConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> lc = (LimitConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge>) curConstraint;
                // TODO A better way? really long
                // cone - AB
                Element pathElem = document.createElement("path");
                pathElem.setAttribute("domain", lc.getCone().AB.getDomain().getName());
                pathElem.setAttribute("codomain", lc.getCone().AB.getCoDomain().getName());
                for (SketchEdge edge : lc.getCone().AB.getEdges()) {
                    Element edgeElem = document.createElement("edgeref");
                    edgeElem.setAttribute("id", edge.getName());
                    pathElem.appendChild(edgeElem);
                }
                thisConstraint.appendChild(pathElem);
                // cone - BC
                pathElem = document.createElement("path");
                pathElem.setAttribute("domain", lc.getCone().BC.getDomain().getName());
                pathElem.setAttribute("codomain", lc.getCone().BC.getCoDomain().getName());
                for (SketchEdge edge : lc.getCone().BC.getEdges()) {
                    Element edgeElem = document.createElement("edgeref");
                    edgeElem.setAttribute("id", edge.getName());
                    pathElem.appendChild(edgeElem);
                }
                thisConstraint.appendChild(pathElem);
                // cone - AC
                pathElem = document.createElement("path");
                pathElem.setAttribute("domain", lc.getCone().AC.getDomain().getName());
                pathElem.setAttribute("codomain", lc.getCone().AC.getCoDomain().getName());
                for (SketchEdge edge : lc.getCone().AC.getEdges()) {
                    Element edgeElem = document.createElement("edgeref");
                    edgeElem.setAttribute("id", edge.getName());
                    pathElem.appendChild(edgeElem);
                }
                thisConstraint.appendChild(pathElem);
                // limit cone 1 - AB
                pathElem = document.createElement("path");
                pathElem.setAttribute("domain", lc.getLimitCone1().AB.getDomain().getName());
                pathElem.setAttribute("codomain", lc.getLimitCone1().AB.getCoDomain().getName());
                for (SketchEdge edge : lc.getLimitCone1().AB.getEdges()) {
                    Element edgeElem = document.createElement("edgeref");
                    edgeElem.setAttribute("id", edge.getName());
                    pathElem.appendChild(edgeElem);
                }
                thisConstraint.appendChild(pathElem);
                // limit cone 1 - BC
                pathElem = document.createElement("path");
                pathElem.setAttribute("domain", lc.getLimitCone1().BC.getDomain().getName());
                pathElem.setAttribute("codomain", lc.getLimitCone1().BC.getCoDomain().getName());
                for (SketchEdge edge : lc.getLimitCone1().BC.getEdges()) {
                    Element edgeElem = document.createElement("edgeref");
                    edgeElem.setAttribute("id", edge.getName());
                    pathElem.appendChild(edgeElem);
                }
                thisConstraint.appendChild(pathElem);
                // limit cone 1 - AC
                pathElem = document.createElement("path");
                pathElem.setAttribute("domain", lc.getLimitCone1().AC.getDomain().getName());
                pathElem.setAttribute("codomain", lc.getLimitCone1().AC.getCoDomain().getName());
                for (SketchEdge edge : lc.getLimitCone1().AC.getEdges()) {
                    Element edgeElem = document.createElement("edgeref");
                    edgeElem.setAttribute("id", edge.getName());
                    pathElem.appendChild(edgeElem);
                }
                thisConstraint.appendChild(pathElem);
                // limit cone 2 - AB
                pathElem = document.createElement("path");
                pathElem.setAttribute("domain", lc.getLimitCone2().AB.getDomain().getName());
                pathElem.setAttribute("codomain", lc.getLimitCone2().AB.getCoDomain().getName());
                for (SketchEdge edge : lc.getLimitCone2().AB.getEdges()) {
                    Element edgeElem = document.createElement("edgeref");
                    edgeElem.setAttribute("id", edge.getName());
                    pathElem.appendChild(edgeElem);
                }
                thisConstraint.appendChild(pathElem);
                // limit cone 2 - BC
                pathElem = document.createElement("path");
                pathElem.setAttribute("domain", lc.getLimitCone2().BC.getDomain().getName());
                pathElem.setAttribute("codomain", lc.getLimitCone2().BC.getCoDomain().getName());
                for (SketchEdge edge : lc.getLimitCone2().BC.getEdges()) {
                    Element edgeElem = document.createElement("edgeref");
                    edgeElem.setAttribute("id", edge.getName());
                    pathElem.appendChild(edgeElem);
                }
                thisConstraint.appendChild(pathElem);
                // limit cone 2 - AC
                pathElem = document.createElement("path");
                pathElem.setAttribute("domain", lc.getLimitCone2().AC.getDomain().getName());
                pathElem.setAttribute("codomain", lc.getLimitCone2().AC.getCoDomain().getName());
                for (SketchEdge edge : lc.getLimitCone2().AC.getEdges()) {
                    Element edgeElem = document.createElement("edgeref");
                    edgeElem.setAttribute("id", edge.getName());
                    pathElem.appendChild(edgeElem);
                }
                thisConstraint.appendChild(pathElem);
                // Add constraint to constraints
                constraints.appendChild(thisConstraint);
                continue;
            }
            for (ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> path : curConstraint.getPaths()) {
                // Add pathref to constraint
                Element pathElem = document.createElement("path");
                pathElem.setAttribute("domain", path.getDomain().getName());
                pathElem.setAttribute("codomain", path.getCoDomain().getName());
                for (SketchEdge edge : path.getEdges()) {
                    Element edgeElem = document.createElement("edgeref");
                    edgeElem.setAttribute("id", edge.getName());
                    pathElem.appendChild(edgeElem);
                }
                thisConstraint.appendChild(pathElem);
            }
            // Add constraint to constraints
            constraints.appendChild(thisConstraint);
        }
        rootElement.appendChild(constraints);
        return rootElement;
    } catch (Exception e) {
        return null;
    }
}
Also used : LimitConstraint(easik.model.constraint.LimitConstraint) EntityAttribute(easik.model.attribute.EntityAttribute) Element(org.w3c.dom.Element) PartialEdge(easik.sketch.edge.PartialEdge) EntityNode(easik.sketch.vertex.EntityNode) SketchGraphModel(easik.sketch.util.graph.SketchGraphModel) SketchFrame(easik.ui.SketchFrame) InjectiveEdge(easik.sketch.edge.InjectiveEdge) SketchEdge(easik.sketch.edge.SketchEdge) Sketch(easik.sketch.Sketch) EasikType(easik.database.types.EasikType) DocumentInfo(easik.DocumentInfo) UniqueIndexable(easik.model.keys.UniqueIndexable)

Example 10 with SketchFrame

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

the class MySQLExporter method createConstraint.

/**
 * @param constraint
 * @param id
 *
 * @return
 */
@Override
public List<String> createConstraint(final PullbackConstraint<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> constraint, final String id) {
    final List<String> sql = new LinkedList<>();
    String tmpS = null;
    // Create the trigger for inserting into the pullback table:
    final EntityNode pb = constraint.getSource();
    final EntityNode target = constraint.getTarget();
    final String BASE_PATH_NAME = "_pathId";
    final int constraintWidth = constraint.getWidth();
    final String insConName = "pullbackConstraint" + id + "Insert" + cleanId(pb);
    StringBuilder proc = new StringBuilder(500);
    proc.append("CREATE PROCEDURE ");
    proc.append(quoteId(insConName));
    proc.append("(_newPBid ");
    proc.append(pkType());
    proc.append(") BEGIN");
    proc.append(lineSep);
    proc.append("   DECLARE ");
    for (int i = 0; i < constraintWidth; i++) {
        proc.append(BASE_PATH_NAME);
        proc.append(i);
        if (i != constraintWidth - 1) {
            proc.append(", ");
        } else {
            proc.append(" ");
        }
    }
    proc.append(pkType()).append(';').append(lineSep);
    final String basePK = qualifiedPK(pb);
    final String targetPK = qualifiedPK(target);
    for (int i = 0; i < constraintWidth; i++) {
        proc.append("       SELECT ").append(targetPK).append(" INTO " + BASE_PATH_NAME + i).append(lineSep).append("           FROM ").append(joinPath(constraint.getFullPath(i))).append(lineSep).append("           WHERE ").append(basePK).append(" = _newPBid;").append(lineSep);
    }
    // Make sure the paths match
    proc.append("   IF NOT");
    for (int i = 1; i < constraintWidth; i++) {
        proc.append(" (" + BASE_PATH_NAME + (i - 1) + " <=> " + BASE_PATH_NAME + i + ')');
        if (i != constraintWidth - 1) {
            proc.append(" OR NOT");
        }
    }
    proc.append(" THEN").append(lineSep).append("               CALL constraint_failure('Invalid entry in pullback constraint.');").append(lineSep).append("       END IF;").append(lineSep);
    addFail = true;
    final String pbQ = quoteId(pb);
    SketchEdge[] firstEdges = new SketchEdge[constraintWidth];
    for (int i = 0; i < constraintWidth; i++) {
        firstEdges[i] = constraint.getFullPath(i).getFirstEdge();
    }
    if (!addedUnique.isUnique(pb, firstEdges)) {
        tmpS = "ALTER TABLE " + pbQ + " ADD UNIQUE " + quoteId("pullbackConstraint" + id + "UniqueIndex") + " (";
        for (int i = 0; i < constraintWidth; i++) {
            tmpS += quoteId(tableFK(firstEdges[i]));
            if (i != constraintWidth - 1) {
                tmpS += ", ";
            }
        }
        tmpS += ')' + $;
        sql.add(tmpS);
        addedUnique.add(pb, firstEdges);
    }
    proc.append("END");
    sql.add(proc.toString());
    addTrigger(pb, "AFTER INSERT", "CALL " + quoteId(insConName) + "(NEW." + quoteId(tablePK(pb)) + ')');
    for (int i = 0; i < constraintWidth; i++) {
        ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> tar = constraint.getTargetPath(i);
        ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> pro = constraint.getProjectionPath(i);
        pbAfterInsertProcs(sql, id, constraint, tar, pro, i);
    }
    // Finally, the before-delete on the pullback:
    final String delConName = "pullbackConstraint" + id + "Delete" + cleanId(pb);
    proc = new StringBuilder(500);
    proc.append("CREATE PROCEDURE ").append(quoteId(delConName)).append("(toDeleteId ").append(pkType()).append(") BEGIN").append(lineSep);
    proc.append("   DELETE ").append(quoteId(target)).append(" FROM ").append(pbQ).append(" JOIN ").append('(');
    for (int i = 0; i < constraintWidth; i++) {
        LinkedList<SketchEdge> tmpPath = new LinkedList<>(constraint.getFullPath(i).getEdges());
        SketchEdge firstEdge = tmpPath.removeFirst();
        @SuppressWarnings("unused") SketchEdge lastEdge = tmpPath.getLast();
        @SuppressWarnings("unused") SketchEdge secondEdge = tmpPath.removeFirst();
        EntityNode first = firstEdge.getTargetEntity();
        proc.append(quoteId(first));
        if (i != constraintWidth - 1) {
            proc.append(", ");
        }
    }
    proc.append(") ON ");
    for (int i = 0; i < constraintWidth; i++) {
        LinkedList<SketchEdge> tmpPath = new LinkedList<>(constraint.getFullPath(i).getEdges());
        SketchEdge firstEdge = tmpPath.removeFirst();
        @SuppressWarnings("unused") SketchEdge lastEdge = tmpPath.getLast();
        @SuppressWarnings("unused") SketchEdge secondEdge = tmpPath.removeFirst();
        EntityNode first = firstEdge.getTargetEntity();
        proc.append(qualifiedFK(firstEdge)).append(" = ").append(qualifiedPK(first));
        if (i != constraintWidth - 1) {
            proc.append(" AND ");
        }
    }
    for (int i = 0; i < constraintWidth; i++) {
        LinkedList<SketchEdge> tmpPath = new LinkedList<>(constraint.getFullPath(i).getEdges());
        SketchEdge firstEdge = tmpPath.removeFirst();
        @SuppressWarnings("unused") SketchEdge lastEdge = tmpPath.getLast();
        SketchEdge secondEdge = tmpPath.removeFirst();
        @SuppressWarnings("unused") EntityNode first = firstEdge.getTargetEntity();
        if (tmpPath.size() > 0) {
            proc.append(" JOIN ").append(joinPath(tmpPath, false, qualifiedFK(secondEdge) + " = " + qualifiedPK(secondEdge.getTargetEntity())));
        }
    }
    proc.append(" JOIN ").append(quoteId(target)).append(" ON ");
    for (int i = 0; i < constraintWidth; i++) {
        LinkedList<SketchEdge> tmpPath = new LinkedList<>(constraint.getFullPath(i).getEdges());
        SketchEdge firstEdge = tmpPath.removeFirst();
        SketchEdge lastEdge = tmpPath.getLast();
        @SuppressWarnings("unused") SketchEdge secondEdge = tmpPath.removeFirst();
        @SuppressWarnings("unused") EntityNode first = firstEdge.getTargetEntity();
        proc.append(qualifiedFK(lastEdge)).append(" = ").append(qualifiedPK(target));
        if (i != constraintWidth - 1) {
            proc.append(" AND ");
        }
    }
    proc.append(lineSep).append("           WHERE ").append(qualifiedPK(pb)).append(" = toDeleteId;").append(lineSep).append("END");
    sql.add(proc.toString());
    addTrigger(pb, "BEFORE DELETE", "CALL " + quoteId(delConName) + "(OLD." + quoteId(tablePK(pb)) + ')');
    // Federico Mora
    int j = 0;
    // c.b_id = b.id JOIN d ON d.c_id = c.id...> WHERE <begin> = id;
    for (int i = 0; i < constraintWidth; i++) {
        ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> p = constraint.getProjectionPath(i);
        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 (int k = 0; k < constraintWidth; k++) {
            ModelPath<SketchFrame, SketchGraphModel, Sketch, EntityNode, SketchEdge> q = constraint.getProjectionPath(k);
            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 Update" + constraint.getID() + "Proj" + (j - 1) + "()");
            addTrigger(p.getCoDomain(), "AFTER UPDATE", "CALL Update" + constraint.getID() + "Proj" + (j - 1) + "()");
        }
    }
    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

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