+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.db.layer0.request;\r
+\r
+import java.util.Collection;\r
+import java.util.Map;\r
+\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.WriteGraph;\r
+import org.simantics.db.common.utils.CommonDBUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.layer0.Layer0;\r
+\r
+/**\r
+ * Instantiates given type using templates attached to the type.\r
+ * @author Hannu Niemistö\r
+ */\r
+public class InstantiateRequest extends ApplyTemplatesRequest {\r
+ Resource type;\r
+ \r
+ public InstantiateRequest(Resource type, Map<String, Object> parameters,\r
+ Collection<Resource> contexts) {\r
+ super(parameters, contexts);\r
+ this.type = type;\r
+ }\r
+ \r
+ public InstantiateRequest(Resource type, Map<String, Object> parameters) {\r
+ super(parameters);\r
+ this.type = type;\r
+ }\r
+ \r
+ public InstantiateRequest(Resource type) {\r
+ super();\r
+ this.type = type;\r
+ }\r
+\r
+ public Resource perform(WriteGraph g) throws DatabaseException {\r
+ Layer0 b = Layer0.getInstance(g);\r
+ \r
+ Resource parent = (Resource)parameters.get("parent");\r
+ if(parent != null) CommonDBUtils.selectClusterSet(g, parent);\r
+ \r
+ // Default instantiation logic\r
+ Resource instance = g.newResource();\r
+ g.newClusterSet(instance);\r
+ g.claim(instance, b.InstanceOf, null, type);\r
+ \r
+ perform(g, instance);\r
+ \r
+ return instance;\r
+ }\r
+}\r