spntoolsdata.crud.servispnCrud.java Source code

Java tutorial

Introduction

Here is the source code for spntoolsdata.crud.servispnCrud.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package spntoolsdata.crud;

import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import java.util.ArrayList;
import java.util.Iterator;
import spntoolsdata.map.Artefacto;
import spntoolsdata.map.Canton;
import spntoolsdata.map.Cliente;
import spntoolsdata.map.Historial;
import spntoolsdata.map.Marca;
import spntoolsdata.map.Orden;
import spntoolsdata.map.Provincia;
import spntoolsdata.map.Repuesto;
import spntoolsdata.map.Usuario;

/**
 *
 * @author Ivansito
 */
public class servispnCrud {

    public void insertCliente(MongoClient mongo, Cliente us) {
        DB db = mongo.getDB("dbservispntools");
        DBCollection DBCliente = db.getCollection("Clientes");
        DBCliente.insert(us.getDBObjectCliente());
    }

    public void insertUsuario(MongoClient mongo, Usuario us) {
        DB db = mongo.getDB("dbservispntools");
        DBCollection DBCliente = db.getCollection("Usuarios");
        DBCliente.insert(us.getDBObjectUsuario());
    }

    public void insertProvincia(MongoClient mongo, Provincia prv) {
        DB db = mongo.getDB("dbservispntools");
        DBCollection DBCliente = db.getCollection("Provincias");
        DBCliente.insert(prv.getDBObjectProvincia());
    }

    public void insertArtefacto(MongoClient mongo, Artefacto artf) {
        DB db = mongo.getDB("dbservispntools");
        DBCollection DBCliente = db.getCollection("Artefactos");
        DBCliente.insert(artf.getDBObjectArtefacto());
    }

    public void insertMarca(MongoClient mongo, Marca mra) {
        DB db = mongo.getDB("dbservispntools");
        DBCollection DBCliente = db.getCollection("Marcas");
        DBCliente.insert(mra.getDBObjectMarca());
    }

    public void addOrdenClient(MongoClient mongo, Cliente us, Orden or) {
        DB db = mongo.getDB("dbservispntools");
        DBCollection DBCliente = db.getCollection("Clientes");
        DBObject query = new BasicDBObject();
        query.put("Codigo", us.getCodigo());
        DBObject update = new BasicDBObject();
        update.put("$push", new BasicDBObject("Ordenes", or.getDBObjectOrden()));
        DBCliente.update(query, update);

    }

    public void addCantonsProv(MongoClient mongo, Provincia prv, Canton cnt) {
        DB db = mongo.getDB("dbservispntools");
        DBCollection DBCliente = db.getCollection("Provincias");
        DBObject query = new BasicDBObject();
        query.put("Provincia", prv.getProvincia());
        DBObject update = new BasicDBObject();
        update.put("$push", new BasicDBObject("Cantones", cnt.getDBObjectCanton()));
        DBCliente.update(query, update);

    }

    public void addHistoOrdenClient(MongoClient mongo, Cliente us, Orden or, Historial hi) {
        DB db = mongo.getDB("dbservispntools");
        DBCollection DBCliente = db.getCollection("Clientes");
        DBObject query = new BasicDBObject();
        query.put("Codigo", us.getCodigo());
        query.put("Ordenes.CodigoOrden", or.getCodigoOrden());
        DBObject update = new BasicDBObject();
        update.put("$push", new BasicDBObject("Ordenes.$.Historial", hi.getDBObjectHistorial()));
        DBCliente.update(query, update);

    }

    public void addRepotoOrdenClient(MongoClient mongo, Cliente us, Orden or, Repuesto ri) {
        DB db = mongo.getDB("dbservispntools");
        DBCollection DBCliente = db.getCollection("Clientes");
        DBObject query = new BasicDBObject();
        query.put("Codigo", us.getCodigo());
        query.put("Ordenes.CodigoOrden", or.getCodigoOrden());
        DBObject update = new BasicDBObject();
        update.put("$push", new BasicDBObject("Ordenes.$.Repuestos", ri.getDBObjectRepuesto()));
        DBCliente.update(query, update);

    }

    public void updateClient(MongoClient mongo, Cliente antiguo, Cliente actualizado) {
        DB db = mongo.getDB("dbservispntools");
        DBCollection DBCliente = db.getCollection("Clientes");
        DBObject query = antiguo.getDBObjectCliente();
        DBObject update = new BasicDBObject();
        update.put("$set", actualizado.getDBObjectCliente());
        DBCliente.updateMulti(query, update);

    }

    public Cliente findCliente(MongoClient mongo, Cliente us) {
        DB db = mongo.getDB("dbservispntools");
        DBCollection DBUsuario = db.getCollection("Clientes");
        DBCursor find = DBUsuario.find(us.getDBObjectCliente());
        DBObject usr = null;
        Cliente usrf = new Cliente();
        while (find.hasNext()) {
            usr = find.next();
            usrf.makePojofromBson(usr);
        }
        usrf.setOrdenes(getOrdens(usrf.getDbOrdenes()));
        usrf.setOrdenes(getOrdersDocs(usrf.getOrdenes()));
        return usrf;
    }

