[jboss-cvs] jboss-seam/examples/dvdstore/src/com/jboss/dvd/seam ...
Gavin King
gavin.king at jboss.com
Sat Jun 23 18:04:52 EDT 2007
User: gavin
Date: 07/06/23 18:04:52
Modified: examples/dvdstore/src/com/jboss/dvd/seam Actor.java
Category.java Product.java
Added: examples/dvdstore/src/com/jboss/dvd/seam
FullTextSearch.java FullTextSearchAction.java
Indexer.java IndexerAction.java
Log:
Hibernate Search for DVDStore
Revision Changes Path
1.3 +8 -0 jboss-seam/examples/dvdstore/src/com/jboss/dvd/seam/Actor.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: Actor.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/examples/dvdstore/src/com/jboss/dvd/seam/Actor.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -b -r1.2 -r1.3
--- Actor.java 14 May 2006 18:48:54 -0000 1.2
+++ Actor.java 23 Jun 2007 22:04:52 -0000 1.3
@@ -14,8 +14,14 @@
import javax.persistence.Id;
import javax.persistence.Table;
+import org.hibernate.search.annotations.DocumentId;
+import org.hibernate.search.annotations.Field;
+import org.hibernate.search.annotations.Index;
+import org.hibernate.search.annotations.Indexed;
+
@Entity
@Table(name="ACTORS")
+ at Indexed
public class Actor
implements Serializable
{
@@ -24,6 +30,7 @@
@Id @GeneratedValue
@Column(name="ID")
+ @DocumentId
public long getId() {
return id;
}
@@ -32,6 +39,7 @@
}
@Column(name="NAME", length=50)
+ @Field(index = Index.TOKENIZED)
public String getName() {
return name;
}
1.8 +7 -0 jboss-seam/examples/dvdstore/src/com/jboss/dvd/seam/Category.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: Category.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/examples/dvdstore/src/com/jboss/dvd/seam/Category.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -b -r1.7 -r1.8
--- Category.java 21 Nov 2006 03:20:31 -0000 1.7
+++ Category.java 23 Jun 2007 22:04:52 -0000 1.8
@@ -16,10 +16,15 @@
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
+import org.hibernate.search.annotations.DocumentId;
+import org.hibernate.search.annotations.Field;
+import org.hibernate.search.annotations.Index;
+import org.hibernate.search.annotations.Indexed;
@Entity
@Table(name="CATEGORIES")
@Cache(usage=CacheConcurrencyStrategy.READ_ONLY)
+ at Indexed
public class Category
implements Serializable
{
@@ -28,6 +33,7 @@
@Id @GeneratedValue
@Column(name="CATEGORY")
+ @DocumentId
public int getCategoryId() {
return id;
}
@@ -36,6 +42,7 @@
}
@Column(name="NAME",nullable=false,unique=true,length=50)
+ @Field(index = Index.TOKENIZED)
public String getName() {
return name;
}
1.13 +11 -0 jboss-seam/examples/dvdstore/src/com/jboss/dvd/seam/Product.java
(In the diff below, changes in quantity of whitespace are not shown.)
Index: Product.java
===================================================================
RCS file: /cvsroot/jboss/jboss-seam/examples/dvdstore/src/com/jboss/dvd/seam/Product.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -b -r1.12 -r1.13
--- Product.java 4 Jan 2007 23:02:16 -0000 1.12
+++ Product.java 23 Jun 2007 22:04:52 -0000 1.13
@@ -22,9 +22,16 @@
import javax.persistence.OneToOne;
import javax.persistence.Table;
+import org.hibernate.search.annotations.DocumentId;
+import org.hibernate.search.annotations.Field;
+import org.hibernate.search.annotations.Index;
+import org.hibernate.search.annotations.Indexed;
+import org.hibernate.search.annotations.IndexedEmbedded;
+
@Entity
@Table(name="PRODUCTS")
+ at Indexed
public class Product
implements Serializable
{
@@ -41,6 +48,7 @@
@Id @GeneratedValue
@Column(name="PROD_ID")
+ @DocumentId
public long getProductId() {
return productId;
}
@@ -49,6 +57,7 @@
}
@Column(name="ASIN", length=16)
+ @Field(index=Index.UN_TOKENIZED)
public String getASIN() {
return asin;
}
@@ -69,6 +78,7 @@
@JoinTable(name="PRODUCT_ACTORS",
joinColumns=@JoinColumn(name="PROD_ID"),
inverseJoinColumns=@JoinColumn(name="ACTOR_ID"))
+ @IndexedEmbedded
public List<Actor> getActors() {
return actors;
}
@@ -89,6 +99,7 @@
}
@Column(name="TITLE",nullable=false,length=100)
+ @Field(index=Index.TOKENIZED)
public String getTitle() {
return title;
}
1.1 date: 2007/06/23 22:04:52; author: gavin; state: Exp;jboss-seam/examples/dvdstore/src/com/jboss/dvd/seam/FullTextSearch.java
Index: FullTextSearch.java
===================================================================
//$Id: FullTextSearch.java,v 1.1 2007/06/23 22:04:52 gavin Exp $
package com.jboss.dvd.seam;
/**
* @author Emmanuel Bernard
*/
public interface FullTextSearch
{
public String getSearchQuery();
public void setSearchQuery(String searchQuery);
public int getNumberOfResults();
public void nextPage();
public void prevPage();
public boolean isLastPage();
public boolean isFirstPage();
public String doSearch();
public void selectFromRequest();
public void addToCart();
public void addAllToCart();
public int getPageSize();
public void setPageSize(int pageSize);
/*public void setCategory(Category category) ;
public Category getCategory();*/
public void reset();
public void destroy();
}
1.1 date: 2007/06/23 22:04:52; author: gavin; state: Exp;jboss-seam/examples/dvdstore/src/com/jboss/dvd/seam/FullTextSearchAction.java
Index: FullTextSearchAction.java
===================================================================
//$Id: FullTextSearchAction.java,v 1.1 2007/06/23 22:04:52 gavin Exp $
package com.jboss.dvd.seam;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.hibernate.search.FullTextQuery;
import org.hibernate.search.FullTextSession;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Begin;
import org.jboss.seam.annotations.Destroy;
import org.jboss.seam.annotations.End;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Install;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.annotations.RequestParameter;
import org.jboss.seam.annotations.datamodel.DataModel;
import org.jboss.seam.annotations.datamodel.DataModelSelection;
/**
* Hibernate Search version of the store querying mechanism
* @author Emmanuel Bernard
*/
@Stateful
@Name("search")
@Install(precedence=Install.DEPLOYMENT)
public class FullTextSearchAction
implements FullTextSearch,
Serializable
{
static final long serialVersionUID = -6536629890251170098L;
@In(create = true)
ShoppingCart cart;
@PersistenceContext
EntityManager em;
@RequestParameter
Long id;
//Category category;
int pageSize = 15;
int currentPage = 0;
boolean hasMore = false;
int numberOfResults;
String searchQuery;
@DataModel
List<Product> searchResults;
@DataModelSelection
Product selectedProduct;
@Out(required = false)
Product dvd;
@Out(scope = ScopeType.CONVERSATION, required = false)
Map<Product, Boolean> searchSelections;
public String getSearchQuery()
{
return searchQuery;
}
public void setSearchQuery(String searchQuery)
{
this.searchQuery = searchQuery;
}
public int getNumberOfResults()
{
return numberOfResults;
}
@Begin(join = true)
public String doSearch()
{
currentPage = 0;
updateResults();
return "browse";
}
public void nextPage()
{
if (!isLastPage())
{
currentPage++;
updateResults();
}
}
public void prevPage()
{
if (!isFirstPage())
{
currentPage--;
updateResults();
}
}
@Begin(join = true)
public void selectFromRequest()
{
if (id != null)
{
dvd = em.find(Product.class, id);
}
else if (selectedProduct != null)
{
dvd = selectedProduct;
}
}
public boolean isLastPage()
{
return ( searchResults != null ) && !hasMore;
}
public boolean isFirstPage()
{
return ( searchResults != null ) && ( currentPage == 0 );
}
private void updateResults()
{
FullTextQuery query;
try
{
query = searchQuery(searchQuery);
}
catch (ParseException pe) { return; }
List<Product> items = query
.setMaxResults(pageSize + 1)
.setFirstResult(pageSize * currentPage)
.list();
numberOfResults = query.getResultSize();
if (items.size() > pageSize)
{
searchResults = new ArrayList(items.subList(0, pageSize));
hasMore = true;
}
else
{
searchResults = items;
hasMore = false;
}
searchSelections = new HashMap<Product, Boolean>();
}
private FullTextQuery searchQuery(String searchQuery) throws ParseException
{
Map<String,Float> boostPerField = new HashMap<String,Float>();
boostPerField.put( "title", 4f );
boostPerField.put( "description", 2f );
boostPerField.put( "actors.name", 2f );
String[] productFields = {"title", "description", "actors.name"};
QueryParser parser = new MultiFieldQueryParser(productFields, new StandardAnalyzer(), boostPerField);
parser.setAllowLeadingWildcard(true);
org.apache.lucene.search.Query luceneQuery;
luceneQuery = parser.parse(searchQuery);
return getFullTextSession().createFullTextQuery(luceneQuery, Product.class);
}
private FullTextSession getFullTextSession()
{
return (FullTextSession) em.getDelegate();
}
/**
* Add the selected DVD to the cart
*/
public void addToCart()
{
cart.addProduct(dvd, 1);
}
/**
* Add many items to cart
*/
public void addAllToCart()
{
for (Product item : searchResults)
{
Boolean selected = searchSelections.get(item);
if (selected != null && selected)
{
searchSelections.put(item, false);
cart.addProduct(item, 1);
}
}
}
/*public void setCategory(Category category)
{
this.category = category;
}
public Category getCategory()
{
return category;
}*/
public int getPageSize()
{
return pageSize;
}
public void setPageSize(int pageSize)
{
this.pageSize = pageSize;
}
@End
public void reset()
{
}
@Destroy
@Remove
public void destroy()
{
}
}
1.1 date: 2007/06/23 22:04:52; author: gavin; state: Exp;jboss-seam/examples/dvdstore/src/com/jboss/dvd/seam/Indexer.java
Index: Indexer.java
===================================================================
//$Id: Indexer.java,v 1.1 2007/06/23 22:04:52 gavin Exp $
package com.jboss.dvd.seam;
import java.util.Date;
/**
* @author Emmanuel Bernard
*/
public interface Indexer
{
Date getLastIndexingTime();
void index();
void stop();
}
1.1 date: 2007/06/23 22:04:52; author: gavin; state: Exp;jboss-seam/examples/dvdstore/src/com/jboss/dvd/seam/IndexerAction.java
Index: IndexerAction.java
===================================================================
//$Id: IndexerAction.java,v 1.1 2007/06/23 22:04:52 gavin Exp $
package com.jboss.dvd.seam;
import java.util.Date;
import java.util.List;
import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.hibernate.FetchMode;
import org.hibernate.search.FullTextSession;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Create;
import org.jboss.seam.annotations.Destroy;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.annotations.Startup;
/**
* Re index the needed entities
*
* @author Emmanuel Bernard
*/
@Name("indexer")
@Stateful
@Scope(ScopeType.APPLICATION)
@Startup
public class IndexerAction implements Indexer
{
private Date lastIndexingTime;
@PersistenceContext
private EntityManager em;
public Date getLastIndexingTime()
{
return lastIndexingTime;
}
@Create
public void index()
{
indexAllClasses(Actor.class, Category.class);
indexProducts();
lastIndexingTime = new Date();
}
private void indexProducts()
{
FullTextSession fullTextSession = getFullTextSession();
List results = fullTextSession.createCriteria(Product.class)
.setFetchMode("actors", FetchMode.JOIN)
.setFetchMode("categories", FetchMode.JOIN)
.list();
for (Object obj : results)
{
fullTextSession.index(obj);
}
}
private FullTextSession getFullTextSession()
{
return (FullTextSession) em.getDelegate();
}
private void indexAllClasses(Class... entityTypes)
{
FullTextSession fullTextSession = getFullTextSession();
for (Class entityType : entityTypes)
{
for (Object obj : fullTextSession.createCriteria(entityType).list())
{
fullTextSession.index(obj);
}
}
}
@Remove
@Destroy
public void stop() {}
}
More information about the jboss-cvs-commits
mailing list