]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/TGRemover.java
Better emptying of trash bin
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / adapter / impl / TGRemover.java
index 88594883488f71059dd6ef128b53e9d5176ce85c..be34bb9ba5165c9714e3e8573caed16077ccbcd6 100644 (file)
  *******************************************************************************/
 package org.simantics.db.layer0.adapter.impl;
 
+import java.util.ArrayList;
+import java.util.List;
+
 import org.eclipse.core.runtime.IProgressMonitor;
 import org.simantics.db.Resource;
+import org.simantics.db.Statement;
 import org.simantics.db.WriteGraph;
+import org.simantics.db.common.procedure.adapter.DirectStatementProcedure;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.layer0.util.ModelTransferableGraphSource;
 import org.simantics.db.layer0.util.ModelTransferableGraphSourceRequest;
 import org.simantics.db.layer0.util.TransferableGraphConfiguration2;
+import org.simantics.db.service.DirectQuerySupport;
 import org.simantics.db.service.SerialisationSupport;
 import org.simantics.graph.db.TransferableGraphSource.TransferableGraphSourceProcedure;
+import org.simantics.graph.representation.Identity;
+import org.simantics.graph.representation.Internal;
+import org.simantics.graph.representation.Root;
+import org.simantics.layer0.Layer0;
 
 /**
  * @author Tuukka Lehtonen
@@ -27,7 +37,9 @@ import org.simantics.graph.db.TransferableGraphSource.TransferableGraphSourcePro
 public class TGRemover extends AbstractRemover {
 
     @SuppressWarnings("unused")
-    private IProgressMonitor monitor;
+    private IProgressMonitor    monitor;
+
+    private ArrayList<Resource> roots = new ArrayList<>();
 
     public TGRemover(Resource resource) {
         super(resource);
@@ -38,46 +50,78 @@ public class TGRemover extends AbstractRemover {
         this.monitor = monitor;
     }
 
+    public List<Resource> getRoots() {
+        return roots;
+    }
+
     @Override
     public void remove(final WriteGraph graph) throws DatabaseException {
-       
-       TransferableGraphConfiguration2 conf = new TransferableGraphConfiguration2(graph, resource);
-       conf.values = false;
-       final SerialisationSupport ss = graph.getService(SerialisationSupport.class);
-       
-       try (ModelTransferableGraphSource source = graph.syncRequest(new ModelTransferableGraphSourceRequest(conf))) {
-                       source.forResourceStatements(graph, new TransferableGraphSourceProcedure<int[]>() {
-                               
-                               @Override
-                               public void execute(int[] value) throws Exception {
-                                       Resource s = ss.getResource(value[0]);
-                                       Resource p = ss.getResource(value[1]);
-                                       Resource i = null;
-                                       if(value[2] != -1) i = ss.getResource(value[2]);
-                                       Resource o = ss.getResource(value[3]);
-                                       
-//                                     System.err.println("s=" + s + " p=" + graph.getPossibleURI(p) + " o=" + o + " p:" + p + " i:" + i);
-                                       
-                                       graph.deny(s,p,i,o);
-                               }
-                               
-                       });
-                       
-                       source.forValueResources(graph, new TransferableGraphSourceProcedure<int[]>() {
-                               
-                               @Override
-                               public void execute(int[] value) throws Exception {
-                                       Resource s = ss.getResource(value[0]);
-//                                     System.err.println("s=" + s + " p=" + graph.getPossibleURI(p) + " o=" + o + " " + i);
-                                       graph.denyValue(s);
-                               }
-                               
-                       });
-                       
-               } catch (Exception e) {
-                       throw new DatabaseException(e);
-               }
-       
+
+        Layer0 L0 = Layer0.getInstance(graph);
+
+        TransferableGraphConfiguration2 conf = new TransferableGraphConfiguration2(graph, resource);
+        conf.values = false;
+        final SerialisationSupport ss = graph.getService(SerialisationSupport.class);
+        final DirectQuerySupport dqs = graph.getService(DirectQuerySupport.class);
+
+        try (ModelTransferableGraphSource source = graph.syncRequest(new ModelTransferableGraphSourceRequest(conf))) {
+
+            long[] rev = source.getResourceArray(graph);
+
+            source.forIdentities(graph, new TransferableGraphSourceProcedure<Identity>() {
+
+                @Override
+                public void execute(Identity value) throws Exception {
+                    if (value.definition instanceof Internal) {
+                        long res = rev[value.resource];
+                        Resource r = ss.getResource(res);
+                        Resource name = graph.getPossibleObject(r, L0.HasName);
+                        if (name != null) {
+                            graph.deny(r, L0.HasName, L0.NameOf, name);
+                            graph.denyValue(name);
+                            DirectStatementProcedure proc = new DirectStatementProcedure();
+                            dqs.forEachDirectPersistentStatement(graph, name, proc);
+                            for (Statement stm : proc.getOrThrow()) {
+                                graph.deny(name, stm.getPredicate(), stm.getObject());
+                            }
+                        }
+                    } else if (value.definition instanceof Root) {
+                        long res = rev[value.resource];
+                        Resource r = ss.getResource(res);
+                        roots.add(r);
+                    }
+                }
+
+            });
+
+            source.forResourceStatements(graph, new TransferableGraphSourceProcedure<int[]>() {
+
+                @Override
+                public void execute(int[] value) throws Exception {
+                    Resource s = ss.getResource(value[0]);
+                    Resource p = ss.getResource(value[1]);
+                    Resource i = null;
+                    if (value[2] != -1)
+                        i = ss.getResource(value[2]);
+                    Resource o = ss.getResource(value[3]);
+                    graph.deny(s, p, i, o);
+                }
+
+            });
+
+            source.forValueResources(graph, new TransferableGraphSourceProcedure<int[]>() {
+
+                @Override
+                public void execute(int[] value) throws Exception {
+                    graph.denyValue(ss.getResource(value[0]));
+                }
+
+            });
+
+        } catch (Exception e) {
+            throw new DatabaseException(e);
+        }
+
     }
 
 }