]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.common/src/org/simantics/db/common/utils/Versions.java
Always show version label for non-initial versions.
[simantics/platform.git] / bundles / org.simantics.db.common / src / org / simantics / db / common / utils / Versions.java
1 package org.simantics.db.common.utils;
2
3 import java.util.Collection;
4 import java.util.HashSet;
5 import java.util.Set;
6
7 import org.simantics.databoard.Bindings;
8 import org.simantics.databoard.util.URIStringUtils;
9 import org.simantics.db.ReadGraph;
10 import org.simantics.db.Resource;
11 import org.simantics.db.common.NamedResource;
12 import org.simantics.db.common.request.PossibleIndexRoot;
13 import org.simantics.db.exception.DatabaseException;
14 import org.simantics.layer0.Layer0;
15
16 public class Versions {
17
18         public static String make(String baseName, String version) {
19                 return baseName + "@" + version;
20         }
21         
22         public static String getBaseVersion(String version) {
23                 try {
24                         Integer.parseInt(version);
25                         return "1";
26                 } catch (NumberFormatException e) {
27                 }
28                 return "A";
29         }
30         
31         public static String getBaseName(String name) {
32                 int pos = name.lastIndexOf('@');
33                 if(pos == -1) return name;
34                 return name.substring(0, pos);
35         }
36
37         public static String getVersion(String name) {
38         if(name == null) return null;
39         if(!name.contains("@")) return null;
40                 int pos = name.lastIndexOf('@');
41                 return name.substring(pos+1);
42         }
43
44         public static String getBaseName(ReadGraph graph, Resource artifact) throws DatabaseException {
45         Layer0 L0 = Layer0.getInstance(graph);
46         String name = graph.getPossibleRelatedValue(artifact, L0.HasName, Bindings.STRING);
47         if(name == null) return "";
48         if(!name.contains("@")) return name;
49                 int pos = name.lastIndexOf('@');
50                 return name.substring(0, pos);
51         }
52
53         public static String getVersion(ReadGraph graph, Resource artifact) throws DatabaseException {
54         Layer0 L0 = Layer0.getInstance(graph);
55         String name = graph.getPossibleRelatedValue(artifact, L0.HasName, Bindings.STRING);
56         return getVersion(name);
57         }
58
59         public static Collection<NamedResource> getOlderVersions(ReadGraph graph, Resource artifact) throws DatabaseException {
60
61                 Set<NamedResource> result = new HashSet<NamedResource>();
62                 
63                 VersionInfo info = graph.syncRequest(new VersionInfoRequest(artifact));
64                 result.addAll(info.getOlderVersions());
65                 
66                 Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(artifact));
67                 if(indexRoot == null || indexRoot.equals(artifact)) return result;
68
69                 VersionInfo versions = graph.syncRequest(new VersionInfoRequest(indexRoot));
70                 for(NamedResource ontology : versions.getOlderVersions()) {
71                         VersionMap map = match(graph, artifact, ontology.getResource());
72                         result.addAll(map.getOlderOrEqualVersions(graph, artifact));
73                 }
74                 
75                 return result;
76                 
77         }
78         
79         public static Collection<NamedResource> getNewerVersions(ReadGraph graph, Resource artifact) throws DatabaseException {
80
81                 Set<NamedResource> result = new HashSet<NamedResource>();
82                 
83                 VersionInfo info = graph.syncRequest(new VersionInfoRequest(artifact));
84                 result.addAll(info.getNewerVersions());
85                 
86                 Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(artifact));
87                 if(indexRoot == null || indexRoot.equals(artifact)) return result;
88                 
89                 VersionInfo versions = graph.syncRequest(new VersionInfoRequest(indexRoot));
90                 for(NamedResource ontology : versions.getNewerVersions()) {
91                         VersionMap map = match(graph, artifact, ontology.getResource());
92                         result.addAll(map.getNewerOrEqualVersions(graph, artifact));
93                 }
94
95                 return result;
96                 
97         }
98
99         public static VersionMap match(ReadGraph graph, Resource type, Resource newOntology) throws DatabaseException {
100         Resource originalOntology = graph.syncRequest(new PossibleIndexRoot(type));
101         String originalURI = graph.getURI(originalOntology);
102         String newOntologyURI = graph.getURI(newOntology);
103         String typeURI = graph.getURI(type);
104         String typeURIInNewOntology = newOntologyURI + typeURI.substring(originalURI.length());
105         String[] parts = URIStringUtils.splitURI(typeURIInNewOntology); 
106         Resource newLibrary = graph.getPossibleResource(parts[0]);
107         if(newLibrary == null) return null;
108         return graph.syncRequest(new VersionMapRequest(newLibrary));
109     }
110
111     public static String getStandardNameString(ReadGraph graph, Resource r) throws DatabaseException {
112         
113         Layer0 L0 = Layer0.getInstance(graph);
114         String name = graph.getPossibleRelatedValue(r, L0.HasName, Bindings.STRING);
115         if(name != null) {
116                 VersionInfo versions = graph.syncRequest(new VersionInfoRequest(r));
117                 int nVersions = versions.versions.size();
118                 // No version display, if "A", "1" or "" is the only version present
119                 if ("".equals(versions.version))
120                         return nVersions == 1 ? name : name + " [No version]";
121                 String base = versions.baseName;
122                 String version = versions.version;
123                 if (("A".equals(version) || "1".equals(version)) && nVersions == 1)
124                         return base;
125                 else
126                         return base + " [Version " + version + "]";
127         }
128         // Fallback logic
129         return NameUtils.getSafeName(graph, r);
130         
131     }
132     
133     public static String getStandardPathNameString(ReadGraph graph, Resource r) throws DatabaseException {
134         
135         Layer0 L0 = Layer0.getInstance(graph);
136
137         String standardName = getStandardNameString(graph, r); 
138         
139         Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(r));
140         if(indexRoot.equals(r)) return standardName;
141         
142         Resource indexRootParent = graph.getPossibleObject(indexRoot, L0.PartOf);
143         
144         String indexRootName = getStandardNameString(graph, indexRoot);
145         String indexRootParentURI = graph.getPossibleURI(indexRootParent);
146         
147         Resource parent = graph.getPossibleObject(r, L0.PartOf);
148         String parentURI = graph.getURI(parent);
149         String indexRootURI = graph.getPossibleURI(indexRoot);
150         String path = (parent.equals(indexRoot)) ? "" : parentURI.substring(indexRootURI.length()+1) + "/"; 
151         
152         return path + standardName + "  in  " + indexRootParentURI + indexRootName;
153         
154     }
155
156 }