Java tutorial
/* * 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 Presentation.Bean; import BusinessLogic.Controller.MenuController; import DataAccess.Entity.Menu; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.MongoClient; import java.io.Serializable; import java.util.ArrayList; import java.util.Collection; import java.util.List; import javax.faces.bean.ManagedBean; import javax.faces.bean.ViewScoped; @ManagedBean(name = "menuBean") @ViewScoped /** * * @author Ace */ public class MenuBean implements Serializable { public String menuName; private String entrada; private String pFuerte; private String postre; private String bebida; private String otro; private Boolean activo; private Collection<Menu> menuCollection; private String message; private List<Menu> listMenu = new ArrayList<Menu>(); private static final ArrayList<Menu> menuList = new ArrayList<Menu>(); public ArrayList<Menu> getMenuList() { // To connect to mongo dbserver MongoClient mongoClient = new MongoClient("localhost", 27017); // Now connect to your databases DB db = mongoClient.getDB("Restaurant"); System.out.println("Connect to database successfully"); DBCollection collection = db.getCollection("Menus"); System.out.println("Collection menu selected successfully"); // PASO 4.1: "CREATE" -> Metemos los objetos men (o documentos en Mongo) en la coleccion Menu for (Menu men : menuList) { collection.insert(men.toDBObjectMenu()); } // PASO 4.2.1: "READ" -> Leemos todos los documentos de la base de datos int numDocumentos = (int) collection.getCount(); System.out.println("Nmero de documentos en la coleccin Menu: " + numDocumentos + "\n"); // Busco todos los documentos de la coleccin y los imprimo DBCursor cursor = collection.find(); try { while (cursor.hasNext()) { System.out.println(cursor.next().toString()); } } finally { cursor.close(); } return menuList; } public List<Menu> getListMenu() { return listMenu; } public Collection<Menu> getMenuCollection() { return menuCollection; } public String getMenuName() { return menuName; } public void setMenuName(String menuName) { this.menuName = menuName; } public String getEntrada() { return entrada; } public void setEntrada(String entrada) { this.entrada = entrada; } public String getPFuerte() { return pFuerte; } public void setPFuerte(String pFuerte) { this.pFuerte = pFuerte; } public String getPostre() { return postre; } public void setPostre(String postre) { this.postre = postre; } public String getBebida() { return bebida; } public void setBebida(String bebida) { this.bebida = bebida; } public String getOtro() { return otro; } public void setOtro(String otro) { this.otro = otro; } public Boolean getActivo() { return activo; } public void setActivo(Boolean activo) { this.activo = activo; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public void createMenu() { MenuController menuController = new MenuController(); message = menuController.createMenu(menuName, entrada, pFuerte, postre, bebida, otro, activo); } public void consultMenu() { MenuController menuController = new MenuController(); Menu consult; consult = menuController.consultMenu(menuName); message = "No se encuentra en la registrado"; if (consult.getEntrada() != null) { entrada = consult.getEntrada(); pFuerte = consult.getpFuerte(); postre = consult.getPostre(); bebida = consult.getBebida(); otro = consult.getOtro(); activo = consult.isActivo(); message = "Consultado Con Exito"; } } public ArrayList listMenu() { //boolean activo = true; MenuController menuController = new MenuController(); //ArrayList consult; //consult = menuController.consultMenuList(); return menuController.consultMenuList(); //menuName = consult.get(0).toString(); //// entrada = consult.set(1, listMenu).toString(); //// pFuerte = consult.set(2, listMenu).toString(); // postre = consult.get(2).toString(); // bebida = consult.get(3).toString(); // otro = consult.get(4).toString(); // activo = Boolean.parseBoolean(consult.get(5).toString()); } public void updateMenu() { MenuController menuController = new MenuController(); message = menuController.updateMenu(menuName, entrada, pFuerte, postre, bebida, otro, activo); } public void deleteMenu() { MenuController menuController = new MenuController(); message = menuController.deleteMenu(menuName); } }