package framework;

import com.mongodb.client.FindIterable;
import entities.Resource;
import org.bson.Document;
import com.mongodb.Block;
import java.util.ArrayList;
import static com.mongodb.client.model.Filters.regex;

/**
 * Created with IntelliJ IDEA.
 * User: jhasmanyquiroz
 * Date: 12/10/15
 * Time: 9:11 AM
 * To change this template use File | Settings | File Templates.
 */
public class DBQuery {
    private static DBQuery instance;

    protected DBQuery() {
    }

    public static DBQuery getInstance() {
        if (instance == null)
            instance = new DBQuery();

        return instance;
    }

    public String searchByValue(String collection, final String key, String value) {
        final String [] res = new String[1];
        Object objectRes = null;
        try {
            FindIterable<Document> iterable = DBManager.getInstance().getCollection(collection)
                    .find(new Document(key, value ));
            iterable.forEach(new Block<Document>() {
                @Override
                public void apply(final Document document) {
                    String element = document.toJson();
                    String elementId = element.split(",")[0];
                    String []id = elementId.split(":")[2].split("\"");
                    res[0] = id[1];
                }
            });
        } catch (Exception e) {
            System.err.println("ERROR: "+e.getClass().getName() + ": " + e.getMessage());
        } finally {
            DBManager.getInstance().closeMongoDB();
        }
        return res [0];
    }

    public void closeMongoDB() {
        DBManager.getInstance().closeMongoDB();
    }

    public ArrayList<Resource> getResourcesBySearchCriteria(String searchCriteria) {
        final ArrayList<Resource> resources = new ArrayList<Resource>();
        try {
            FindIterable<Document> iterable =
                    DBManager
                            .getInstance()
                            .getCollection("resourcemodels")
                            .find(
                                    regex("name", searchCriteria)
                            );

            iterable.forEach(new Block<Document>() {
                @Override
                public void apply(final Document document) {
                    Resource resource = new Resource();
                    resource.setName((String)document.get("name"));
                    resource.setDisplayName((String) document.get("customName"));
                    resource.setIcon((String) document.get("fontIcon"));
                    resource.setDescription((String) document.get("description"));
                    resources.add(resource);
                }
            });
        } catch (Exception e) {
            System.err.println("ERROR: "+e.getClass().getName() + ": " + e.getMessage());
        } finally {
            DBManager.getInstance().closeMongoDB();
        }
        return resources;
    }
}
@@ -53,16 +53,139 @@
 erable;%0A
+import com.mongodb.client.MongoCollection;%0Aimport com.mongodb.client.MongoCursor;%0Aimport com.mongodb.client.MongoIterable;%0A
 import e
@@ -281,16 +281,68 @@
 rayList;
+%0A%0Aimport static com.mongodb.client.model.Filters.eq;
 %0Aimport 
@@ -793,1057 +793,8 @@
  %7D%0A%0A
-    public String searchByValue(String collection, final String key, String value) %7B%0A        final String %5B%5D res = new String%5B1%5D;%0A        Object objectRes = null;%0A        try %7B%0A            FindIterable%3CDocument%3E iterable = DBManager.getInstance().getCollection(collection)%0A                    .find(new Document(key, value ));%0A            iterable.forEach(new Block%3CDocument%3E() %7B%0A                @Override%0A                public void apply(final Document document) %7B%0A                    String element = document.toJson();%0A                    String elementId = element.split(%22,%22)%5B0%5D;%0A                    String %5B%5Did = elementId.split(%22:%22)%5B2%5D.split(%22%5C%22%22);%0A                    res%5B0%5D = id%5B1%5D;%0A                %7D%0A            %7D);%0A        %7D catch (Exception e) %7B%0A            System.err.println(%22ERROR: %22+e.getClass().getName() + %22: %22 + e.getMessage());%0A        %7D finally %7B%0A            DBManager.getInstance().closeMongoDB();%0A        %7D%0A        return res %5B0%5D;%0A    %7D%0A%0A    public void closeMongoDB() %7B%0A        DBManager.getInstance().closeMongoDB();%0A    %7D%0A%0A
     
@@ -1990,105 +1990,698 @@
    %7D
- finally %7B%0A            DBManager.getInstance().closeMongoDB();%0A        %7D%0A        return resources
+%0A%0A        return resources;%0A    %7D%0A%0A    public String getIdByKey(String collection, String key, String value) %7B%0A        String res = %22%22;%0A        MongoCollection%3CDocument%3E docs =%0A                DBManager%0A                        .getInstance()%0A                        .getCollection(collection)%0A                        ;%0A        MongoCursor%3CDocument%3E cursor =%0A                docs%0A                        .find(eq(key, value))%0A                        .iterator();%0A        while (cursor.hasNext()) %7B%0A            Document doc = cursor.next();%0A            res += doc.get(%22_id%22);%0A        %7D%0A        return res;%0A    %7D%0A%0A    public void closeMongoDB() %7B%0A        DBManager.getInstance().closeMongoDB()
 ;%0A  

Add the method searchByCriteria and delete the method that not use