// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
package nosql.bdbje;

import java.io.File;
import java.math.BigDecimal;

import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.je.Transaction;
import com.sleepycat.je.TransactionConfig;
import com.sleepycat.persist.EntityStore;
import com.sleepycat.persist.PrimaryIndex;
import com.sleepycat.persist.StoreConfig;
import com.sleepycat.persist.model.Entity;
import com.sleepycat.persist.model.PrimaryKey;

public class TestTxO {
	@Entity
    public static class Account {
    	@PrimaryKey
    	private String name;
        private BigDecimal amount;
        public Account() {
        	this("", new BigDecimal("0.00"));
        }
        public Account(String name, BigDecimal amount) {
        	this.name = name;
            this.amount = amount;
        }
        public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public BigDecimal getAmount() {
            return amount;
        }
        public void transfer(BigDecimal delta) {
            amount = amount.add(delta);
        }
    }
    public static void saveAccount(PrimaryIndex<String, Account> pk, Account acct) {
        TransactionConfig txcfg = new TransactionConfig();
        txcfg.setReadCommitted(true);
        Transaction tx = pk.getDatabase().getEnvironment().beginTransaction(null, txcfg);
        pk.put(acct);
        tx.commitSync();
    }
    public static void transfer(PrimaryIndex<String, Account> pk, String fromid, String toid, BigDecimal amount) {
        TransactionConfig txcfg = new TransactionConfig();
        txcfg.setReadCommitted(true);
        Transaction tx = pk.getDatabase().getEnvironment().beginTransaction(null, txcfg);
        Account fromacct = pk.get(fromid);
        Account toacct = pk.get(toid);
        fromacct.transfer(BigDecimal.ZERO.subtract(amount));
        toacct.transfer(amount);
        pk.put(fromacct);
        pk.put(toacct);
        tx.commitSync();
        //tx.abort();
    }
    public static void main(String[] args) {
        // open database
        EnvironmentConfig envcfg = new EnvironmentConfig();
        envcfg.setAllowCreateVoid(true);
        envcfg.setTransactional(true);
        Environment env = new Environment(new File("/work/BDB"), envcfg);
        StoreConfig stcfg = new StoreConfig();
        stcfg.setAllowCreate(true);
        stcfg.setTransactional(true);
        EntityStore est = new EntityStore(env, "acctdb", stcfg);
        PrimaryIndex<String, Account> pk = est.getPrimaryIndex(String.class, Account.class);
        // setup accounts
        saveAccount(pk, new Account("A", new BigDecimal("100.00")));
        saveAccount(pk, new Account("B", new BigDecimal("0.00")));
        // display accounts
        Account acct_a = pk.get("A");
        System.out.printf("%s : %s\n", acct_a.getName(), acct_a.getAmount());
        Account acct_b = pk.get("B");
        System.out.printf("%s : %s\n", acct_b.getName(), acct_b.getAmount());
        // transfer money
        transfer(pk, "A", "B", new BigDecimal("20.00"));
        // display accounts
        acct_a = pk.get("A");
        System.out.printf("%s : %s\n", acct_a.getName(), acct_a.getAmount());
        acct_b = pk.get("B");
        System.out.printf("%s : %s\n", acct_b.getName(), acct_b.getAmount());
        // close database
        est.close();
        env.close();
    }
}
