X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.modeling.tests%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Ftests%2Fcommands%2FResourceWriteCommand.java;fp=bundles%2Forg.simantics.modeling.tests%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2Ftests%2Fcommands%2FResourceWriteCommand.java;h=bc21a31c28bde97a3eb4c0c3321f77d9301854d8;hb=ca6fcd858a0b82eb1b5a8f12c4d5df30e84393e0;hp=0000000000000000000000000000000000000000;hpb=67fd62f9c742337ec80eef658192db198a0efaac;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.modeling.tests/src/org/simantics/modeling/tests/commands/ResourceWriteCommand.java b/bundles/org.simantics.modeling.tests/src/org/simantics/modeling/tests/commands/ResourceWriteCommand.java new file mode 100644 index 000000000..bc21a31c2 --- /dev/null +++ b/bundles/org.simantics.modeling.tests/src/org/simantics/modeling/tests/commands/ResourceWriteCommand.java @@ -0,0 +1,68 @@ +package org.simantics.modeling.tests.commands; + +import org.simantics.Simantics; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.WriteResultRequest; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.testing.common.Command; +import org.simantics.db.testing.common.CommandSequenceEnvironment; +import org.simantics.modeling.tests.traits.SingleResourceTrait; +import org.simantics.modeling.tests.traits.UriResourceTrait; + +abstract public class ResourceWriteCommand extends Command implements SingleResourceTrait { + + private Resource resource; + + @Override + final public void run(final T environment) throws Exception { + resource = runResource(environment); + afterRun(environment); + } + + public Resource runResource(final T environment) throws Exception { + return Simantics.getSession().sync(new WriteResultRequest() { + + @Override + public Resource perform(WriteGraph graph) throws DatabaseException { + return ResourceWriteCommand.this.run(graph, environment); + } + + @Override + public String toString() { + return ResourceWriteCommand.this.toString(); + } + + }); + } + + protected Resource run(WriteGraph graph, T environment) throws DatabaseException { + throw new IllegalStateException(); + } + + protected void afterRun(T environment) throws Exception { + + } + + @Override + public String toString() { + return "ResourceWriteCommand " + getClass().getSimpleName(); + } + + @Override + public Resource getResource() throws DatabaseException { + return resource; + } + + @Override + public Resource getResource(ReadGraph graph) throws DatabaseException { + return resource; + } + + @Override + public SingleResourceTrait child(String path) { + return new UriResourceTrait(this, path); + } + +}