]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/request/ApplyTemplatesRequest.java
Initial support for concurrency in databoard, bindings and serializers
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / request / ApplyTemplatesRequest.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in 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.request;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.HashMap;
17 import java.util.HashSet;
18 import java.util.Map;
19
20 import org.simantics.db.Resource;
21 import org.simantics.db.WriteGraph;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.db.layer0.adapter.Template;
24 import org.simantics.operation.Layer0X;
25
26 /**
27  * Applies templates to an instance.
28  * @author Hannu Niemist�
29  */
30 public class ApplyTemplatesRequest {
31         Map<String, Object> parameters;
32         Collection<Resource> contexts;
33         
34         public ApplyTemplatesRequest(Map<String, Object> parameters,
35                         Collection<Resource> contexts) {
36                 this.parameters = parameters;
37                 this.contexts = contexts;
38         }
39         
40         public ApplyTemplatesRequest(Map<String, Object> parameters) {
41                 this(parameters, new ArrayList<Resource>(2));
42         }
43         
44         public ApplyTemplatesRequest() {
45                 this(new HashMap<String, Object>());
46         }
47
48         public void perform(WriteGraph g, Resource resource) throws DatabaseException {
49             Layer0X L0X = Layer0X.getInstance(g);
50
51                 parameters.put("", resource);
52                 
53                 // Use default context if no other contexts have been defined
54                 if(contexts.isEmpty())
55                         contexts.add(L0X.HasTemplate);
56                 
57                 // Collect templates
58                 HashSet<Resource> templates = new HashSet<Resource>();
59                 for(Resource context : contexts)
60                         templates.addAll(g.getObjects(resource, context));
61                 
62                 // Apply templates
63                 for(Resource template : templates)
64                         g.adapt(template, Template.class).apply(g, parameters);
65         }
66         
67         public void addContext(Resource context) {
68                 contexts.add(context);
69         }
70         
71         public void addParameter(String key, Object value) {
72                 parameters.put(key, value);
73         }
74         
75         public <T> T getParameter(String key) {
76                 return (T)parameters.get(key);
77         }
78         
79         public Map<String,Object> getParameters() {
80                 return parameters;
81         }
82 }