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.issues.common;
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.HashSet;
20 import org.simantics.databoard.Bindings;
21 import org.simantics.db.ReadGraph;
22 import org.simantics.db.Resource;
23 import org.simantics.db.ResourceSet;
24 import org.simantics.db.VirtualGraph;
25 import org.simantics.db.WriteGraph;
26 import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;
27 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
28 import org.simantics.db.common.request.BinaryRead;
29 import org.simantics.db.common.request.DependentInstances3;
30 import org.simantics.db.common.request.ResourceSetGraph;
31 import org.simantics.db.common.request.WriteRequest;
32 import org.simantics.db.common.utils.NameUtils;
33 import org.simantics.db.exception.DatabaseException;
34 import org.simantics.db.layer0.changeset.MetadataUtils;
35 import org.simantics.db.layer0.genericrelation.DependencyChanges;
36 import org.simantics.db.layer0.genericrelation.DependencyChanges.Change;
37 import org.simantics.db.layer0.genericrelation.DependencyChanges.ComponentAddition;
38 import org.simantics.db.layer0.genericrelation.DependencyChanges.ComponentModification;
39 import org.simantics.db.layer0.genericrelation.DependencyChanges.ComponentRemoval;
40 import org.simantics.db.service.CollectionSupport;
41 import org.simantics.db.service.ManagementSupport;
42 import org.simantics.db.service.VirtualGraphSupport;
43 import org.simantics.issues.ontology.IssueResource;
45 public class IssueSourceUtils {
47 public static final boolean DEBUG = false;
48 public static final boolean DEBUG_CHANGE_ENTRIES = false;
49 public static final boolean DEBUG_RESOURCES = false;
51 public static void update(WriteGraph graph, final Resource source) throws DatabaseException {
53 IssueResource ISSUE = IssueResource.getInstance(graph);
55 Long revisionId = graph.getPossibleRelatedValue(source, ISSUE.ContinuousIssueSource_lastUpdateRevision, Bindings.LONG);
56 if(revisionId == null) revisionId = 0L;
58 ManagementSupport support = graph.getService(ManagementSupport.class);
59 final long headRevision = support.getHeadRevisionId()+1;
61 if(revisionId < headRevision) {
62 VirtualGraphSupport vgs = graph.getService(VirtualGraphSupport.class);
63 VirtualGraph vg = vgs.getWorkspacePersistent(IssueConstants.ISSUE_VG);
64 graph.syncRequest(new WriteRequest(vg) {
67 public void perform(WriteGraph graph) throws DatabaseException {
68 IssueResource ISSUE = IssueResource.getInstance(graph);
69 graph.claimLiteral(source, ISSUE.ContinuousIssueSource_lastUpdateRevision, headRevision, Bindings.LONG);
71 System.err.println("-update[" + NameUtils.getSafeName(graph, source) + "][" + headRevision + "]");
79 public static boolean hasChanges(ReadGraph graph, DependencyChanges event, Resource root, Resource from) throws DatabaseException {
80 Change[] changes = event.get(root);
81 if(changes == null) return false;
82 for(Change c : changes) {
83 if(c instanceof ComponentAddition) {
85 } else if(c instanceof ComponentRemoval) {
87 } else if(c instanceof ComponentModification) {
94 public static class Test3 extends BinaryRead<Resource, ResourceSet, Collection<Resource>> {
96 public Test3(Resource component, ResourceSet types) {
97 super(component, types);
101 public Collection<Resource> perform(ReadGraph graph) throws DatabaseException {
102 ResourceSetGraph rsg = graph.syncRequest(new DependentInstances3(parameter), TransientCacheListener.<ResourceSetGraph>instance());
103 return rsg.resolve(graph, parameter2);
108 private static void test(ReadGraph graph, Resource component, ResourceSet types, Collection<Resource> result) throws DatabaseException {
109 Collection<Resource> data = graph.syncRequest(new Test3(component, types), TransientCacheAsyncListener.<Collection<Resource>>instance());
110 if(DEBUG_RESOURCES) {
111 for(Resource r : data) {
112 System.err.println("IssueSourceUtils.test " + " " + NameUtils.getSafeName(graph, component) + " " + NameUtils.getSafeName(graph, types) + " " + NameUtils.getSafeName(graph, r));
118 public static Set<Resource> getChangedDependencies(ReadGraph graph, Resource model, Resource from, Set<Resource> types, DependencyChanges event) throws DatabaseException {
120 Change[] changes = event.get(model);
121 if(changes == null) return Collections.emptySet();
123 CollectionSupport cs = graph.getService(CollectionSupport.class);
124 ResourceSet typeSet = cs.getResourceSet(graph, types);
126 Set<Resource> result = new HashSet<Resource>();
127 for(Change c : changes) {
128 processChange(graph, c, typeSet, result);
135 public static void processChange(ReadGraph graph, Change c, ResourceSet types, Set<Resource> result) throws DatabaseException {
136 if(DEBUG_CHANGE_ENTRIES)
137 System.err.println("-processChange: " + c.toString(graph));
138 if(c instanceof ComponentAddition) {
139 ComponentAddition addition = (ComponentAddition)c;
140 test(graph, addition.component, types, result);
141 } else if(c instanceof ComponentRemoval) {
142 ComponentRemoval removal = (ComponentRemoval)c;
143 result.add(removal.component);
144 } else if(c instanceof ComponentModification) {
145 ComponentModification modification = (ComponentModification)c;
146 test(graph, modification.component, types, result);
151 public static Set<Resource> getChangedDependencies(ReadGraph graph, Resource source, Resource model, Resource from, Set<Resource> types) throws DatabaseException {
153 IssueResource ISSUE = IssueResource.getInstance(graph);
155 Long revisionId = graph.getPossibleRelatedValue(source, ISSUE.ContinuousIssueSource_lastUpdateRevision, Bindings.LONG);
156 if(revisionId == null) revisionId = 0L;
159 System.err.println("getChangedDependencies[" + NameUtils.getSafeName(graph, source) + "][" + revisionId + "]");
161 Map<Resource, Collection<Change>> index = MetadataUtils.getDependencyChangesFrom(graph, revisionId+1);
163 CollectionSupport cs = graph.getService(CollectionSupport.class);
164 ResourceSet typeSet = cs.getResourceSet(graph, types);
166 Collection<Change> modelChanges = index.get(model);
167 if(modelChanges == null) return Collections.emptySet();
169 Set<Resource> result = new HashSet<Resource>();
170 for(Change c : modelChanges) {
171 processChange(graph, c, typeSet, result);