package com.atlassian.bitbucket.mesh.transaction;

import com.atlassian.bitbucket.mesh.repository.Repository;
import com.atlassian.bitbucket.mesh.rpc.v1.RpcTransaction;

import javax.annotation.Nonnull;

/**
 * An action that has been prepared in a {@link Transaction}.
 *
 * @see TransactionManager#prepared(PreparedAction)
 */
public interface PreparedAction {

    /**
     * Called by the system when the {@link Transaction} that this entity was
     * {@link TransactionManager#prepared(PreparedAction) enrolled} in is committed.
     */
    default void commit() {
    }

    /**
     * @return the repository this operation affects
     */
    @Nonnull
    Repository getRepository();

    /**
     * @param builder a builder that is pre-populated with the transaction ID, timestamp and repository
     * @return the builder with details that fully describe this transactional operation added to it
     */
    @Nonnull
    RpcTransaction.Builder populateTransaction(@Nonnull RpcTransaction.Builder builder);

    /**
     * Called by the system when the {@link Transaction} that this entity was
     * {@link TransactionManager#prepared(PreparedAction) enrolled} in is rolled back.
     */
    default void rollBack() {
    }
}
