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); } }