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