Author: epbernard
Date: 2009-11-20 09:01:40 -0500 (Fri, 20 Nov 2009)
New Revision: 18015
Added:
search/trunk/src/main/java/org/hibernate/search/cfg/CalendarBridgeMapping.java
search/trunk/src/main/java/org/hibernate/search/cfg/ContainedInMapping.java
search/trunk/src/main/java/org/hibernate/search/cfg/DateBridgeMapping.java
search/trunk/src/main/java/org/hibernate/search/cfg/FullTextFilterDefMapping.java
search/trunk/src/main/java/org/hibernate/search/cfg/IndexEmbeddedMapping.java
search/trunk/src/main/java/org/hibernate/search/cfg/IndexedMapping.java
search/trunk/src/main/java/org/hibernate/search/cfg/ProvidedIdMapping.java
search/trunk/src/test/java/org/hibernate/search/test/configuration/Item.java
search/trunk/src/test/java/org/hibernate/search/test/configuration/ProductCatalog.java
search/trunk/src/test/java/org/hibernate/search/test/configuration/ProvidedIdEntry.java
search/trunk/src/test/java/org/hibernate/search/test/configuration/SecurityFilterFactory.java
Modified:
search/trunk/src/main/java/org/hibernate/search/cfg/AnalyzerDefMapping.java
search/trunk/src/main/java/org/hibernate/search/cfg/DocumentIdMapping.java
search/trunk/src/main/java/org/hibernate/search/cfg/EntityDescriptor.java
search/trunk/src/main/java/org/hibernate/search/cfg/EntityMapping.java
search/trunk/src/main/java/org/hibernate/search/cfg/FieldBridgeMapping.java
search/trunk/src/main/java/org/hibernate/search/cfg/FieldMapping.java
search/trunk/src/main/java/org/hibernate/search/cfg/PropertyDescriptor.java
search/trunk/src/main/java/org/hibernate/search/cfg/PropertyMapping.java
search/trunk/src/main/java/org/hibernate/search/cfg/SearchMapping.java
search/trunk/src/main/java/org/hibernate/search/cfg/TokenFilterDefMapping.java
search/trunk/src/main/java/org/hibernate/search/impl/MappingModelMetadataProvider.java
search/trunk/src/test/java/org/hibernate/search/test/configuration/Address.java
search/trunk/src/test/java/org/hibernate/search/test/configuration/BlogEntry.java
search/trunk/src/test/java/org/hibernate/search/test/configuration/Country.java
search/trunk/src/test/java/org/hibernate/search/test/configuration/ProgrammaticMappingTest.java
Log:
HSEARCH-411 Get a feature complete programmatic mapping (Amin Mohammed-Coleman)
Modified: search/trunk/src/main/java/org/hibernate/search/cfg/AnalyzerDefMapping.java
===================================================================
--- search/trunk/src/main/java/org/hibernate/search/cfg/AnalyzerDefMapping.java 2009-11-20
11:21:01 UTC (rev 18014)
+++ search/trunk/src/main/java/org/hibernate/search/cfg/AnalyzerDefMapping.java 2009-11-20
14:01:40 UTC (rev 18015)
@@ -69,11 +69,8 @@
return new AnalyzerDefMapping(name, tokenizerFactory, mapping);
}
- public EntityMapping indexedClass(Class<?> entityType) {
- return new EntityMapping(entityType, null, mapping);
+ public EntityMapping entity(Class<?> entityType) {
+ return new EntityMapping(entityType,mapping);
}
- public EntityMapping indexedClass(Class<?> entityType, String indexName) {
- return new EntityMapping(entityType, indexName, mapping);
- }
}
Added: search/trunk/src/main/java/org/hibernate/search/cfg/CalendarBridgeMapping.java
===================================================================
--- search/trunk/src/main/java/org/hibernate/search/cfg/CalendarBridgeMapping.java
(rev 0)
+++
search/trunk/src/main/java/org/hibernate/search/cfg/CalendarBridgeMapping.java 2009-11-20
14:01:40 UTC (rev 18015)
@@ -0,0 +1,46 @@
+package org.hibernate.search.cfg;
+
+import java.lang.annotation.ElementType;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.solr.analysis.TokenizerFactory;
+import org.hibernate.search.SearchException;
+import org.hibernate.search.annotations.Resolution;
+
+public class CalendarBridgeMapping {
+
+ private final SearchMapping mapping;
+ private final Map<String, Object> resolution;
+ private EntityDescriptor entity;
+ private PropertyDescriptor property;
+
+ public CalendarBridgeMapping(SearchMapping mapping,EntityDescriptor
entity,PropertyDescriptor property, Resolution resolution) {
+ if (resolution == null) {
+ throw new SearchException("Resolution required in order to index calendar
property");
+ }
+ this.mapping = mapping;
+ this.resolution = new HashMap<String, Object>();
+ this.entity = entity;
+ this.property = property;
+ this.resolution.put("resolution", resolution);
+ property.setCalendarBridge(this.resolution);
+ }
+
+ public FieldMapping field() {
+ return new FieldMapping(property, entity, mapping);
+ }
+
+ public PropertyMapping property(String name, ElementType type) {
+ return new PropertyMapping(name, type, entity, mapping);
+ }
+
+ public AnalyzerDefMapping analyzerDef(String name, Class<? extends
TokenizerFactory> tokenizerFactory) {
+ return new AnalyzerDefMapping(name, tokenizerFactory, mapping);
+ }
+
+ public EntityMapping entity(Class<?> entityType) {
+ return new EntityMapping(entityType, mapping);
+ }
+
+}
Added: search/trunk/src/main/java/org/hibernate/search/cfg/ContainedInMapping.java
===================================================================
--- search/trunk/src/main/java/org/hibernate/search/cfg/ContainedInMapping.java
(rev 0)
+++ search/trunk/src/main/java/org/hibernate/search/cfg/ContainedInMapping.java 2009-11-20
14:01:40 UTC (rev 18015)
@@ -0,0 +1,39 @@
+package org.hibernate.search.cfg;
+
+import java.lang.annotation.ElementType;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.solr.analysis.TokenizerFactory;
+
+public class ContainedInMapping {
+
+ private final SearchMapping mapping;
+ private final PropertyDescriptor property;
+ private final EntityDescriptor entity;
+
+ public ContainedInMapping(SearchMapping mapping,PropertyDescriptor property,
EntityDescriptor entity) {
+ this.mapping = mapping;
+ this.property = property;
+ this.entity = entity;
+ Map<String, Object> containedIn = new HashMap<String, Object>();
+ property.setContainedIn(containedIn);
+ }
+
+ public FieldMapping field() {
+ return new FieldMapping(property, entity, mapping);
+ }
+
+ public PropertyMapping property(String name, ElementType type) {
+ return new PropertyMapping(name, type, entity, mapping);
+ }
+
+ public AnalyzerDefMapping analyzerDef(String name, Class<? extends
TokenizerFactory> tokenizerFactory) {
+ return new AnalyzerDefMapping(name, tokenizerFactory, mapping);
+ }
+
+ public EntityMapping entity(Class<?> entityType) {
+ return new EntityMapping(entityType, mapping);
+ }
+
+}
Added: search/trunk/src/main/java/org/hibernate/search/cfg/DateBridgeMapping.java
===================================================================
--- search/trunk/src/main/java/org/hibernate/search/cfg/DateBridgeMapping.java
(rev 0)
+++ search/trunk/src/main/java/org/hibernate/search/cfg/DateBridgeMapping.java 2009-11-20
14:01:40 UTC (rev 18015)
@@ -0,0 +1,47 @@
+package org.hibernate.search.cfg;
+
+import java.lang.annotation.ElementType;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.solr.analysis.TokenizerFactory;
+import org.hibernate.search.SearchException;
+import org.hibernate.search.annotations.Resolution;
+
+public class DateBridgeMapping {
+
+ private final SearchMapping mapping;
+ private final Map<String, Object> resolution;
+ private EntityDescriptor entity;
+ private PropertyDescriptor property;
+
+ public DateBridgeMapping(SearchMapping mapping,EntityDescriptor
entity,PropertyDescriptor property, Resolution resolution) {
+ if (resolution == null) {
+ throw new SearchException("Resolution required in order to index calendar
property");
+ }
+ this.mapping = mapping;
+ this.resolution = new HashMap<String, Object>();
+ this.entity = entity;
+ this.property = property;
+ this.resolution.put("resolution", resolution);
+ property.setDateBridge(this.resolution);
+ }
+
+
+ public FieldMapping field() {
+ return new FieldMapping(property, entity, mapping);
+ }
+
+ public PropertyMapping property(String name, ElementType type) {
+ return new PropertyMapping(name, type, entity, mapping);
+ }
+
+ public AnalyzerDefMapping analyzerDef(String name, Class<? extends
TokenizerFactory> tokenizerFactory) {
+ return new AnalyzerDefMapping(name, tokenizerFactory, mapping);
+ }
+
+ public EntityMapping entity(Class<?> entityType) {
+ return new EntityMapping(entityType, mapping);
+ }
+
+}
Modified: search/trunk/src/main/java/org/hibernate/search/cfg/DocumentIdMapping.java
===================================================================
--- search/trunk/src/main/java/org/hibernate/search/cfg/DocumentIdMapping.java 2009-11-20
11:21:01 UTC (rev 18014)
+++ search/trunk/src/main/java/org/hibernate/search/cfg/DocumentIdMapping.java 2009-11-20
14:01:40 UTC (rev 18015)
@@ -63,12 +63,8 @@
return new AnalyzerDefMapping(name, tokenizerFactory, mapping);
}
- public EntityMapping indexedClass(Class<?> entityType) {
- return new EntityMapping(entityType, null, mapping);
+ public EntityMapping entity(Class<?> entityType) {
+ return new EntityMapping(entityType, mapping);
}
- public EntityMapping indexedClass(Class<?> entityType, String indexName) {
- return new EntityMapping(entityType, indexName, mapping);
- }
-
}
Modified: search/trunk/src/main/java/org/hibernate/search/cfg/EntityDescriptor.java
===================================================================
--- search/trunk/src/main/java/org/hibernate/search/cfg/EntityDescriptor.java 2009-11-20
11:21:01 UTC (rev 18014)
+++ search/trunk/src/main/java/org/hibernate/search/cfg/EntityDescriptor.java 2009-11-20
14:01:40 UTC (rev 18015)
@@ -26,7 +26,9 @@
import java.lang.annotation.ElementType;
import java.util.HashMap;
+import java.util.HashSet;
import java.util.Map;
+import java.util.Set;
/**
* @author Emmanuel Bernard
@@ -38,7 +40,10 @@
private Map<String, Object> similarity;
private Map<String, Object> boost;
private Map<String, Object> analyzerDiscriminator;
-
+ private Set<Map<String, Object>> fullTextFilterDefs = new
HashSet<Map<String, Object>>();
+ private Map<String,Object> providedId;
+
+
public Map<String, Object> getIndexed() {
return indexed;
}
@@ -88,7 +93,24 @@
public Map<String, Object> getAnalyzerDiscriminator() {
return analyzerDiscriminator;
}
+
+ public Set<Map<String, Object>> getFullTextFilterDefs() {
+ return fullTextFilterDefs;
+ }
+
+ public void addFulltextFilterDef(Map<String, Object> fullTextFilterDef) {
+ fullTextFilterDefs.add(fullTextFilterDef);
+ }
+
+ public void setProvidedId(Map<String, Object> providedId) {
+ this.providedId = providedId;
+ }
+
+ public Map<String, Object> getProvidedId() {
+ return this.providedId;
+ }
+
private static class PropertyKey {
private String name;
private ElementType type;
@@ -126,4 +148,5 @@
return result;
}
}
+
}
Modified: search/trunk/src/main/java/org/hibernate/search/cfg/EntityMapping.java
===================================================================
--- search/trunk/src/main/java/org/hibernate/search/cfg/EntityMapping.java 2009-11-20
11:21:01 UTC (rev 18014)
+++ search/trunk/src/main/java/org/hibernate/search/cfg/EntityMapping.java 2009-11-20
14:01:40 UTC (rev 18015)
@@ -29,7 +29,6 @@
import java.util.Map;
import org.apache.solr.analysis.TokenizerFactory;
-
import org.hibernate.search.analyzer.Discriminator;
/**
@@ -38,15 +37,16 @@
public class EntityMapping {
private SearchMapping mapping;
private EntityDescriptor entity;
-
- public EntityMapping(Class<?> entityType, String name, SearchMapping mapping) {
+
+ public EntityMapping(Class<?> entityType, SearchMapping mapping) {
this.mapping = mapping;
entity = mapping.getEntity(entityType);
- Map<String, Object> indexed = new HashMap<String, Object>();
- if (name != null) indexed.put( "index", name );
- entity.setIndexed(indexed);
}
-
+
+ public IndexedMapping indexed() {
+ return new IndexedMapping(mapping,entity, this);
+ }
+
public EntityMapping similarity(Class<?> impl) {
Map<String, Object> similarity = new HashMap<String, Object>(1);
similarity.put( "impl", impl );
@@ -67,7 +67,12 @@
entity.setAnalyzerDiscriminator(discriminatorAnn);
return this;
}
-
+
+
+ public FullTextFilterDefMapping fullTextFilterDef(String name, Class<?> impl) {
+ return new FullTextFilterDefMapping(mapping, entity, name, impl);
+ }
+
public PropertyMapping property(String name, ElementType type) {
return new PropertyMapping(name, type, entity, mapping);
}
@@ -76,11 +81,12 @@
return new AnalyzerDefMapping(name, tokenizerFactory, mapping);
}
- public EntityMapping indexedClass(Class<?> entityType) {
- return new EntityMapping(entityType, null, mapping);
+ public EntityMapping entity(Class<?> entityType) {
+ return new EntityMapping(entityType, mapping);
}
- public EntityMapping indexedClass(Class<?> entityType, String indexName) {
- return new EntityMapping(entityType, indexName, mapping);
+ public ProvidedIdMapping providedId() {
+ return new ProvidedIdMapping(mapping,entity);
}
+
}
Modified: search/trunk/src/main/java/org/hibernate/search/cfg/FieldBridgeMapping.java
===================================================================
--- search/trunk/src/main/java/org/hibernate/search/cfg/FieldBridgeMapping.java 2009-11-20
11:21:01 UTC (rev 18014)
+++ search/trunk/src/main/java/org/hibernate/search/cfg/FieldBridgeMapping.java 2009-11-20
14:01:40 UTC (rev 18015)
@@ -108,11 +108,8 @@
return new AnalyzerDefMapping(name, tokenizerFactory, mapping);
}
- public EntityMapping indexedClass(Class<?> entityType) {
- return new EntityMapping(entityType, null, mapping);
+ public EntityMapping entity(Class<?> entityType) {
+ return new EntityMapping(entityType, mapping);
}
- public EntityMapping indexedClass(Class<?> entityType, String indexName) {
- return new EntityMapping(entityType, indexName, mapping);
- }
}
Modified: search/trunk/src/main/java/org/hibernate/search/cfg/FieldMapping.java
===================================================================
--- search/trunk/src/main/java/org/hibernate/search/cfg/FieldMapping.java 2009-11-20
11:21:01 UTC (rev 18014)
+++ search/trunk/src/main/java/org/hibernate/search/cfg/FieldMapping.java 2009-11-20
14:01:40 UTC (rev 18015)
@@ -31,6 +31,7 @@
import org.apache.solr.analysis.TokenizerFactory;
import org.hibernate.search.annotations.Index;
+import org.hibernate.search.annotations.Resolution;
import org.hibernate.search.annotations.Store;
import org.hibernate.search.annotations.TermVector;
@@ -103,16 +104,20 @@
return new PropertyMapping(name, type, entity, mapping);
}
+ public DateBridgeMapping dateBridge(Resolution resolution) {
+ return new DateBridgeMapping(mapping, entity, property, resolution);
+ }
+
public AnalyzerDefMapping analyzerDef(String name, Class<? extends
TokenizerFactory> tokenizerFactory) {
return new AnalyzerDefMapping(name, tokenizerFactory, mapping);
}
- public EntityMapping indexedClass(Class<?> entityType) {
- return new EntityMapping(entityType, null, mapping);
+ public EntityMapping entity(Class<?> entityType) {
+ return new EntityMapping(entityType, mapping);
}
- public EntityMapping indexedClass(Class<?> entityType, String indexName) {
- return new EntityMapping(entityType, indexName, mapping);
+ public CalendarBridgeMapping calendarBridge(Resolution resolution) {
+ return new CalendarBridgeMapping(mapping,entity,property, resolution);
}
}
Added: search/trunk/src/main/java/org/hibernate/search/cfg/FullTextFilterDefMapping.java
===================================================================
--- search/trunk/src/main/java/org/hibernate/search/cfg/FullTextFilterDefMapping.java
(rev 0)
+++
search/trunk/src/main/java/org/hibernate/search/cfg/FullTextFilterDefMapping.java 2009-11-20
14:01:40 UTC (rev 18015)
@@ -0,0 +1,60 @@
+package org.hibernate.search.cfg;
+
+import java.lang.annotation.ElementType;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.solr.analysis.TokenizerFactory;
+import org.hibernate.search.annotations.FilterCacheModeType;
+
+/**
+ * Mapping class responsible for collecting data for constructing
+ * FullTextFilterDef annotation.
+ */
+public class FullTextFilterDefMapping {
+
+ private final SearchMapping mapping;
+ private final Map<String,Object> fullTextFilterDef;
+ private final EntityDescriptor entity;
+
+ public FullTextFilterDefMapping(SearchMapping mapping, EntityDescriptor entity, String
name, Class<?> impl) {
+ this.mapping = mapping;
+ this.entity = entity;
+ this.fullTextFilterDef =new HashMap<String, Object>();
+ this.fullTextFilterDef.put("name", name);
+ this.fullTextFilterDef.put("impl", impl);
+ entity.addFulltextFilterDef(fullTextFilterDef);
+ }
+
+ /**
+ * Add cache implementation for fulltextfilterdef mapping
+ * @param cache
+ * @return FullTextFilterDefMapping
+ */
+ public FullTextFilterDefMapping cache(FilterCacheModeType cache) {
+ this.fullTextFilterDef.put("cache", cache);
+ return this;
+ }
+
+ public FullTextFilterDefMapping fullTextFilterDef(String name, Class<?> impl) {
+ return new FullTextFilterDefMapping(mapping,entity,name, impl);
+ }
+
+ public PropertyMapping property(String name, ElementType type) {
+ return new PropertyMapping(name, type, entity, mapping);
+ }
+
+ public AnalyzerDefMapping analyzerDef(String name, Class<? extends
TokenizerFactory> tokenizerFactory) {
+ return new AnalyzerDefMapping(name, tokenizerFactory, mapping);
+ }
+
+ public EntityMapping entity(Class<?> entityType) {
+ return new EntityMapping(entityType, mapping);
+ }
+
+ public ProvidedIdMapping providedId() {
+ return new ProvidedIdMapping(mapping,entity);
+ }
+
+
+}
Added: search/trunk/src/main/java/org/hibernate/search/cfg/IndexEmbeddedMapping.java
===================================================================
--- search/trunk/src/main/java/org/hibernate/search/cfg/IndexEmbeddedMapping.java
(rev 0)
+++
search/trunk/src/main/java/org/hibernate/search/cfg/IndexEmbeddedMapping.java 2009-11-20
14:01:40 UTC (rev 18015)
@@ -0,0 +1,56 @@
+package org.hibernate.search.cfg;
+
+import java.lang.annotation.ElementType;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.solr.analysis.TokenizerFactory;
+
+public class IndexEmbeddedMapping {
+
+ private final SearchMapping mapping;
+ private final Map<String,Object> indexEmbedded;
+ private EntityDescriptor entity;
+ private PropertyDescriptor property;
+
+
+ public IndexEmbeddedMapping(SearchMapping mapping, PropertyDescriptor property,
EntityDescriptor entity) {
+ this.mapping = mapping;
+ this.indexEmbedded = new HashMap<String, Object>();
+ this.property = property;
+ this.entity = entity;
+ this.property.setIndexEmbedded(indexEmbedded);
+ }
+
+ public IndexEmbeddedMapping prefix(String prefix) {
+ this.indexEmbedded.put("prefix",prefix);
+ return this;
+ }
+
+ public IndexEmbeddedMapping targetElement(Class<?> targetElement) {
+ this.indexEmbedded.put("targetElement",targetElement);
+ return this;
+ }
+
+ public IndexEmbeddedMapping depth(int depth) {
+ this.indexEmbedded.put("depth", depth);
+ return this;
+ }
+
+ public PropertyMapping property(String name, ElementType type) {
+ return new PropertyMapping(name, type, entity, mapping);
+ }
+
+ public AnalyzerDefMapping analyzerDef(String name, Class<? extends
TokenizerFactory> tokenizerFactory) {
+ return new AnalyzerDefMapping(name, tokenizerFactory, mapping);
+ }
+
+ public EntityMapping entity(Class<?> entityType) {
+ return new EntityMapping(entityType, mapping);
+ }
+
+ public FieldMapping field() {
+ return new FieldMapping(property, entity, mapping);
+ }
+
+}
Added: search/trunk/src/main/java/org/hibernate/search/cfg/IndexedMapping.java
===================================================================
--- search/trunk/src/main/java/org/hibernate/search/cfg/IndexedMapping.java
(rev 0)
+++ search/trunk/src/main/java/org/hibernate/search/cfg/IndexedMapping.java 2009-11-20
14:01:40 UTC (rev 18015)
@@ -0,0 +1,72 @@
+package org.hibernate.search.cfg;
+
+import java.lang.annotation.ElementType;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.solr.analysis.TokenizerFactory;
+import org.hibernate.search.analyzer.Discriminator;
+
+public class IndexedMapping {
+
+ private final SearchMapping mapping;
+ private final EntityDescriptor entity;
+ private final Map<String, Object> indexed;
+ private final EntityMapping entityMapping;
+
+ public IndexedMapping(SearchMapping mapping, EntityDescriptor entity, EntityMapping
entityMapping) {
+ this.entityMapping = entityMapping;
+ this.mapping = mapping;
+ this.entity = entity;
+ indexed = new HashMap<String, Object>();
+ entity.setIndexed(indexed);
+ }
+
+ public EntityMapping indexName(String indexName) {
+ this.indexed.put("index", indexName);
+ return entityMapping;
+ }
+
+ public IndexedMapping similarity(Class<?> impl) {
+ Map<String, Object> similarity = new HashMap<String, Object>(1);
+ similarity.put( "impl", impl );
+ entity.setSimilariy(similarity);
+ return this;
+ }
+
+ public IndexedMapping boost(float boost) {
+ final Map<String, Object> boostAnn = new HashMap<String, Object>();
+ boostAnn.put( "value", boost );
+ entity.setBoost(boostAnn);
+ return this;
+ }
+
+ public IndexedMapping analyzerDiscriminator(Class<? extends Discriminator>
discriminator) {
+ final Map<String, Object> discriminatorAnn = new HashMap<String,
Object>();
+ discriminatorAnn.put( "impl", discriminator );
+ entity.setAnalyzerDiscriminator(discriminatorAnn);
+ return this;
+ }
+
+
+ public FullTextFilterDefMapping fullTextFilterDef(String name, Class<?> impl) {
+ return new FullTextFilterDefMapping(mapping, entity, name, impl);
+ }
+
+ public PropertyMapping property(String name, ElementType type) {
+ return new PropertyMapping(name, type, entity, mapping);
+ }
+
+ public AnalyzerDefMapping analyzerDef(String name, Class<? extends
TokenizerFactory> tokenizerFactory) {
+ return new AnalyzerDefMapping(name, tokenizerFactory, mapping);
+ }
+
+ public EntityMapping entity(Class<?> entityType) {
+ return new EntityMapping(entityType, mapping);
+ }
+
+ public ProvidedIdMapping providedId() {
+ return new ProvidedIdMapping(mapping,entity);
+ }
+
+}
Modified: search/trunk/src/main/java/org/hibernate/search/cfg/PropertyDescriptor.java
===================================================================
--- search/trunk/src/main/java/org/hibernate/search/cfg/PropertyDescriptor.java 2009-11-20
11:21:01 UTC (rev 18014)
+++ search/trunk/src/main/java/org/hibernate/search/cfg/PropertyDescriptor.java 2009-11-20
14:01:40 UTC (rev 18015)
@@ -25,9 +25,10 @@
package org.hibernate.search.cfg;
import java.lang.annotation.ElementType;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
import java.util.Map;
-import java.util.Collection;
-import java.util.ArrayList;
/**
* @author Emmanuel Bernard
@@ -36,6 +37,11 @@
private ElementType type;
private String name;
private Collection<Map<String, Object>> fields = new
ArrayList<Map<String, Object>>();
+ private Map<String, Object> dateBridge= new HashMap<String, Object>();
+ private Map<String, Object> calendarBridge= new HashMap<String, Object>();
+ private Map<String,Object> indexEmbedded;
+ private Map<String,Object> containedIn;
+
private Map<String, Object> documentId;
private Map<String, Object> analyzerDiscriminator;
@@ -52,6 +58,13 @@
fields.add( field );
}
+ public void setDateBridge(Map<String,Object> dateBridge) {
+ this.dateBridge = dateBridge;
+ }
+ public void setCalendarBridge(Map<String,Object> calendarBridge) {
+ this.calendarBridge = calendarBridge;
+ }
+
public Collection<Map<String, Object>> getFields() {
return fields;
}
@@ -64,7 +77,32 @@
return analyzerDiscriminator;
}
+
+ public Map<String, Object> getDateBridge() {
+ return dateBridge;
+ }
+ public Map<String, Object> getCalendarBridge() {
+ return calendarBridge;
+ }
+
+
public void setAnalyzerDiscriminator(Map<String, Object> analyzerDiscriminator) {
this.analyzerDiscriminator = analyzerDiscriminator;
}
+
+ public Map<String, Object> getIndexEmbedded() {
+ return indexEmbedded;
+ }
+
+ public void setIndexEmbedded(Map<String, Object> indexEmbedded) {
+ this.indexEmbedded = indexEmbedded;
+ }
+ public Map<String, Object> getContainedIn() {
+ return containedIn;
+ }
+
+ public void setContainedIn(Map<String, Object> containedIn) {
+ this.containedIn = containedIn;
+ }
+
}
Modified: search/trunk/src/main/java/org/hibernate/search/cfg/PropertyMapping.java
===================================================================
--- search/trunk/src/main/java/org/hibernate/search/cfg/PropertyMapping.java 2009-11-20
11:21:01 UTC (rev 18014)
+++ search/trunk/src/main/java/org/hibernate/search/cfg/PropertyMapping.java 2009-11-20
14:01:40 UTC (rev 18015)
@@ -31,6 +31,7 @@
import org.apache.solr.analysis.TokenizerFactory;
import org.hibernate.search.analyzer.Discriminator;
+import org.hibernate.search.annotations.Resolution;
/**
* @author Emmanuel Bernard
@@ -54,6 +55,14 @@
return new FieldMapping(property, entity, mapping);
}
+ public DateBridgeMapping dateBridge(Resolution resolution) {
+ return new DateBridgeMapping(mapping, entity, property, resolution);
+ }
+
+ public CalendarBridgeMapping calendarBridge(Resolution resolution) {
+ return new CalendarBridgeMapping(mapping, entity, property, resolution);
+ }
+
public PropertyMapping analyzerDiscriminator(Class<? extends Discriminator>
discriminator) {
Map<String, Object> analyzerDiscriminatorAnn = new HashMap<String,
Object>();
analyzerDiscriminatorAnn.put( "impl", discriminator );
@@ -69,11 +78,15 @@
return new AnalyzerDefMapping(name, tokenizerFactory, mapping);
}
- public EntityMapping indexedClass(Class<?> entityType) {
- return new EntityMapping(entityType, null, mapping);
+ public EntityMapping entity(Class<?> entityType) {
+ return new EntityMapping(entityType, mapping);
}
- public EntityMapping indexedClass(Class<?> entityType, String indexName) {
- return new EntityMapping(entityType, indexName, mapping);
+ public IndexEmbeddedMapping indexEmbedded() {
+ return new IndexEmbeddedMapping(mapping,property,entity);
}
+
+ public ContainedInMapping containedIn() {
+ return new ContainedInMapping(mapping, property, entity);
+ }
}
Added: search/trunk/src/main/java/org/hibernate/search/cfg/ProvidedIdMapping.java
===================================================================
--- search/trunk/src/main/java/org/hibernate/search/cfg/ProvidedIdMapping.java
(rev 0)
+++ search/trunk/src/main/java/org/hibernate/search/cfg/ProvidedIdMapping.java 2009-11-20
14:01:40 UTC (rev 18015)
@@ -0,0 +1,47 @@
+package org.hibernate.search.cfg;
+
+import java.lang.annotation.ElementType;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.solr.analysis.TokenizerFactory;
+
+public class ProvidedIdMapping {
+
+ private final SearchMapping searchMapping;
+ private final Map<String,Object> providedIdMapping;
+ private EntityDescriptor entity;
+
+ public ProvidedIdMapping(SearchMapping searchMapping, EntityDescriptor entity) {
+ this.searchMapping = searchMapping;
+ this.entity =entity;
+ providedIdMapping = new HashMap<String,Object>();
+ entity.setProvidedId(providedIdMapping);
+ }
+
+ public ProvidedIdMapping name(String name) {
+ this.providedIdMapping.put("name", name);
+ return this;
+ }
+
+ public FieldBridgeMapping bridge(Class<?> impl) {
+ return new FieldBridgeMapping( impl, providedIdMapping, null, null, entity,
searchMapping );
+ }
+
+ public FullTextFilterDefMapping fullTextFilterDef(String name, Class<?> impl) {
+ return new FullTextFilterDefMapping(searchMapping, entity, name, impl);
+ }
+
+ public PropertyMapping property(String name, ElementType type) {
+ return new PropertyMapping(name, type, entity, searchMapping);
+ }
+
+ public AnalyzerDefMapping analyzerDef(String name, Class<? extends
TokenizerFactory> tokenizerFactory) {
+ return new AnalyzerDefMapping(name, tokenizerFactory, searchMapping);
+ }
+
+ public EntityMapping entity(Class<?> entityType) {
+ return new EntityMapping(entityType, searchMapping);
+ }
+
+}
Modified: search/trunk/src/main/java/org/hibernate/search/cfg/SearchMapping.java
===================================================================
--- search/trunk/src/main/java/org/hibernate/search/cfg/SearchMapping.java 2009-11-20
11:21:01 UTC (rev 18014)
+++ search/trunk/src/main/java/org/hibernate/search/cfg/SearchMapping.java 2009-11-20
14:01:40 UTC (rev 18015)
@@ -43,7 +43,7 @@
public Set<Map<String, Object>> getAnalyzerDefs() {
return analyzerDefs;
}
-
+
public EntityDescriptor getEntityDescriptor(Class<?> entityType) {
return entities.get( entityType );
}
@@ -52,14 +52,10 @@
return new AnalyzerDefMapping(name, tokenizerFactory, this);
}
- public EntityMapping indexedClass(Class<?> entityType) {
- return new EntityMapping(entityType, null, this);
+ public EntityMapping entity(Class<?> entityType) {
+ return new EntityMapping(entityType, this);
}
- public EntityMapping indexedClass(Class<?> entityType, String indexName) {
- return new EntityMapping(entityType, indexName, this);
- }
-
/**
* eg @Containing(things={@Thing(...), @Thing(...)}
* Map<String, Object> addedThing = addElementToAnnotationArray(containing,
"things");
@@ -88,4 +84,5 @@
}
return entity;
}
+
}
Modified: search/trunk/src/main/java/org/hibernate/search/cfg/TokenFilterDefMapping.java
===================================================================
---
search/trunk/src/main/java/org/hibernate/search/cfg/TokenFilterDefMapping.java 2009-11-20
11:21:01 UTC (rev 18014)
+++
search/trunk/src/main/java/org/hibernate/search/cfg/TokenFilterDefMapping.java 2009-11-20
14:01:40 UTC (rev 18015)
@@ -61,14 +61,10 @@
return new TokenFilterDefMapping(factory, analyzerDef, mapping );
}
- public EntityMapping indexedClass(Class<?> entityType) {
- return new EntityMapping(entityType, null, mapping);
+ public EntityMapping entity(Class<?> entityType) {
+ return new EntityMapping(entityType, mapping);
}
- public EntityMapping indexedClass(Class<?> entityType, String indexName) {
- return new EntityMapping(entityType, indexName, mapping);
- }
-
public AnalyzerDefMapping analyzerDef(String name, Class<? extends
TokenizerFactory> tokenizerFactory) {
return new AnalyzerDefMapping(name, tokenizerFactory, mapping);
}
Modified:
search/trunk/src/main/java/org/hibernate/search/impl/MappingModelMetadataProvider.java
===================================================================
---
search/trunk/src/main/java/org/hibernate/search/impl/MappingModelMetadataProvider.java 2009-11-20
11:21:01 UTC (rev 18014)
+++
search/trunk/src/main/java/org/hibernate/search/impl/MappingModelMetadataProvider.java 2009-11-20
14:01:40 UTC (rev 18015)
@@ -24,41 +24,50 @@
*/
package org.hibernate.search.impl;
-import java.util.Map;
-import java.util.List;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.Collection;
+import java.beans.Introspector;
+import java.lang.annotation.Annotation;
+import java.lang.annotation.ElementType;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
-import java.lang.annotation.Annotation;
-import java.lang.annotation.ElementType;
-import java.beans.Introspector;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.Map.Entry;
+import org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor;
+import org.hibernate.annotations.common.annotationfactory.AnnotationFactory;
+import org.hibernate.annotations.common.reflection.AnnotationReader;
+import org.hibernate.annotations.common.reflection.Filter;
import org.hibernate.annotations.common.reflection.MetadataProvider;
-import org.hibernate.annotations.common.reflection.AnnotationReader;
import org.hibernate.annotations.common.reflection.ReflectionUtil;
-import org.hibernate.annotations.common.reflection.Filter;
-import org.hibernate.annotations.common.annotationfactory.AnnotationFactory;
-import org.hibernate.annotations.common.annotationfactory.AnnotationDescriptor;
-import org.hibernate.search.cfg.SearchMapping;
-import org.hibernate.search.cfg.EntityDescriptor;
-import org.hibernate.search.cfg.PropertyDescriptor;
import org.hibernate.search.SearchException;
-import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.Analyzer;
-import org.hibernate.search.annotations.Fields;
import org.hibernate.search.annotations.AnalyzerDef;
-import org.hibernate.search.annotations.TokenizerDef;
-import org.hibernate.search.annotations.Parameter;
-import org.hibernate.search.annotations.TokenFilterDef;
import org.hibernate.search.annotations.AnalyzerDefs;
+import org.hibernate.search.annotations.AnalyzerDiscriminator;
import org.hibernate.search.annotations.Boost;
+import org.hibernate.search.annotations.CalendarBridge;
+import org.hibernate.search.annotations.ContainedIn;
+import org.hibernate.search.annotations.DateBridge;
+import org.hibernate.search.annotations.DocumentId;
import org.hibernate.search.annotations.FieldBridge;
+import org.hibernate.search.annotations.Fields;
+import org.hibernate.search.annotations.FullTextFilterDef;
+import org.hibernate.search.annotations.FullTextFilterDefs;
+import org.hibernate.search.annotations.Indexed;
+import org.hibernate.search.annotations.IndexedEmbedded;
+import org.hibernate.search.annotations.Parameter;
+import org.hibernate.search.annotations.ProvidedId;
import org.hibernate.search.annotations.Similarity;
-import org.hibernate.search.annotations.DocumentId;
-import org.hibernate.search.annotations.AnalyzerDiscriminator;
+import org.hibernate.search.annotations.TokenFilterDef;
+import org.hibernate.search.annotations.TokenizerDef;
+import org.hibernate.search.cfg.EntityDescriptor;
+import org.hibernate.search.cfg.PropertyDescriptor;
+import org.hibernate.search.cfg.SearchMapping;
/**
* @author Emmanuel Bernard
@@ -114,7 +123,8 @@
}
return defs;
}
-
+
+
private AnalyzerDef createAnalyzerDef(Map<String, Object> analyzerDef) {
AnnotationDescriptor analyzerDefAnnotation = new AnnotationDescriptor(
AnalyzerDef.class );
for ( Map.Entry<String, Object> entry : analyzerDef.entrySet() ) {
@@ -133,7 +143,7 @@
analyzerDefAnnotation.setValue( "tokenizer", AnnotationFactory.create(
tokenizerAnnotation ) );
}
else if ( entry.getKey().equals( "filters" ) ) {
- TokenFilterDef[] filtersArray = createFilters( (List<Map<String,
Object>>) entry.getValue() );
+ @SuppressWarnings("unchecked") TokenFilterDef[] filtersArray =
createFilters( (List<Map<String, Object>>) entry.getValue() );
analyzerDefAnnotation.setValue( "filters", filtersArray );
}
else {
@@ -144,7 +154,7 @@
}
static private void addParamsToAnnotation(AnnotationDescriptor annotationDescriptor,
Map.Entry<String, Object> entry) {
- Parameter[] paramsArray = createParams( ( List<Map<String, Object>> )
entry.getValue() );
+ @SuppressWarnings("unchecked") Parameter[] paramsArray = createParams( (
List<Map<String, Object>> ) entry.getValue() );
annotationDescriptor.setValue( "params", paramsArray );
}
@@ -193,7 +203,7 @@
this.delegate = delegate.getAnnotationReader( el );
this.mapping = mapping;
if ( el instanceof Class ) {
- entityType = (Class) el;
+ entityType = (Class<?>) el;
}
else if ( el instanceof Field ) {
Field field = (Field) el;
@@ -254,6 +264,9 @@
createDocumentId( property );
createAnalyzerDiscriminator( property );
createFields( property );
+ createIndexEmbedded(property);
+ createContainedIn(property);
+
}
}
}
@@ -266,6 +279,24 @@
}
}
+ private void createDateBridge(PropertyDescriptor property) {
+ Map<String, Object> map = property.getDateBridge();
+ for(Map.Entry<String, Object> entry: map.entrySet()) {
+ AnnotationDescriptor dateBrigeAnnotation = new AnnotationDescriptor(
DateBridge.class );
+ dateBrigeAnnotation.setValue(entry.getKey(), entry.getValue());
+ annotations.put( DateBridge.class, AnnotationFactory.create( dateBrigeAnnotation )
);
+ }
+ }
+
+ private void createCalendarBridge(PropertyDescriptor property) {
+ Map<String, Object> map = property.getCalendarBridge();
+ for(Map.Entry<String, Object> entry: map.entrySet()) {
+ AnnotationDescriptor calendarBrigeAnnotation = new AnnotationDescriptor(
CalendarBridge.class );
+ calendarBrigeAnnotation.setValue(entry.getKey(), entry.getValue());
+ annotations.put( CalendarBridge.class, AnnotationFactory.create(
calendarBrigeAnnotation ) );
+ }
+ }
+
private void createDocumentId(PropertyDescriptor property) {
Map<String, Object> documentId = property.getDocumentId();
if (documentId != null) {
@@ -276,7 +307,7 @@
annotations.put( DocumentId.class, AnnotationFactory.create( documentIdAnnotation )
);
}
}
-
+
private void createAnalyzerDiscriminator(PropertyDescriptor property) {
Map<String, Object> analyzerDiscriminator =
property.getAnalyzerDiscriminator();
if (analyzerDiscriminator != null) {
@@ -340,15 +371,45 @@
final org.hibernate.search.annotations.Field[] fieldAsArray =
fieldAnnotations.toArray( fieldArray );
fieldsAnnotation.setValue( "value", fieldAsArray );
annotations.put( Fields.class, AnnotationFactory.create( fieldsAnnotation ) );
+ createDateBridge(property);
+ createCalendarBridge(property);
}
+
+
+ private void createContainedIn(PropertyDescriptor property) {
+ if (property.getContainedIn() != null) {
+ Map<String, Object> containedIn = property.getContainedIn();
+ AnnotationDescriptor containedInAnn = new AnnotationDescriptor( ContainedIn.class );
+ Set<Entry<String,Object>> entrySet = containedIn.entrySet();
+ for (Entry<String, Object> entry : entrySet) {
+ containedInAnn.setValue(entry.getKey(), entry.getValue());
+ }
+ annotations.put(ContainedIn.class,AnnotationFactory.create(containedInAnn));
+ }
+ }
+
+ private void createIndexEmbedded(PropertyDescriptor property) {
+ Map<String, Object> indexEmbedded = property.getIndexEmbedded();
+ if (indexEmbedded != null) {
+ AnnotationDescriptor indexEmbeddedAnn = new
AnnotationDescriptor(IndexedEmbedded.class);
+ Set<Entry<String,Object>> entrySet = indexEmbedded.entrySet();
+ for (Entry<String, Object> entry : entrySet) {
+ indexEmbeddedAnn.setValue(entry.getKey(), entry.getValue());
+ }
+ annotations.put(IndexedEmbedded.class, AnnotationFactory.create(indexEmbeddedAnn));
+ }
+ }
+
private void createIndexed(EntityDescriptor entity) {
Class<? extends Annotation> annotationType = Indexed.class;
AnnotationDescriptor annotation = new AnnotationDescriptor( annotationType );
- for ( Map.Entry<String, Object> entry : entity.getIndexed().entrySet() ) {
- annotation.setValue( entry.getKey(), entry.getValue() );
+ if (entity.getIndexed() != null) {
+ for ( Map.Entry<String, Object> entry : entity.getIndexed().entrySet() ) {
+ annotation.setValue( entry.getKey(), entry.getValue() );
+ }
+ annotations.put( annotationType, AnnotationFactory.create( annotation ) );
}
- annotations.put( annotationType, AnnotationFactory.create( annotation ) );
if ( entity.getSimilarity() != null ) {
annotation = new AnnotationDescriptor( Similarity.class );
@@ -373,8 +434,62 @@
}
annotations.put( AnalyzerDiscriminator.class, AnnotationFactory.create( annotation )
);
}
+ if (entity.getFullTextFilterDefs().size() > 0) {
+ AnnotationDescriptor fullTextFilterDefsAnnotation = new AnnotationDescriptor(
FullTextFilterDefs.class );
+ FullTextFilterDef[] fullTextFilterDefArray = createFullTextFilterDefArray(entity);
+ fullTextFilterDefsAnnotation.setValue("value", fullTextFilterDefArray);
+ annotations.put( FullTextFilterDefs.class, AnnotationFactory.create(
fullTextFilterDefsAnnotation ) );
+ }
+ if (entity.getProvidedId() != null) {
+ createProvidedId(entity);
+ }
}
+ private void createProvidedId(EntityDescriptor entity) {
+ AnnotationDescriptor annotation = new AnnotationDescriptor( ProvidedId.class );
+ Set<Entry<String,Object>> entrySet = entity.getProvidedId().entrySet();
+ for (Entry<String, Object> entry : entrySet) {
+ if (entry.getKey().equals("bridge")) {
+ AnnotationDescriptor bridgeAnnotation = new AnnotationDescriptor( FieldBridge.class
);
+ @SuppressWarnings("unchecked")
+ Map<String, Object> bridge = (Map<String, Object>) entry.getValue();
+ for( Map.Entry<String, Object> bridgeEntry : bridge.entrySet() ) {
+ if ( bridgeEntry.getKey().equals( "params" ) ) {
+ addParamsToAnnotation( bridgeAnnotation, bridgeEntry );
+ }
+ else {
+ bridgeAnnotation.setValue( bridgeEntry.getKey(), bridgeEntry.getValue() );
+ }
+ }
+ annotation.setValue( "bridge", AnnotationFactory.create( bridgeAnnotation
) );
+ } else {
+ annotation.setValue(entry.getKey(), entry.getValue());
+ }
+ }
+ annotations.put( ProvidedId.class, AnnotationFactory.create( annotation ) );
+ }
+
+
+ private FullTextFilterDef[] createFullTextFilterDefArray(EntityDescriptor entity) {
+ Set<Map<String, Object>> fullTextFilterDefs =
entity.getFullTextFilterDefs();
+ FullTextFilterDef[] filters = new FullTextFilterDef[fullTextFilterDefs.size()];
+ int index = 0;
+ for(Map<String,Object> filterDef : fullTextFilterDefs) {
+ filters[index] = createFullTextFilterDef(filterDef);
+ index++;
+ }
+ return filters;
+ }
+
+ private FullTextFilterDef createFullTextFilterDef(Map<String,Object> filterDef)
{
+ AnnotationDescriptor fullTextFilterDefAnnotation = new AnnotationDescriptor(
FullTextFilterDef.class );
+ for (Entry<String, Object> entry : filterDef.entrySet()) {
+ fullTextFilterDefAnnotation.setValue(entry.getKey(), entry.getValue());
+ }
+
+ return AnnotationFactory.create( fullTextFilterDefAnnotation );
+ }
+
private void populateAnnotationArray() {
annotationsArray = new Annotation[ annotations.size() ];
int index = 0;
Modified: search/trunk/src/test/java/org/hibernate/search/test/configuration/Address.java
===================================================================
---
search/trunk/src/test/java/org/hibernate/search/test/configuration/Address.java 2009-11-20
11:21:01 UTC (rev 18014)
+++
search/trunk/src/test/java/org/hibernate/search/test/configuration/Address.java 2009-11-20
14:01:40 UTC (rev 18015)
@@ -24,11 +24,16 @@
*/
package org.hibernate.search.test.configuration;
+import java.util.Calendar;
+import java.util.Date;
+
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
+import org.hibernate.search.test.embedded.nested.Place;
+
/**
* @author Emmanuel Bernard
*/
@@ -41,7 +46,10 @@
private String street2;
@ManyToOne
private Country country;
-
+ private Date dateCreated;
+ private Calendar lastUpdated;
+ private String owner;
+
public Long getAddressId() {
return addressId;
}
@@ -69,4 +77,29 @@
public void setStreet2(String street2) {
this.street2 = street2;
}
+
+ public Calendar getLastUpdated() {
+ return lastUpdated;
+ }
+
+ public void setLastUpdated(Calendar lastUpdated) {
+ this.lastUpdated = lastUpdated;
+ }
+
+ public Date getDateCreated() {
+ return dateCreated;
+ }
+
+ public void setDateCreated(Date dateCreated) {
+ this.dateCreated = dateCreated;
+ }
+
+ public void setOwner(String owner) {
+ this.owner = owner;
+ }
+
+ public String getOwner() {
+ return this.owner;
+ }
+
}
Modified:
search/trunk/src/test/java/org/hibernate/search/test/configuration/BlogEntry.java
===================================================================
---
search/trunk/src/test/java/org/hibernate/search/test/configuration/BlogEntry.java 2009-11-20
11:21:01 UTC (rev 18014)
+++
search/trunk/src/test/java/org/hibernate/search/test/configuration/BlogEntry.java 2009-11-20
14:01:40 UTC (rev 18015)
@@ -1,22 +1,21 @@
package org.hibernate.search.test.configuration;
+import java.util.Date;
+
import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
import javax.persistence.Id;
-import javax.persistence.GeneratedValue;
-import org.apache.solr.analysis.StandardTokenizerFactory;
-import org.apache.solr.analysis.LowerCaseFilterFactory;
import org.apache.solr.analysis.EnglishPorterFilterFactory;
import org.apache.solr.analysis.GermanStemFilterFactory;
-
+import org.apache.solr.analysis.LowerCaseFilterFactory;
+import org.apache.solr.analysis.StandardTokenizerFactory;
+import org.hibernate.search.analyzer.Discriminator;
+import org.hibernate.search.annotations.AnalyzerDef;
+import org.hibernate.search.annotations.AnalyzerDefs;
import org.hibernate.search.annotations.Indexed;
-import org.hibernate.search.annotations.AnalyzerDefs;
-import org.hibernate.search.annotations.AnalyzerDef;
+import org.hibernate.search.annotations.TokenFilterDef;
import org.hibernate.search.annotations.TokenizerDef;
-import org.hibernate.search.annotations.TokenFilterDef;
-import org.hibernate.search.annotations.DocumentId;
-import org.hibernate.search.annotations.AnalyzerDiscriminator;
-import org.hibernate.search.analyzer.Discriminator;
/**
* @author Emmanuel Bernard
@@ -43,6 +42,7 @@
private String language;
private String title;
private String description;
+ private Date dateCreated;
@Id @GeneratedValue
public Long getId() {
@@ -77,6 +77,13 @@
this.description = description;
}
+ public Date getDateCreated() {
+ return dateCreated;
+ }
+
+ public void setDateCreated(Date dateCreated) {
+ this.dateCreated = dateCreated;
+ }
public static class BlogLangDiscriminator implements Discriminator {
public String getAnalyzerDefinitionName(Object value, Object entity, String field) {
Modified: search/trunk/src/test/java/org/hibernate/search/test/configuration/Country.java
===================================================================
---
search/trunk/src/test/java/org/hibernate/search/test/configuration/Country.java 2009-11-20
11:21:01 UTC (rev 18014)
+++
search/trunk/src/test/java/org/hibernate/search/test/configuration/Country.java 2009-11-20
14:01:40 UTC (rev 18015)
@@ -24,9 +24,13 @@
*/
package org.hibernate.search.test.configuration;
+import java.util.HashSet;
+import java.util.Set;
+
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
+import javax.persistence.OneToMany;
/**
* @author Emmanuel Bernard
@@ -36,7 +40,18 @@
@Id
@GeneratedValue
private Long id;
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(Long id) {
+ this.id = id;
+ }
+
private String name;
+
+ @OneToMany(mappedBy = "country")
+ private Set<Address> addresses = new HashSet<Address>();;
public String getName() {
return name;
@@ -45,4 +60,12 @@
public void setName(String name) {
this.name = name;
}
+
+ public void addAddress(Address address) {
+ addresses.add( address );
+ }
+
+ public Set<Address> getAddresses() {
+ return this.addresses;
+ }
}
Added: search/trunk/src/test/java/org/hibernate/search/test/configuration/Item.java
===================================================================
--- search/trunk/src/test/java/org/hibernate/search/test/configuration/Item.java
(rev 0)
+++
search/trunk/src/test/java/org/hibernate/search/test/configuration/Item.java 2009-11-20
14:01:40 UTC (rev 18015)
@@ -0,0 +1,45 @@
+package org.hibernate.search.test.configuration;
+
+import javax.persistence.CascadeType;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.ManyToOne;
+
+@Entity
+public class Item {
+
+ @Id @GeneratedValue
+ private Integer id;
+ private String description;
+
+
+ @ManyToOne( cascade = { CascadeType.PERSIST, CascadeType.REMOVE } )
+ private ProductCatalog productCatalog;
+
+ public ProductCatalog getProductCatalog() {
+ return productCatalog;
+ }
+
+ public void setProductCatalog(ProductCatalog productCatalog) {
+ this.productCatalog = productCatalog;
+ }
+
+ public Integer getId() {
+ return id;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
+
+
+ public String getDescription() {
+ return description;
+ }
+
+ public void setDescription(String description) {
+ this.description = description;
+ }
+
+}
Added:
search/trunk/src/test/java/org/hibernate/search/test/configuration/ProductCatalog.java
===================================================================
---
search/trunk/src/test/java/org/hibernate/search/test/configuration/ProductCatalog.java
(rev 0)
+++
search/trunk/src/test/java/org/hibernate/search/test/configuration/ProductCatalog.java 2009-11-20
14:01:40 UTC (rev 18015)
@@ -0,0 +1,51 @@
+package org.hibernate.search.test.configuration;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.persistence.Entity;
+import javax.persistence.FetchType;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.OneToMany;
+
+import org.hibernate.annotations.Cascade;
+import org.hibernate.annotations.IndexColumn;
+
+@Entity
+public class ProductCatalog {
+
+ @Id @GeneratedValue
+ private Integer id;
+ private String name;
+
+ @OneToMany(fetch = FetchType.LAZY)
+ @IndexColumn(name = "list_position")
+ @Cascade(org.hibernate.annotations.CascadeType.ALL)
+ private List<Item> items = new ArrayList<Item>();
+
+ public Integer getId() {
+ return id;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+
+ public List<Item> getItems() {
+ return items;
+ }
+
+ public void addItem(Item item) {
+ this.items.add(item);
+ }
+}
Modified:
search/trunk/src/test/java/org/hibernate/search/test/configuration/ProgrammaticMappingTest.java
===================================================================
---
search/trunk/src/test/java/org/hibernate/search/test/configuration/ProgrammaticMappingTest.java 2009-11-20
11:21:01 UTC (rev 18014)
+++
search/trunk/src/test/java/org/hibernate/search/test/configuration/ProgrammaticMappingTest.java 2009-11-20
14:01:40 UTC (rev 18015)
@@ -25,30 +25,41 @@
package org.hibernate.search.test.configuration;
import java.lang.annotation.ElementType;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.GregorianCalendar;
import java.util.List;
+import java.util.TimeZone;
-import org.apache.solr.analysis.StandardTokenizerFactory;
-import org.apache.solr.analysis.SnowballPorterFilterFactory;
+import org.apache.lucene.analysis.standard.StandardAnalyzer;
+import org.apache.lucene.queryParser.ParseException;
+import org.apache.lucene.queryParser.QueryParser;
+import org.apache.lucene.search.DefaultSimilarity;
+import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.search.Query;
+import org.apache.lucene.search.TopDocs;
+import org.apache.solr.analysis.EnglishPorterFilterFactory;
+import org.apache.solr.analysis.GermanStemFilterFactory;
import org.apache.solr.analysis.LowerCaseFilterFactory;
import org.apache.solr.analysis.NGramFilterFactory;
-import org.apache.solr.analysis.EnglishPorterFilterFactory;
-import org.apache.solr.analysis.GermanStemFilterFactory;
-import org.apache.lucene.queryParser.QueryParser;
-import org.apache.lucene.queryParser.ParseException;
-import org.apache.lucene.analysis.standard.StandardAnalyzer;
-import org.apache.lucene.search.DefaultSimilarity;
-
+import org.apache.solr.analysis.SnowballPorterFilterFactory;
+import org.apache.solr.analysis.StandardTokenizerFactory;
+import org.hibernate.Session;
+import org.hibernate.Transaction;
+import org.hibernate.cfg.Configuration;
+import org.hibernate.search.FullTextQuery;
+import org.hibernate.search.FullTextSession;
+import org.hibernate.search.Search;
+import org.hibernate.search.annotations.FilterCacheModeType;
+import org.hibernate.search.annotations.Index;
+import org.hibernate.search.annotations.Resolution;
+import org.hibernate.search.annotations.Store;
+import org.hibernate.search.bridge.builtin.LongBridge;
+import org.hibernate.search.cfg.ConcatStringBridge;
import org.hibernate.search.cfg.SearchMapping;
-import org.hibernate.search.cfg.ConcatStringBridge;
-import org.hibernate.search.annotations.Store;
-import org.hibernate.search.annotations.Index;
+import org.hibernate.search.store.DirectoryProvider;
+import org.hibernate.search.test.SearchTestCase;
import org.hibernate.search.test.analyzer.inheritance.ISOLatin1Analyzer;
-import org.hibernate.search.test.SearchTestCase;
-import org.hibernate.search.FullTextSession;
-import org.hibernate.search.Search;
-import org.hibernate.search.FullTextQuery;
-import org.hibernate.cfg.Configuration;
-import org.hibernate.Transaction;
/**
* @author Emmanuel Bernard
@@ -177,6 +188,9 @@
s.close();
}
+
+
+
public void testAnalyzerDiscriminator() throws Exception{
FullTextSession s = Search.getFullTextSession( openSession() );
Transaction tx = s.beginTransaction();
@@ -213,6 +227,280 @@
s.close();
}
+
+ public void testDateBridgeMapping() throws Exception{
+ FullTextSession s = Search.getFullTextSession( openSession() );
+ Transaction tx = s.beginTransaction();
+
+ Address address = new Address();
+ address.setStreet1( "Peachtree Rd NE" );
+ address.setStreet2( "Peachtnot Rd NE" );
+ Calendar c = GregorianCalendar.getInstance();
+ c.setTimeZone( TimeZone.getTimeZone( "GMT" ) ); //for the sake of tests
+ c.set( 2009, Calendar.NOVEMBER, 15);
+
+ Date date = new Date( c.getTimeInMillis() );
+ address.setDateCreated(date);
+ s.persist( address );
+
+ address = new Address();
+ address.setStreet1( "Peachtnot Rd NE" );
+ address.setStreet2( "Peachtree Rd NE" );
+ address.setDateCreated(date);
+ s.persist( address );
+
+ BlogEntry enEntry = new BlogEntry();
+ enEntry.setTitle( "acknowledgment" );
+ enEntry.setDescription( "acknowledgment" );
+ enEntry.setLanguage( "en" );
+ enEntry.setDateCreated(date);
+ s.persist( enEntry );
+
+ tx.commit();
+
+ s.clear();
+
+ tx = s.beginTransaction();
+
+ QueryParser parser = new QueryParser( "id", new StandardAnalyzer( ) );
+ org.apache.lucene.search.Query luceneQuery = parser.parse( "date-created:20091115
OR blog-entry-created:20091115" );
+ FullTextQuery query = s.createFullTextQuery( luceneQuery ).setProjection(
FullTextQuery.THIS, FullTextQuery.SCORE );
+ assertEquals( "expecting 3 results", 3, query.getResultSize() );
+
+ @SuppressWarnings( "unchecked" )
+ List<Object[]> results = query.list();
+
+ for( Object[] result : results ) {
+ s.delete( result[0] );
+ }
+ tx.commit();
+ s.close();
+ }
+
+ public void testCalendarBridgeMapping() throws Exception{
+ FullTextSession s = Search.getFullTextSession( openSession() );
+ Transaction tx = s.beginTransaction();
+
+ Address address = new Address();
+ address.setStreet1( "Peachtree Rd NE" );
+ address.setStreet2( "Peachtnot Rd NE" );
+ Calendar c = GregorianCalendar.getInstance();
+ c.setTimeZone( TimeZone.getTimeZone( "GMT" ) ); //for the sake of tests
+ c.set( 2009, Calendar.NOVEMBER, 15);
+
+ address.setLastUpdated(c);
+ s.persist( address );
+
+ address = new Address();
+ address.setStreet1( "Peachtnot Rd NE" );
+ address.setStreet2( "Peachtree Rd NE" );
+ address.setLastUpdated(c);
+ s.persist( address );
+
+ tx.commit();
+
+ s.clear();
+
+ tx = s.beginTransaction();
+
+ QueryParser parser = new QueryParser( "id", new StandardAnalyzer( ) );
+ org.apache.lucene.search.Query luceneQuery = parser.parse(
"last-updated:20091115" );
+ FullTextQuery query = s.createFullTextQuery( luceneQuery ).setProjection(
FullTextQuery.THIS, FullTextQuery.SCORE );
+ assertEquals( "expecting 2 results", 2, query.getResultSize() );
+
+ @SuppressWarnings( "unchecked" )
+ List<Object[]> results = query.list();
+
+ for( Object[] result : results ) {
+ s.delete( result[0] );
+ }
+ tx.commit();
+ s.close();
+ }
+
+
+ public void testProvidedIdMapping() throws Exception{
+ ProvidedIdEntry person1 = new ProvidedIdEntry();
+ person1.setName( "Big Goat" );
+ person1.setBlurb( "Eats grass" );
+
+ ProvidedIdEntry person2 = new ProvidedIdEntry();
+ person2.setName( "Mini Goat" );
+ person2.setBlurb( "Eats cheese" );
+
+ ProvidedIdEntry person3 = new ProvidedIdEntry();
+ person3.setName( "Regular goat" );
+ person3.setBlurb( "Is anorexic" );
+
+ Session session = openSession();
+ FullTextSession fullTextSession = Search.getFullTextSession( session );
+ Transaction transaction = session.beginTransaction();
+ session.persist( person1 );
+ session.persist( person2 );
+ session.persist( person3 );
+
+ transaction.commit();
+ session.clear();
+
+ transaction = fullTextSession.beginTransaction();
+
+ QueryParser parser = new QueryParser( "providedidentry.name", new
StandardAnalyzer() );
+ Query luceneQuery = parser.parse( "Goat" );
+
+ //we cannot use FTQuery because @ProvidedId does not provide the getter id and
Hibernate Hsearch Query extension
+ //needs it. So we use plain Lucene
+
+ //we know there is only one DP
+ DirectoryProvider<?> provider = fullTextSession.getSearchFactory()
+ .getDirectoryProviders( ProvidedIdEntry.class )[0];
+ IndexSearcher searcher = new IndexSearcher( provider.getDirectory() );
+ TopDocs hits = searcher.search( luceneQuery, 1000 );
+ searcher.close();
+ transaction.commit();
+ session.close();
+
+ assertEquals( 3, hits.totalHits );
+ }
+
+
+
+ public void testFullTextFilterDef() throws Exception{
+ FullTextSession s = Search.getFullTextSession( openSession() );
+ Transaction tx = s.beginTransaction();
+
+ Address address = new Address();
+ address.setStreet1( "Peachtree Rd NE" );
+ address.setStreet2( "Peachtnot Rd NE" );
+ address.setOwner("test");
+ Calendar c = GregorianCalendar.getInstance();
+ c.setTimeZone( TimeZone.getTimeZone( "GMT" ) ); //for the sake of tests
+ c.set( 2009, Calendar.NOVEMBER, 15);
+
+ address.setLastUpdated(c);
+ s.persist( address );
+
+ address = new Address();
+ address.setStreet1( "Peachtnot Rd NE" );
+ address.setStreet2( "Peachtree Rd NE" );
+ address.setLastUpdated(c);
+ address.setOwner("test2");
+ s.persist( address );
+
+ tx.commit();
+
+ s.clear();
+
+ tx = s.beginTransaction();
+
+ QueryParser parser = new QueryParser( "id", new StandardAnalyzer( ) );
+ org.apache.lucene.search.Query luceneQuery = parser.parse(
"street1:Peachtnot" );
+ FullTextQuery query = s.createFullTextQuery( luceneQuery ).setProjection(
FullTextQuery.THIS, FullTextQuery.SCORE );
+ query.enableFullTextFilter("security").setParameter("ownerName",
"test");
+ assertEquals( "expecting 1 results", 1, query.getResultSize() );
+
+ @SuppressWarnings( "unchecked" )
+ List<Object[]> results = query.list();
+
+ for( Object[] result : results ) {
+ s.delete( result[0] );
+ }
+ tx.commit();
+ s.close();
+ }
+
+
+ public void testIndexEmbedded() throws Exception{
+ FullTextSession s = Search.getFullTextSession( openSession() );
+ Transaction tx = s.beginTransaction();
+
+ ProductCatalog productCatalog = new ProductCatalog();
+ productCatalog.setName("Cars");
+ Item item = new Item();
+ item.setDescription("Ferrari");
+ item.setProductCatalog(productCatalog);
+ productCatalog.addItem(item);
+
+ s.persist(item);
+ s.persist(productCatalog);
+ tx.commit();
+
+ s.clear();
+
+ tx = s.beginTransaction();
+
+ QueryParser parser = new QueryParser( "id", new StandardAnalyzer( ) );
+ org.apache.lucene.search.Query luceneQuery = parser.parse(
"items.description:Ferrari" );
+ FullTextQuery query = s.createFullTextQuery( luceneQuery ).setProjection(
FullTextQuery.THIS, FullTextQuery.SCORE );
+ assertEquals( "expecting 1 results", 1, query.getResultSize() );
+
+ @SuppressWarnings( "unchecked" )
+ List<Object[]> results = query.list();
+
+ for( Object[] result : results ) {
+ s.delete( result[0] );
+ }
+ tx.commit();
+ s.close();
+ }
+
+ public void testContainedIn() throws Exception{
+ FullTextSession s = Search.getFullTextSession( openSession() );
+ Transaction tx = s.beginTransaction();
+
+ ProductCatalog productCatalog = new ProductCatalog();
+ productCatalog.setName("Cars");
+ Item item = new Item();
+ item.setDescription("test");
+ item.setProductCatalog(productCatalog);
+ productCatalog.addItem(item);
+
+ s.persist(item);
+ s.persist(productCatalog);
+ tx.commit();
+
+ s.clear();
+
+ tx = s.beginTransaction();
+
+ QueryParser parser = new QueryParser( "id", new StandardAnalyzer( ) );
+ org.apache.lucene.search.Query luceneQuery = parser.parse(
"items.description:test" );
+ FullTextQuery query = s.createFullTextQuery( luceneQuery ).setProjection(
FullTextQuery.THIS, FullTextQuery.SCORE );
+ assertEquals( "expecting 1 results", 1, query.getResultSize() );
+ tx.commit();
+
+ tx = s.beginTransaction();
+
+ Item loaded = (Item) s.get(Item.class, item.getId());
+ loaded.setDescription("Ferrari");
+
+ s.update(loaded);
+ tx.commit();
+
+
+ tx = s.beginTransaction();
+
+ parser = new QueryParser( "id", new StandardAnalyzer( ) );
+ luceneQuery = parser.parse( "items.description:test" );
+ query = s.createFullTextQuery( luceneQuery ).setProjection( FullTextQuery.THIS,
FullTextQuery.SCORE );
+ assertEquals( "expecting 0 results", 0, query.getResultSize() );
+
+ parser = new QueryParser( "id", new StandardAnalyzer( ) );
+ luceneQuery = parser.parse( "items.description:Ferrari" );
+ query = s.createFullTextQuery( luceneQuery ).setProjection( FullTextQuery.THIS,
FullTextQuery.SCORE );
+ assertEquals( "expecting 1 results", 1, query.getResultSize() );
+ tx.commit();
+
+ tx = s.beginTransaction();
+ @SuppressWarnings( "unchecked" )
+ List<Object[]> results = query.list();
+
+ for( Object[] result : results ) {
+ s.delete( result[0] );
+ }
+ tx.commit();
+ s.close();
+ }
+
private int nbrOfMatchingResults(String field, String token, FullTextSession s) throws
ParseException {
QueryParser parser = new QueryParser( field, new StandardAnalyzer() );
org.apache.lucene.search.Query luceneQuery = parser.parse( token );
@@ -225,6 +513,7 @@
super.configure( cfg );
//cfg.setProperty( "hibernate.search.default.directory_provider",
FSDirectoryProvider.class.getName() );
SearchMapping mapping = new SearchMapping();
+
mapping.analyzerDef( "ngram", StandardTokenizerFactory.class )
.filter( LowerCaseFilterFactory.class )
.filter( NGramFilterFactory.class )
@@ -236,11 +525,21 @@
.analyzerDef( "de", StandardTokenizerFactory.class )
.filter( LowerCaseFilterFactory.class )
.filter( GermanStemFilterFactory.class )
-
- .indexedClass( Address.class )
+ .entity( Address.class ).indexed()
.similarity( DefaultSimilarity.class )
.boost( 2 )
+ .fullTextFilterDef("security",
SecurityFilterFactory.class).cache(FilterCacheModeType.INSTANCE_ONLY)
.property( "addressId", ElementType.FIELD ).documentId().name(
"id" )
+ .property("lastUpdated", ElementType.FIELD)
+ .field().name("last-updated")
+ .analyzer("en").store(Store.YES)
+ .calendarBridge(Resolution.DAY)
+ .property("dateCreated", ElementType.FIELD)
+ .field().name("date-created").index(Index.TOKENIZED)
+ .analyzer("en").store( Store.YES )
+ .dateBridge(Resolution.DAY)
+ .property("owner", ElementType.FIELD)
+ .field()
.property( "street1", ElementType.FIELD )
.field()
.field().name( "street1_ngram" ).analyzer( "ngram" )
@@ -249,14 +548,40 @@
.bridge( ConcatStringBridge.class ).param( ConcatStringBridge.SIZE, "4"
)
.property( "street2", ElementType.METHOD )
.field().name( "idx_street2" ).store( Store.YES ).boost( 2 )
- .indexedClass( BlogEntry.class )
+ .entity(ProvidedIdEntry.class).indexed()
+ .providedId().name("providedidentry").bridge(LongBridge.class)
+ .property("name", ElementType.FIELD)
+ .field().name("providedidentry.name").analyzer("en").index(Index.TOKENIZED).store(Store.YES)
+ .property("blurb", ElementType.FIELD)
+ .field().name("providedidentry.blurb").analyzer("en").index(Index.TOKENIZED).store(Store.YES)
+ .property("age", ElementType.FIELD)
+ .field().name("providedidentry.age").analyzer("en").index(Index.TOKENIZED).store(Store.YES)
+ .entity(ProductCatalog.class).indexed()
+ .similarity( DefaultSimilarity.class )
+ .boost( 2 )
+ .property( "id", ElementType.FIELD ).documentId().name( "id" )
+ .property("name", ElementType.FIELD)
+ .field().name("productCatalogName").index(Index.TOKENIZED).analyzer("en").store(Store.YES)
+ .property("items", ElementType.FIELD)
+ .indexEmbedded()
+ .entity(Item.class)
+ .property("description", ElementType.FIELD)
+ .field().name("description").analyzer("en").index(Index.TOKENIZED).store(Store.YES)
+ .property("productCatalog", ElementType.FIELD)
+ .containedIn()
+ .entity( BlogEntry.class ).indexed()
.property( "title", ElementType.METHOD )
.field()
.property( "description", ElementType.METHOD )
.field()
.property( "language", ElementType.METHOD )
.analyzerDiscriminator(BlogEntry.BlogLangDiscriminator.class)
- ;
+ .property("dateCreated", ElementType.METHOD)
+ .field()
+ .name("blog-entry-created")
+ .analyzer("en")
+ .store(Store.YES)
+ .dateBridge(Resolution.DAY);
cfg.getProperties().put( "hibernate.search.mapping_model", mapping );
}
@@ -275,7 +600,7 @@
.filter( NGramFilterFactory.class)
.param("minGramSize", "3")
.param("maxGramSize", "3")
- .indexedClass(Address.class, "Address_Index")
+ .entity(Address.class).indexed().indexName("Address_Index")
.property("street1", ElementType.FIELD)
.field()
.field()
@@ -286,18 +611,22 @@
.field()
.name("street1_ngram")
.analyzer("ngram")
- .indexedClass(User.class)
+ .entity(User.class).indexed()
.property("name", ElementType.METHOD)
.field()
.analyzerDef( "minimal", StandardTokenizerFactory.class );
}
- protected Class[] getMappings() {
- return new Class[] {
+ protected Class<?>[] getMappings() {
+ return new Class<?>[] {
Address.class,
Country.class,
- BlogEntry.class
+ BlogEntry.class,
+ ProvidedIdEntry.class,
+ ProductCatalog.class,
+ Item.class
+
};
}
}
Added:
search/trunk/src/test/java/org/hibernate/search/test/configuration/ProvidedIdEntry.java
===================================================================
---
search/trunk/src/test/java/org/hibernate/search/test/configuration/ProvidedIdEntry.java
(rev 0)
+++
search/trunk/src/test/java/org/hibernate/search/test/configuration/ProvidedIdEntry.java 2009-11-20
14:01:40 UTC (rev 18015)
@@ -0,0 +1,46 @@
+package org.hibernate.search.test.configuration;
+
+import java.io.Serializable;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+
+@Entity
+public class ProvidedIdEntry implements Serializable {
+
+ private static final long serialVersionUID = 6756516458812576484L;
+
+ @Id
+ @GeneratedValue
+ private long id;
+
+ private String name;
+ private String blurb;
+ private int age;
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public String getBlurb() {
+ return blurb;
+ }
+
+ public void setBlurb(String blurb) {
+ this.blurb = blurb;
+ }
+
+ public int getAge() {
+ return age;
+ }
+
+ public void setAge(int age) {
+ this.age = age;
+ }
+
+}
Added:
search/trunk/src/test/java/org/hibernate/search/test/configuration/SecurityFilterFactory.java
===================================================================
---
search/trunk/src/test/java/org/hibernate/search/test/configuration/SecurityFilterFactory.java
(rev 0)
+++
search/trunk/src/test/java/org/hibernate/search/test/configuration/SecurityFilterFactory.java 2009-11-20
14:01:40 UTC (rev 18015)
@@ -0,0 +1,60 @@
+package org.hibernate.search.test.configuration;
+
+import java.io.IOException;
+
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.Term;
+import org.apache.lucene.index.TermDocs;
+import org.apache.lucene.search.CachingWrapperFilter;
+import org.apache.lucene.search.DocIdSet;
+import org.apache.lucene.search.Filter;
+import org.apache.lucene.search.QueryWrapperFilter;
+import org.apache.lucene.util.OpenBitSet;
+import org.hibernate.search.annotations.Factory;
+import org.hibernate.search.annotations.Key;
+import org.hibernate.search.filter.FilterKey;
+import org.hibernate.search.filter.StandardFilterKey;
+
+public class SecurityFilterFactory {
+
+ private static final long serialVersionUID = -19238668272676998L;
+
+ private String ownerName;
+
+ public void setOwnerName(String ownerName) {
+ this.ownerName = ownerName;
+ }
+
+ @Factory
+ public Filter buildSecurityFilter() {
+ SecurityFilter securityFilter = new SecurityFilter(ownerName);
+ return new CachingWrapperFilter(securityFilter);
+ }
+
+ @Key
+ public FilterKey getKey() {
+ StandardFilterKey key = new StandardFilterKey();
+ key.addParameter(ownerName);
+ return key;
+ }
+
+ private final class SecurityFilter extends Filter {
+ private static final long serialVersionUID = -5105989141875576599L;
+ private final String ownerName;
+
+ private SecurityFilter(final String ownerName) {
+ this.ownerName = ownerName;
+ }
+
+ public DocIdSet getDocIdSet(IndexReader reader) throws IOException {
+ OpenBitSet bitSet = new OpenBitSet( reader.maxDoc() );
+ TermDocs termDocs = reader.termDocs( new Term( "owner", ownerName ) );
+ while ( termDocs.next() ) {
+ bitSet.set( termDocs.doc() );
+ }
+ return bitSet;
+ }
+
+ }
+
+}