List of usage examples for org.springframework.data.mongodb.core CollectionCallback CollectionCallback
CollectionCallback
From source file:org.springframework.data.mongodb.core.geo.GeoSpatialTests.java
public List<DBObject> getIndexInfo(Class<?> clazz) { return template.execute(clazz, new CollectionCallback<List<DBObject>>() { public List<DBObject> doInCollection(DBCollection collection) throws MongoException, DataAccessException { return collection.getIndexInfo(); }// ww w. j av a2 s . co m }); }
From source file:org.springframework.data.mongodb.core.mapping.MappingTests.java
@Test public void testIndexesCreatedInRightCollection() { CustomCollectionWithIndex ccwi = new CustomCollectionWithIndex("test"); template.insert(ccwi);//from w w w.ja v a 2 s .c om assertTrue(template.execute("foobar", new CollectionCallback<Boolean>() { public Boolean doInCollection(DBCollection collection) throws MongoException, DataAccessException { List<DBObject> indexes = collection.getIndexInfo(); for (DBObject dbo : indexes) { if (dbo.get("name") != null && dbo.get("name") instanceof String && ((String) dbo.get("name")).startsWith("name")) { return true; } } return false; } })); DetectedCollectionWithIndex dcwi = new DetectedCollectionWithIndex("test"); template.insert(dcwi); assertTrue( template.execute(MongoCollectionUtils.getPreferredCollectionName(DetectedCollectionWithIndex.class), new CollectionCallback<Boolean>() { public Boolean doInCollection(DBCollection collection) throws MongoException, DataAccessException { List<DBObject> indexes = collection.getIndexInfo(); for (DBObject dbo : indexes) { if (dbo.get("name") != null && dbo.get("name") instanceof String && ((String) dbo.get("name")).startsWith("name")) { return true; } } return false; } })); }
From source file:org.springframework.data.mongodb.core.MongoTemplate.java
public void dropCollection(String collectionName) { execute(collectionName, new CollectionCallback<Void>() { public Void doInCollection(DBCollection collection) throws MongoException, DataAccessException { collection.drop();//from w w w .j a v a 2 s . c om if (LOGGER.isDebugEnabled()) { LOGGER.debug("Dropped collection [" + collection.getFullName() + "]"); } return null; } }); }
From source file:org.springframework.data.mongodb.core.MongoTemplate.java
private long count(Query query, Class<?> entityClass, String collectionName) { Assert.hasText(collectionName);// w w w. j a v a 2 s. c o m final DBObject dbObject = query == null ? null : mapper.getMappedObject(query.getQueryObject(), entityClass == null ? null : mappingContext.getPersistentEntity(entityClass)); return execute(collectionName, new CollectionCallback<Long>() { public Long doInCollection(DBCollection collection) throws MongoException, DataAccessException { return collection.count(dbObject); } }); }
From source file:org.springframework.data.mongodb.core.MongoTemplate.java
protected Object insertDBObject(final String collectionName, final DBObject dbDoc, final Class<?> entityClass) { if (LOGGER.isDebugEnabled()) { LOGGER.debug(//from w w w . j a va 2 s. com "insert DBObject containing fields: " + dbDoc.keySet() + " in collection: " + collectionName); } return execute(collectionName, new CollectionCallback<Object>() { public Object doInCollection(DBCollection collection) throws MongoException, DataAccessException { MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.INSERT, collectionName, entityClass, dbDoc, null); WriteConcern writeConcernToUse = prepareWriteConcern(mongoAction); WriteResult wr; if (writeConcernToUse == null) { wr = collection.insert(dbDoc); } else { wr = collection.insert(dbDoc, writeConcernToUse); } handleAnyWriteResultErrors(wr, dbDoc, "insert"); return dbDoc.get(ID); } }); }
From source file:org.springframework.data.mongodb.core.MongoTemplate.java
protected List<ObjectId> insertDBObjectList(final String collectionName, final List<DBObject> dbDocList) { if (dbDocList.isEmpty()) { return Collections.emptyList(); }// www . ja v a 2 s . c om if (LOGGER.isDebugEnabled()) { LOGGER.debug("insert list of DBObjects containing " + dbDocList.size() + " items"); } execute(collectionName, new CollectionCallback<Void>() { public Void doInCollection(DBCollection collection) throws MongoException, DataAccessException { MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.INSERT_LIST, collectionName, null, null, null); WriteConcern writeConcernToUse = prepareWriteConcern(mongoAction); WriteResult wr; if (writeConcernToUse == null) { wr = collection.insert(dbDocList); } else { wr = collection.insert(dbDocList.toArray((DBObject[]) new BasicDBObject[dbDocList.size()]), writeConcernToUse); } handleAnyWriteResultErrors(wr, null, "insert_list"); return null; } }); List<ObjectId> ids = new ArrayList<ObjectId>(); for (DBObject dbo : dbDocList) { Object id = dbo.get(ID); if (id instanceof ObjectId) { ids.add((ObjectId) id); } else { // no id was generated ids.add(null); } } return ids; }
From source file:org.springframework.data.mongodb.core.MongoTemplate.java
protected Object saveDBObject(final String collectionName, final DBObject dbDoc, final Class<?> entityClass) { if (LOGGER.isDebugEnabled()) { LOGGER.debug("save DBObject containing fields: " + dbDoc.keySet()); }/*from w w w. j a v a 2s. co m*/ return execute(collectionName, new CollectionCallback<Object>() { public Object doInCollection(DBCollection collection) throws MongoException, DataAccessException { MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.SAVE, collectionName, entityClass, dbDoc, null); WriteConcern writeConcernToUse = prepareWriteConcern(mongoAction); WriteResult wr; if (writeConcernToUse == null) { wr = collection.save(dbDoc); } else { wr = collection.save(dbDoc, writeConcernToUse); } handleAnyWriteResultErrors(wr, dbDoc, "save"); return dbDoc.get(ID); } }); }
From source file:org.springframework.data.mongodb.core.MongoTemplate.java
protected WriteResult doUpdate(final String collectionName, final Query query, final Update update, final Class<?> entityClass, final boolean upsert, final boolean multi) { return execute(collectionName, new CollectionCallback<WriteResult>() { public WriteResult doInCollection(DBCollection collection) throws MongoException, DataAccessException { MongoPersistentEntity<?> entity = entityClass == null ? null : getPersistentEntity(entityClass); DBObject queryObj = query == null ? new BasicDBObject() : mapper.getMappedObject(query.getQueryObject(), entity); DBObject updateObj = update.getUpdateObject(); for (String key : updateObj.keySet()) { updateObj.put(key, mongoConverter.convertToMongoType(updateObj.get(key))); }/*w w w .j a v a 2s .c o m*/ if (LOGGER.isDebugEnabled()) { LOGGER.debug("calling update using query: " + queryObj + " and update: " + updateObj + " in collection: " + collectionName); } WriteResult wr; MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.UPDATE, collectionName, entityClass, updateObj, queryObj); WriteConcern writeConcernToUse = prepareWriteConcern(mongoAction); if (writeConcernToUse == null) { wr = collection.update(queryObj, updateObj, upsert, multi); } else { wr = collection.update(queryObj, updateObj, upsert, multi, writeConcernToUse); } handleAnyWriteResultErrors(wr, queryObj, "update with '" + updateObj + "'"); return wr; } }); }
From source file:org.springframework.data.mongodb.core.MongoTemplate.java
protected <T> void doRemove(final String collectionName, final Query query, final Class<T> entityClass) { if (query == null) { throw new InvalidDataAccessApiUsageException("Query passed in to remove can't be null"); }/*from www . j a va2s .co m*/ final DBObject queryObject = query.getQueryObject(); final MongoPersistentEntity<?> entity = getPersistentEntity(entityClass); execute(collectionName, new CollectionCallback<Void>() { public Void doInCollection(DBCollection collection) throws MongoException, DataAccessException { DBObject dboq = mapper.getMappedObject(queryObject, entity); WriteResult wr = null; MongoAction mongoAction = new MongoAction(writeConcern, MongoActionOperation.REMOVE, collectionName, entityClass, null, queryObject); WriteConcern writeConcernToUse = prepareWriteConcern(mongoAction); if (LOGGER.isDebugEnabled()) { LOGGER.debug("remove using query: " + dboq + " in collection: " + collection.getName()); } if (writeConcernToUse == null) { wr = collection.remove(dboq); } else { wr = collection.remove(dboq, writeConcernToUse); } handleAnyWriteResultErrors(wr, dboq, "remove"); return null; } }); }
From source file:org.springframework.data.mongodb.crossstore.MongoChangeSetPersister.java
public void getPersistentState(Class<? extends ChangeSetBacked> entityClass, Object id, final ChangeSet changeSet) throws DataAccessException, NotFoundException { if (id == null) { log.debug("Unable to load MongoDB data for null id"); return;// w w w .j av a2 s . c o m } String collName = getCollectionNameForEntity(entityClass); final DBObject dbk = new BasicDBObject(); dbk.put(ENTITY_ID, id); dbk.put(ENTITY_CLASS, entityClass.getName()); if (log.isDebugEnabled()) { log.debug("Loading MongoDB data for " + dbk); } mongoTemplate.execute(collName, new CollectionCallback<Object>() { public Object doInCollection(DBCollection collection) throws MongoException, DataAccessException { for (DBObject dbo : collection.find(dbk)) { String key = (String) dbo.get(ENTITY_FIELD_NAME); if (log.isDebugEnabled()) { log.debug("Processing key: " + key); } if (!changeSet.getValues().containsKey(key)) { String className = (String) dbo.get(ENTITY_FIELD_CLASS); if (className == null) { throw new DataIntegrityViolationException("Unble to convert property " + key + ": Invalid metadata, " + ENTITY_FIELD_CLASS + " not available"); } Class<?> clazz = ClassUtils.resolveClassName(className, ClassUtils.getDefaultClassLoader()); Object value = mongoTemplate.getConverter().read(clazz, dbo); if (log.isDebugEnabled()) { log.debug("Adding to ChangeSet: " + key); } changeSet.set(key, value); } } return null; } }); }