X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.db.common%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fcommon%2Futils%2FVersions.java;fp=bundles%2Forg.simantics.db.common%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fcommon%2Futils%2FVersions.java;h=d022a251e7b2a7df25b986fd4cc27fd482fe918a;hb=0ae2b770234dfc3cbb18bd38f324125cf0faca07;hp=8670896b990e426246581700de9cef15d463f1a3;hpb=24e2b34260f219f0d1644ca7a138894980e25b14;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.db.common/src/org/simantics/db/common/utils/Versions.java b/bundles/org.simantics.db.common/src/org/simantics/db/common/utils/Versions.java index 8670896b9..d022a251e 100644 --- a/bundles/org.simantics.db.common/src/org/simantics/db/common/utils/Versions.java +++ b/bundles/org.simantics.db.common/src/org/simantics/db/common/utils/Versions.java @@ -1,155 +1,155 @@ -package org.simantics.db.common.utils; - -import java.util.Collection; -import java.util.HashSet; -import java.util.Set; - -import org.simantics.databoard.Bindings; -import org.simantics.databoard.util.URIStringUtils; -import org.simantics.db.ReadGraph; -import org.simantics.db.Resource; -import org.simantics.db.common.NamedResource; -import org.simantics.db.common.request.PossibleIndexRoot; -import org.simantics.db.exception.DatabaseException; -import org.simantics.layer0.Layer0; - -public class Versions { - - public static String make(String baseName, String version) { - return baseName + "@" + version; - } - - public static String getBaseVersion(String version) { - try { - Integer.parseInt(version); - return "1"; - } catch (NumberFormatException e) { - } - return "A"; - } - - public static String getBaseName(String name) { - int pos = name.lastIndexOf('@'); - if(pos == -1) return name; - return name.substring(0, pos); - } - - public static String getVersion(String name) { - if(name == null) return null; - if(!name.contains("@")) return null; - int pos = name.lastIndexOf('@'); - return name.substring(pos+1); - } - - public static String getBaseName(ReadGraph graph, Resource artifact) throws DatabaseException { - Layer0 L0 = Layer0.getInstance(graph); - String name = graph.getPossibleRelatedValue(artifact, L0.HasName, Bindings.STRING); - if(name == null) return ""; - if(!name.contains("@")) return name; - int pos = name.lastIndexOf('@'); - return name.substring(0, pos); - } - - public static String getVersion(ReadGraph graph, Resource artifact) throws DatabaseException { - Layer0 L0 = Layer0.getInstance(graph); - String name = graph.getPossibleRelatedValue(artifact, L0.HasName, Bindings.STRING); - return getVersion(name); - } - - public static Collection getOlderVersions(ReadGraph graph, Resource artifact) throws DatabaseException { - - Set result = new HashSet(); - - VersionInfo info = graph.syncRequest(new VersionInfoRequest(artifact)); - result.addAll(info.getOlderVersions()); - - Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(artifact)); - if(indexRoot == null || indexRoot.equals(artifact)) return result; - - VersionInfo versions = graph.syncRequest(new VersionInfoRequest(indexRoot)); - for(NamedResource ontology : versions.getOlderVersions()) { - VersionMap map = match(graph, artifact, ontology.getResource()); - result.addAll(map.getOlderOrEqualVersions(graph, artifact)); - } - - return result; - - } - - public static Collection getNewerVersions(ReadGraph graph, Resource artifact) throws DatabaseException { - - Set result = new HashSet(); - - VersionInfo info = graph.syncRequest(new VersionInfoRequest(artifact)); - result.addAll(info.getNewerVersions()); - - Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(artifact)); - if(indexRoot == null || indexRoot.equals(artifact)) return result; - - VersionInfo versions = graph.syncRequest(new VersionInfoRequest(indexRoot)); - for(NamedResource ontology : versions.getNewerVersions()) { - VersionMap map = match(graph, artifact, ontology.getResource()); - result.addAll(map.getNewerOrEqualVersions(graph, artifact)); - } - - return result; - - } - - public static VersionMap match(ReadGraph graph, Resource type, Resource newOntology) throws DatabaseException { - Resource originalOntology = graph.syncRequest(new PossibleIndexRoot(type)); - String originalURI = graph.getURI(originalOntology); - String newOntologyURI = graph.getURI(newOntology); - String typeURI = graph.getURI(type); - String typeURIInNewOntology = newOntologyURI + typeURI.substring(originalURI.length()); - String[] parts = URIStringUtils.splitURI(typeURIInNewOntology); - Resource newLibrary = graph.getPossibleResource(parts[0]); - if(newLibrary == null) return null; - return graph.syncRequest(new VersionMapRequest(newLibrary)); - } - - public static String getStandardNameString(ReadGraph graph, Resource r) throws DatabaseException { - - Layer0 L0 = Layer0.getInstance(graph); - String name = graph.getPossibleRelatedValue(r, L0.HasName, Bindings.STRING); - if(name != null) { - VersionInfo versions = graph.syncRequest(new VersionInfoRequest(r)); - if(versions.versions.size() != 1) { - int pos = name.lastIndexOf('@'); - if(pos == -1) return name + " [No version]"; - String base = name.substring(0, pos); - String version = name.substring(pos+1); - return base + " [Version " + version + "]"; - } else { - return versions.baseName; - } - } - // Fallback logic - return NameUtils.getSafeName(graph, r); - - } - - public static String getStandardPathNameString(ReadGraph graph, Resource r) throws DatabaseException { - - Layer0 L0 = Layer0.getInstance(graph); - - String standardName = getStandardNameString(graph, r); - - Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(r)); - if(indexRoot.equals(r)) return standardName; - - Resource indexRootParent = graph.getPossibleObject(indexRoot, L0.PartOf); - - String indexRootName = getStandardNameString(graph, indexRoot); - String indexRootParentURI = graph.getPossibleURI(indexRootParent); - - Resource parent = graph.getPossibleObject(r, L0.PartOf); - String parentURI = graph.getURI(parent); - String indexRootURI = graph.getPossibleURI(indexRoot); - String path = (parent.equals(indexRoot)) ? "" : parentURI.substring(indexRootURI.length()+1) + "/"; - - return path + standardName + " in " + indexRootParentURI + indexRootName; - - } - -} +package org.simantics.db.common.utils; + +import java.util.Collection; +import java.util.HashSet; +import java.util.Set; + +import org.simantics.databoard.Bindings; +import org.simantics.databoard.util.URIStringUtils; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.NamedResource; +import org.simantics.db.common.request.PossibleIndexRoot; +import org.simantics.db.exception.DatabaseException; +import org.simantics.layer0.Layer0; + +public class Versions { + + public static String make(String baseName, String version) { + return baseName + "@" + version; + } + + public static String getBaseVersion(String version) { + try { + Integer.parseInt(version); + return "1"; + } catch (NumberFormatException e) { + } + return "A"; + } + + public static String getBaseName(String name) { + int pos = name.lastIndexOf('@'); + if(pos == -1) return name; + return name.substring(0, pos); + } + + public static String getVersion(String name) { + if(name == null) return null; + if(!name.contains("@")) return null; + int pos = name.lastIndexOf('@'); + return name.substring(pos+1); + } + + public static String getBaseName(ReadGraph graph, Resource artifact) throws DatabaseException { + Layer0 L0 = Layer0.getInstance(graph); + String name = graph.getPossibleRelatedValue(artifact, L0.HasName, Bindings.STRING); + if(name == null) return ""; + if(!name.contains("@")) return name; + int pos = name.lastIndexOf('@'); + return name.substring(0, pos); + } + + public static String getVersion(ReadGraph graph, Resource artifact) throws DatabaseException { + Layer0 L0 = Layer0.getInstance(graph); + String name = graph.getPossibleRelatedValue(artifact, L0.HasName, Bindings.STRING); + return getVersion(name); + } + + public static Collection getOlderVersions(ReadGraph graph, Resource artifact) throws DatabaseException { + + Set result = new HashSet(); + + VersionInfo info = graph.syncRequest(new VersionInfoRequest(artifact)); + result.addAll(info.getOlderVersions()); + + Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(artifact)); + if(indexRoot == null || indexRoot.equals(artifact)) return result; + + VersionInfo versions = graph.syncRequest(new VersionInfoRequest(indexRoot)); + for(NamedResource ontology : versions.getOlderVersions()) { + VersionMap map = match(graph, artifact, ontology.getResource()); + result.addAll(map.getOlderOrEqualVersions(graph, artifact)); + } + + return result; + + } + + public static Collection getNewerVersions(ReadGraph graph, Resource artifact) throws DatabaseException { + + Set result = new HashSet(); + + VersionInfo info = graph.syncRequest(new VersionInfoRequest(artifact)); + result.addAll(info.getNewerVersions()); + + Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(artifact)); + if(indexRoot == null || indexRoot.equals(artifact)) return result; + + VersionInfo versions = graph.syncRequest(new VersionInfoRequest(indexRoot)); + for(NamedResource ontology : versions.getNewerVersions()) { + VersionMap map = match(graph, artifact, ontology.getResource()); + result.addAll(map.getNewerOrEqualVersions(graph, artifact)); + } + + return result; + + } + + public static VersionMap match(ReadGraph graph, Resource type, Resource newOntology) throws DatabaseException { + Resource originalOntology = graph.syncRequest(new PossibleIndexRoot(type)); + String originalURI = graph.getURI(originalOntology); + String newOntologyURI = graph.getURI(newOntology); + String typeURI = graph.getURI(type); + String typeURIInNewOntology = newOntologyURI + typeURI.substring(originalURI.length()); + String[] parts = URIStringUtils.splitURI(typeURIInNewOntology); + Resource newLibrary = graph.getPossibleResource(parts[0]); + if(newLibrary == null) return null; + return graph.syncRequest(new VersionMapRequest(newLibrary)); + } + + public static String getStandardNameString(ReadGraph graph, Resource r) throws DatabaseException { + + Layer0 L0 = Layer0.getInstance(graph); + String name = graph.getPossibleRelatedValue(r, L0.HasName, Bindings.STRING); + if(name != null) { + VersionInfo versions = graph.syncRequest(new VersionInfoRequest(r)); + if(versions.versions.size() != 1) { + int pos = name.lastIndexOf('@'); + if(pos == -1) return name + " [No version]"; + String base = name.substring(0, pos); + String version = name.substring(pos+1); + return base + " [Version " + version + "]"; + } else { + return versions.baseName; + } + } + // Fallback logic + return NameUtils.getSafeName(graph, r); + + } + + public static String getStandardPathNameString(ReadGraph graph, Resource r) throws DatabaseException { + + Layer0 L0 = Layer0.getInstance(graph); + + String standardName = getStandardNameString(graph, r); + + Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(r)); + if(indexRoot.equals(r)) return standardName; + + Resource indexRootParent = graph.getPossibleObject(indexRoot, L0.PartOf); + + String indexRootName = getStandardNameString(graph, indexRoot); + String indexRootParentURI = graph.getPossibleURI(indexRootParent); + + Resource parent = graph.getPossibleObject(r, L0.PartOf); + String parentURI = graph.getURI(parent); + String indexRootURI = graph.getPossibleURI(indexRoot); + String path = (parent.equals(indexRoot)) ? "" : parentURI.substring(indexRootURI.length()+1) + "/"; + + return path + standardName + " in " + indexRootParentURI + indexRootName; + + } + +}