Changeset 4614


Ignore:
Timestamp:
03/03/14 16:10:50 (10 years ago)
Author:
Twan Goosen
Message:

separated field reading and string concatenation functionalities of SolrFieldModel?

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  
    1919import eu.clarin.cmdi.vlo.FacetConstants;
    2020import eu.clarin.cmdi.vlo.wicket.model.NullFallbackModel;
    21 import eu.clarin.cmdi.vlo.wicket.model.SolrFieldModel;
     21import eu.clarin.cmdi.vlo.wicket.model.SolrFieldStringModel;
    2222import org.apache.solr.common.SolrDocument;
    2323import org.apache.wicket.markup.html.basic.Label;
     
    4141
    4242        public SolrFieldLabel(String id, IModel<SolrDocument> documentModel, String fieldName) {
    43             super(id, new SolrFieldModel(documentModel, fieldName));
     43            super(id, new SolrFieldStringModel(documentModel, fieldName));
    4444        }
    4545
     
    4747            super(id,
    4848                    new NullFallbackModel(
    49                             new SolrFieldModel(documentModel, fieldName), nullFallback));
     49                            new SolrFieldStringModel(documentModel, fieldName), nullFallback));
    5050        }
    5151
  • vlo/branches/vlo-3.0/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/wicket/model/SolrFieldModel.java

    r4606 r4614  
    1717package eu.clarin.cmdi.vlo.wicket.model;
    1818
     19import com.google.common.base.Function;
     20import com.google.common.collect.Collections2;
    1921import java.util.Collection;
    20 import java.util.Iterator;
    2122import org.apache.solr.common.SolrDocument;
    2223import org.apache.wicket.model.AbstractReadOnlyModel;
     
    2425
    2526/**
    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
    2928 *
    3029 * @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!
    3133 */
    32 public class SolrFieldModel extends AbstractReadOnlyModel<String> {
     34public class SolrFieldModel<T> extends AbstractReadOnlyModel<Collection<T>> {
    3335
    3436    private final IModel<SolrDocument> documentModel;
     
    3638
    3739    /**
    38      * 
     40     *
    3941     * @param documentModel model of document that holds the field values
    4042     * @param fieldName name of the field to take value from
     
    4648
    4749    @Override
    48     public String getObject() {
     50    public Collection<T> getObject() {
    4951        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;
    6554        } else {
    66             return null;
     55            return transformCollectionType(fieldValues);
    6756        }
    6857    }
    6958
    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;
    7672            }
    77         }
    78         return valuesBuilder.toString();
     73        });
    7974    }
    8075
     
    8479        documentModel.detach();
    8580    }
    86    
    87    
     81
    8882}
  • vlo/branches/vlo-3.0/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/wicket/model/SolrFieldStringModel.java

    r4606 r4614  
    2424
    2525/**
    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
     26 * Model that provides field values as String values for a given field values
     27 * model, both for singular values and multiple values (imploding the
    2828 * latter into a single string)
    2929 *
    3030 * @author twagoo
    3131 */
    32 public class SolrFieldModel extends AbstractReadOnlyModel<String> {
     32public class SolrFieldStringModel extends AbstractReadOnlyModel<String> {
    3333
    34     private final IModel<SolrDocument> documentModel;
    35     private final String fieldName;
     34    private final IModel<Collection<Object>> fieldModel;
    3635
    3736    /**
    38      *
     37     * Wraps the document model and specified field name into a
     38     * {@link SolrFieldModel} to obtain field values
     39     *
    3940     * @param documentModel model of document that holds the field values
    4041     * @param fieldName name of the field to take value from
    4142     */
    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;
    4553    }
    4654
    4755    @Override
    4856    public String getObject() {
    49         final Collection<Object> fieldValues = documentModel.getObject().getFieldValues(fieldName);
     57        final Collection<Object> fieldValues = fieldModel.getObject();
    5058        if (fieldValues != null) {
    5159            return getValueString(fieldValues);
     
    8290    public void detach() {
    8391        super.detach();
    84         documentModel.detach();
     92        fieldModel.detach();
    8593    }
    86    
    87    
     94
    8895}
Note: See TracChangeset for help on using the changeset viewer.