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;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
47 public class IssueSourceUtils {
49 private static final Logger LOGGER = LoggerFactory.getLogger(IssueSourceUtils.class);
50 public static final boolean DEBUG = false;
51 public static final boolean DEBUG_CHANGE_ENTRIES = false;
52 public static final boolean DEBUG_RESOURCES = false;
54 public static void update(WriteGraph graph, final Resource source) throws DatabaseException {
56 IssueResource ISSUE = IssueResource.getInstance(graph);
58 Long revisionId = graph.getPossibleRelatedValue(source, ISSUE.ContinuousIssueSource_lastUpdateRevision, Bindings.LONG);
59 if(revisionId == null) revisionId = 0L;
61 ManagementSupport support = graph.getService(ManagementSupport.class);
62 final long headRevision = support.getHeadRevisionId()+1;
64 if(revisionId < headRevision) {
65 VirtualGraphSupport vgs = graph.getService(VirtualGraphSupport.class);
66 VirtualGraph vg = vgs.getWorkspacePersistent(IssueConstants.ISSUE_VG);
67 graph.syncRequest(new WriteRequest(vg) {
70 public void perform(WriteGraph graph) throws DatabaseException {
71 IssueResource ISSUE = IssueResource.getInstance(graph);
72 graph.claimLiteral(source, ISSUE.ContinuousIssueSource_lastUpdateRevision, headRevision, Bindings.LONG);
74 LOGGER.info("-update[" + NameUtils.getSafeName(graph, source) + "][" + headRevision + "]");
82 public static boolean hasChanges(ReadGraph graph, DependencyChanges event, Resource root, Resource from) throws DatabaseException {
83 Change[] changes = event.get(root);
84 if(changes == null) return false;
85 for(Change c : changes) {
86 if(c instanceof ComponentAddition) {
88 } else if(c instanceof ComponentRemoval) {
90 } else if(c instanceof ComponentModification) {
97 public static class Test3 extends BinaryRead<Resource, ResourceSet, Collection<Resource>> {
99 public Test3(Resource component, ResourceSet types) {
100 super(component, types);
104 public Collection<Resource> perform(ReadGraph graph) throws DatabaseException {
105 ResourceSetGraph rsg = graph.syncRequest(new DependentInstances3(parameter), TransientCacheListener.<ResourceSetGraph>instance());
106 return rsg.resolve(graph, parameter2);
111 private static void test(ReadGraph graph, Resource component, ResourceSet types, Collection<Resource> result) throws DatabaseException {
112 Collection<Resource> data = graph.syncRequest(new Test3(component, types), TransientCacheAsyncListener.<Collection<Resource>>instance());
113 if(DEBUG_RESOURCES) {
114 for(Resource r : data) {
115 LOGGER.info("IssueSourceUtils.test " + " " + NameUtils.getSafeName(graph, component) + " " + NameUtils.getSafeName(graph, types) + " " + NameUtils.getSafeName(graph, r));
121 public static Set<Resource> getChangedDependencies(ReadGraph graph, Resource model, Resource from, Set<Resource> types, DependencyChanges event) throws DatabaseException {
123 Change[] changes = event.get(model);
124 if(changes == null) return Collections.emptySet();
126 CollectionSupport cs = graph.getService(CollectionSupport.class);
127 ResourceSet typeSet = cs.getResourceSet(graph, types);
129 Set<Resource> result = new HashSet<Resource>();
130 for(Change c : changes) {
131 processChange(graph, c, typeSet, result);
138 public static void processChange(ReadGraph graph, Change c, ResourceSet types, Set<Resource> result) throws DatabaseException {
139 if(DEBUG_CHANGE_ENTRIES)
140 LOGGER.info("-processChange: " + c.toString(graph));
141 if(c instanceof ComponentAddition) {
142 ComponentAddition addition = (ComponentAddition)c;
143 test(graph, addition.component, types, result);
144 } else if(c instanceof ComponentRemoval) {
145 ComponentRemoval removal = (ComponentRemoval)c;
146 result.add(removal.component);
147 } else if(c instanceof ComponentModification) {
148 ComponentModification modification = (ComponentModification)c;
149 test(graph, modification.component, types, result);
154 public static Set<Resource> getChangedDependencies(ReadGraph graph, Resource source, Resource model, Resource from, Set<Resource> types) throws DatabaseException {
156 IssueResource ISSUE = IssueResource.getInstance(graph);
158 Long revisionId = graph.getPossibleRelatedValue(source, ISSUE.ContinuousIssueSource_lastUpdateRevision, Bindings.LONG);
159 if(revisionId == null) revisionId = 0L;
162 LOGGER.info("getChangedDependencies[" + NameUtils.getSafeName(graph, source) + "][" + revisionId + "]");
164 Map<Resource, Collection<Change>> index = MetadataUtils.getDependencyChangesFrom(graph, revisionId+1);
166 CollectionSupport cs = graph.getService(CollectionSupport.class);
167 ResourceSet typeSet = cs.getResourceSet(graph, types);
169 Collection<Change> modelChanges = index.get(model);
170 if(modelChanges == null) return Collections.emptySet();
172 Set<Resource> result = new HashSet<Resource>();
173 for(Change c : modelChanges) {
174 processChange(graph, c, typeSet, result);