1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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.request;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.HashMap;
17 import java.util.HashSet;
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;
27 * Applies templates to an instance.
28 * @author Hannu Niemist�
30 public class ApplyTemplatesRequest {
31 Map<String, Object> parameters;
32 Collection<Resource> contexts;
34 public ApplyTemplatesRequest(Map<String, Object> parameters,
35 Collection<Resource> contexts) {
36 this.parameters = parameters;
37 this.contexts = contexts;
40 public ApplyTemplatesRequest(Map<String, Object> parameters) {
41 this(parameters, new ArrayList<Resource>(2));
44 public ApplyTemplatesRequest() {
45 this(new HashMap<String, Object>());
48 public void perform(WriteGraph g, Resource resource) throws DatabaseException {
49 Layer0X L0X = Layer0X.getInstance(g);
51 parameters.put("", resource);
53 // Use default context if no other contexts have been defined
54 if(contexts.isEmpty())
55 contexts.add(L0X.HasTemplate);
58 HashSet<Resource> templates = new HashSet<Resource>();
59 for(Resource context : contexts)
60 templates.addAll(g.getObjects(resource, context));
63 for(Resource template : templates)
64 g.adapt(template, Template.class).apply(g, parameters);
67 public void addContext(Resource context) {
68 contexts.add(context);
71 public void addParameter(String key, Object value) {
72 parameters.put(key, value);
75 public <T> T getParameter(String key) {
76 return (T)parameters.get(key);
79 public Map<String,Object> getParameters() {