1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.layer0.adapter.impl;
14 import java.util.Collections;
15 import java.util.HashMap;
18 import org.simantics.databoard.Bindings;
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.Resource;
21 import org.simantics.db.WriteOnlyGraph;
22 import org.simantics.db.common.request.FreshEscapedName;
23 import org.simantics.db.common.request.PossibleIndexRoot;
24 import org.simantics.db.common.utils.Versions;
25 import org.simantics.db.exception.DatabaseException;
26 import org.simantics.db.service.SerialisationSupport;
27 import org.simantics.graph.db.AbstractImportAdvisor2;
28 import org.simantics.graph.db.ImportAdvisors;
29 import org.simantics.graph.db.TransferableGraphImporter;
30 import org.simantics.graph.representation.Root;
31 import org.simantics.layer0.Layer0;
33 public class DefaultPasteImportAdvisor extends AbstractImportAdvisor2 {
35 protected Resource library;
36 protected Resource temp;
37 protected Resource model;
38 protected String singleType = null;
40 protected Map<String, String> nameMappings = new HashMap<String, String>();
42 final protected Map<String, Object> context;
44 public DefaultPasteImportAdvisor(ReadGraph graph, Resource library) throws DatabaseException {
45 this(library, graph.syncRequest(new PossibleIndexRoot(library)), Collections.<String,Object>emptyMap());
48 public DefaultPasteImportAdvisor(Resource library) {
49 this(library, library, Collections.<String,Object>emptyMap());
52 public DefaultPasteImportAdvisor(Resource library, Map<String, Object> context) {
53 this(library, library, context);
56 public DefaultPasteImportAdvisor(Resource library, Resource model, Map<String, Object> context) {
57 this.library = library;
59 this.context = context;
63 public void redirect(Resource temp) {
67 private Resource getRedirectTarget() {
68 if(temp != null) return temp;
72 public Resource getTarget() {
76 public void analyzeType(ReadGraph graph, Root root) throws DatabaseException {
80 public Resource analyzeRoot(ReadGraph graph, Root root) throws DatabaseException {
82 if("%model".equals(root.name)) {
86 analyzeType(graph, root);
88 String type = root.type;
89 if(singleType != null) {
90 if(!type.equals(singleType)) throw new DatabaseException("Paste of a set of different types of objects is not supported.");
96 String newName = newName(graph, library, root.name);
97 nameMappings.put(root.name, newName);
104 public String newName(ReadGraph graph, Resource library, String name) throws DatabaseException {
106 Map<String,String> renameMap = (Map<String,String>)context.get(ImportAdvisors.RENAME_MAP);
107 if(renameMap != null) {
108 String renamed = renameMap.get(name);
109 if(renamed != null) return renamed;
113 String version = Versions.getVersion(name);
114 if(version != null) {
115 String baseName = Versions.getBaseName(name);
116 String newBaseName = graph.syncRequest(new FreshEscapedName(library, Layer0.getInstance(graph).ConsistsOf, baseName));
117 return Versions.make(newBaseName, Versions.getBaseVersion(version));
119 return graph.syncRequest(new FreshEscapedName(library, Layer0.getInstance(graph).ConsistsOf, name));
125 public Resource createRoot(WriteOnlyGraph graph, Root root) throws DatabaseException {
126 return createRoot(graph, root, null);
129 public String getName(Root root) {
130 String name = root.name;
131 String newName = nameMappings.get(name);
132 if(newName != null) name = newName;
137 public Resource createRoot(WriteOnlyGraph graph, Root root, Resource resource) throws DatabaseException {
139 Layer0 l0 = graph.getService(Layer0.class);
141 if(resource == null) resource = graph.newResource();
142 if(getRedirectTarget() != null) {
143 graph.claim(getRedirectTarget(), l0.ConsistsOf, l0.PartOf, resource);
145 String newName = getName(root);
146 graph.addLiteral(resource, l0.HasName, l0.NameOf, l0.String, newName, Bindings.STRING);
148 addRootInfo(root, newName, resource);
155 public void beforeWrite(WriteOnlyGraph graph, TransferableGraphImporter process)
156 throws DatabaseException {
160 public void afterWrite(WriteOnlyGraph graph, TransferableGraphImporter process)
161 throws DatabaseException {
162 ids = process.getResourceIds(graph.getService(SerialisationSupport.class));
166 public boolean allowImmutableModifications() {
171 public Resource createChild(WriteOnlyGraph graph, TransferableGraphImporter process, Resource parent,
172 String name) throws DatabaseException {
173 return process.createChild(graph, parent, null, name);
177 public Resource createChild(WriteOnlyGraph graph, TransferableGraphImporter process, Resource parent, Resource child,
178 String name) throws DatabaseException {
179 return process.createChild(graph, parent, child, name);
182 public long[] getResourceIds() {