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