+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.issues.common;\r
+\r
+import java.util.Collection;\r
+import java.util.Collections;\r
+import java.util.HashSet;\r
+import java.util.Map;\r
+import java.util.Set;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.ResourceSet;\r
+import org.simantics.db.VirtualGraph;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.procedure.adapter.TransientCacheAsyncListener;\r
+import org.simantics.db.common.procedure.adapter.TransientCacheListener;\r
+import org.simantics.db.common.request.BinaryRead;\r
+import org.simantics.db.common.request.DependentInstances3;\r
+import org.simantics.db.common.request.ResourceSetGraph;\r
+import org.simantics.db.common.request.WriteRequest;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.changeset.MetadataUtils;\r
+import org.simantics.db.layer0.genericrelation.DependencyChanges;\r
+import org.simantics.db.layer0.genericrelation.DependencyChanges.Change;\r
+import org.simantics.db.layer0.genericrelation.DependencyChanges.ComponentAddition;\r
+import org.simantics.db.layer0.genericrelation.DependencyChanges.ComponentModification;\r
+import org.simantics.db.layer0.genericrelation.DependencyChanges.ComponentRemoval;\r
+import org.simantics.db.service.CollectionSupport;\r
+import org.simantics.db.service.ManagementSupport;\r
+import org.simantics.db.service.VirtualGraphSupport;\r
+import org.simantics.issues.ontology.IssueResource;\r
+\r
+public class IssueSourceUtils {\r
+ \r
+ public static final boolean DEBUG = false;\r
+ public static final boolean DEBUG_CHANGE_ENTRIES = false;\r
+ public static final boolean DEBUG_RESOURCES = false;\r
+\r
+ public static void update(WriteGraph graph, final Resource source) throws DatabaseException {\r
+\r
+ IssueResource ISSUE = IssueResource.getInstance(graph);\r
+ \r
+ Long revisionId = graph.getPossibleRelatedValue(source, ISSUE.ContinuousIssueSource_lastUpdateRevision, Bindings.LONG);\r
+ if(revisionId == null) revisionId = 0L;\r
+ \r
+ ManagementSupport support = graph.getService(ManagementSupport.class);\r
+ final long headRevision = support.getHeadRevisionId()+1;\r
+ \r
+ if(revisionId < headRevision) {\r
+ VirtualGraphSupport vgs = graph.getService(VirtualGraphSupport.class);\r
+ VirtualGraph vg = vgs.getWorkspacePersistent(IssueConstants.ISSUE_VG);\r
+ graph.syncRequest(new WriteRequest(vg) {\r
+\r
+ @Override\r
+ public void perform(WriteGraph graph) throws DatabaseException {\r
+ IssueResource ISSUE = IssueResource.getInstance(graph);\r
+ graph.claimLiteral(source, ISSUE.ContinuousIssueSource_lastUpdateRevision, headRevision, Bindings.LONG);\r
+ if(DEBUG)\r
+ System.err.println("-update[" + NameUtils.getSafeName(graph, source) + "][" + headRevision + "]");\r
+ }\r
+\r
+ });\r
+ }\r
+ \r
+ }\r
+ \r
+ public static boolean hasChanges(ReadGraph graph, DependencyChanges event, Resource root, Resource from) throws DatabaseException {\r
+ Change[] changes = event.get(root);\r
+ if(changes == null) return false;\r
+ for(Change c : changes) {\r
+ if(c instanceof ComponentAddition) {\r
+ return true;\r
+ } else if(c instanceof ComponentRemoval) {\r
+ return true;\r
+ } else if(c instanceof ComponentModification) {\r
+ return true;\r
+ }\r
+ }\r
+ return false;\r
+ }\r
+\r
+ public static class Test3 extends BinaryRead<Resource, ResourceSet, Collection<Resource>> {\r
+\r
+ public Test3(Resource component, ResourceSet types) {\r
+ super(component, types);\r
+ }\r
+\r
+ @Override\r
+ public Collection<Resource> perform(ReadGraph graph) throws DatabaseException {\r
+ ResourceSetGraph rsg = graph.syncRequest(new DependentInstances3(parameter), TransientCacheListener.<ResourceSetGraph>instance());\r
+ return rsg.resolve(graph, parameter2);\r
+ }\r
+\r
+ };\r
+\r
+ private static void test(ReadGraph graph, Resource component, ResourceSet types, Collection<Resource> result) throws DatabaseException {\r
+ Collection<Resource> data = graph.syncRequest(new Test3(component, types), TransientCacheAsyncListener.<Collection<Resource>>instance());\r
+ if(DEBUG_RESOURCES) {\r
+ for(Resource r : data) {\r
+ System.err.println("IssueSourceUtils.test " + " " + NameUtils.getSafeName(graph, component) + " " + NameUtils.getSafeName(graph, types) + " " + NameUtils.getSafeName(graph, r));\r
+ }\r
+ }\r
+ result.addAll(data);\r
+ }\r
+\r
+ public static Set<Resource> getChangedDependencies(ReadGraph graph, Resource model, Resource from, Set<Resource> types, DependencyChanges event) throws DatabaseException {\r
+\r
+ Change[] changes = event.get(model);\r
+ if(changes == null) return Collections.emptySet();\r
+\r
+ CollectionSupport cs = graph.getService(CollectionSupport.class);\r
+ ResourceSet typeSet = cs.getResourceSet(graph, types);\r
+ \r
+ Set<Resource> result = new HashSet<Resource>();\r
+ for(Change c : changes) {\r
+ processChange(graph, c, typeSet, result);\r
+ }\r
+\r
+ return result;\r
+ \r
+ }\r
+\r
+ public static void processChange(ReadGraph graph, Change c, ResourceSet types, Set<Resource> result) throws DatabaseException {\r
+ if(DEBUG_CHANGE_ENTRIES)\r
+ System.err.println("-processChange: " + c.toString(graph));\r
+ if(c instanceof ComponentAddition) {\r
+ ComponentAddition addition = (ComponentAddition)c;\r
+ test(graph, addition.component, types, result);\r
+ } else if(c instanceof ComponentRemoval) {\r
+ ComponentRemoval removal = (ComponentRemoval)c;\r
+ result.add(removal.component);\r
+ } else if(c instanceof ComponentModification) {\r
+ ComponentModification modification = (ComponentModification)c;\r
+ test(graph, modification.component, types, result);\r
+ }\r
+ \r
+ }\r
+\r
+ public static Set<Resource> getChangedDependencies(ReadGraph graph, Resource source, Resource model, Resource from, Set<Resource> types) throws DatabaseException {\r
+\r
+ IssueResource ISSUE = IssueResource.getInstance(graph);\r
+ \r
+ Long revisionId = graph.getPossibleRelatedValue(source, ISSUE.ContinuousIssueSource_lastUpdateRevision, Bindings.LONG);\r
+ if(revisionId == null) revisionId = 0L;\r
+\r
+ if(DEBUG)\r
+ System.err.println("getChangedDependencies[" + NameUtils.getSafeName(graph, source) + "][" + revisionId + "]");\r
+ \r
+ Map<Resource, Collection<Change>> index = MetadataUtils.getDependencyChangesFrom(graph, revisionId+1);\r
+\r
+ CollectionSupport cs = graph.getService(CollectionSupport.class);\r
+ ResourceSet typeSet = cs.getResourceSet(graph, types);\r
+\r
+ Collection<Change> modelChanges = index.get(model);\r
+ if(modelChanges == null) return Collections.emptySet();\r
+ \r
+ Set<Resource> result = new HashSet<Resource>();\r
+ for(Change c : modelChanges) {\r
+ processChange(graph, c, typeSet, result);\r
+ }\r
+\r
+ return result;\r
+ \r
+ }\r
+ \r
+}\r