Skip to content

Commit

Permalink
hibernate#13 change BatchItemWriter to do lucene work.
Browse files Browse the repository at this point in the history
The processor build AddLuceneWork using the entities obtained from JPA entity manager. The code has been separated into logical modules and different functions.
  • Loading branch information
mincong-h committed Jun 2, 2016
1 parent 6d70db1 commit 81891b9
Showing 1 changed file with 42 additions and 22 deletions.
Expand Up @@ -10,10 +10,8 @@
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder.In;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Root;
import javax.persistence.metamodel.EntityType;

import org.hibernate.Session;
import org.hibernate.engine.spi.SessionImplementor;
Expand Down Expand Up @@ -83,20 +81,21 @@ public Object processItem(Object item) throws Exception {
List<Address> addresses = null;
List<AddLuceneWork> addWorks = null;


// obtain entities using JPA criteria query
CriteriaQuery<Address> q = em.getCriteriaBuilder().createQuery(Address.class);
Root<Address> address = q.from(Address.class);
Path<Integer> addressId = address.get("addressId");
In<Integer> inIds = em.getCriteriaBuilder().in(addressId);
for (int id : ids) {
inIds.value(id);
}
q.where(inIds);
CriteriaQuery<Address> q = buildCriteriaQuery(Address.class, ids);
addresses = em.createQuery(q).getResultList();
return addresses;
/*
// produce lucene works
addWorks = buildAddLuceneWorks(addresses);

return addWorks;
}

/**
* Build addLuceneWorks using entities. This method is inspired by the
* current mass indexer implementation.
*
* @param entities entities obtained from JPA entity manager
* @return a list of addLuceneWorks
*/
private <T> List<AddLuceneWork> buildAddLuceneWorks(List<T> entities) {
session = em.unwrap(Session.class);
searchIntegrator = ContextHelper.getSearchintegrator(session);
docBuilder = searchIntegrator
Expand All @@ -108,9 +107,9 @@ public Object processItem(Object item) throws Exception {
(SessionImplementor) session
);
String tenantId = null;
addWorks = new LinkedList<>();
for (Address address: addresses) {
Serializable id = session.getIdentifier(address);
List<AddLuceneWork> addWorks = new LinkedList<>();
for (T entity: entities) {
Serializable id = session.getIdentifier(entity);
TwoWayFieldBridge idBridge = docBuilder.getIdBridge();
conversionContext.pushProperty(docBuilder.getIdKeywordName());
String idInString = null;
Expand All @@ -124,18 +123,39 @@ public Object processItem(Object item) throws Exception {
}
AddLuceneWork addWork = docBuilder.createAddWork(
tenantId,
Address.class,
address,
entity.getClass(),
entity,
id,
idInString,
sessionInitializer,
conversionContext
);
addWorks.add(addWork);
}
return addWorks;
*/
}

/**
* Build criteria query using JPA criteria builder.
*
* TODO: the type of entry array ids should be generic.
*
* @param clazz the target class
* @param ids the identifiers, of which the correspondent entities should be
* selected.
* @return the criteria query built
*/
private <T> CriteriaQuery<T> buildCriteriaQuery(Class<T> clazz, int[] ids) {
CriteriaQuery<T> q = em.getCriteriaBuilder().createQuery(clazz);
Root<T> root = q.from(clazz);
// TODO: get attribute id in generic type
Path<Integer> attrId = root.get("addressId");
In<Integer> inIds = em.getCriteriaBuilder().in(attrId);
for (int id : ids) {
inIds.value(id);
}
q.where(inIds);
return q;
}

/**
Expand Down

0 comments on commit 81891b9

Please sign in to comment.