1 package org.simantics.db.common.utils;
\r
3 import java.util.Collection;
\r
4 import java.util.HashSet;
\r
5 import java.util.Set;
\r
7 import org.simantics.databoard.Bindings;
\r
8 import org.simantics.databoard.util.URIStringUtils;
\r
9 import org.simantics.db.ReadGraph;
\r
10 import org.simantics.db.Resource;
\r
11 import org.simantics.db.common.NamedResource;
\r
12 import org.simantics.db.common.request.PossibleIndexRoot;
\r
13 import org.simantics.db.exception.DatabaseException;
\r
14 import org.simantics.layer0.Layer0;
\r
16 public class Versions {
\r
18 public static String make(String baseName, String version) {
\r
19 return baseName + "@" + version;
\r
22 public static String getBaseVersion(String version) {
\r
24 Integer.parseInt(version);
\r
26 } catch (NumberFormatException e) {
\r
31 public static String getBaseName(String name) {
\r
32 int pos = name.lastIndexOf('@');
\r
33 if(pos == -1) return name;
\r
34 return name.substring(0, pos);
\r
37 public static String getVersion(String name) {
\r
38 if(name == null) return null;
\r
39 if(!name.contains("@")) return null;
\r
40 int pos = name.lastIndexOf('@');
\r
41 return name.substring(pos+1);
\r
44 public static String getBaseName(ReadGraph graph, Resource artifact) throws DatabaseException {
\r
45 Layer0 L0 = Layer0.getInstance(graph);
\r
46 String name = graph.getPossibleRelatedValue(artifact, L0.HasName, Bindings.STRING);
\r
47 if(name == null) return "";
\r
48 if(!name.contains("@")) return name;
\r
49 int pos = name.lastIndexOf('@');
\r
50 return name.substring(0, pos);
\r
53 public static String getVersion(ReadGraph graph, Resource artifact) throws DatabaseException {
\r
54 Layer0 L0 = Layer0.getInstance(graph);
\r
55 String name = graph.getPossibleRelatedValue(artifact, L0.HasName, Bindings.STRING);
\r
56 return getVersion(name);
\r
59 public static Collection<NamedResource> getOlderVersions(ReadGraph graph, Resource artifact) throws DatabaseException {
\r
61 Set<NamedResource> result = new HashSet<NamedResource>();
\r
63 VersionInfo info = graph.syncRequest(new VersionInfoRequest(artifact));
\r
64 result.addAll(info.getOlderVersions());
\r
66 Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(artifact));
\r
67 if(indexRoot == null || indexRoot.equals(artifact)) return result;
\r
69 VersionInfo versions = graph.syncRequest(new VersionInfoRequest(indexRoot));
\r
70 for(NamedResource ontology : versions.getOlderVersions()) {
\r
71 VersionMap map = match(graph, artifact, ontology.getResource());
\r
72 result.addAll(map.getOlderOrEqualVersions(graph, artifact));
\r
79 public static Collection<NamedResource> getNewerVersions(ReadGraph graph, Resource artifact) throws DatabaseException {
\r
81 Set<NamedResource> result = new HashSet<NamedResource>();
\r
83 VersionInfo info = graph.syncRequest(new VersionInfoRequest(artifact));
\r
84 result.addAll(info.getNewerVersions());
\r
86 Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(artifact));
\r
87 if(indexRoot == null || indexRoot.equals(artifact)) return result;
\r
89 VersionInfo versions = graph.syncRequest(new VersionInfoRequest(indexRoot));
\r
90 for(NamedResource ontology : versions.getNewerVersions()) {
\r
91 VersionMap map = match(graph, artifact, ontology.getResource());
\r
92 result.addAll(map.getNewerOrEqualVersions(graph, artifact));
\r
99 public static VersionMap match(ReadGraph graph, Resource type, Resource newOntology) throws DatabaseException {
\r
100 Resource originalOntology = graph.syncRequest(new PossibleIndexRoot(type));
\r
101 String originalURI = graph.getURI(originalOntology);
\r
102 String newOntologyURI = graph.getURI(newOntology);
\r
103 String typeURI = graph.getURI(type);
\r
104 String typeURIInNewOntology = newOntologyURI + typeURI.substring(originalURI.length());
\r
105 String[] parts = URIStringUtils.splitURI(typeURIInNewOntology);
\r
106 Resource newLibrary = graph.getPossibleResource(parts[0]);
\r
107 if(newLibrary == null) return null;
\r
108 return graph.syncRequest(new VersionMapRequest(newLibrary));
\r
111 public static String getStandardNameString(ReadGraph graph, Resource r) throws DatabaseException {
\r
113 Layer0 L0 = Layer0.getInstance(graph);
\r
114 String name = graph.getPossibleRelatedValue(r, L0.HasName, Bindings.STRING);
\r
116 VersionInfo versions = graph.syncRequest(new VersionInfoRequest(r));
\r
117 if(versions.versions.size() != 1) {
\r
118 int pos = name.lastIndexOf('@');
\r
119 if(pos == -1) return name + " [No version]";
\r
120 String base = name.substring(0, pos);
\r
121 String version = name.substring(pos+1);
\r
122 return base + " [Version " + version + "]";
\r
124 return versions.baseName;
\r
128 return NameUtils.getSafeName(graph, r);
\r
132 public static String getStandardPathNameString(ReadGraph graph, Resource r) throws DatabaseException {
\r
134 Layer0 L0 = Layer0.getInstance(graph);
\r
136 String standardName = getStandardNameString(graph, r);
\r
138 Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(r));
\r
139 if(indexRoot.equals(r)) return standardName;
\r
141 Resource indexRootParent = graph.getPossibleObject(indexRoot, L0.PartOf);
\r
143 String indexRootName = getStandardNameString(graph, indexRoot);
\r
144 String indexRootParentURI = graph.getPossibleURI(indexRootParent);
\r
146 Resource parent = graph.getPossibleObject(r, L0.PartOf);
\r
147 String parentURI = graph.getURI(parent);
\r
148 String indexRootURI = graph.getPossibleURI(indexRoot);
\r
149 String path = (parent.equals(indexRoot)) ? "" : parentURI.substring(indexRootURI.length()+1) + "/";
\r
151 return path + standardName + " in " + indexRootParentURI + indexRootName;
\r