X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.modeling%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Futils%2FDumpOntologyStructure.java;h=e058f2c36fed2fb0ba8d6753c5fd47a244aa4963;hp=79622f15e5839bf1cae00c32dc228e75967eb427;hb=bb9e29bd1e5a056c54e04df779bfb4d76a2ff678;hpb=4bc25acb8086df68836950af08faeacd382e5427 diff --git a/bundles/org.simantics.modeling/src/org/simantics/modeling/utils/DumpOntologyStructure.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/utils/DumpOntologyStructure.java index 79622f15e..e058f2c36 100644 --- a/bundles/org.simantics.modeling/src/org/simantics/modeling/utils/DumpOntologyStructure.java +++ b/bundles/org.simantics.modeling/src/org/simantics/modeling/utils/DumpOntologyStructure.java @@ -5,7 +5,9 @@ import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.Collection; import java.util.HashMap; +import java.util.HashSet; import java.util.Map; +import java.util.Set; import java.util.TreeMap; import org.simantics.Simantics; @@ -30,21 +32,52 @@ public class DumpOntologyStructure { private static final Logger LOGGER = LoggerFactory.getLogger(DumpOntologyStructure.class); private Resource ontology; - + private Map names = new HashMap<>(); - private Map parents = new HashMap<>(); + private Map parents = new TreeMap<>(); private Map libraryFolders = new HashMap<>(); private Map contentDumps = new HashMap<>(); private void readNameAndParent(ReadGraph graph, Resource container, Resource r) throws DatabaseException { + String name = NameUtils.getSafeName(graph, r); parents.put(r, container); - names.put(r, NameUtils.getSafeName(graph, r)); + names.put(r, FileUtils.escapeFileName(name)); } - - private Collection containers() { - return parents.values(); + + /* + * This shall return containers sorted by full path. + * This makes processing order stable and ensures that + * directories are processed before their contents. + */ + private Collection sortedContainers(File rootFolder) { + Set parentSet = new HashSet(parents.values()); + TreeMap result = new TreeMap<>(); + for(Resource r : parentSet) { + File f = getFolder(rootFolder, r); + result.put(f.getAbsolutePath(), r); + } + return result.values(); } - + + private Collection sortedResources(File rootFolder) { + TreeMap result = new TreeMap<>(); + for(Resource r : parents.keySet()) { + byte[] dump = contentDumps.get(r); + if(dump == null) + dump = "".getBytes(StandardCharsets.UTF_8); + if(isParent(r)) { + if(dump.length > 0) { + File f = new File(getFolder(rootFolder, r), "__contents__"); + result.put(f.getAbsolutePath(), r); + } + } else { + File f = getFile(rootFolder, r); + result.put(f.getAbsolutePath(), r); + } + } + return result.values(); + } + private void readHierarchy(ReadGraph graph, Resource container) throws DatabaseException { Layer0 L0 = Layer0.getInstance(graph); for(Resource r : CommonDBUtils.objectsWithType(graph, container, L0.ConsistsOf, L0.Entity)) { @@ -94,64 +127,80 @@ public class DumpOntologyStructure { } } - public void read(ReadGraph graph, Resource ontology) throws DatabaseException { + public DumpOntologyStructure read(ReadGraph graph, Resource ontology) throws DatabaseException { this.ontology = ontology; readHierarchy(graph, ontology); readGeneric(graph); + return this; } - - private File escapeFile(File file) { + private File escapeFile(File file) { if(file.exists()) return file; - return new File(escapeFile(file.getParentFile()), FileUtils.escapeFileName(file.getName())); - } - + public void write(File unsafeFolder) throws IOException { File folder = escapeFile(unsafeFolder); - if(folder.exists()) - FileUtils.deleteAll(folder); - folder.mkdirs(); + FileUtils.delete(folder.toPath()); + folder.getParentFile().mkdirs(); writeDirectories(folder); writeResources(folder); } + + Resource getParent(Resource r) { + return parents.get(r); + } private File getFolder(File root, Resource library) { if(ontology.equals(library)) return root; - Resource parent = parents.get(library); + Resource parent = getParent(library); if(parent == null) throw new IllegalStateException("null parent for " + library); File parentFolder = getFolder(root, parent); - return new File(parentFolder, FileUtils.escapeFileName(names.get(library))); + return new File(parentFolder, names.get(library)); } private File getFile(File rootFolder, Resource r) { - Resource parent = parents.get(r); + Resource parent = getParent(r); File folder = getFolder(rootFolder, parent); - return new File(folder, FileUtils.escapeFileName(names.get(r))); + return new File(folder, names.get(r)); } + private File makeUnique(File original, Resource r) { + int counter = 2; + File file = new File(original.getParent(), original.getName()); + File test = file; + while(test.exists()) { + // Here we have a name clash with small and big letters! (windows) + test = new File(file.getParent(), file.getName() + "____" + (counter++)); + } + // Enforce this renaming in future operations also + names.put(r, test.getName()); + return test; + } + private void writeDirectories(File rootFolder) { - for(Resource library : containers()) { - File folder = getFolder(rootFolder, library); + // Here stuff shall be returned in alphabetical order + for(Resource library : sortedContainers(rootFolder)) { + File folder = makeUnique(getFolder(rootFolder, library), library); folder.mkdirs(); libraryFolders.put(library, folder); } } private void writeResources(File rootFolder) throws IOException { - for(Resource r : parents.keySet()) { + // Here stuff shall be returned in alphabetical order + for(Resource r : sortedResources(rootFolder)) { writeResource(rootFolder, r); } } - + private boolean isParent(Resource r) { return parents.values().contains(r); } - + private void writeResource(File rootFolder, Resource resource) throws IOException { byte[] dump = contentDumps.get(resource); if(dump == null) @@ -165,7 +214,7 @@ public class DumpOntologyStructure { } private void write(File rootFolder, Resource resource, byte[] bytes) throws IOException { - FileUtils.writeFile(getFile(rootFolder, resource), bytes); + FileUtils.writeFile(makeUnique(getFile(rootFolder, resource), resource), bytes); } -} +} \ No newline at end of file