X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.db.procore%2Fsrc%2Ffi%2Fvtt%2Fsimantics%2Fprocore%2Finternal%2FWriteStateBase.java;fp=bundles%2Forg.simantics.db.procore%2Fsrc%2Ffi%2Fvtt%2Fsimantics%2Fprocore%2Finternal%2FWriteStateBase.java;h=303e42cfc989e58da286c3f8d8dc5521258fc4b1;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/WriteStateBase.java b/bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/WriteStateBase.java new file mode 100644 index 000000000..303e42cfc --- /dev/null +++ b/bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/WriteStateBase.java @@ -0,0 +1,83 @@ +package fi.vtt.simantics.procore.internal; + +import java.util.concurrent.Semaphore; + +import org.simantics.db.common.utils.Logger; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.procedure.Procedure; +import org.simantics.db.request.WriteTraits; + +/** + * @author Tuukka Lehtonen + * + * @param result value type + */ +public class WriteStateBase { + + final protected WriteTraits request; + final protected Semaphore notify; + final protected Procedure procedure; + + protected T result; + protected Throwable exception; + + public WriteStateBase(WriteTraits request, Semaphore notify, Procedure procedure) { + assert(request != null); + + this.request = request; + this.notify = notify; + this.procedure = procedure; + } + + public WriteTraits getRequest() { + return request; + } + + @SuppressWarnings("unchecked") + public void setResult(Object result) { + this.result = (T) result; + } + + public void except(Throwable throwable) { + this.exception = throwable; + } + +// public void cancel(CancelTransactionException e) { +// this.exception = e; +// } +// +// public boolean isCanceled() { +// return exception instanceof CancelTransactionException; +// } + + public boolean isExcepted() { + return exception != null; + } + + public void finish() { + + if(procedure != null) { + + try { + // Callback is client code, we have to be prepared for it to throw unexpected exceptions. + // All we can do here is to log those, can't really pass them anywhere. + if (procedure != null) { + if(isExcepted()) { + if(exception instanceof DatabaseException) procedure.exception(exception); + else procedure.exception(new DatabaseException(exception)); + } + else procedure.execute(result); + } + } catch (Throwable t) { + Logger.defaultLogError("Write request callback caused an unexpected error, see exception.", t); + if (SessionImplSocket.DEBUG) + t.printStackTrace(); + } + + } + + if(notify != null) notify.release(); + + } + +}