--- /dev/null
+/*******************************************************************************\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.db.layer0.adapter.impl;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collections;\r
+import java.util.HashMap;\r
+import java.util.HashSet;\r
+import java.util.List;\r
+import java.util.Map;\r
+import java.util.Set;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.databoard.util.URIStringUtils;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteOnlyGraph;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.util.Layer0Utils;\r
+import org.simantics.db.service.XSupport;\r
+import org.simantics.graph.db.AbstractImportAdvisor2;\r
+import org.simantics.graph.db.ImportAdvisors;\r
+import org.simantics.graph.db.MissingDependencyException;\r
+import org.simantics.graph.db.TransferableGraphImporter;\r
+import org.simantics.graph.representation.Root;\r
+import org.simantics.layer0.Layer0;\r
+import org.simantics.utils.datastructures.Triple;\r
+\r
+public class SharedOntologyImportAdvisor extends AbstractImportAdvisor2 {\r
+\r
+ private Layer0 L0;\r
+ \r
+ private Map<Root, Triple<Resource, Resource, List<String>>> toCreate = new HashMap<Root, Triple<Resource, Resource, List<String>>>();\r
+ private Map<Root, Resource> created = new HashMap<Root, Resource>();\r
+\r
+ final private boolean published;\r
+\r
+ final private Map<String,Object> context;\r
+ \r
+ public SharedOntologyImportAdvisor(boolean published) {\r
+ this(Collections.<String,Object>emptyMap(), published);\r
+ }\r
+\r
+ public SharedOntologyImportAdvisor(Map<String,Object> context, boolean published) {\r
+ this.context = context;\r
+ this.published = published;\r
+ }\r
+\r
+ private Resource getNames(ReadGraph graph, String uri, List<String> names) throws DatabaseException {\r
+ \r
+ if("http://".equals(uri) || "http:/".equals(uri)) {\r
+ return graph.getRootLibrary();\r
+ }\r
+ \r
+ final String[] parts = URIStringUtils.splitURI(uri);\r
+ if(parts != null && parts.length == 2) {\r
+ Resource base = graph.getPossibleResource(parts[0]);\r
+ if(base == null) base = getNames(graph, parts[0], names);\r
+ names.add(parts[1]);\r
+ return base;\r
+ }\r
+ \r
+ return null;\r
+ \r
+ }\r
+ \r
+ \r
+ @Override\r
+ public Resource analyzeRoot(ReadGraph graph, Root r) throws DatabaseException {\r
+\r
+ L0 = Layer0.getInstance(graph);\r
+ \r
+ String uri = r.name;\r
+ Map<String,String> renameMap = (Map<String,String>)context.get(ImportAdvisors.RENAME_MAP);\r
+ if(renameMap != null) {\r
+ String renamed = renameMap.get(uri);\r
+ if(renamed != null) uri = renamed;\r
+ }\r
+ \r
+ Resource existing = graph.getPossibleResource(uri);\r
+ if(existing != null) throw new DatabaseException("Shared library " + uri + " exists already.");\r
+ \r
+ Resource type = graph.getPossibleResource(r.type);\r
+ if(type == null) { \r
+ Set<String> types = new HashSet<String>();\r
+ types.add(r.type);\r
+ throw new MissingDependencyException(types);\r
+ }\r
+ //if(type == null) throw new DatabaseException("Shared library type " + r.type + " was not found.");\r
+ \r
+ List<String> namesToCreate = new ArrayList<String>();\r
+ Resource base = getNames(graph, uri, namesToCreate);\r
+ toCreate.put(r, Triple.make(base, type, namesToCreate));\r
+ \r
+ return null;\r
+ \r
+ }\r
+\r
+ @Override\r
+ public Resource getTarget() {\r
+ return null;\r
+ }\r
+ \r
+ @Override\r
+ public Resource createRoot(WriteOnlyGraph graph, Root r) throws DatabaseException {\r
+ return createRoot(graph, r, null);\r
+ }\r
+ \r
+ @Override\r
+ public Resource createRoot(WriteOnlyGraph graph, Root root, Resource resource) throws DatabaseException {\r
+ return created.get(root);\r
+ }\r
+ \r
+ @Override\r
+ public void beforeWrite(WriteOnlyGraph graph, TransferableGraphImporter process)\r
+ throws DatabaseException {\r
+ \r
+ graph.markUndoPoint();\r
+ if(published) {\r
+ XSupport xs = graph.getService(XSupport.class);\r
+ xs.setServiceMode(false, true);\r
+ }\r
+ \r
+ graph.setClusterSet4NewResource(graph.getRootLibrary());\r
+ graph.flushCluster();\r
+\r
+ for(Map.Entry<Root, Triple<Resource, Resource, List<String>>> entry : toCreate.entrySet()) {\r
+ \r
+ Triple<Resource, Resource, List<String>> recipe = entry.getValue();\r
+\r
+ Resource base = recipe.first;\r
+ for(int i=0;i<recipe.third.size()-1;i++) {\r
+ Resource lib = graph.newResource();\r
+ graph.claim(lib, L0.InstanceOf, null, L0.Library);\r
+ graph.addLiteral(lib, L0.HasName, L0.NameOf, URIStringUtils.unescape( recipe.third.get(i) ), Bindings.STRING);\r
+ graph.claim(base, L0.ConsistsOf, L0.PartOf, lib);\r
+ base = lib;\r
+ }\r
+\r
+ Resource lib = graph.newResource();\r
+ graph.newClusterSet(lib);\r
+ graph.setClusterSet4NewResource(lib);\r
+ graph.claim(lib, L0.InstanceOf, null, recipe.second);\r
+ String name = URIStringUtils.unescape( recipe.third.get(recipe.third.size()-1) );\r
+ graph.addLiteral(lib, L0.HasName, L0.NameOf, name, Bindings.STRING);\r
+ graph.claim(base, L0.ConsistsOf, L0.PartOf, lib);\r
+\r
+ addRootInfo(entry.getKey(), name, lib);\r
+ \r
+ created.put(entry.getKey(), lib);\r
+ \r
+ }\r
+ \r
+ graph.flushCluster();\r
+ \r
+ }\r
+\r
+ @Override\r
+ public void afterWrite(WriteOnlyGraph graph, TransferableGraphImporter process)\r
+ throws DatabaseException {\r
+ \r
+ String name = getRootInfo().iterator().next().name;\r
+ Resource resource = getRootInfo().iterator().next().resource;\r
+ Layer0Utils.addCommentMetadata(graph, "Imported Shared Ontology " + name + " " + resource);\r
+ \r
+ if(published) {\r
+ XSupport xs = graph.getService(XSupport.class);\r
+ xs.setServiceMode(false, false);\r
+ }\r
+\r
+ graph.setClusterSet4NewResource(graph.getRootLibrary());\r
+ graph.flushCluster();\r
+ \r
+ }\r
+ \r
+ @Override\r
+ public boolean allowImmutableModifications() {\r
+ return false;\r
+ }\r
+ \r
+ @Override\r
+ public Resource createChild(WriteOnlyGraph graph, TransferableGraphImporter process, Resource parent,\r
+ String name) throws DatabaseException {\r
+ return process.createChild(graph, parent, null, name);\r
+ }\r
+ \r
+ @Override\r
+ public Resource createChild(WriteOnlyGraph graph, TransferableGraphImporter process, Resource parent, Resource child,\r
+ String name) throws DatabaseException {\r
+ return process.createChild(graph, parent, child, name);\r
+ }\r
+ \r
+}\r