-/*******************************************************************************\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
+/*******************************************************************************
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.db.layer0.adapter.impl;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.simantics.databoard.Bindings;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.WriteOnlyGraph;
+import org.simantics.db.common.request.FreshEscapedName;
+import org.simantics.db.common.request.PossibleIndexRoot;
+import org.simantics.db.common.utils.Versions;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.service.SerialisationSupport;
+import org.simantics.graph.db.AbstractImportAdvisor2;
+import org.simantics.graph.db.ImportAdvisors;
+import org.simantics.graph.db.TransferableGraphImporter;
+import org.simantics.graph.representation.Root;
+import org.simantics.layer0.Layer0;
+
+public class DefaultPasteImportAdvisor extends AbstractImportAdvisor2 {
+
+ protected Resource library;
+ protected Resource temp;
+ protected Resource model;
+ protected String singleType = null;
+ protected long[] ids;
+ protected Map<String, String> nameMappings = new HashMap<String, String>();
+
+ final protected Map<String, Object> context;
+
+ public DefaultPasteImportAdvisor(ReadGraph graph, Resource library) throws DatabaseException {
+ this(library, graph.syncRequest(new PossibleIndexRoot(library)), Collections.<String,Object>emptyMap());
+ }
+
+ public DefaultPasteImportAdvisor(Resource library) {
+ this(library, library, Collections.<String,Object>emptyMap());
+ }
+
+ public DefaultPasteImportAdvisor(Resource library, Map<String, Object> context) {
+ this(library, library, context);
+ }
+
+ public DefaultPasteImportAdvisor(Resource library, Resource model, Map<String, Object> context) {
+ this.library = library;
+ this.model = model;
+ this.context = context;
+ }
+
+ @Override
+ public void redirect(Resource temp) {
+ this.temp = temp;
+ }
+
+ private Resource getRedirectTarget() {
+ if(temp != null) return temp;
+ else return library;
+ }
+
+ public Resource getTarget() {
+ return library;
+ }
+
+ public void analyzeType(ReadGraph graph, Root root) throws DatabaseException {
+ }
+
+ @Override
+ public Resource analyzeRoot(ReadGraph graph, Root root) throws DatabaseException {
+
+ if("%model".equals(root.name)) {
+ return model;
+ }
+
+ analyzeType(graph, root);
+
+ String type = root.type;
+ if(singleType != null) {
+ if(!type.equals(singleType)) throw new DatabaseException("Paste of a set of different types of objects is not supported.");
+ } else {
+ singleType = type;
+ }
+
+ if(library != null) {
+ String newName = newName(graph, library, root.name);
+ nameMappings.put(root.name, newName);
+ }
+
+ return null;
+
+ }
+
+ public String newName(ReadGraph graph, Resource library, String name) throws DatabaseException {
+
+ Map<String,String> renameMap = (Map<String,String>)context.get(ImportAdvisors.RENAME_MAP);
+ if(renameMap != null) {
+ String renamed = renameMap.get(name);
+ if(renamed != null) return renamed;
+ }
+
+
+ String version = Versions.getVersion(name);
+ if(version != null) {
+ String baseName = Versions.getBaseName(name);
+ String newBaseName = graph.syncRequest(new FreshEscapedName(library, Layer0.getInstance(graph).ConsistsOf, baseName));
+ return Versions.make(newBaseName, Versions.getBaseVersion(version));
+ } else {
+ return graph.syncRequest(new FreshEscapedName(library, Layer0.getInstance(graph).ConsistsOf, name));
+ }
+
+ }
+
+ @Override
+ public Resource createRoot(WriteOnlyGraph graph, Root root) throws DatabaseException {
+ return createRoot(graph, root, null);
+ }
+
+ public String getName(Root root) {
+ String name = root.name;
+ String newName = nameMappings.get(name);
+ if(newName != null) name = newName;
+ return name;
+ }
+
+ @Override
+ public Resource createRoot(WriteOnlyGraph graph, Root root, Resource resource) throws DatabaseException {
+
+ Layer0 l0 = graph.getService(Layer0.class);
+
+ if(resource == null) resource = graph.newResource();
+ if(getRedirectTarget() != null) {
+ graph.claim(getRedirectTarget(), l0.ConsistsOf, l0.PartOf, resource);
+ }
+ String newName = getName(root);
+ graph.addLiteral(resource, l0.HasName, l0.NameOf, l0.String, newName, Bindings.STRING);
+
+ addRootInfo(root, newName, resource);
+
+ return resource;
+
+ }
+
+ @Override
+ public void beforeWrite(WriteOnlyGraph graph, TransferableGraphImporter process)
+ throws DatabaseException {
+ }
+
+ @Override
+ public void afterWrite(WriteOnlyGraph graph, TransferableGraphImporter process)
+ throws DatabaseException {
+ ids = process.getResourceIds(graph.getService(SerialisationSupport.class));
+ }
+
+ @Override
+ public boolean allowImmutableModifications() {
+ return false;
+ }
+
+ @Override
+ public Resource createChild(WriteOnlyGraph graph, TransferableGraphImporter process, Resource parent,
+ String name) throws DatabaseException {
+ return process.createChild(graph, parent, null, name);
+ }
+
+ @Override
+ public Resource createChild(WriteOnlyGraph graph, TransferableGraphImporter process, Resource parent, Resource child,
+ String name) throws DatabaseException {
+ return process.createChild(graph, parent, child, name);
+ }
+
+ public long[] getResourceIds() {
+ return ids;
+ }
+
+}