]> gerrit.simantics Code Review - simantics/platform.git/blob
48ee1aac7f0a0b806fd1cf31a594ae116f95b286
[simantics/platform.git] /
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.HashMap;
15 import java.util.HashSet;
16 import java.util.Map;
17
18 import org.simantics.db.Resource;
19 import org.simantics.db.VirtualGraph;
20 import org.simantics.db.WriteGraph;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.db.layer0.adapter.Template;
23 import org.simantics.db.request.UndoTraits;
24 import org.simantics.db.request.WriteResult;
25 import org.simantics.layer0.Layer0;
26
27 public class DefaultInstanceFactoryRequest implements WriteResult<Map<String, Object>> {
28         
29         Resource type;
30         HashMap<String, Object> parameters;
31         Resource[] contexts;
32         
33         public DefaultInstanceFactoryRequest(Resource type, Map<String, Object> parameters,
34                         Resource ... contexts) {
35                 this.parameters = new HashMap<String, Object>(parameters);
36                 this.contexts = contexts;
37                 this.type = type;
38         }
39         
40         public Map<String, Object> perform(WriteGraph g) throws DatabaseException {
41
42         Layer0 b = Layer0.getInstance(g);
43                 
44                 // Default instantiation logic
45                 Resource instance = g.newResource();
46                 g.claim(instance, b.InstanceOf, null, type);
47
48                 parameters.put("", instance);
49
50                 // Collect templates
51                 HashSet<Resource> templates = new HashSet<Resource>();
52                 for(Resource context : contexts)
53                         templates.addAll(g.getObjects(instance, context));
54
55                 // Apply templates
56                 for(Resource template : templates)
57                         g.adapt(template, Template.class).apply(g, parameters);
58
59                 return parameters;
60                 
61         }
62
63         @Override
64         public UndoTraits getUndoTraits() {
65                 return null;
66         }
67
68         @Override
69         public VirtualGraph getProvider() {
70                 return null;
71         }
72 }