]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling.tests/src/org/simantics/modeling/tests/commands/ResourceWriteCommand.java
bc21a31c28bde97a3eb4c0c3321f77d9301854d8
[simantics/platform.git] / bundles / org.simantics.modeling.tests / src / org / simantics / modeling / tests / commands / ResourceWriteCommand.java
1 package org.simantics.modeling.tests.commands;
2
3 import org.simantics.Simantics;
4 import org.simantics.db.ReadGraph;
5 import org.simantics.db.Resource;
6 import org.simantics.db.WriteGraph;
7 import org.simantics.db.common.request.WriteResultRequest;
8 import org.simantics.db.exception.DatabaseException;
9 import org.simantics.db.testing.common.Command;
10 import org.simantics.db.testing.common.CommandSequenceEnvironment;
11 import org.simantics.modeling.tests.traits.SingleResourceTrait;
12 import org.simantics.modeling.tests.traits.UriResourceTrait;
13
14 abstract public class ResourceWriteCommand<T extends CommandSequenceEnvironment> extends Command<T> implements SingleResourceTrait {
15
16         private Resource resource;
17         
18         @Override
19         final public void run(final T environment) throws Exception {
20                 resource = runResource(environment); 
21                 afterRun(environment);
22         }
23         
24         public Resource runResource(final T environment) throws Exception {
25                 return Simantics.getSession().sync(new WriteResultRequest() {
26
27                         @Override
28                         public Resource perform(WriteGraph graph) throws DatabaseException {
29                                 return ResourceWriteCommand.this.run(graph, environment);
30                         }
31                         
32                         @Override
33                         public String toString() {
34                                 return ResourceWriteCommand.this.toString();
35                         }
36                         
37                 });
38         }
39         
40         protected Resource run(WriteGraph graph, T environment) throws DatabaseException {
41                 throw new IllegalStateException();
42         }
43         
44         protected void afterRun(T environment) throws Exception {
45                 
46         }
47
48         @Override
49         public String toString() {
50                 return "ResourceWriteCommand " + getClass().getSimpleName();
51         }
52         
53         @Override
54         public Resource getResource() throws DatabaseException {
55                 return resource;
56         }
57         
58         @Override
59         public Resource getResource(ReadGraph graph) throws DatabaseException {
60                 return resource;
61         }
62         
63         @Override
64         public SingleResourceTrait child(String path) {
65                 return new UriResourceTrait(this, path);
66         }
67         
68 }