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

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.eclipse.jnosql.communication.mongodb.document.MongoDBDocumentConfiguration;

import jakarta.nosql.Settings;
import jakarta.nosql.TypeReference;
import jakarta.nosql.document.DocumentCollectionManager;
import jakarta.nosql.document.DocumentCollectionManagerFactory;
import jakarta.nosql.document.DocumentConfiguration;
import jakarta.nosql.document.DocumentEntity;
import jakarta.nosql.document.DocumentQuery;

public class Test2 {
    private static void dump(DocumentCollectionManager dcm, DocumentQuery dq) {
        System.out.println("dumping " + dq);
        for(DocumentEntity de : dcm.select(dq).collect(Collectors.toList())) {
        	Order o = new Order();
        	Util.autoMapTo(de, o, new TypeReference<List<OrderLine>>() { });
        	System.out.printf("Id       : %d\n", o.getOrderId());
            System.out.printf("Customer : %s\n", o.getCustomer());
            System.out.printf("Price    : %s\n", o.getPrice());
            for(OrderLine ol : o.getItems()) {
                System.out.printf("  %2d %-30s %10s %4d %10s\n", ol.getItemNo(), ol.getItemName(), ol.getItemPrice(), ol.getQuantity(), ol.getPrice());
            }
        }
    }
    public static void main(String[] args) {
        // open
    	DocumentConfiguration cfg = new MongoDBDocumentConfiguration();
        Map<String, Object> props = new HashMap<>();
        props.put("mongodb.host", "localhost:27017");
    	DocumentCollectionManagerFactory dcmf = cfg.get(Settings.of(props));
    	DocumentCollectionManager dcm = dcmf.get("java_order");
        // setup
        Order o1 = new Order(1, "A A");
        o1.getItems().add(new OrderLine(1, "A good Java book", new BigDecimal("19.95"), 1));
        DocumentEntity de1 = DocumentEntity.of("java_order");
        Util.autoMapFrom(o1, de1);
        dcm.insert(de1);
        Order o2 = new Order(2, "B B");
        o2.getItems().add(new OrderLine(1, "ASUS MB", new BigDecimal("249.00"), 1));
        o2.getItems().add(new OrderLine(2, "i5 CPU", new BigDecimal("299.00"), 1));
        o2.getItems().add(new OrderLine(3, "4 GB kit", new BigDecimal("29.00"), 4));
        DocumentEntity de2 = DocumentEntity.of("java_order");
        Util.autoMapFrom(o2, de2);
        dcm.insert(de2);
        // query
        DocumentQuery dq;
        dq = DocumentQuery.select().from("java_order").build(); // all
        dump(dcm, dq);
        dq = DocumentQuery.select().from("java_order").where("orderId").eq(2).build(); // where id=2
        dump(dcm, dq);
        dq = DocumentQuery.select().from("java_order").where("customer").eq("A A").build(); // where customer='A A'
        dump(dcm, dq);
        dq = DocumentQuery.select().from("java_order").where("customer").like("A").build(); // where customer like 'A%'
        dump(dcm, dq);
        // where items.name='i5 CPU'
        // NOT SUPPORTED
        // where items.name like '%Java%'
        // NOT SUPPORTED
        List<Integer> lst = new ArrayList<>();
        lst.add(1);
        lst.add(2);
        dq = DocumentQuery.select().from("java_order").where("orderId").in(lst).build();
        dump(dcm, dq);
        // close
    	dcm.close();
    	dcmf.close();
    }
}
