1 /*******************************************************************************
\r
2 * Copyright (c) 2011 Association for Decentralized Information Management in
\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.Map;
\r
16 import org.simantics.databoard.Bindings;
\r
17 import org.simantics.db.ReadGraph;
\r
18 import org.simantics.db.Resource;
\r
19 import org.simantics.db.WriteGraph;
\r
20 import org.simantics.db.WriteOnlyGraph;
\r
21 import org.simantics.db.exception.DatabaseException;
\r
22 import org.simantics.db.layer0.adapter.Template;
\r
23 import org.simantics.graph.db.IImportAdvisor;
\r
24 import org.simantics.graph.db.TransferableGraphs;
\r
25 import org.simantics.graph.representation.Root;
\r
26 import org.simantics.graph.representation.TransferableGraph1;
\r
27 import org.simantics.layer0.Layer0;
\r
29 public class GraphTemplate implements Template {
\r
31 String[] parameters;
\r
32 TransferableGraph1 tg;
\r
34 public GraphTemplate(String[] parameters, TransferableGraph1 tg) {
\r
35 this.parameters = parameters;
\r
39 public static GraphTemplate create(ReadGraph g, Resource r) throws DatabaseException {
\r
40 Layer0 l0 = Layer0.getInstance(g);
\r
41 String[] parameters = g.getRelatedValue(r, l0.HasTemplateParameters, Bindings.STRING_ARRAY);
\r
42 TransferableGraph1 tg = g.getRelatedValue(r, l0.HasTemplate, TransferableGraph1.BINDING);
\r
43 return new GraphTemplate(parameters, tg);
\r
47 public void apply(WriteGraph graph, final Map<String, Object> parameters)
\r
48 throws DatabaseException {
\r
49 TransferableGraphs.importGraph1(graph, tg, new IImportAdvisor() {
\r
51 public Resource createRoot(WriteOnlyGraph graph, Root root) throws DatabaseException {
\r
52 String name = root.name;
\r
53 Object obj = parameters.get(name);
\r
55 Resource r = graph.newResource();
\r
56 parameters.put(name, r);
\r
61 if(obj instanceof Resource)
\r
64 r = graph.newResource();
\r
65 graph.claimValue(r, obj);
\r
72 public Resource analyzeRoot(ReadGraph graph, Root root) throws DatabaseException {
\r