    public Provincia findProvincia(MongoClient mongo, Provincia us) {
        DB db = mongo.getDB("dbservispntools");
        DBCollection DBUsuario = db.getCollection("Provincias");
        DBCursor find = DBUsuario.find(us.getDBObjectProvincia());
        DBObject usr = null;
        Provincia usrf = new Provincia();
        while (find.hasNext()) {
            usr = find.next();
            usrf.makePojofromBson(usr);
        }
        usrf.setCantones(getCantons(usrf.getDbCantones()));
        return usrf;
    }

    public ArrayList<Cliente> findClientes(MongoClient mongo, Cliente us) {
        DB db = mongo.getDB("dbservispntools");
        DBCollection DBUsuario = db.getCollection("Clientes");
        DBCursor find = DBUsuario.find(us.getDBObjectCliente());
        ArrayList<DBObject> ListObject = new ArrayList<>();
        DBObject usr = null;
        while (find.hasNext()) {
            usr = find.next();
            ListObject.add(usr);
        }
        ArrayList<Cliente> usrf = listMakePojoClients(ListObject);
        return usrf;
    }

    public ArrayList<Provincia> findProvincias(MongoClient mongo) {
        DB db = mongo.getDB("dbservispntools");
        DBCollection DBUsuario = db.getCollection("Provincias");
        DBCursor find = DBUsuario.find();
        ArrayList<DBObject> ListObject = new ArrayList<>();
        DBObject usr = null;
        while (find.hasNext()) {
            usr = find.next();
            ListObject.add(usr);
        }
        ArrayList<Provincia> usrf = listMakePojoProvincias(ListObject);
        return usrf;
    }

    public ArrayList<Artefacto> findArtefactos(MongoClient mongo) {
        DB db = mongo.getDB("dbservispntools");
        DBCollection DBUsuario = db.getCollection("Artefactos");
        DBCursor find = DBUsuario.find();
        ArrayList<DBObject> ListObject = new ArrayList<>();
        DBObject usr = null;
        while (find.hasNext()) {
            usr = find.next();
            ListObject.add(usr);
        }
        ArrayList<Artefacto> usrf = listMakePojoArtefactos(ListObject);
        return usrf;
    }

    public ArrayList<Marca> findMarcas(MongoClient mongo) {
        DB db = mongo.getDB("dbservispntools");
        DBCollection DBUsuario = db.getCollection("Marcas");
        DBCursor find = DBUsuario.find();
        ArrayList<DBObject> ListObject = new ArrayList<>();
        DBObject usr = null;
        while (find.hasNext()) {
            usr = find.next();
            ListObject.add(usr);
        }
        ArrayList<Marca> usrf = listMakePojoMarcas(ListObject);
        return usrf;
    }

    public ArrayList<Cliente> filterClients(MongoClient mongo, String filterProperty, String filterValue) {
        DB db = mongo.getDB("dbservispntools");
        DBCollection DBUsuario = db.getCollection("Clientes");
        BasicDBObject query = new BasicDBObject();
        query.put(filterProperty, filterValue);
        DBCursor find = DBUsuario.find(query);
        ArrayList<DBObject> ListObject = new ArrayList<>();
        DBObject usr = null;
        while (find.hasNext()) {
            usr = find.next();
            ListObject.add(usr);
        }
        ArrayList<Cliente> usrf = listMakePojoClients(ListObject);
        return usrf;
    }

    public int getCountClients(MongoClient mongo) {
        DB db = mongo.getDB("dbservispntools");
        DBCollection DBUsuario = db.getCollection("Clientes");
        int count = DBUsuario.find().count();
        return count;
    }

    public ArrayList<Cliente> getLimitClinets(MongoClient mongo, int vf) {
        DB db = mongo.getDB("dbservispntools");
        DBCollection DBUsuario = db.getCollection("Clientes");
        DBCursor find = DBUsuario.find().limit(vf);
        ArrayList<DBObject> ListObject = new ArrayList<>();
        DBObject usr = null;
        while (find.hasNext()) {
            usr = find.next();
            ListObject.add(usr);
        }
        ArrayList<Cliente> usrf = listMakePojoClients(ListObject);

        return usrf;
    }

    public ArrayList<Cliente> getLazyClinets(MongoClient mongo, int vo, int vf) {
        DB db = mongo.getDB("dbservispntools");
        DBCollection DBUsuario = db.getCollection("Clientes");
        DBCursor ao = DBUsuario.find().limit(vo);
        DBCursor af = DBUsuario.find().limit(vf);
        ArrayList<Cliente> listVo = listMakePojoClients(getListClientDBObject(ao));
        ArrayList<Cliente> listVf = listMakePojoClients(getListClientDBObject(af));
        return removeLazy(listVo, listVf);
    }

    public ArrayList<DBObject> getListClientDBObject(DBCursor find) {
        ArrayList<DBObject> list = new ArrayList<>();
        DBObject cli = null;
        while (find.hasNext()) {
            cli = find.next();
            list.add(cli);
        }
        return list;
    }

