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

import java.math.BigDecimal;
import java.util.List;

import com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.OCommandSQL;
import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery;

public class TestX {
    private static Data deserialize(ODocument doc) {
        Data res = new Data();
        res.setId(doc.field("id"));
        res.setIv(doc.field("iv"));
        if(doc.field("xv") instanceof Double) {  // values seems to be able to end up as double, float and BigDecimal
        	res.setXv(doc.field("xv"));
        } else if(doc.field("xv") instanceof Float) {
        	res.setXv((float)doc.field("xv"));
        } else if(doc.field("xv") instanceof BigDecimal) {
        	res.setXv(((BigDecimal)doc.field("xv")).doubleValue());
        }
        res.setSv(doc.field("sv"));
        return res;
    }
    private static void dump(ODatabaseDocumentTx db, String id) {
        System.out.println("dumping " + id);
        List<ODocument> rs = db.query(new OSQLSynchQuery<ODocument>(String.format("SELECT FROM Data WHERE id = '%s'", id)));
        if(rs.size() > 0) {
        	ODocument d = rs.get(0);
        	Data o = deserialize(d);
            System.out.println(o);
        } else {
            System.out.println("Not found");
        }
    }
    private static final int NREC = 1000;
    private static final int JAVA_OFFSET = 1 * NREC;
    public static void main(String[] args) {
        // open
    	ODatabaseDocumentTx db = new ODatabaseDocumentTx("remote:192.168.60.129/test");
    	db.open("admin", "hemmeligt");
db.command(new OCommandSQL("DELETE FROM Data")).execute(); 
        // put data
        for(int i = 0; i < NREC; i++) {
            Data o = new Data("Doc-" + (JAVA_OFFSET + i + 1), i + 1, i + 1.0, String.format("This is value %d", i + 1));
            db.command(new OCommandSQL(String.format("INSERT INTO Data (id,iv,xv,sv) VALUES('%s',%d,%f,'%s')", o.getId(), o.getIv(), o.getXv(), o.getSv()))).execute();
        }
        //
        String id;
        ODocument d;
        id = "Doc-" + (JAVA_OFFSET + 77);
        // get
        dump(db, id);
        // delete
        d = (ODocument)db.query(new OSQLSynchQuery<ODocument>(String.format("SELECT FROM Data WHERE id = '%s'", id))).get(0);
        d.delete();
        // get non existing
        dump(db, id);
        // update and get
        id = "Doc-" + (JAVA_OFFSET + 88);
        dump(db, id);
        d = (ODocument)db.query(new OSQLSynchQuery<ODocument>(String.format("SELECT FROM Data WHERE id = '%s'", id))).get(0);
        Data o = deserialize(d);
        o.setIv(o.getIv() + 1);
        o.setXv(o.getXv() + 0.1);
        o.setSv(o.getSv() + " updated");
        db.command(new OCommandSQL(String.format("UPDATE Data SET iv = %d, xv = %f, sv = '%s' WHERE id = '%s'", o.getIv(), o.getXv(), o.getSv(), o.getId()))).execute();
        dump(db, id);
        // list all
        int n = 0;
        for(ODocument itd : db.browseClass("Data")) {
            Data ito = deserialize(itd);
            if(!ito.getId().startsWith("Doc-")) {
                System.out.println("Unexpected document id: " + ito.getId());
            }
            if(ito.getIv() < 1 || NREC < ito.getIv()) {
                System.out.println("Unexpected value :" + ito);
            }
            n++;
        }
        System.out.println(n);
        // list documents where "Doc#n075" <= id < "Doc#n085"
        int n2 = 0;
        List<ODocument> rs = db.query(new OSQLSynchQuery<ODocument>(String.format("SELECT FROM Data WHERE id BETWEEN '%s' AND '%s'", "Doc-" + (JAVA_OFFSET + 75), "Doc-" + (JAVA_OFFSET + 84))));
        for(ODocument it2d : rs) {
            Data it2o = deserialize(it2d);
            if(!it2o.getId().startsWith("Doc-")) {
                System.out.println("Unexpected document id: " + it2o.getId());
            }
            n2++;
        }
        System.out.println(n2);
        // close
        db.close();
    }
}
