TransactionStatus status = TransactionUtils.createTransaction("readProducts",
TransactionDefinition.PROPAGATION_REQUIRED, transactionManager, true);
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("Building index - page: [%s], pageSize: [%s]", page, pageSize));
}
StopWatch s = new StopWatch();
boolean cacheOperationManaged = false;
try {
CatalogStructure cache = SolrIndexCachedOperation.getCache();
if (cache != null) {
cacheOperationManaged = true;
} else {
cache = new CatalogStructure();
SolrIndexCachedOperation.setCache(cache);
}
List<Product> products = readAllActiveProducts(page, pageSize);
List<Long> productIds = BLCCollectionUtils.collectList(products, new TypedTransformer<Long>() {
@Override
public Long transform(Object input) {
return ((Product) input).getId();
}
});
solrIndexDao.populateCatalogStructure(productIds, SolrIndexCachedOperation.getCache());
List<Field> fields = fieldDao.readAllProductFields();
List<Locale> locales = getAllLocales();
Collection<SolrInputDocument> documents = new ArrayList<SolrInputDocument>();
for (Product product : products) {
SolrInputDocument doc = buildDocument(product, fields, locales);
//If someone overrides the buildDocument method and determines that they don't want a product
//indexed, then they can return null. If the document is null it does not get added to
//to the index.
if (doc != null) {
documents.add(doc);
}
}
logDocuments(documents);
if (!CollectionUtils.isEmpty(documents)) {
SolrServer server = useReindexServer ? SolrContext.getReindexServer() : SolrContext.getServer();
server.add(documents);
server.commit();
}
TransactionUtils.finalizeTransaction(status, transactionManager, false);
} catch (SolrServerException e) {
TransactionUtils.finalizeTransaction(status, transactionManager, true);
throw new ServiceException("Could not rebuild index", e);
} catch (IOException e) {
TransactionUtils.finalizeTransaction(status, transactionManager, true);
throw new ServiceException("Could not rebuild index", e);
} catch (RuntimeException e) {
TransactionUtils.finalizeTransaction(status, transactionManager, true);
throw e;
} finally {
if (!cacheOperationManaged) {
SolrIndexCachedOperation.clearCache();
}
}
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("Built index - page: [%s], pageSize: [%s] in [%s]", page, pageSize, s.toLapString()));
}
}