Example usage for com.mongodb DBCursor hasNext

List of usage examples for com.mongodb DBCursor hasNext

Introduction

In this page you can find the example usage for com.mongodb DBCursor hasNext.

Prototype

@Override
public boolean hasNext() 

Source Link

Document

Checks if there is another object available.

Usage

From source file:com.example.rest.DbConnection.java

public User getData(int number) {
    BasicDBObject searchQuery = new BasicDBObject();
    User retUser = null;//  w  w w  .  j a va 2s. c o m

    searchQuery.append("DriverLicense", number);

    DBCursor cursor = table.find(searchQuery);

    while (cursor.hasNext()) {
        DBObject obj = cursor.next();
        retUser = new User((int) obj.get("DriverLicense"));
        retUser.setFirstName((String) obj.get("FirstName"));
        retUser.setLastName((String) obj.get("LastName"));
        retUser.setDob((String) obj.get("DOB"));
        retUser.setAddress((String) obj.get("Address"));
        //retUser = (User) cursor.next();
    }
    return retUser;
}

From source file:com.example.rest.DbConnection.java

public void removeData(int number) {
    BasicDBObject removeQuery = new BasicDBObject();
    removeQuery.append("DriverLicense", number);
    DBCursor cursor = table.find(removeQuery);
    if (cursor.hasNext()) {
        table.remove(removeQuery);/*from   w  w w .  j a v a2 s  .c  om*/
    }
}

From source file:com.example.rest.DbConnection.java

public List<User> allCustomer() {
    BasicDBObject query = new BasicDBObject();
    DBCursor cursor = table.find();
    List<User> all = new ArrayList<User>();
    User retUser = null;/*from  w ww . jav  a2s.c o  m*/
    while (cursor.hasNext()) {
        DBObject obj = cursor.next();
        retUser = new User((int) obj.get("DriverLicense"));
        retUser.setFirstName((String) obj.get("FirstName"));
        retUser.setLastName((String) obj.get("LastName"));
        retUser.setDob((String) obj.get("DOB"));
        retUser.setAddress((String) obj.get("Address"));
        all.add(retUser);
    }
    return all;
}

From source file:com.example.rest.DbConnection.java

public String getOrder(int number) {
    BasicDBObject document = new BasicDBObject();
    String order = null;//  ww w . j  a v  a2  s  .  c om

    document.append("DriverLicense", number);
    DBCursor cursor = orderTable.find(document);
    if (cursor.hasNext()) {
        DBObject obj = cursor.next();
        order = (String) obj.get("OrderDetails");
    }
    return order;
}

From source file:com.example.rest.DbConnection.java

public void deleteOrder(int number) {
    BasicDBObject document = new BasicDBObject();
    document.append("DriverLicense", number);
    DBCursor cursor = orderTable.find(document);
    if (cursor.hasNext()) {
        orderTable.remove(document);/*from   w ww. java2s . co  m*/
    }

}

From source file:com.ff.reportgenerator.mongodb.DynamicDatabase.java

public String query() {
    String records = "<table>" + Utility.formTableHead() + "<tbody>";

    DB myDB = getDB(DB_NAME);//from  ww  w  . ja v a  2  s  .c om
    DBCollection coll = myDB.getCollection("projects");

    DBCursor ret = coll.find();
    BasicDBObject sort = new BasicDBObject("PROJECT_ID", 1);
    ret.sort(sort);

    try {
        while (ret.hasNext()) {
            records = records + "<tr>";

            DBObject rec = (DBObject) ret.next();
            Iterator keys = Utility.DATA_KEYS.iterator();
            while (keys.hasNext()) {
                String key = (String) keys.next();
                String value = (String) rec.get(key);
                if (key.equals("PROJECT_ID")) {
                    records = records + "<td><a href=\"" + Utility.PROJECT_URL_PREFIX + value + "\">" + value
                            + "</td>";
                } else {
                    records = records + "<td>" + value + "</td>";

                }
            }

            records = records + "</tr>";
            //System.out.println(rec);
        }
    } finally {
        ret.close();
    }

    records = records + "</tbody></table>";
    return records;
}

