DefaultTransactionDefinition.java 7.88 KB
/*
 * Decompiled with CFR 0_118.
 */
package com.adobe.pdfg.transaction;

import com.adobe.pdfg.transaction.TransactionDefinition;
import java.io.Serializable;

public class DefaultTransactionDefinition
implements TransactionDefinition,
Serializable {
    public static final String PREFIX_PROPAGATION = "PROPAGATION_";
    public static final String PROPAGATION_REQUIRED = "PROPAGATION_REQUIRED";
    public static final String PROPAGATION_SUPPORTS = "PROPAGATION_SUPPORTS";
    public static final String PROPAGATION_MANDATORY = "PROPAGATION_MANDATORY";
    public static final String PROPAGATION_REQUIRES_NEW = "PROPAGATION_REQUIRES_NEW";
    public static final String PROPAGATION_NOT_SUPPORTED = "PROPAGATION_NOT_SUPPORTED";
    public static final String PROPAGATION_NEVER = "PROPAGATION_NEVER";
    public static final String PROPAGATION_NESTED = "PROPAGATION_NESTED";
    public static final String ISOLATION_DEFAULT = "ISOLATION_DEFAULT";
    public static final String ISOLATION_READ_UNCOMMITTED = "ISOLATION_READ_UNCOMMITTED";
    public static final String ISOLATION_READ_COMMITTED = "ISOLATION_READ_COMMITTED";
    public static final String ISOLATION_REPEATABLE_READ = "ISOLATION_REPEATABLE_READ";
    public static final String ISOLATION_SERIALIZABLE = "ISOLATION_SERIALIZABLE";
    public static final String PREFIX_ISOLATION = "ISOLATION_";
    public static final String PREFIX_TIMEOUT = "timeout_";
    public static final String READ_ONLY_MARKER = "readOnly";
    private int propagationBehavior = 0;
    private int isolationLevel = -1;
    private int timeout = -1;
    private boolean readOnly = false;
    private String name;

    public DefaultTransactionDefinition() {
    }

    public DefaultTransactionDefinition(TransactionDefinition other) {
        this.propagationBehavior = other.getPropagationBehavior();
        this.isolationLevel = other.getIsolationLevel();
        this.timeout = other.getTimeout();
        this.readOnly = other.isReadOnly();
        this.name = other.getName();
    }

    public DefaultTransactionDefinition(int propagationBehavior) {
        this.propagationBehavior = propagationBehavior;
    }

    public final void setPropagationBehaviorName(String constantName) throws IllegalArgumentException {
        if (constantName == null || !constantName.startsWith("PROPAGATION_")) {
            throw new IllegalArgumentException("Only propagation constants allowed");
        }
        this.setPropagationBehavior(DefaultTransactionDefinition.getPropagationBehaviorVal(constantName));
    }

    public final void setPropagationBehavior(int propagationBehavior) {
        this.propagationBehavior = propagationBehavior;
    }

    public final int getPropagationBehavior() {
        return this.propagationBehavior;
    }

    public final void setIsolationLevelName(String constantName) throws IllegalArgumentException {
        if (constantName == null || !constantName.startsWith("ISOLATION_")) {
            throw new IllegalArgumentException("Only isolation constants allowed");
        }
        this.setIsolationLevel(DefaultTransactionDefinition.getIsolationLevelVal(constantName));
    }

    public final void setIsolationLevel(int isolationLevel) {
        this.isolationLevel = isolationLevel;
    }

    public final int getIsolationLevel() {
        return this.isolationLevel;
    }

    public final void setTimeout(int timeout) {
        if (timeout < -1) {
            throw new IllegalArgumentException("Timeout must be a positive integer or TIMEOUT_DEFAULT");
        }
        this.timeout = timeout;
    }

    public final int getTimeout() {
        return this.timeout;
    }

    public final void setReadOnly(boolean readOnly) {
        this.readOnly = readOnly;
    }

    public final boolean isReadOnly() {
        return this.readOnly;
    }

    public final void setName(String name) {
        this.name = name;
    }

    public final String getName() {
        return this.name;
    }

    public boolean equals(Object other) {
        return other instanceof TransactionDefinition && this.toString().equals(other.toString());
    }

    public int hashCode() {
        return this.toString().hashCode();
    }

    public String toString() {
        return this.getDefinitionDescription().toString();
    }

    protected final StringBuilder getDefinitionDescription() {
        StringBuilder result = new StringBuilder();
        result.append(DefaultTransactionDefinition.getPropagationBehaviorStr(this.propagationBehavior));
        result.append(',');
        result.append(DefaultTransactionDefinition.getIsolationLevelStr(this.isolationLevel));
        if (this.timeout != -1) {
            result.append(',');
            result.append("timeout_").append(this.timeout);
        }
        if (this.readOnly) {
            result.append(',');
            result.append("readOnly");
        }
        return result;
    }

    private static String getPropagationBehaviorStr(int propagationBehavior) {
        switch (propagationBehavior) {
            case 0: {
                return "PROPAGATION_REQUIRED";
            }
            case 1: {
                return "PROPAGATION_SUPPORTS";
            }
            case 2: {
                return "PROPAGATION_MANDATORY";
            }
            case 3: {
                return "PROPAGATION_REQUIRES_NEW";
            }
            case 4: {
                return "PROPAGATION_NOT_SUPPORTED";
            }
            case 5: {
                return "PROPAGATION_NEVER";
            }
            case 6: {
                return "PROPAGATION_NESTED";
            }
        }
        return "PROPAGATION_REQUIRED";
    }

    private static int getPropagationBehaviorVal(String propagationBehavior) {
        if (propagationBehavior != null && propagationBehavior.trim().equals("PROPAGATION_REQUIRED")) {
            return 0;
        }
        if (propagationBehavior != null && propagationBehavior.trim().equals("PROPAGATION_SUPPORTS")) {
            return 1;
        }
        if (propagationBehavior != null && propagationBehavior.trim().equals("PROPAGATION_MANDATORY")) {
            return 2;
        }
        if (propagationBehavior != null && propagationBehavior.trim().equals("PROPAGATION_REQUIRES_NEW")) {
            return 3;
        }
        if (propagationBehavior != null && propagationBehavior.trim().equals("PROPAGATION_NOT_SUPPORTED")) {
            return 4;
        }
        if (propagationBehavior != null && propagationBehavior.trim().equals("PROPAGATION_NEVER")) {
            return 5;
        }
        if (propagationBehavior != null && propagationBehavior.trim().equals("PROPAGATION_NESTED")) {
            return 6;
        }
        return 0;
    }

    private static String getIsolationLevelStr(int isolationLevel) {
        switch (isolationLevel) {
            case -1: {
                return "ISOLATION_DEFAULT";
            }
            case 1: {
                return "ISOLATION_READ_UNCOMMITTED";
            }
            case 2: {
                return "ISOLATION_READ_COMMITTED";
            }
            case 4: {
                return "ISOLATION_REPEATABLE_READ";
            }
            case 8: {
                return "ISOLATION_SERIALIZABLE";
            }
        }
        return "ISOLATION_DEFAULT";
    }

    private static int getIsolationLevelVal(String isolationLevel) {
        if (isolationLevel != null && isolationLevel.trim().equals("ISOLATION_DEFAULT")) {
            return -1;
        }
        if (isolationLevel != null && isolationLevel.trim().equals("ISOLATION_READ_UNCOMMITTED")) {
            return 1;
        }
        if (isolationLevel != null && isolationLevel.trim().equals("ISOLATION_READ_COMMITTED")) {
            return 2;
        }
        if (isolationLevel != null && isolationLevel.trim().equals("ISOLATION_REPEATABLE_READ")) {
            return 4;
        }
        if (isolationLevel != null && isolationLevel.trim().equals("ISOLATION_SERIALIZABLE")) {
            return 8;
        }
        return -1;
    }
}