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