package se.lth.cs.connect.events;

import java.math.BigDecimal;

import iot.jcypher.query.JcQueryResult;
import iot.jcypher.query.api.IClause;
import iot.jcypher.query.factories.clause.DO;
import iot.jcypher.query.factories.clause.MATCH;
import iot.jcypher.query.factories.clause.RETURN;
import iot.jcypher.query.factories.clause.WHERE;
import iot.jcypher.query.values.JcNode;
import iot.jcypher.query.values.JcNumber;
import iot.jcypher.query.values.JcRelation;
import se.lth.cs.connect.modules.Database;



public class DeleteCollectionEvent implements UserEvent {
    private long cid;

    public DeleteCollectionEvent(long id) {
        this.cid = id;
    }

    @Override
	public void execute() {
        detachMembers();
        detachEntries();
        destroy();
    }

     private void detachMembers() {
        JcNode collection = new JcNode("c");
        JcRelation rel = new JcRelation("m");

        Database.query(Database.access(), new IClause[]{
            MATCH.node(collection).label("collection")
                .relation(rel).node().label("user"),
            WHERE.valueOf(collection.id()).EQUALS(cid),
            DO.DELETE(rel)
        });
    }

    private void detachEntries() {
        JcNode entry = new JcNode("e");
        JcNode collection = new JcNode("c");
        JcNumber entryId = new JcNumber("x");

        /* A more efficient variant that only returns the entries which
           must be deleted (since they will be orphaned).

            OPTIONAL MATCH (e:entry)-[r]-(c)
            WHERE id(c) = XYZ
                AND NOT
                EXISTS((c)--(e)<-[:CONTAINS]-(:collection))
            DETACH DELETE c
            RETURN id(e)
        */
        JcQueryResult res = Database.query(Database.access(), new IClause[]{
            MATCH.node(collection).label("collection")
                .relation().node(entry).label("entry"),
            WHERE.valueOf(collection.id()).EQUALS(cid),
            RETURN.value(entry.id()).AS(entryId)
        });

        for (BigDecimal eid : res.resultOf(entryId))
            new DetachEntryEvent(cid, eid.longValue()).execute();
    }

    private void destroy() {
        JcNode collection = new JcNode("c");

        Database.query(Database.access(), new IClause[]{
            MATCH.node(collection).label("collection"),
            WHERE.valueOf(collection.id()).EQUALS(cid),
            DO.DELETE(collection)
        });
    }
}
@@ -28,16 +28,37 @@
 vents;%0A%0A
+import java.io.File;%0A
 import j
@@ -520,16 +520,61 @@
 tabase;%0A
+import se.lth.cs.connect.modules.TaxonomyDB;%0A
 %0A%0A%0Apubli
@@ -808,24 +808,50 @@
 hEntries();%0A
+        deleteTaxonomy();%0A
         dest
@@ -2202,32 +2202,502 @@
 execute();%0A    %7D
+%0A    %0A    private void deleteTaxonomy() %7B%0A        JcNode collection = new JcNode(%22c%22);%0A        JcNode taxonomy = new JcNode(%22t%22);%0A        %0A    %09Database.query(Database.access(), new IClause%5B%5D%7B%0A            MATCH.node(collection).label(%22collection%22)%0A                .relation().node(taxonomy).label(%22taxonomy%22),%0A            WHERE.valueOf(collection.id()).EQUALS(cid),%0A            DO.DELETE(taxonomy)%0A        %7D);%0A    %09%0A    %09new File(TaxonomyDB.getPath(cid)).delete();%0A    %7D
 %0A%0A    private vo

Add taxonomy cleanup on collection delete