]> gerrit.simantics Code Review - simantics/platform.git/blob
1cb22d57453b46853154ef234de345475d3d285e
[simantics/platform.git] /
1 package org.simantics.issues.common;
2
3 import java.util.Collections;
4 import java.util.HashSet;
5 import java.util.Set;
6
7 import org.simantics.db.Issue;
8 import org.simantics.db.Resource;
9 import org.simantics.db.WriteGraph;
10 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
11 import org.simantics.db.common.request.WriteRequest;
12 import org.simantics.db.exception.DatabaseException;
13 import org.simantics.db.layer0.util.RemoverUtil;
14 import org.simantics.issues.common.DependencyIssueValidator2.Contexts;
15 import org.simantics.issues.common.DependencyIssueValidator2.DependencyIssueDescriptions;
16 import org.simantics.issues.ontology.IssueResource;
17 import org.simantics.layer0.Layer0;
18
19 public class DependencyIssueSynchronizer2 extends WriteRequest {
20
21         public static final boolean DEBUG = false;
22         
23         private Resource resource;
24         private Resource source;
25
26         public DependencyIssueSynchronizer2(Resource resource, Resource source) {
27                 this.resource = resource;
28                 this.source = source;
29         }
30
31         @Override
32         public void perform(WriteGraph graph) throws DatabaseException {
33
34                 if(DEBUG) System.err.println("Running DependencySynchronizer for " + resource);
35
36                 Set<Issue> existing = graph.syncRequest(new DependencyIssueDescriptions(source, resource), TransientCacheListener.<Set<Issue>>instance());
37                 Set<Issue> contexts = Collections.emptySet();
38                 
39                 if(graph.hasStatement(resource)) {
40                         contexts = graph.syncRequest(new Contexts(source, resource), TransientCacheListener.<Set<Issue>>instance());
41                 }
42                 
43                 if(!existing.equals(contexts)) {
44                 
45                         Set<Issue> added = new HashSet<Issue>(contexts);
46                         Set<Issue> removed = new HashSet<Issue>(existing);
47                         added.removeAll(existing);
48                         removed.removeAll(contexts);
49
50                         for(Issue add : added) {
51                                 add.write(graph, source);
52                         }
53                         for(Issue remove : removed) {
54                                 Resource issue = graph.sync(new IssueByList(source, remove));
55                                 if (issue == null)
56                                         // FIXME: when can this happen and what should be done in this case?
57                                         continue;
58                                 graph.deny(issue, Layer0.getInstance(graph).PartOf);
59                                 graph.deny(source, IssueResource.getInstance(graph).IssueSource_Manages, issue);
60                                 RemoverUtil.remove(graph, issue);
61                         }
62                         
63                 }
64
65         }
66
67 }