<commit_before>package org.interledger.everledger.impl.manager;

import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;

import javax.money.MonetaryAmount;

import org.interledger.cryptoconditions.Condition;
import org.interledger.cryptoconditions.Fulfillment;
import org.interledger.everledger.LedgerAccountManagerFactory;
import org.interledger.everledger.ifaces.account.IfaceLocalAccount;
import org.interledger.everledger.ifaces.transfer.IfaceTransfer;
import org.interledger.everledger.ifaces.transfer.IfaceTransferManager;
import org.interledger.everledger.impl.SimpleTransfer;
import org.interledger.everledger.ledger.transfer.Credit;
import org.interledger.everledger.ledger.transfer.DTTM;
import org.interledger.everledger.ledger.transfer.Debit;
import org.interledger.everledger.ledger.transfer.ILPSpecTransferID;
import org.interledger.everledger.ledger.transfer.LocalTransferID;
import org.interledger.everledger.util.ILPExceptionSupport;
import org.interledger.ilp.ledger.model.TransferStatus;
//import org.javamoney.moneta.Money;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
/**
 * Simple in-memory {@code SimpleLedgerTransferManager}.
 *
 * @author earizon
 * 
 * FIXME:
 *  All the @Override methods will be transactional in a real database 
 *  JEE / Hibernate / ... . Mark them "somehow"
 *  REF:
 *    - http://docs.oracle.com/cd/E23095_01/Platform.93/ATGProgGuide/html/s1205transactiondemarcation01.html
 *    - http://docs.oracle.com/javaee/6/tutorial/doc/bncij.html
 *    - ...
 */
public class SimpleLedgerTransferManager implements IfaceTransferManager {

    private static final Logger log = LoggerFactory.getLogger(SimpleLedgerTransferManager.class);

    private Map<LocalTransferID, IfaceTransfer> transferMap = 
        new HashMap<LocalTransferID, IfaceTransfer>();// In-memory database of pending/executed/cancelled transfers

    private static SimpleLedgerTransferManager singleton = new SimpleLedgerTransferManager();

    private static final SimpleLedgerAccountManager accountManager = LedgerAccountManagerFactory.getLedgerAccountManagerSingleton();

    private static final IfaceLocalAccount HOLDS_URI = accountManager.getHOLDAccountILP();

    // Make default constructor private to avoid instantiating new classes.
    private SimpleLedgerTransferManager() {}

    public void developerTestingResetTransfers() { // TODO:(?) Make static?
        if (! org.interledger.everledger.Config.unitTestsActive) {
            throw new RuntimeException("developer.unitTestsActive must be true @ application.conf "
                    + "to be able to reset tests");
        }
        transferMap = new HashMap<LocalTransferID, IfaceTransfer>();
    }

    public static IfaceTransferManager getTransferManager() {
        // TODO:(0) Move function to factory
        //      similar to LedgerAccountManagerFactory
        return (IfaceTransferManager) singleton;
    }

//    public static IfaceLocalTransferManager _getLocalTransferManager() {
//        // TODO:(0) Move function to factory
//        //      similar to LedgerAccountManagerFactory
//        return (IfaceLocalTransferManager) singleton;
//    }
//
//    public static IfaceILPSpecTransferManager _getILPSpecTransferManager() {
//        // TODO:(0) Move function to factory
//        //      similar to LedgerAccountManagerFactory
//        return (IfaceILPSpecTransferManager) singleton;
//    }

    // START IfaceLocalTransferManager implementation {
    @Override
    public IfaceTransfer getTransferById(LocalTransferID transferId) {
        IfaceTransfer result = transferMap.get(transferId);
        if (result == null) {
            throw ILPExceptionSupport.createILPNotFoundException("transfer '"+transferId.transferID+"' not found");
        }
        if (result.getTransferStatus() == TransferStatus.REJECTED) {
            throw ILPExceptionSupport.createILPInternalException(
                    this.getClass().getName() + "This transfer has already been rejected");
        }
    
        return result;
    }