    public ArrayList<Cliente> removeLazy(ArrayList<Cliente> listVo, ArrayList<Cliente> listVf) {
        for (int i = 0; i < listVf.size(); i++) {
            for (int j = 0; j < listVf.size(); j++) {
                for (int k = 0; k < listVo.size(); k++) {
                    if (listVf.get(j).getTelefono().equals(listVo.get(k).getTelefono())) {
                        System.out
                                .println("->" + listVf.get(j).getApellidos() + "->" + listVf.get(j).getTelefono());
                        listVf.remove(j);
                    }
                }
            }
        }
        return listVf;
    }

    public ArrayList<Cliente> listMakePojoClients(ArrayList<DBObject> ListObject) {
        ArrayList<Cliente> listClient = new ArrayList<>();
        for (int i = 0; i < ListObject.size(); i++) {
            Cliente cliente = new Cliente();
            cliente.makePojofromBson(ListObject.get(i));
            cliente.setOrdenes(getOrdens(cliente.getDbOrdenes()));
            cliente.setOrdenes(getOrdersDocs(cliente.getOrdenes()));
            listClient.add(cliente);
        }
        return listClient;
    }

    public ArrayList<Provincia> listMakePojoProvincias(ArrayList<DBObject> ListObject) {
        ArrayList<Provincia> listCants = new ArrayList<>();
        for (int i = 0; i < ListObject.size(); i++) {
            Provincia provincia = new Provincia();
            provincia.makePojofromBson(ListObject.get(i));
            provincia.setCantones(getCantons(provincia.getDbCantones()));
            listCants.add(provincia);
        }
        return listCants;
    }

    public ArrayList<Artefacto> listMakePojoArtefactos(ArrayList<DBObject> ListObject) {
        ArrayList<Artefacto> listCants = new ArrayList<>();
        for (int i = 0; i < ListObject.size(); i++) {
            Artefacto artf = new Artefacto();
            artf.makePojofromBson(ListObject.get(i));
            listCants.add(artf);
        }
        return listCants;
    }

    public ArrayList<Marca> listMakePojoMarcas(ArrayList<DBObject> ListObject) {
        ArrayList<Marca> listCants = new ArrayList<>();
        for (int i = 0; i < ListObject.size(); i++) {
            Marca mrc = new Marca();
            mrc.makePojofromBson(ListObject.get(i));
            listCants.add(mrc);
        }
        return listCants;
    }

    public Usuario findUsuario(MongoClient mongo, Usuario us) {
        DB db = mongo.getDB("dbservispntools");
        DBCollection DBUsuario = db.getCollection("Usuarios");
        DBCursor find = DBUsuario.find(us.getDBObjectUsuario());
        DBObject usr = null;
        Usuario usrf = new Usuario();
        while (find.hasNext()) {
            usr = find.next();
            usrf.makePojofromBson(usr);
        }
        return usrf;
    }

    public ArrayList<Orden> getOrdersDocs(ArrayList<Orden> list) {
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setHistorial(getHisrotials(list.get(i).getDbsHistorial()));
            list.get(i).setRepuestos(getRepuestos(list.get(i).getDbsRepuesos()));
        }
        return list;
    }

    public ArrayList<Historial> getHisrotials(BasicDBList list) {
        ArrayList<Historial> hl = new ArrayList<>();
        BasicDBList HistCli = list;
        for (Iterator<Object> it = HistCli.iterator(); it.hasNext();) {
            BasicDBObject dbo = (BasicDBObject) it.next();
            Historial hi = new Historial();
            hi.makePojofromBson(dbo);
            hl.add(hi);
        }
        return hl;
    }

    public ArrayList<Orden> getOrdens(BasicDBList list) {
        ArrayList<Orden> ol = new ArrayList<>();
        BasicDBList HistCli = list;
        for (Iterator<Object> it = HistCli.iterator(); it.hasNext();) {
            BasicDBObject dbo = (BasicDBObject) it.next();
            Orden or = new Orden();
            or.maketPojofromBson(dbo);
            ol.add(or);
        }
        return ol;
    }

    public ArrayList<Canton> getCantons(BasicDBList list) {
        ArrayList<Canton> cl = new ArrayList<>();
        BasicDBList HistCli = list;
        for (Iterator<Object> it = HistCli.iterator(); it.hasNext();) {
            BasicDBObject dbo = (BasicDBObject) it.next();
            Canton cnt = new Canton();
            cnt.makePojofromBson(dbo);
            cl.add(cnt);
        }
        return cl;
    }

    public ArrayList<Repuesto> getRepuestos(BasicDBList list) {
        ArrayList<Repuesto> rl = new ArrayList<>();
        BasicDBList HistCli = list;
        for (Iterator<Object> it = HistCli.iterator(); it.hasNext();) {
            BasicDBObject dbo = (BasicDBObject) it.next();
            Repuesto or = new Repuesto();
            or.makePojofromBson(dbo);
            rl.add(or);
        }
        return rl;
    }

}