Commit e2fa402e authored by Matija Obreza's avatar Matija Obreza

Use lucene bridge for Accession entity

parent da314c87
......@@ -52,7 +52,7 @@
<jackson.version>2.2.1</jackson.version>
<!--Container -->
<jetty.version>9.0.3.v20130506</jetty.version>
<jetty.version>9.0.5.v20130815</jetty.version>
<maven.test.skip>false</maven.test.skip>
......
package org.crophub.rest.common.lucene.genesys;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.crophub.rest.common.model.genesys.Accession;
import org.crophub.rest.common.model.genesys.AllAccnames;
import org.crophub.rest.common.model.genesys.AllAcqBreeding;
import org.crophub.rest.common.model.genesys.AllAcqCollect;
import org.crophub.rest.common.model.genesys.AllAcqExchange;
import org.crophub.rest.common.model.genesys.Taxonomy;
import org.crophub.rest.common.model.impl.Country;
import org.crophub.rest.common.model.impl.FaoInstitute;
import org.hibernate.search.bridge.FieldBridge;
import org.hibernate.search.bridge.LuceneOptions;
public class AccessionBridge implements FieldBridge {
public static final Log LOG = LogFactory.getLog(AccessionBridge.class);
@Override
public void set(String name, Object value, Document document, LuceneOptions luceneOptions) {
// In this particular class the name of the new field was passed
// from the name field of the ClassBridge Annotation. This is not
// a requirement. It just works that way in this instance. The
// actual name could be supplied by hard coding it below.
Accession accession = (Accession) value;
StringDocument sd = new StringDocument();
sd.append(accession.getAccessionName());
sd.append(accession.getAcquisitionSource());
// sb.append(accession.getGenus());
sd.append(accession.getInstituteCode());
sd.append(accession.getOrigin());
Country countryOfOrigin = accession.getCountryOfOrigin();
if (countryOfOrigin != null) {
sd.append(countryOfOrigin.getName());
}
Taxonomy taxonomy = accession.getTaxonomy();
if (taxonomy != null) {
sd.append(taxonomy.getTaxonName());
}
FaoInstitute institute = accession.getInstitute();
if (institute != null) {
sd.append(institute.getAcronym());
sd.append(institute.getFullName());
if (institute.getCountry() != null)
sd.append(institute.getCountry().getName());
}
AllAccnames accNames = accession.getAccessionNames();
if (accNames != null) {
sd.append(accNames.getOtherIds());
sd.append(accNames.getAccNames());
}
AllAcqBreeding accBreeding = accession.getAccessionBreeding();
if (accBreeding != null) {
sd.append(accBreeding.getPedigree());
sd.append(accBreeding.getBreederCode());
}
AllAcqCollect accCollect = accession.getAccessionCollection();
if (accCollect != null) {
sd.append(accCollect.getCollectSite());
sd.append(accCollect.getCollectorsNumb());
sd.append(accCollect.getCollectingInstitute());
}
AllAcqExchange accExchange = accession.getAccessionExchange();
if (accExchange != null) {
sd.append(accExchange.getDonorInstitute());
sd.append(accExchange.getAccNumbDonor());
}
LOG.debug("IDX." + name + " = " + sd.toString());
Field field = new Field(name, sd.toString(), luceneOptions.getStore(), luceneOptions.getIndex(), luceneOptions.getTermVector());
field.setBoost(luceneOptions.getBoost());
document.add(field);
}
}
package org.crophub.rest.common.lucene.genesys;
import org.apache.commons.lang.StringUtils;
public class StringDocument {
private StringBuffer sb = new StringBuffer();
public StringDocument append(String value) {
if (StringUtils.isNotBlank(value)) {
sb.append(value);
sb.append(" ");
}
return this;
}
@Override
public String toString() {
return sb.toString();
}
}
......@@ -26,19 +26,21 @@ import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import org.crophub.rest.common.lucene.genesys.AccessionBridge;
import org.crophub.rest.common.model.impl.Country;
import org.crophub.rest.common.model.impl.FaoInstitute;
import org.hibernate.search.annotations.ClassBridge;
import org.hibernate.search.annotations.DocumentId;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.Store;
@Entity
// @Table(name = "accession")
@Table(name = "accession")
@Indexed
@ClassBridge(name = "body", impl = AccessionBridge.class)
public class Accession implements java.io.Serializable {
private static final long serialVersionUID = -7630113633534038876L;
private Long id;
......@@ -58,11 +60,15 @@ public class Accession implements java.io.Serializable {
private Boolean availability;
private Boolean mlsStatus;
private String genus;
private AllAccnames accessionNames;
private AllAcqBreeding accessionBreeding;
private AllAcqCollect accessionCollection;
private AllAcqExchange accessionExchange;
private AllEnvironment accessionEnvironment;
public Accession() {
}
@Id
@GeneratedValue
@Column(name = "id", unique = true, nullable = false)
......@@ -95,7 +101,6 @@ public class Accession implements java.io.Serializable {
}
@Column(name = "ACC_Numb_HI", nullable = false, length = 128)
@Field(name="title", store=Store.NO)
public String getAccessionName() {
return this.accNumbHi;
}
......@@ -140,13 +145,13 @@ public class Accession implements java.io.Serializable {
public void setOrigin(final String origin) {
this.origin = origin;
}
@ManyToOne(cascade = {}, optional = true)
@JoinColumn(name = "originId", nullable = true)
public Country getCountryOfOrigin() {
return countryOfOrigin;
}
public void setCountryOfOrigin(Country countryOfOrigin) {
this.countryOfOrigin = countryOfOrigin;
}
......@@ -223,10 +228,58 @@ public class Accession implements java.io.Serializable {
this.genus = genuss;
}
@OneToOne
@JoinColumn(name = "id", referencedColumnName = "ALIS_Id")
public AllAccnames getAccessionNames() {
return accessionNames;
}
public void setAccessionNames(AllAccnames accessionNames) {
this.accessionNames = accessionNames;
}
@OneToOne
@JoinColumn(name = "id", referencedColumnName = "ALIS_Id")
public AllAcqBreeding getAccessionBreeding() {
return accessionBreeding;
}
public void setAccessionBreeding(AllAcqBreeding accessionBreeding) {
this.accessionBreeding = accessionBreeding;
}
@OneToOne
@JoinColumn(name = "id", referencedColumnName = "ALIS_Id")
public AllAcqCollect getAccessionCollection() {
return accessionCollection;
}
public void setAccessionCollection(AllAcqCollect accessionCollection) {
this.accessionCollection = accessionCollection;
}
@OneToOne
@JoinColumn(name = "id", referencedColumnName = "ALIS_Id")
public AllAcqExchange getAccessionExchange() {
return accessionExchange;
}
public void setAccessionExchange(AllAcqExchange accessionExchange) {
this.accessionExchange = accessionExchange;
}
@OneToOne
@JoinColumn(name = "id", referencedColumnName = "ALIS_Id")
public AllEnvironment getAccessionEnvironment() {
return accessionEnvironment;
}
public void setAccessionEnvironment(AllEnvironment accessionEnvironment) {
this.accessionEnvironment = accessionEnvironment;
}
@Override
public String toString() {
return MessageFormat.format(
"Accession id={0,number,#} A={3} inst={1} genus={2}", id,
institute.getCode(), genus, accNumbHi);
return MessageFormat.format("Accession id={0,number,#} A={3} inst={1} genus={2}", id, institute.getCode(), genus, accNumbHi);
}
}
package org.crophub.rest.common.service;
public interface LuceneIndexer {
void reindexEverything();
}
......@@ -5,6 +5,7 @@ import javax.persistence.EntityManagerFactory;
import org.apache.commons.lang.time.StopWatch;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.crophub.rest.common.service.LuceneIndexer;
import org.hibernate.Session;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;
......@@ -15,13 +16,13 @@ import org.springframework.transaction.annotation.Transactional;
@Transactional
@Component
public class LuceneIndexer {
public class LuceneIndexerImpl implements LuceneIndexer {
public static final Log LOG = LogFactory.getLog(LuceneIndexer.class);
@Autowired
private EntityManagerFactory entityManagerFactory;
@Override
@PreAuthorize("hasRole('ADMINISTRATOR')")
public void reindexEverything() {
......@@ -30,9 +31,10 @@ public class LuceneIndexer {
StopWatch timer = new StopWatch();
Session session = (Session) this.entityManagerFactory.createEntityManager().getDelegate();
FullTextSession fullTextSession = Search.getFullTextSession(session);
try {
timer.start();
LOG.info("Start and Wait...");
fullTextSession.createIndexer().startAndWait();
timer.stop();
} catch (InterruptedException e) {
......
......@@ -23,7 +23,7 @@ import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional(readOnly = true)
public class SearchServiceImpl implements SearchService {
public static final Log LOG = LogFactory.getLog(LuceneIndexer.class);
public static final Log LOG = LogFactory.getLog(SearchServiceImpl.class);
@PersistenceContext
private EntityManager entityManager;
......
......@@ -21,8 +21,8 @@ import java.io.IOException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.crophub.rest.common.service.GeoService;
import org.crophub.rest.common.service.LuceneIndexer;
import org.crophub.rest.common.service.impl.InstituteUpdater;
import org.crophub.rest.common.service.impl.LuceneIndexer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
......
......@@ -29,3 +29,4 @@ log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
log4j.rootLogger=info, stdout
log4j.category.org.crophub=info
log4j.category.org.hibernate.search=debug
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment