com.data.DAO.MetodoPagoDAO.java Source code

Java tutorial

Introduction

Here is the source code for com.data.DAO.MetodoPagoDAO.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 com.data.DAO;

import com.mongodb.MongoClient;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.DBCursor;
import java.util.ArrayList;

/**
 *
 * @author drdagerm
 */
public class MetodoPagoDAO {

    public static String crearRecibo(Integer idMetodoPago, String descripcion, Integer cuotas, float precio,
            Integer idReserva) {
        String reciboCreado = null;
        try {
            // To connect to mongo dbserver
            MongoClient mongoClient = new MongoClient("localhost", 27017);

            // Now connect to your databases
            DB db = mongoClient.getDB("hoex");
            System.out.println("Connect to database successfully");

            DBCollection coll = db.getCollection("Metodo_pago");
            System.out.println("Collection Metodo_pago selected successfully");

            BasicDBObject doc = new BasicDBObject("idMetodoPago", idMetodoPago).append("descripcion", descripcion)
                    .append("cuotas", cuotas).append("precio", precio).append("idReserva", idReserva);

            coll.insert(doc);
            System.out.println("Document inserted successfully");
            reciboCreado = "Success";
        } catch (Exception e) {
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
        }

        if (reciboCreado != null) {
            return "El recibo fue creado satisfactoriamente!";
        } else {
            return "El recibo no ha sido creado!";
        }
    }

    public static ArrayList consultarRecibo(Integer idMetodoPago) {
        ArrayList reciboConsulta = new ArrayList();
        try {
            // To connect to mongo dbserver
            MongoClient mongoClient = new MongoClient("localhost", 27017);

            // Now connect to your databases
            DB db = mongoClient.getDB("hoex");
            System.out.println("Connect to database successfully");

            // Use an especific collection
            DBCollection coll = db.getCollection("Metodo_pago");
            System.out.println("Collection Metodo_pago selected successfully");

            BasicDBObject whereQuery = new BasicDBObject();
            // Sentence to search one account number
            whereQuery.put("idMetodoPago", idMetodoPago);

            DBCursor cursor = coll.find(whereQuery);

            while (cursor.hasNext()) {
                DBObject consultDocument = cursor.next();
                reciboConsulta.add(consultDocument.get("descripcion"));
                reciboConsulta.add(consultDocument.get("cuotas"));
                reciboConsulta.add(consultDocument.get("precio"));
                reciboConsulta.add(consultDocument.get("idReserva"));
            }
            System.out.println("Document consulted successfully");
        } catch (Exception e) {
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
        }
        if (!reciboConsulta.isEmpty()) {
            return reciboConsulta;
        } else {
            return null;
        }
    }

    public static String actualizarRecibo(Integer idMetodoPago, String descripcion, Integer cuotas, float precio,
            Integer idReserva) {
        String reciboActualizar = null;
        try {
            // To connect to mongo dbserver
            MongoClient mongoClient = new MongoClient("localhost", 27017);

            // Now connect to your databases
            DB db = mongoClient.getDB("hoex");
            System.out.println("Connect to database successfully");

            DBCollection coll = db.getCollection("Metodo_pago");
            System.out.println("Collection Metodo_pago selected successfully");

            BasicDBObject whereQuery = new BasicDBObject();
            whereQuery.put("idMetodoPago", idMetodoPago);

            DBCursor cursor = coll.find(whereQuery);

            while (cursor.hasNext()) {
                DBObject updateDocument = cursor.next();
                updateDocument.put("descripcion", descripcion);
                updateDocument.put("cuotas", cuotas);
                updateDocument.put("precio", precio);
                updateDocument.put("idReserva", idReserva);
                coll.update(whereQuery, updateDocument);
            }
            System.out.println("Document updated successfully");
            reciboActualizar = "Success";
        } catch (Exception e) {
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
        }
        if (reciboActualizar != null) {
            return "El recibo se ha actualizado correctamente!";
        } else {
            return "El recibo no se ha actualizado!";
        }
    }

    public static String borrarRecibo(Integer idMetodoPago) {
        String reciboBorrar = null;
        try {
            // To connect to mongo dbserver
            MongoClient mongoClient = new MongoClient("localhost", 27017);

            // Now connect to your databases
            DB db = mongoClient.getDB("hoex");
            System.out.println("Connect to database successfully");

            DBCollection coll = db.getCollection("Metodo_pago");
            System.out.println("Collection Metodo_pago selected successfully");

            BasicDBObject whereQuery = new BasicDBObject();
            whereQuery.put("idMetodoPago", idMetodoPago);

            DBCursor cursor = coll.find(whereQuery);

            coll.remove(whereQuery);
            System.out.println("Documento elminado exitosamente");
            reciboBorrar = "Success";
        } catch (Exception e) {
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
        }

        if (reciboBorrar != null) {
            return "El recibo ha sido borrado exitosamente!";
        } else {
            return "El recibo no ha sido borrado!";
        }
    }
}