mirror of https://github.com/apache/lucene.git
SOLR-1551 Provide DocumentObjectBinder.getBean() method
git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@834780 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
bb19fb01d8
commit
69ac6942a3
|
@ -43,21 +43,30 @@ public class DocumentObjectBinder {
|
||||||
|
|
||||||
for(int j=0;j<solrDocList.size();j++) {
|
for(int j=0;j<solrDocList.size();j++) {
|
||||||
SolrDocument sdoc = solrDocList.get(j);
|
SolrDocument sdoc = solrDocList.get(j);
|
||||||
|
result.add(getBean(clazz, fields, sdoc));
|
||||||
T obj = null;
|
|
||||||
try {
|
|
||||||
obj = clazz.newInstance();
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new RuntimeException("Could not instantiate object of " + clazz,e);
|
|
||||||
}
|
|
||||||
for (int i = 0; i < fields.size(); i++) {
|
|
||||||
DocField docField = fields.get(i);
|
|
||||||
docField.inject(obj, sdoc);
|
|
||||||
}
|
|
||||||
result.add(obj);
|
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
public <T> T getBean(Class<T> clazz, SolrDocument solrDoc) {
|
||||||
|
return getBean(clazz, null,solrDoc);
|
||||||
|
}
|
||||||
|
|
||||||
|
private <T> T getBean(Class<T> clazz, List<DocField> fields, SolrDocument solrDoc) {
|
||||||
|
if (fields == null) {
|
||||||
|
fields = getDocFields(clazz);
|
||||||
|
}
|
||||||
|
T obj = null;
|
||||||
|
try {
|
||||||
|
obj = clazz.newInstance();
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException("Could not instantiate object of " + clazz, e);
|
||||||
|
}
|
||||||
|
for (int i = 0; i < fields.size(); i++) {
|
||||||
|
DocField docField = fields.get(i);
|
||||||
|
docField.inject(obj, solrDoc);
|
||||||
|
}
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
public SolrInputDocument toSolrInputDocument( Object obj )
|
public SolrInputDocument toSolrInputDocument( Object obj )
|
||||||
{
|
{
|
||||||
|
|
|
@ -106,12 +106,17 @@ public class TestDocumentObjectBinder extends TestCase
|
||||||
SolrDocumentList docs = new SolrDocumentList();
|
SolrDocumentList docs = new SolrDocumentList();
|
||||||
docs.add( ClientUtils.toSolrDocument(doc) );
|
docs.add( ClientUtils.toSolrDocument(doc) );
|
||||||
Item out = binder.getBeans( Item.class, docs ).get( 0 );
|
Item out = binder.getBeans( Item.class, docs ).get( 0 );
|
||||||
|
Item singleOut = binder.getBean(Item.class, ClientUtils.toSolrDocument(doc));
|
||||||
|
|
||||||
// make sure it came out the same
|
// make sure it came out the same
|
||||||
Assert.assertEquals( item.id, out.id );
|
Assert.assertEquals( item.id, out.id );
|
||||||
Assert.assertEquals( item.inStock, out.inStock );
|
Assert.assertEquals( item.inStock, out.inStock );
|
||||||
Assert.assertEquals( item.categories.length, out.categories.length );
|
Assert.assertEquals( item.categories.length, out.categories.length );
|
||||||
Assert.assertEquals( item.features, out.features );
|
Assert.assertEquals( item.features, out.features );
|
||||||
|
Assert.assertEquals( item.id, singleOut.id );
|
||||||
|
Assert.assertEquals( item.inStock, singleOut.inStock );
|
||||||
|
Assert.assertEquals( item.categories.length, singleOut.categories.length );
|
||||||
|
Assert.assertEquals( item.features, singleOut.features );
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class Item {
|
public static class Item {
|
||||||
|
|
Loading…
Reference in New Issue