]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/DefaultPasteImportAdvisor.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / adapter / impl / DefaultPasteImportAdvisor.java
diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/DefaultPasteImportAdvisor.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/DefaultPasteImportAdvisor.java
new file mode 100644 (file)
index 0000000..c4189f6
--- /dev/null
@@ -0,0 +1,186 @@
+/*******************************************************************************\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.Collections;\r
+import java.util.HashMap;\r
+import java.util.Map;\r
+\r
+import org.simantics.databoard.Bindings;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteOnlyGraph;\r
+import org.simantics.db.common.request.FreshEscapedName;\r
+import org.simantics.db.common.request.PossibleIndexRoot;\r
+import org.simantics.db.common.utils.Versions;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.service.SerialisationSupport;\r
+import org.simantics.graph.db.AbstractImportAdvisor2;\r
+import org.simantics.graph.db.ImportAdvisors;\r
+import org.simantics.graph.db.TransferableGraphImporter;\r
+import org.simantics.graph.representation.Root;\r
+import org.simantics.layer0.Layer0;\r
+\r
+public class DefaultPasteImportAdvisor extends AbstractImportAdvisor2 {\r
+\r
+       protected Resource library;\r
+       protected Resource temp;\r
+       protected Resource model;\r
+       protected String singleType = null;\r
+       protected long[] ids;\r
+       protected Map<String, String> nameMappings = new HashMap<String, String>();\r
+       \r
+       final protected Map<String, Object> context;\r
+\r
+       public DefaultPasteImportAdvisor(ReadGraph graph, Resource library) throws DatabaseException {\r
+               this(library, graph.syncRequest(new PossibleIndexRoot(library)), Collections.<String,Object>emptyMap());\r
+       }\r
+\r
+       public DefaultPasteImportAdvisor(Resource library) {\r
+               this(library, library, Collections.<String,Object>emptyMap());\r
+       }\r
+\r
+       public DefaultPasteImportAdvisor(Resource library, Map<String, Object> context) {\r
+               this(library, library, context);\r
+       }\r
+\r
+       public DefaultPasteImportAdvisor(Resource library, Resource model, Map<String, Object> context) {\r
+               this.library = library;\r
+               this.model = model;\r
+               this.context = context;\r
+       }\r
+\r
+       @Override\r
+       public void redirect(Resource temp) {\r
+               this.temp = temp;\r
+       }\r
+       \r
+       private Resource getRedirectTarget() {\r
+               if(temp != null) return temp;\r
+               else return library;\r
+       }\r
+       \r
+       public Resource getTarget() {\r
+               return library;\r
+       }\r
+       \r
+       public void analyzeType(ReadGraph graph, Root root) throws DatabaseException {\r
+       }\r
+       \r
+       @Override\r
+       public Resource analyzeRoot(ReadGraph graph, Root root) throws DatabaseException {\r
+               \r
+               if("%model".equals(root.name)) {\r
+                       return model;\r
+               }\r
+\r
+               analyzeType(graph, root);\r
+\r
+               String type = root.type;\r
+               if(singleType != null) {\r
+                       if(!type.equals(singleType)) throw new DatabaseException("Paste of a set of different types of objects is not supported.");\r
+               } else {\r
+                       singleType = type;\r
+               }\r
+               \r
+               if(library != null) {\r
+                       String newName = newName(graph, library, root.name);\r
+                       nameMappings.put(root.name, newName);\r
+               }\r
+               \r
+               return null;\r
+\r
+       }\r
+       \r
+       public String newName(ReadGraph graph, Resource library, String name) throws DatabaseException {\r
+               \r
+               Map<String,String> renameMap = (Map<String,String>)context.get(ImportAdvisors.RENAME_MAP);\r
+               if(renameMap != null) {\r
+                       String renamed = renameMap.get(name);\r
+                       if(renamed != null) return renamed;\r
+               }\r
+               \r
+\r
+               String version = Versions.getVersion(name);\r
+               if(version != null) {\r
+                       String baseName = Versions.getBaseName(name);\r
+                       String newBaseName = graph.syncRequest(new FreshEscapedName(library, Layer0.getInstance(graph).ConsistsOf, baseName));\r
+                       return Versions.make(newBaseName, Versions.getBaseVersion(version));\r
+               } else {\r
+                       return graph.syncRequest(new FreshEscapedName(library, Layer0.getInstance(graph).ConsistsOf, name)); \r
+               }\r
+               \r
+       }\r
+\r
+       @Override\r
+       public Resource createRoot(WriteOnlyGraph graph, Root root) throws DatabaseException {\r
+               return createRoot(graph, root, null);\r
+       }\r
+       \r
+       public String getName(Root root) {\r
+               String name = root.name;\r
+               String newName = nameMappings.get(name);\r
+               if(newName != null) name = newName;\r
+               return name;\r
+       }\r
+       \r
+       @Override\r
+       public Resource createRoot(WriteOnlyGraph graph, Root root, Resource resource) throws DatabaseException {\r
+               \r
+               Layer0 l0 = graph.getService(Layer0.class);\r
+\r
+               if(resource == null) resource = graph.newResource();\r
+               if(getRedirectTarget() != null) {\r
+                       graph.claim(getRedirectTarget(), l0.ConsistsOf, l0.PartOf, resource);\r
+               }\r
+               String newName = getName(root);\r
+               graph.addLiteral(resource, l0.HasName, l0.NameOf, l0.String, newName, Bindings.STRING);\r
+               \r
+               addRootInfo(root, newName, resource);\r
+               \r
+               return resource;\r
+               \r
+       }\r
+\r
+    @Override\r
+    public void beforeWrite(WriteOnlyGraph graph, TransferableGraphImporter process)\r
+            throws DatabaseException {\r
+    }\r
+\r
+    @Override\r
+    public void afterWrite(WriteOnlyGraph graph, TransferableGraphImporter process)\r
+            throws DatabaseException {\r
+       ids = process.getResourceIds(graph.getService(SerialisationSupport.class));\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
+    public long[] getResourceIds() {\r
+       return ids;\r
+    }\r
+    \r
+}\r