// Collection of code snippets by Arne Vajhøj 
// (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
import java.math.BigDecimal;

import dk.vajhoej.isam.IsamException;
import dk.vajhoej.isam.IsamResult;
import dk.vajhoej.isam.IsamSource;
import dk.vajhoej.isam.Key;
import dk.vajhoej.isam.local.LocalIsamSource;
import dk.vajhoej.record.RecordException;

public class List {
    public static void main(String[] args) throws IsamException, RecordException {
        IsamSource cisam = new LocalIsamSource("customer.isq", "dk.vajhoej.vms.rms.IndexSequential", false);
        IsamSource oisam = new LocalIsamSource("order.isq", "dk.vajhoej.vms.rms.IndexSequential", false);
        IsamResult<Customer> crs = cisam.readStart(Customer.class);
        while(crs.read()) {
            Customer c = crs.current();
            if(c instanceof CustomerPotential) {
                CustomerPotential cp = (CustomerPotential)c;
                System.out.printf("**future customer** %s, %s, %s\n", cp.getName(), cp.getPhone(), cp.getSource());
            }
            if(c instanceof CustomerActual) {
                CustomerActual ca = (CustomerActual)c;
                System.out.printf("%d, %s, %s, %s, %s\n", ca.getId(), ca.getName(), ca.getAddress1(), ca.getAddress2(), ca.getPhone());
                IsamResult<Order> ors = oisam.readGE(Order.class, new Key<Integer>(1, ca.getId()));
                while(ors.read()) {
                    Order o = ors.current();
                    if(o.getCustomer() > ca.getId()) break;
                    BigDecimal sum = new BigDecimal(0);
                    for(int i = 0; i < o.getNlines(); i++) {
                        sum = sum.add(o.getLine()[i].getPrice().multiply(new BigDecimal(o.getLine()[i].getQuantity())));
                    }
                    System.out.printf("  %d, %s, %.2f\n", o.getId(), o.getStatus(), sum);
                }
            }
        }
        cisam.close();
        oisam.close();
    }
}
