1 /*******************************************************************************
2 * Copyright (c) 2013 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.graphfile.adapters;
14 import java.util.HashMap;
17 import org.simantics.databoard.Bindings;
18 import org.simantics.db.ReadGraph;
19 import org.simantics.db.Resource;
20 import org.simantics.db.WriteGraph;
21 import org.simantics.db.WriteOnlyGraph;
22 import org.simantics.db.common.request.FreshEscapedName;
23 import org.simantics.db.exception.DatabaseException;
24 import org.simantics.db.layer0.request.PossibleModel;
25 import org.simantics.graph.db.IImportAdvisor;
26 import org.simantics.graph.representation.Root;
27 import org.simantics.graphfile.ontology.GraphFileResource;
28 import org.simantics.layer0.Layer0;
30 public class SystemResourcePasteImportAdvisor implements IImportAdvisor {
32 protected Resource root;
33 protected Resource library;
34 protected Resource model;
35 protected String singleType = null;
36 protected Map<String, String> nameMappings = new HashMap<String, String>();
38 public SystemResourcePasteImportAdvisor(ReadGraph graph, Resource library) throws DatabaseException {
39 this.library = library;
40 this.model = graph.syncRequest(new PossibleModel(library));
43 public Resource getTarget() {
47 public Resource analyzeType(ReadGraph graph, Root root) throws DatabaseException {
52 public Resource analyzeRoot(ReadGraph graph, Root root) throws DatabaseException {
54 if("%model".equals(root.name)) return model;
56 analyzeType(graph, root);
58 String type = root.type;
59 if(singleType != null) {
60 if(!type.equals(singleType)) throw new DatabaseException("Paste of a set of different types of objects is not supported.");
65 String newName = newName(graph, library, root.name);
66 nameMappings.put(root.name, newName);
72 public String newName(ReadGraph graph, Resource library, String name) throws DatabaseException {
73 return graph.syncRequest(new FreshEscapedName(library, GraphFileResource.getInstance(graph).HasSystemResource, name));
77 public Resource createRoot(WriteOnlyGraph graph, Root root) throws DatabaseException {
79 Layer0 l0 = graph.getService(Layer0.class);
81 this.root = graph.newResource();
82 String name = root.name;
83 String newName = nameMappings.get(name);
84 graph.addLiteral(this.root, l0.HasName, l0.NameOf, l0.String, newName, Bindings.STRING);
89 public Resource getRoot() {
93 public void attachRoot(WriteGraph graph) throws DatabaseException {
94 GraphFileResource gf = GraphFileResource.getInstance(graph);
95 if (graph.isInstanceOf(library, gf.Folder)) {
96 if (graph.isInstanceOf(root, gf.Folder)) {
97 graph.claim(library, gf.HasFolder, root);
99 } else if (graph.isInstanceOf(root, gf.File)) {
100 graph.claim(library, gf.HasFile, root);
104 throw new DatabaseException("Unknown type, cannot attach copied resource " + root);