Changeset 4614
- Timestamp:
- 03/03/14 16:10:50 (10 years ago)
- Location:
- vlo/branches/vlo-3.0/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/wicket
- Files:
-
- 2 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
vlo/branches/vlo-3.0/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/wicket/components/SearchResultItemPanel.java
r4560 r4614 19 19 import eu.clarin.cmdi.vlo.FacetConstants; 20 20 import eu.clarin.cmdi.vlo.wicket.model.NullFallbackModel; 21 import eu.clarin.cmdi.vlo.wicket.model.SolrField Model;21 import eu.clarin.cmdi.vlo.wicket.model.SolrFieldStringModel; 22 22 import org.apache.solr.common.SolrDocument; 23 23 import org.apache.wicket.markup.html.basic.Label; … … 41 41 42 42 public SolrFieldLabel(String id, IModel<SolrDocument> documentModel, String fieldName) { 43 super(id, new SolrField Model(documentModel, fieldName));43 super(id, new SolrFieldStringModel(documentModel, fieldName)); 44 44 } 45 45 … … 47 47 super(id, 48 48 new NullFallbackModel( 49 new SolrField Model(documentModel, fieldName), nullFallback));49 new SolrFieldStringModel(documentModel, fieldName), nullFallback)); 50 50 } 51 51 -
vlo/branches/vlo-3.0/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/wicket/model/SolrFieldModel.java
r4606 r4614 17 17 package eu.clarin.cmdi.vlo.wicket.model; 18 18 19 import com.google.common.base.Function; 20 import com.google.common.collect.Collections2; 19 21 import java.util.Collection; 20 import java.util.Iterator;21 22 import org.apache.solr.common.SolrDocument; 22 23 import org.apache.wicket.model.AbstractReadOnlyModel; … … 24 25 25 26 /** 26 * Model that provides field values as String values for a given Solr document 27 * and a field name, both for singular values and multiple values (imploding the 28 * latter into a single string) 27 * Model that provides field values as for a given Solr document 29 28 * 30 29 * @author twagoo 30 * @param <T> type of elements in the value collection in the field. Values will 31 * be cast on the fly, so specifying a non-matching type here might result in 32 * runtime errors! 31 33 */ 32 public class SolrFieldModel extends AbstractReadOnlyModel<String> {34 public class SolrFieldModel<T> extends AbstractReadOnlyModel<Collection<T>> { 33 35 34 36 private final IModel<SolrDocument> documentModel; … … 36 38 37 39 /** 38 * 40 * 39 41 * @param documentModel model of document that holds the field values 40 42 * @param fieldName name of the field to take value from … … 46 48 47 49 @Override 48 public StringgetObject() {50 public Collection<T> getObject() { 49 51 final Collection<Object> fieldValues = documentModel.getObject().getFieldValues(fieldName); 50 if (fieldValues != null) { 51 return getValueString(fieldValues); 52 } 53 return null; 54 } 55 56 private String getValueString(final Collection<Object> fieldValues) { 57 final Iterator<Object> iterator = fieldValues.iterator(); 58 if (iterator.hasNext()) { 59 final String firstValue = iterator.next().toString(); 60 if (iterator.hasNext()) { 61 return getMultipleValuesString(firstValue, iterator); 62 } else { 63 return firstValue; 64 } 52 if (fieldValues == null) { 53 return null; 65 54 } else { 66 return null;55 return transformCollectionType(fieldValues); 67 56 } 68 57 } 69 58 70 protected String getMultipleValuesString(final String firstValue, final Iterator<Object> iterator) { 71 final StringBuilder valuesBuilder = new StringBuilder(firstValue); 72 while (iterator.hasNext()) { 73 valuesBuilder.append(iterator.next().toString()); 74 if (iterator.hasNext()) { 75 valuesBuilder.append("; "); 59 /** 60 * Transforms object collection to a typed collection by means of an 61 * on-the-fly cast 62 * 63 * @param fieldValues 64 * @return 65 */ 66 private Collection<T> transformCollectionType(final Collection<Object> fieldValues) { 67 return Collections2.transform(fieldValues, new Function<Object, T>() { 68 69 @Override 70 public T apply(Object input) { 71 return (T) input; 76 72 } 77 } 78 return valuesBuilder.toString(); 73 }); 79 74 } 80 75 … … 84 79 documentModel.detach(); 85 80 } 86 87 81 88 82 } -
vlo/branches/vlo-3.0/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/wicket/model/SolrFieldStringModel.java
r4606 r4614 24 24 25 25 /** 26 * Model that provides field values as String values for a given Solr document27 * and a field name, both for singular values and multiple values (imploding the26 * Model that provides field values as String values for a given field values 27 * model, both for singular values and multiple values (imploding the 28 28 * latter into a single string) 29 29 * 30 30 * @author twagoo 31 31 */ 32 public class SolrField Model extends AbstractReadOnlyModel<String> {32 public class SolrFieldStringModel extends AbstractReadOnlyModel<String> { 33 33 34 private final IModel<SolrDocument> documentModel; 35 private final String fieldName; 34 private final IModel<Collection<Object>> fieldModel; 36 35 37 36 /** 38 * 37 * Wraps the document model and specified field name into a 38 * {@link SolrFieldModel} to obtain field values 39 * 39 40 * @param documentModel model of document that holds the field values 40 41 * @param fieldName name of the field to take value from 41 42 */ 42 public SolrFieldModel(IModel<SolrDocument> documentModel, String fieldName) { 43 this.documentModel = documentModel; 44 this.fieldName = fieldName; 43 public SolrFieldStringModel(IModel<SolrDocument> documentModel, String fieldName) { 44 this(new SolrFieldModel<Object>(documentModel, fieldName)); 45 } 46 47 /** 48 * 49 * @param fieldModel model that provides field values as a collection 50 */ 51 public SolrFieldStringModel(IModel<Collection<Object>> fieldModel) { 52 this.fieldModel = fieldModel; 45 53 } 46 54 47 55 @Override 48 56 public String getObject() { 49 final Collection<Object> fieldValues = documentModel.getObject().getFieldValues(fieldName);57 final Collection<Object> fieldValues = fieldModel.getObject(); 50 58 if (fieldValues != null) { 51 59 return getValueString(fieldValues); … … 82 90 public void detach() { 83 91 super.detach(); 84 documentModel.detach();92 fieldModel.detach(); 85 93 } 86 87 94 88 95 }
Note: See TracChangeset
for help on using the changeset viewer.