1 package org.simantics.issues.common;
3 import java.util.Collections;
4 import java.util.HashSet;
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 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
21 public class DependencyIssueSynchronizer2 extends WriteRequest {
23 private static final Logger LOGGER = LoggerFactory.getLogger(DependencyIssueSynchronizer2.class);
24 public static final boolean DEBUG = false;
26 private Resource resource;
27 private Resource source;
29 public DependencyIssueSynchronizer2(Resource resource, Resource source) {
30 this.resource = resource;
35 public void perform(WriteGraph graph) throws DatabaseException {
37 if(DEBUG) LOGGER.info("Running DependencySynchronizer for " + resource);
39 Set<Issue> existing = graph.syncRequest(new DependencyIssueDescriptions(source, resource), TransientCacheListener.<Set<Issue>>instance());
40 Set<Issue> contexts = Collections.emptySet();
42 if(graph.hasStatement(resource)) {
43 contexts = graph.syncRequest(new Contexts(source, resource), TransientCacheListener.<Set<Issue>>instance());
46 if(!existing.equals(contexts)) {
48 Set<Issue> added = new HashSet<Issue>(contexts);
49 Set<Issue> removed = new HashSet<Issue>(existing);
50 added.removeAll(existing);
51 removed.removeAll(contexts);
53 for(Issue add : added) {
54 add.write(graph, source);
56 for(Issue remove : removed) {
57 Resource issue = graph.sync(new IssueByList(source, remove));
59 // FIXME: when can this happen and what should be done in this case?
61 graph.deny(issue, Layer0.getInstance(graph).PartOf);
62 graph.deny(source, IssueResource.getInstance(graph).IssueSource_Manages, issue);
63 RemoverUtil.remove(graph, issue);