]> gerrit.simantics Code Review - simantics/sysdyn.git/blob
43f0ffac298b03e30619db191cef4312a0dd87d1
[simantics/sysdyn.git] /
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2012 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.sysdyn.ui.browser.actions.newActions;\r
13 \r
14 import java.util.UUID;\r
15 \r
16 import org.simantics.db.Resource;\r
17 import org.simantics.db.WriteGraph;\r
18 import org.simantics.db.common.request.WriteRequest;\r
19 import org.simantics.db.common.utils.NameUtils;\r
20 import org.simantics.db.exception.DatabaseException;\r
21 import org.simantics.db.layer0.adapter.ActionFactory;\r
22 import org.simantics.layer0.Layer0;\r
23 import org.simantics.layer0.utils.direct.GraphUtils;\r
24 import org.simantics.sysdyn.SysdynResource;\r
25 import org.simantics.ui.SimanticsUI;\r
26 \r
27 /**\r
28  * Create a new history data\r
29  * \r
30  * @author Teemu Lempinen\r
31  *\r
32  */\r
33 public class NewHistoryDataAction implements ActionFactory{\r
34 \r
35     @Override\r
36     public Runnable create(Object target) {\r
37         if(!(target instanceof Resource))\r
38             return null;\r
39         final Resource experiment = (Resource)target;\r
40 \r
41         return new Runnable() {\r
42             @Override\r
43             public void run() {\r
44 \r
45                 SimanticsUI.getSession().asyncRequest(new WriteRequest() {\r
46 \r
47                     @Override\r
48                     public void perform(WriteGraph graph) throws DatabaseException {\r
49                         SysdynResource sr = SysdynResource.getInstance(graph);\r
50                         Layer0 l0 = Layer0.getInstance(graph);\r
51                         if(!graph.isInstanceOf(experiment, sr.Experiment))\r
52                             return; // Not called from an experiment\r
53 \r
54                         Resource model = graph.getPossibleObject(experiment, l0.PartOf);\r
55                         // Create the history dataset\r
56                         GraphUtils.create2(graph, \r
57                                 sr.HistoryDataset,\r
58                                 l0.HasName, "HistoryDataset" + UUID.randomUUID().toString(),\r
59                                 l0.HasLabel, NameUtils.findFreshLabel(graph, "History Dataset", experiment),\r
60                                 sr.Experiment_result_Inverse, experiment, \r
61                                 sr.HistoryDataset_columns, Boolean.TRUE,\r
62                                 l0.PartOf, model);\r
63                     }\r
64                 });\r
65             }\r
66         };\r
67     }\r
68 \r
69 }\r