1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.layer0.adapter.impl;
14 import java.util.ArrayList;
15 import java.util.List;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.simantics.db.Resource;
19 import org.simantics.db.Statement;
20 import org.simantics.db.WriteGraph;
21 import org.simantics.db.common.procedure.adapter.DirectStatementProcedure;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.db.layer0.util.ModelTransferableGraphSource;
24 import org.simantics.db.layer0.util.ModelTransferableGraphSourceRequest;
25 import org.simantics.db.layer0.util.TransferableGraphConfiguration2;
26 import org.simantics.db.service.DirectQuerySupport;
27 import org.simantics.db.service.SerialisationSupport;
28 import org.simantics.graph.db.TransferableGraphSource.TransferableGraphSourceProcedure;
29 import org.simantics.graph.representation.Identity;
30 import org.simantics.graph.representation.Internal;
31 import org.simantics.graph.representation.Root;
32 import org.simantics.layer0.Layer0;
35 * @author Tuukka Lehtonen
37 public class TGRemover extends AbstractRemover {
39 @SuppressWarnings("unused")
40 private IProgressMonitor monitor;
42 private ArrayList<Resource> roots = new ArrayList<>();
44 public TGRemover(Resource resource) {
48 public TGRemover(IProgressMonitor monitor, Resource resource) {
50 this.monitor = monitor;
53 public List<Resource> getRoots() {
58 public void remove(final WriteGraph graph) throws DatabaseException {
60 Layer0 L0 = Layer0.getInstance(graph);
62 TransferableGraphConfiguration2 conf = new TransferableGraphConfiguration2(graph, resource);
64 final SerialisationSupport ss = graph.getService(SerialisationSupport.class);
65 final DirectQuerySupport dqs = graph.getService(DirectQuerySupport.class);
67 try (ModelTransferableGraphSource source = graph.syncRequest(new ModelTransferableGraphSourceRequest(conf))) {
69 long[] rev = source.getResourceArray(graph);
71 source.forIdentities(graph, new TransferableGraphSourceProcedure<Identity>() {
74 public void execute(Identity value) throws Exception {
75 if (value.definition instanceof Internal) {
76 long res = rev[value.resource];
77 Resource r = ss.getResource(res);
78 Resource name = graph.getPossibleObject(r, L0.HasName);
80 graph.deny(r, L0.HasName, L0.NameOf, name);
81 graph.denyValue(name);
82 for (Statement stm : dqs.getDirectPersistentStatements(graph, name)) {
83 graph.deny(name, stm.getPredicate(), stm.getObject());
86 } else if (value.definition instanceof Root) {
87 long res = rev[value.resource];
88 Resource r = ss.getResource(res);
95 source.forResourceStatements(graph, new TransferableGraphSourceProcedure<int[]>() {
98 public void execute(int[] value) throws Exception {
99 Resource s = ss.getResource(value[0]);
100 Resource p = ss.getResource(value[1]);
103 i = ss.getResource(value[2]);
104 Resource o = ss.getResource(value[3]);
105 graph.deny(s, p, i, o);
110 source.forValueResources(graph, new TransferableGraphSourceProcedure<int[]>() {
113 public void execute(int[] value) throws Exception {
114 graph.denyValue(ss.getResource(value[0]));
119 } catch (Exception e) {
120 throw new DatabaseException(e);