From source file:com.fileoperations.CopyClass.java

public Boolean forSingleFile() {
    try {/*from   www .ja v  a 2  s  . c  o m*/
        if (name.contains(".")) {

            BasicDBObject query = new BasicDBObject();
            query.put("_id", parentPath + pathMerger + name);
            DBCursor cursor = collection.find(query);

            if (cursor.hasNext()) {
                BasicDBObject checknewquery = new BasicDBObject();
                checknewquery.put("_id", newPath + pathMerger + name);
                DBCursor tempCursor = collection.find(checknewquery);
                if (tempCursor.hasNext()) {
                    return false;
                }
                DBObject copyFile = cursor.next();
                GridFS fileDB = new GridFS(mymongo.getDB(), userCollectionName);
                InputStream data = fileDB.findOne(query).getInputStream();

                BasicDBObject document = new BasicDBObject();
                document.append("_id", newPath + pathMerger + name);
                document.append("folder", "0");
                document.append("parent", newPath);
                document.append("name", name);
                document.append("type", copyFile.get("type").toString());
                collection.insert(document);
                GridFSInputFile inputFile = fileDB.createFile(data);
                inputFile.setId(newPath + pathMerger + name);
                inputFile.put("path", newPath);
                inputFile.setFilename(name);
                inputFile.save();
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    } finally {
        mymongo.closeConnection();
    }
}

From source file:com.fileoperations.FolderDownload.java

private void getPathOfAllChildrenFolder(String parentPath, String folderName) {
    BasicDBObject toFindAllFolderInFolder = new BasicDBObject();
    pathOfChildrenFolders.add(new BasicDBObject("path", parentPath + pathMerger + folderName));
    pathOfChildrenEmptyFolders.add(new BasicDBObject("parent", parentPath + pathMerger + folderName));
    //   pathOfChildrenEmptyFolders.add(new BasicDBObject("path",parentPath+pathMerger+folderName));
    toFindAllFolderInFolder.put("parent", parentPath + pathMerger + folderName);
    DBCursor allFolder = collection.find(toFindAllFolderInFolder);
    while (allFolder.hasNext()) {
        DBObject indivFolder = allFolder.next();
        getPathOfAllChildrenFolder(indivFolder.get("parent").toString(), indivFolder.get("name").toString());

    }//from w  ww .  j av a 2 s .  c  o  m

}

From source file:com.fileoperations.FolderDownload.java

public File makeFolder() throws IOException {
    try {//from   w  w  w  .jav  a  2  s.  com
        String mongoFolder = parentPath + pathMerger + folderName;
        BasicDBObject query = new BasicDBObject();
        query.put("_id", mongoFolder);
        DBCursor cursor = collection.find(query);
        if (cursor.hasNext()) {

            getPathOfAllChildrenFolder(parentPath, folderName);
            BasicDBObject toFindAllFilesInFolder = new BasicDBObject();
            toFindAllFilesInFolder.put("$or", pathOfChildrenFolders);
            GridFS fileStore = new GridFS(mymongo.getDB(), userCollectionName);
            List<GridFSDBFile> AllFiles = fileStore.find(toFindAllFilesInFolder);
            File zip = new File(folderName + ".zip");
            ZipOutputStream folderToZip = new ZipOutputStream(new FileOutputStream(zip));

            for (int i = 0; i < AllFiles.size(); i++) {
                GridFSDBFile indivFile = AllFiles.get(i);
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                InputStream in = indivFile.getInputStream();
                int data = in.read();
                while (data >= 0) {
                    out.write((char) data);
                    data = in.read();
                }

                out.flush();
                String zipPath;
                zipPath = indivFile.get("path").toString() + pathMerger + indivFile.getFilename();
                zipPath = zipPath.replaceFirst(parentPath, "");
                zipPath = zipPath.replaceFirst(pathMerger, "");
                ZipEntry add = new ZipEntry(zipPath);
                folderToZip.putNextEntry(add);
                folderToZip.write(out.toByteArray(), 0, out.toByteArray().length);

            }

            BasicDBObject toFindAllEmptyFilesInFolder = new BasicDBObject();
            toFindAllEmptyFilesInFolder.put("$or", pathOfChildrenEmptyFolders);
            DBCursor emptyFolder = collection.find(toFindAllEmptyFilesInFolder);
            List<String> emptyFolderPathToAdd = new ArrayList<String>();
            while (emptyFolder.hasNext()) {
                DBObject temp = emptyFolder.next();
                BasicDBObject isEmpty = new BasicDBObject();
                isEmpty.put("parent", temp.get("_id").toString());

                if (!collection.find(isEmpty).hasNext()) {
                    if (!temp.get("_id").toString().contains(".")) {
                        emptyFolderPathToAdd.add(temp.get("_id").toString());
                    }
                }

            }
            for (int i = 0; i < emptyFolderPathToAdd.size(); i++) {
                String temp = emptyFolderPathToAdd.get(i).replaceFirst(parentPath, "");
                temp = temp.replaceFirst(pathMerger, "");
                ZipEntry add = new ZipEntry(temp + pathMerger);
                folderToZip.putNextEntry(add);

            }

            folderToZip.closeEntry();
            folderToZip.close();
            return zip;
        } else {
            return null;
        }

    } finally {
        mymongo.closeConnection();
    }

}

From source file:com.fileoperations.FolderDownload.java

public Boolean copyFolder(String newPath) throws IOException {
    try {/*www. j a  va2s.  c  om*/
        String mongoFolder = parentPath + pathMerger + folderName;

        BasicDBObject query = new BasicDBObject();
        query.put("_id", mongoFolder);
        DBCursor cursor = collection.find(query);
        if (cursor.hasNext()) {
            BasicDBObject newquery = new BasicDBObject();
            newquery.put("_id", newPath + pathMerger + folderName);
            if (collection.find(newquery).hasNext()) {
                return false;
            }

            getPathOfAllChildrenFolder(parentPath, folderName);
            BasicDBObject toFindAllFilesInFolder = new BasicDBObject();
            toFindAllFilesInFolder.put("$or", pathOfChildrenFolders);
            GridFS fileStore = new GridFS(mymongo.getDB(), userCollectionName);
            List<GridFSDBFile> AllFiles = fileStore.find(toFindAllFilesInFolder);

            for (int i = 0; i < AllFiles.size(); i++) {
                GridFSDBFile indivFile = AllFiles.get(i);
                InputStream data = indivFile.getInputStream();
                String zipPath;
                zipPath = indivFile.get("path").toString();
                String tempFileName = indivFile.getFilename();

                zipPath = zipPath.replaceFirst(parentPath, newPath);
                BasicDBObject document = new BasicDBObject();
                document.append("_id", zipPath + pathMerger + tempFileName);
                document.append("folder", "0");
                document.append("parent", zipPath);
                document.append("name", tempFileName);
                int index = tempFileName.lastIndexOf(".");
                document.append("type", tempFileName.substring(index));
                collection.insert(document);
                GridFSInputFile inputFile = fileStore.createFile(data);
                inputFile.setId(zipPath + pathMerger + tempFileName);
                inputFile.put("path", zipPath);
                inputFile.setFilename(tempFileName);
                inputFile.save();

            }

            BasicDBObject toFindAllEmptyFilesInFolder = new BasicDBObject();
            toFindAllEmptyFilesInFolder.put("$or", pathOfChildrenEmptyFolders);
            DBCursor allFolders = collection.find(toFindAllEmptyFilesInFolder);
            while (allFolders.hasNext()) {
                DBObject temp = allFolders.next();
                if (temp.get("folder").toString().equals("1")) {
                    String tempPath = temp.get("parent").toString().replaceFirst(parentPath, newPath);
                    BasicDBObject document = new BasicDBObject();
                    document.put("_id", tempPath + pathMerger + temp.get("name"));
                    document.put("folder", "1");
                    document.put("name", temp.get("name"));
                    document.put("parent", tempPath);
                    document.put("type", "1");
                    collection.insert(document);

                }
            }

            return true;
        } else {
            return false;
        }

    } finally {
        mymongo.closeConnection();
    }
}