]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/TGRemover.java
Merge "Better emptying of trash bin"
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / adapter / impl / TGRemover.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.layer0.adapter.impl;
13
14 import java.util.ArrayList;
15 import java.util.List;
16
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;
33
34 /**
35  * @author Tuukka Lehtonen
36  */
37 public class TGRemover extends AbstractRemover {
38
39     @SuppressWarnings("unused")
40     private IProgressMonitor    monitor;
41
42     private ArrayList<Resource> roots = new ArrayList<>();
43
44     public TGRemover(Resource resource) {
45         super(resource);
46     }
47
48     public TGRemover(IProgressMonitor monitor, Resource resource) {
49         super(resource);
50         this.monitor = monitor;
51     }
52
53     public List<Resource> getRoots() {
54         return roots;
55     }
56
57     @Override
58     public void remove(final WriteGraph graph) throws DatabaseException {
59
60         Layer0 L0 = Layer0.getInstance(graph);
61
62         TransferableGraphConfiguration2 conf = new TransferableGraphConfiguration2(graph, resource);
63         conf.values = false;
64         final SerialisationSupport ss = graph.getService(SerialisationSupport.class);
65         final DirectQuerySupport dqs = graph.getService(DirectQuerySupport.class);
66
67         try (ModelTransferableGraphSource source = graph.syncRequest(new ModelTransferableGraphSourceRequest(conf))) {
68
69             long[] rev = source.getResourceArray(graph);
70
71             source.forIdentities(graph, new TransferableGraphSourceProcedure<Identity>() {
72
73                 @Override
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);
79                         if (name != null) {
80                             graph.deny(r, L0.HasName, L0.NameOf, name);
81                             graph.denyValue(name);
82                             DirectStatementProcedure proc = new DirectStatementProcedure();
83                             dqs.forEachDirectPersistentStatement(graph, name, proc);
84                             for (Statement stm : proc.getOrThrow()) {
85                                 graph.deny(name, stm.getPredicate(), stm.getObject());
86                             }
87                         }
88                     } else if (value.definition instanceof Root) {
89                         long res = rev[value.resource];
90                         Resource r = ss.getResource(res);
91                         roots.add(r);
92                     }
93                 }
94
95             });
96
97             source.forResourceStatements(graph, new TransferableGraphSourceProcedure<int[]>() {
98
99                 @Override
100                 public void execute(int[] value) throws Exception {
101                     Resource s = ss.getResource(value[0]);
102                     Resource p = ss.getResource(value[1]);
103                     Resource i = null;
104                     if (value[2] != -1)
105                         i = ss.getResource(value[2]);
106                     Resource o = ss.getResource(value[3]);
107                     graph.deny(s, p, i, o);
108                 }
109
110             });
111
112             source.forValueResources(graph, new TransferableGraphSourceProcedure<int[]>() {
113
114                 @Override
115                 public void execute(int[] value) throws Exception {
116                     graph.denyValue(ss.getResource(value[0]));
117                 }
118
119             });
120
121         } catch (Exception e) {
122             throw new DatabaseException(e);
123         }
124
125     }
126
127 }