BusinessLogic.Service.RestaurantService.java Source code

Java tutorial

Introduction

Here is the source code for BusinessLogic.Service.RestaurantService.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 BusinessLogic.Service;

import DataAccess.DAO.MongoConnection;
import DataAccess.Entity.Restaurant;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoException;
import java.util.ArrayList;
import java.util.Set;

/**
 *
 * @author ace
 */
public class RestaurantService {

    String collName = "Restaurants";

    public String getAll() {
        String response = "";
        try {

            ArrayList userConsult = new ArrayList();
            MongoConnection dbSingleton = MongoConnection.getInstance();

            DB db = dbSingleton.getTestdb();

            // get list of collections
            Set<String> collections = db.getCollectionNames();

            // get a single collection
            DBCollection collection = db.getCollection(collName);
            DBCursor cursor = collection.find();
            try {
                while (cursor.hasNext()) {
                    response = response + cursor.next().toString() + "\n";
                }
            } finally {
                cursor.close();
            }

            System.out.println("Done");

        } catch (MongoException e) {
            e.printStackTrace();
        }
        return response;
    }

    public Restaurant getBy(String name) {
        ArrayList restaurantConsult = new ArrayList();
        MongoConnection dbSingleton = MongoConnection.getInstance();

        DB db = dbSingleton.getTestdb();
        DBCollection coll = db.getCollection(collName);
        BasicDBObject whereQuery = new BasicDBObject();
        // Sentence to search one account number
        whereQuery.put("name", name);

        DBCursor cursor = coll.find(whereQuery);
        while (cursor.hasNext()) {
            DBObject consultDocument = cursor.next();
            restaurantConsult.add(consultDocument.get("name"));
            restaurantConsult.add(consultDocument.get("direccion"));
            restaurantConsult.add(consultDocument.get("phone"));

        }
        Restaurant rs = new Restaurant(whereQuery);
        String response = "";
        response = "Name:" + restaurantConsult.get(0) + " ,Address: " + restaurantConsult.get(1) + " ,Phone: "
                + restaurantConsult.get(2);
        System.out.println(rs);
        return rs;

    }

    public String addRestaurant(Restaurant rs) {
        //String name, String direccion, String phone) 
        String name = rs.getName();
        String direccion = rs.getAddress();
        String phone = rs.getPhone();
        String restaurantCreate = null;

        try {
            // To connect to mongo dbserver
            MongoConnection dbSingleton = MongoConnection.getInstance();
            DB db = dbSingleton.getTestdb();
            DBCollection coll = db.getCollection(collName);

            System.out.println("Collection restaurants selected successfully");

            BasicDBObject doc = new BasicDBObject("name", name).append("direccion", direccion).append("phone",
                    phone);
            coll.insert(doc);
            System.out.println("Document inserted successfully");
            restaurantCreate = "Success";

        } catch (Exception e) {
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
        }

        if (restaurantCreate != null) {
            return "The restaurant has been created successfully!";
        } else {
            return "The restaurant has not been created!";
        }

    }

    public String update(
            //String name, String direccion, String phone) {
            Restaurant rs) {
        String name = rs.getName();
        String direccion = rs.getAddress();
        String phone = rs.getPhone();
        String reservationUpdate = null;
        try {
            // To connect to mongo dbserver
            MongoConnection dbSingleton = MongoConnection.getInstance();
            DB db = dbSingleton.getTestdb();
            DBCollection coll = db.getCollection(collName);

            System.out.println("Collection restaurants selected successfully");

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

            DBCursor cursor = coll.find(whereQuery);

            while (cursor.hasNext()) {

                DBObject updateDocument = cursor.next();
                updateDocument.put("name", name);
                updateDocument.put("direccion", direccion);
                updateDocument.put("phone", phone);

                coll.update(whereQuery, updateDocument);

            }
            System.out.println("Document updated successfully");
            reservationUpdate = "Success";

        } catch (Exception e) {
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
        }
        if (reservationUpdate != null) {
            return "The restaurant has been updated successfully!";
        } else {
            return "The restaurant has not been updated!";
        }
    }

    public String delete(String name) {
        String deleteReservation = null;
        try {
            // To connect to mongo dbserver
            MongoConnection dbSingleton = MongoConnection.getInstance();
            DB db = dbSingleton.getTestdb();
            DBCollection coll = db.getCollection(collName);

            System.out.println("Collection restaurants selected successfully");

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

            DBCursor cursor = coll.find(whereQuery);

            coll.remove(whereQuery);
            System.out.println("Document deleted successfully");
            deleteReservation = "Success";

        } catch (Exception e) {
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
        }
        if (deleteReservation != null) {
            return "The restaurant has been deleted successfully!";
        } else {
            return "The restaurant has not been deleted!";
        }

    }

    public String addRestaurant(String name, String direccion, String phone) {
        String restaurantCreate = null;

        try {
            // To connect to mongo dbserver
            MongoConnection dbSingleton = MongoConnection.getInstance();
            DB db = dbSingleton.getTestdb();
            DBCollection coll = db.getCollection(collName);

            System.out.println("Collection restaurants selected successfully");

            BasicDBObject doc = new BasicDBObject("name", name).append("direccion", direccion).append("phone",
                    phone);
            coll.insert(doc);
            System.out.println("Document inserted successfully");
            restaurantCreate = "Success";

        } catch (Exception e) {
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
        }

        if (restaurantCreate != null) {
            return "The restaurant has been created successfully!";
        } else {
            return "The restaurant has not been created!";
        }
    }

}