    @Override
    public void executeLocalTransfer(IfaceTransfer transfer) {
        // AccountUri sender, AccountUri recipient, MonetaryAmount amount)
        transfer.checkBalancedTransaction();
        Debit[] debit_list = transfer.getDebits();
        if (debit_list.length > 1) {
            // STEP 1: Pass all debits to first account.
            for (int idx=1; idx < debit_list.length ; idx++) {<commit_msg>Fix after testing for PUT .../fulfillment<commit_after>package org.interledger.everledger.impl.manager;

import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;

import javax.money.MonetaryAmount;

import org.interledger.cryptoconditions.Condition;
import org.interledger.cryptoconditions.Fulfillment;
import org.interledger.everledger.LedgerAccountManagerFactory;
import org.interledger.everledger.ifaces.account.IfaceLocalAccount;
import org.interledger.everledger.ifaces.transfer.IfaceTransfer;
import org.interledger.everledger.ifaces.transfer.IfaceTransferManager;
import org.interledger.everledger.impl.SimpleTransfer;
import org.interledger.everledger.ledger.transfer.Credit;
import org.interledger.everledger.ledger.transfer.DTTM;
import org.interledger.everledger.ledger.transfer.Debit;
import org.interledger.everledger.ledger.transfer.ILPSpecTransferID;
import org.interledger.everledger.ledger.transfer.LocalTransferID;
import org.interledger.everledger.util.ILPExceptionSupport;
import org.interledger.ilp.ledger.model.TransferStatus;
//import org.javamoney.moneta.Money;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
/**
 * Simple in-memory {@code SimpleLedgerTransferManager}.
 *
 * @author earizon
 * 
 * FIXME:
 *  All the @Override methods will be transactional in a real database 
 *  JEE / Hibernate / ... . Mark them "somehow"
 *  REF:
 *    - http://docs.oracle.com/cd/E23095_01/Platform.93/ATGProgGuide/html/s1205transactiondemarcation01.html
 *    - http://docs.oracle.com/javaee/6/tutorial/doc/bncij.html
 *    - ...
 */
public class SimpleLedgerTransferManager implements IfaceTransferManager {

    private static final Logger log = LoggerFactory.getLogger(SimpleLedgerTransferManager.class);

    private Map<LocalTransferID, IfaceTransfer> transferMap = 
        new HashMap<LocalTransferID, IfaceTransfer>();// In-memory database of pending/executed/cancelled transfers

    private static SimpleLedgerTransferManager singleton = new SimpleLedgerTransferManager();

    private static final SimpleLedgerAccountManager accountManager = LedgerAccountManagerFactory.getLedgerAccountManagerSingleton();

    private static final IfaceLocalAccount HOLDS_URI = accountManager.getHOLDAccountILP();

    // Make default constructor private to avoid instantiating new classes.
    private SimpleLedgerTransferManager() {}

    public void developerTestingResetTransfers() { // TODO:(?) Make static?
        if (! org.interledger.everledger.Config.unitTestsActive) {
            throw new RuntimeException("developer.unitTestsActive must be true @ application.conf "
                    + "to be able to reset tests");
        }
        transferMap = new HashMap<LocalTransferID, IfaceTransfer>();
    }

    public static IfaceTransferManager getTransferManager() {
        // TODO:(0) Move function to factory
        //      similar to LedgerAccountManagerFactory
        return (IfaceTransferManager) singleton;
    }

//    public static IfaceLocalTransferManager _getLocalTransferManager() {
//        // TODO:(0) Move function to factory
//        //      similar to LedgerAccountManagerFactory
//        return (IfaceLocalTransferManager) singleton;
//    }
//
//    public static IfaceILPSpecTransferManager _getILPSpecTransferManager() {
//        // TODO:(0) Move function to factory
//        //      similar to LedgerAccountManagerFactory
//        return (IfaceILPSpecTransferManager) singleton;
//    }

    // START IfaceLocalTransferManager implementation {
    @Override
    public IfaceTransfer getTransferById(LocalTransferID transferId) {
        IfaceTransfer result = transferMap.get(transferId);
        if (result == null) {
            throw ILPExceptionSupport.createILPNotFoundException("transfer '"+transferId.transferID+"' not found");
        }
        if (result.getTransferStatus() == TransferStatus.REJECTED) {
            throw ILPExceptionSupport.createILPUnprocessableEntityException(
                    "This transfer has already been rejected");
        }
    
        return result;
    }

    @Override
    public void executeLocalTransfer(IfaceTransfer transfer) {
        // AccountUri sender, AccountUri recipient, MonetaryAmount amount)
        transfer.checkBalancedTransaction();
        Debit[] debit_list = transfer.getDebits();
        if (debit_list.length > 1) {
            // STEP 1: Pass all debits to first account.
            for (int idx=1; idx < debit_list.length ; idx++) {
{'fraction_non_alphanumeric': 0.09254189026393604, 'fraction_numerical': 0.005694885554703756, 'mean_word_length': 3.697530864197531, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2774136', 'n_tokens_mistral': 2623, 'n_tokens_neox': 2525, 'n_words': 530}