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