]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.graphfile/src/org/simantics/graphfile/adapters/SystemResourcePasteImportAdvisor.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.graphfile / src / org / simantics / graphfile / adapters / SystemResourcePasteImportAdvisor.java
diff --git a/bundles/org.simantics.graphfile/src/org/simantics/graphfile/adapters/SystemResourcePasteImportAdvisor.java b/bundles/org.simantics.graphfile/src/org/simantics/graphfile/adapters/SystemResourcePasteImportAdvisor.java
new file mode 100644 (file)
index 0000000..f1de1b5
--- /dev/null
@@ -0,0 +1,107 @@
+/*******************************************************************************\r
+ * Copyright (c) 2013 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.graphfile.adapters;\r
+\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.WriteGraph;\r
+import org.simantics.db.WriteOnlyGraph;\r
+import org.simantics.db.common.request.FreshEscapedName;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.layer0.request.PossibleModel;\r
+import org.simantics.graph.db.IImportAdvisor;\r
+import org.simantics.graph.representation.Root;\r
+import org.simantics.graphfile.ontology.GraphFileResource;\r
+import org.simantics.layer0.Layer0;\r
+\r
+public class SystemResourcePasteImportAdvisor implements IImportAdvisor {\r
+\r
+       protected Resource root;\r
+       protected Resource library;\r
+       protected Resource model;\r
+       protected String singleType = null;\r
+       protected Map<String, String> nameMappings = new HashMap<String, String>();\r
+       \r
+       public SystemResourcePasteImportAdvisor(ReadGraph graph, Resource library) throws DatabaseException {\r
+               this.library = library;\r
+               this.model = graph.syncRequest(new PossibleModel(library));\r
+       }\r
+\r
+       public Resource getTarget() {\r
+               return library;\r
+       }\r
+\r
+       public Resource analyzeType(ReadGraph graph, Root root) throws DatabaseException {\r
+               return null;\r
+       }\r
+       \r
+       @Override\r
+       public Resource analyzeRoot(ReadGraph graph, Root root) throws DatabaseException {\r
+               \r
+               if("%model".equals(root.name)) return model;\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
+               String newName = newName(graph, library, root.name);\r
+               nameMappings.put(root.name, newName);\r
+               \r
+               return null;\r
+\r
+       }\r
+       \r
+       public String newName(ReadGraph graph, Resource library, String name) throws DatabaseException {\r
+               return graph.syncRequest(new FreshEscapedName(library, GraphFileResource.getInstance(graph).HasSystemResource, name));\r
+       }\r
+\r
+       @Override\r
+       public Resource createRoot(WriteOnlyGraph graph, Root root) throws DatabaseException {\r
+               \r
+               Layer0 l0 = graph.getService(Layer0.class);\r
+\r
+               this.root = graph.newResource();\r
+               String name = root.name;\r
+               String newName = nameMappings.get(name);\r
+               graph.addLiteral(this.root, l0.HasName, l0.NameOf, l0.String, newName, Bindings.STRING);\r
+               return this.root;\r
+\r
+       }\r
+       \r
+       public Resource getRoot() {\r
+               return root;\r
+       }\r
+       \r
+       public void attachRoot(WriteGraph graph) throws DatabaseException {\r
+               GraphFileResource gf = GraphFileResource.getInstance(graph);\r
+               if (graph.isInstanceOf(library, gf.Folder)) {\r
+                       if (graph.isInstanceOf(root, gf.Folder)) {\r
+                               graph.claim(library, gf.HasFolder, root);\r
+                               return;\r
+                       }  else if (graph.isInstanceOf(root, gf.File)) {\r
+                               graph.claim(library, gf.HasFile, root);\r
+                               return;\r
+                       }\r
+               }\r
+               throw new DatabaseException("Unknown type, cannot attach copied resource " + root);\r
+       }\r
+\r
+}\r