-/*******************************************************************************\r
- * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.db.layer0.adapter.impl;\r
-\r
-import org.eclipse.core.runtime.IProgressMonitor;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.WriteGraph;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.layer0.util.ModelTransferableGraphSource;\r
-import org.simantics.db.layer0.util.ModelTransferableGraphSourceRequest;\r
-import org.simantics.db.layer0.util.TransferableGraphConfiguration2;\r
-import org.simantics.db.service.SerialisationSupport;\r
-import org.simantics.graph.db.TransferableGraphSource.TransferableGraphSourceProcedure;\r
-\r
-/**\r
- * @author Tuukka Lehtonen\r
- */\r
-public class TGRemover extends AbstractRemover {\r
-\r
- @SuppressWarnings("unused")\r
- private IProgressMonitor monitor;\r
-\r
- public TGRemover(Resource resource) {\r
- super(resource);\r
- }\r
-\r
- public TGRemover(IProgressMonitor monitor, Resource resource) {\r
- super(resource);\r
- this.monitor = monitor;\r
- }\r
-\r
- @Override\r
- public void remove(final WriteGraph graph) throws DatabaseException {\r
- \r
- TransferableGraphConfiguration2 conf = new TransferableGraphConfiguration2(graph, resource);\r
- conf.values = false;\r
- ModelTransferableGraphSource source = graph.syncRequest(new ModelTransferableGraphSourceRequest(conf));\r
- final SerialisationSupport ss = graph.getService(SerialisationSupport.class);\r
- \r
- try {\r
- source.forResourceStatements(graph, new TransferableGraphSourceProcedure<int[]>() {\r
- \r
- @Override\r
- public void execute(int[] value) throws Exception {\r
- Resource s = ss.getResource(value[0]);\r
- Resource p = ss.getResource(value[1]);\r
- Resource i = null;\r
- if(value[2] != -1) i = ss.getResource(value[2]);\r
- Resource o = ss.getResource(value[3]);\r
- \r
-// System.err.println("s=" + s + " p=" + graph.getPossibleURI(p) + " o=" + o + " p:" + p + " i:" + i);\r
- \r
- graph.deny(s,p,i,o);\r
- }\r
- \r
- });\r
- \r
- source.forValueResources(graph, new TransferableGraphSourceProcedure<int[]>() {\r
- \r
- @Override\r
- public void execute(int[] value) throws Exception {\r
- Resource s = ss.getResource(value[0]);\r
-// System.err.println("s=" + s + " p=" + graph.getPossibleURI(p) + " o=" + o + " " + i);\r
- graph.denyValue(s);\r
- }\r
- \r
- });\r
- \r
- source.closeStreams();\r
- \r
- } catch (Exception e) {\r
- throw new DatabaseException(e);\r
- }\r
- \r
- }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+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
+ */
+public class TGRemover extends AbstractRemover {
+
+ @SuppressWarnings("unused")
+ private IProgressMonitor monitor;
+
+ private ArrayList<Resource> roots = new ArrayList<>();
+
+ public TGRemover(Resource resource) {
+ super(resource);
+ }
+
+ public TGRemover(IProgressMonitor monitor, Resource resource) {
+ super(resource);
+ this.monitor = monitor;
+ }
+
+ public List<Resource> getRoots() {
+ return roots;
+ }
+
+ @Override
+ public void remove(final WriteGraph graph) throws DatabaseException {
+
+ 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);
+ for (Statement stm : dqs.getDirectPersistentStatements(graph, name)) {
+ 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);
+ }
+
+ }
+
+}