List of usage examples for org.springframework.data.mongodb.core DocumentCallbackHandler DocumentCallbackHandler
DocumentCallbackHandler
From source file:com.seajas.search.profiler.service.repository.RepositoryService.java
/** * Delete all given resources from the repository. * /*from w w w . jav a 2s .c om*/ * @param collection * @param sourceId * @param url * @param startDate * @param endDate * @return boolean */ @SuppressWarnings("deprecation") public boolean deleteResources(final String collection, final Integer sourceId, final String url, final Date startDate, final Date endDate) { try { Query query = createQuery(true, collection, sourceId, null, startDate, endDate, url, null); if (logger.isInfoEnabled()) logger.info("Removing entries from the repository"); // First delete all GridFS references mongoTemplate.executeQuery(query, defaultCollection, new DocumentCallbackHandler() { @Override public void processDocument(final DBObject dbObject) throws MongoException, DataAccessException { if (dbObject.get("originalContent") != null) { ObjectId originalId = (ObjectId) ((BasicDBObject) dbObject.get("originalContent")) .get("_id"); gridFs.remove(originalId); } if (dbObject.get("modifiedContent") != null) { ObjectId modifiedId = (ObjectId) ((BasicDBObject) dbObject.get("modifiedContent")) .get("_id"); gridFs.remove(modifiedId); } } }); // Then delete the repository documents mongoTemplate.remove(query, defaultCollection); return true; } catch (RuntimeException e) { logger.error("Unable to remove the given resource(s) from the repository", e); return false; } }
From source file:com.seajas.search.profiler.service.repository.RepositoryService.java
/** * Process a paged list of all resources within the repository. * /*from w ww . j a v a 2s. com*/ * @param collection * @param sourceId * @param taxonomyMatch * @param url * @param startDate * @param endDate * @param parameters * @param rangeStart * @param rangeEnd * @param processor * @return boolean */ public boolean processResources(final String collection, final Integer sourceId, final String taxonomyMatch, final String url, final Date startDate, final Date endDate, final Map<String, String> parameters, final Integer rangeStart, final Integer rangeEnd, final RepositoryProcessor processor) { Query query = createQuery(true, collection, sourceId, taxonomyMatch, startDate, endDate, url, parameters); query.fields().include("_id"); query.fields().include("currentState"); query.fields().include("element.hostname"); // Determine the total number of document this affects final AtomicLong currentResult = new AtomicLong(0L); // Then skip to it and get going query.skip(rangeStart); if (rangeEnd != null) query.limit(rangeEnd - rangeStart); if (logger.isInfoEnabled()) logger.info(String.format("Processing ranges %d to %s of (unknown) results through the given processor", rangeStart, rangeEnd != null ? rangeEnd.toString() : "end")); mongoTemplate.executeQuery(query, defaultCollection, new DocumentCallbackHandler() { @Override public void processDocument(final DBObject dbObject) throws MongoException, DataAccessException { CompositeState currentState = CompositeState.valueOf((String) dbObject.get("currentState")); if (!EnumSet.of(CompositeState.Content, CompositeState.CompletedDocument, CompositeState.InitialDocument).contains(currentState)) { if (logger.isDebugEnabled()) { ObjectId id = (ObjectId) dbObject.get("_id"); logger.debug("Skipping over element with ID '" + id + "' and current state '" + currentState + "'"); } return; } ObjectId id = (ObjectId) dbObject.get("_id"); String hostname = (String) ((BasicDBObject) dbObject.get("element")).get("hostname"); if (logger.isInfoEnabled()) logger.info("Processing re-indexing entry " + currentResult.getAndIncrement() + " / (unknown) with ID '" + id + "' and hostname '" + hostname + "'"); processor.process(id, hostname); } }); return true; }