]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/adapter/impl/GraphTemplate.java
Some fixes for resource cleaning spreadsheets in simupedia
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / adapter / impl / GraphTemplate.java
1 /*******************************************************************************
2  * Copyright (c) 2011 Association for Decentralized Information Management in
3  * Industry THTH ry.
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
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.layer0.adapter.impl;
13
14 import java.util.Map;
15
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;
28
29 public class GraphTemplate implements Template {
30
31     String[] parameters;
32     TransferableGraph1 tg;
33     
34     public GraphTemplate(String[] parameters, TransferableGraph1 tg) {
35         this.parameters = parameters;
36         this.tg = tg;
37     }
38
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);
44     }
45
46     @Override
47     public void apply(WriteGraph graph, final Map<String, Object> parameters)
48             throws DatabaseException {
49         TransferableGraphs.importGraph1(graph, tg, new IImportAdvisor() {
50             @Override
51             public Resource createRoot(WriteOnlyGraph graph, Root root) throws DatabaseException {
52                 String name = root.name;
53                 Object obj = parameters.get(name);
54                 if(obj == null) {
55                     Resource r = graph.newResource();
56                     parameters.put(name, r);
57                     return r;
58                 }
59                 else {
60                     Resource r;
61                     if(obj instanceof Resource)
62                         r = (Resource)obj;
63                     else {
64                         r = graph.newResource();
65                         graph.claimValue(r, obj);
66                     }
67                     return r;
68                 }
69             }
70
71             @Override
72             public Resource analyzeRoot(ReadGraph graph, Root root) throws DatabaseException {
73                 return null;
74             }
75         });
76     }
77     
78 }