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.effektif.mongo.MongoWorkflowStore.java

License:Apache License

@Override
public List<ExecutableWorkflow> findWorkflows(WorkflowQuery query) {
    if (query == null) {
        query = new WorkflowQuery();
    }//from  w  w w .j ava 2  s  . c om
    List<ExecutableWorkflow> workflows = new ArrayList<>();
    DBCursor cursor = createWorkflowDbCursor(query);
    while (cursor.hasNext()) {
        BasicDBObject dbWorkflow = (BasicDBObject) cursor.next();
        ExecutableWorkflow workflow = mongoToWorkflowApi(dbWorkflow, ExecutableWorkflow.class);
        workflows.add(workflow);
    }
    return workflows;
}

From source file:com.ejbmongoembeddedtomcat.service.MongoService.java

public List<Customer> readAllCustomer() {

    List<Customer> customers = new ArrayList<Customer>();
    DBCursor cursor = col.find();
    while (cursor.hasNext()) {
        DBObject doc = cursor.next();//from w w  w .java 2s  . c  o  m
        Customer cus = CustomerConverter.toCustomer(doc);
        customers.add(cus);
    }
    return customers;
}

From source file:com.emuneee.camerasyncmanager.util.DatabaseUtil.java

License:Apache License

/**
 * Returns a camera with id//from w w w  .  j av a  2  s  .c o  m
 * @param id
 * @return
 */
public Camera getCamera(String id) {
    Camera camera = null;
    DB db = getDatabase();
    DBCursor cursor = null;

    try {
        DBCollection collection = db.getCollection("camera");
        BasicDBObject query = new BasicDBObject("_id", id);
        cursor = collection.find(query);

        if (cursor.hasNext()) {
            BasicDBObject dbObj = (BasicDBObject) cursor.next();
            camera = dbObjectToCamera(dbObj);
        }
    } catch (Exception e) {
        sLogger.error("Exception retrieving camera with id " + id);
        sLogger.error(e);
    } finally {
        HttpHelper.closeResources(new Object[] { cursor });
    }

    return camera;
}

From source file:com.espirit.moddev.examples.uxbridge.newsdrilldown.jpa.NewsHandler.java

License:Apache License

/**
 * loads a news by the document id//from  ww  w .j  a  va  2 s.co m
 *
 * @param id
 *            the id of the document
 * @param language
 *            language abbreviation like EN or DE
 * @return the document or null
 */
private DBObject getNewsById(Long id, String language) {
    BasicDBObject query = new BasicDBObject();
    query.put("fs_id", id);
    query.put("language", language);

    DBCursor cur = dbNews.find(query);

    if (cur.hasNext()) {
        return cur.next();
    }
    return null;
}

From source file:com.espirit.moddev.examples.uxbridge.newsdrilldown.jpa.NewsHandler.java

License:Apache License

/**
 * loads a category by the document id/*  w w  w  .ja  v a  2s .c  o m*/
 *
 * @param id
 *            the id of the document
 * @param language
 *            language abbreviation like EN or DE
 * @return the document or null
 */
private DBObject getCategoryById(Long id, String language) {
    BasicDBObject query = new BasicDBObject();
    query.put("fs_id", id);
    query.put("language", language);

    DBCursor cur = dbCategories.find(query);

    if (cur.hasNext()) {
        return cur.next();
    }
    return null;
}

From source file:com.espirit.moddev.examples.uxbridge.newsdrilldown.jpa.NewsHandler.java

License:Apache License

/**
 * loads a metacategory by the document id
 *
 * @param id//from   w  ww. j  ava 2 s .c om
 *            the id of the document
 * @param language
 *            language abbreviation like EN or DE
 * @return the document or null
 */
private DBObject getMetaCategoryById(Long id, String language) {
    BasicDBObject query = new BasicDBObject();
    query.put("fs_id", id);
    query.put("language", language);

    DBCursor cur = dbMetaCategories.find(query);

    if (cur.hasNext()) {
        return cur.next();
    }
    return null;
}

From source file:com.espirit.moddev.examples.uxbridge.newsdrilldown.jpa.NewsHandler.java

License:Apache License

/**
 * Mongo ID generation like it is done in the grails gorm framework
 *
 * @param collectionName/*from  ww  w .j av  a 2  s  . c  om*/
 *            The name of the collection the id should be generated for
 * @param db
 *            The mongodb connection
 * @return a new id
 */
private Long generateIdentifier(String collectionName, DB db) {

    // get or create the Collection for the ID storage
    DBCollection dbCollection = db.getCollection(collectionName + ".next_id");
    // create entry to store the newly generated id
    DBObject nativeEntry = new BasicDBObject();

    while (true) {
        DBCursor result = dbCollection.find().sort(new BasicDBObject("_id", -1)).limit(1);

        long nextId;
        if (result.hasNext()) {
            final Long current = (Long) result.next().get("_id");
            nextId = current + 1;
        } else {
            nextId = 1;
        }

        nativeEntry.put("_id", nextId);
        final WriteResult writeResult = dbCollection.insert(nativeEntry);
        final CommandResult lastError = writeResult.getLastError();
        if (lastError.ok()) {
            break;
        }

        final Object code = lastError.get("code");
        // duplicate key error try again
        if (code != null && code.equals(11000)) {
            continue;
        }
        break;
    }

    return (Long) nativeEntry.get("_id");
}

From source file:com.espirit.moddev.examples.uxbridge.newswidget.mongodb.ArticleHandler.java

License:Apache License

/**
 * loads an article by the document id/*from   w w  w. java2  s . c  o  m*/
 *
 * @param id       the id of the document
 * @param language language abbreviation like EN or DE
 * @return the document or null
 */
private DBObject getById(Long id, String language) {
    BasicDBObject query = new BasicDBObject();
    query.put("aid", id);
    query.put("language", language);

    DBCursor cur = articles.find(query);

    if (cur.hasNext()) {
        return cur.next();
    }
    return null;
}

From source file:com.ewcms.mongo.demo.repositories.PersonRepositoryImpl.java

License:Open Source License

@Override
public List<Person> findWorkByAgeRang(final int start, final int end) {
    return getMongoOperations().execute(new DbCallback<List<Person>>() {
        @Override//from  ww  w .  ja  v a2  s .c o  m
        public List<Person> doInDB(DB db) throws MongoException, DataAccessException {
            DBCollection coll = db.getCollection("person");
            BasicDBObject query = new BasicDBObject();
            query.put("age", new BasicDBObject("$gte", 20).append("$lte", 60));
            DBCursor cur = coll.find(query);
            List<Person> list = new ArrayList<Person>();
            for (; cur.hasNext();) {
                DBObject source = cur.next();
                Person person = convertEntity(source);
                list.add(person);
            }
            return list;
        }
    });
}

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

public void insertValueDb(User user) throws Exception {
    BasicDBObject document = new BasicDBObject();
    BasicDBObject searchQuery = new BasicDBObject();

    document.append("DriverLicense", user.getDriverLicense());
    document.append("FirstName", user.getFirstName());
    document.append("LastName", user.getLastName());
    document.append("DOB", user.getDob());
    document.append("Address", user.getAddress());
    //System.out.println(document);
    searchQuery.append("DriverLicense", user.getDriverLicense());
    DBCursor cursor = table.find(searchQuery);
    if (!cursor.hasNext()) {
        table.insert(document);/*from w w w . ja  va 2s.c o m*/
    } else
        throw new Exception("Details already exist");
}