Changeset 6046


Ignore:
Timestamp:
02/25/15 08:28:47 (9 years ago)
Author:
Twan Goosen
Message:

Changed strategy for removing language code prefix from descriptions: now using field value converter. This does away with the DescriptionFieldModel?. Converter now also used in SOLR field labels for multiple values. Removed some unused constructors & moved a regex constant into FacetConstants?
refs #699

Location:
vlo/trunk
Files:
1 added
1 deleted
7 edited

Legend:

Unmodified
Added
Removed
  • vlo/trunk/vlo-commons/src/main/java/eu/clarin/cmdi/vlo/FacetConstants.java

    r6030 r6046  
    4343     */
    4444    public static final String FIELD_SEARCHPAGE = "_searchPageRef";
    45    
     45
    4646    //Deprecated fields
    4747    public static final String DEPRECATED_FIELD_LANGUAGE = "language";
     
    7171     */
    7272    public static final String DESCRIPTION_LANGUAGE_PATTERN = "^\\{lang='([a-z]{3})'\\}";
     73
     74    /**
     75     * regular expression that matches the syntax of the 'languageCode' field
     76     * (with either a language code or a name as indicated by the prefix)
     77     */
     78    public static final String LANGUAGE_CODE_PATTERN = "(name|code):(.*)";
    7379}
  • vlo/trunk/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/VloWicketApplication.java

    r5227 r6046  
    1010import eu.clarin.cmdi.vlo.wicket.pages.SimpleSearchPage;
    1111import eu.clarin.cmdi.vlo.wicket.pages.VloBasePage;
     12import eu.clarin.cmdi.vlo.wicket.provider.FieldValueConverterProvider;
    1213import javax.inject.Inject;
    1314import org.apache.wicket.Application;
     
    3738    @Inject
    3839    private XmlTransformationService cmdiTransformationService;
     40    @Inject
     41    private FieldValueConverterProvider fieldValueConverterProvider;
    3942    @Inject
    4043    private VloConfig vloConfig;
     
    145148        return cmdiTransformationService;
    146149    }
     150   
     151    public FieldValueConverterProvider getFieldValueConverterProvider() {
     152        return fieldValueConverterProvider;
     153    }
    147154
    148155}
  • vlo/trunk/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/wicket/components/FieldValueLabel.java

    r6003 r6046  
    1919import eu.clarin.cmdi.vlo.wicket.provider.FieldValueConverterProvider;
    2020import eu.clarin.cmdi.vlo.wicket.provider.FieldValueConverterProviderImpl;
    21 import org.apache.solr.client.solrj.response.FacetField;
    2221import org.apache.wicket.markup.html.basic.Label;
    2322import org.apache.wicket.model.IModel;
  • vlo/trunk/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/wicket/components/SolrFieldLabel.java

    r4871 r6046  
    3535     * @param documentModel model that holds document to show field of
    3636     * @param fieldName name of field to show value of
    37      */
    38     public SolrFieldLabel(String id, IModel<SolrDocument> documentModel, String fieldName) {
    39         super(id, new SolrFieldStringModel(documentModel, fieldName));
    40     }
    41 
    42     /**
    43      *
    44      * @param id id of label
    45      * @param documentModel model that holds document to show field of
    46      * @param fieldName name of field to show value of
    47      * @param maxLength maximum length to allow
    48      * @param truncatePoint point to truncate if string is too long
    49      */
    50     public SolrFieldLabel(String id, IModel<SolrDocument> documentModel, String fieldName, int maxLength, int truncatePoint) {
    51         super(id,
    52                 new TruncatingStringModel(
    53                         new SolrFieldStringModel(documentModel, fieldName), maxLength, truncatePoint));
    54     }
    55 
    56     /**
    57      *
    58      * @param id id of label
    59      * @param documentModel model that holds document to show field of
    60      * @param fieldName name of field to show value of
    6137     * @param nullFallback string to show if actual value is null
    6238     */
  • vlo/trunk/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/wicket/model/SolrFieldStringModel.java

    r5995 r6046  
    1717package eu.clarin.cmdi.vlo.wicket.model;
    1818
    19 import eu.clarin.cmdi.vlo.FacetConstants;
     19import eu.clarin.cmdi.vlo.VloWicketApplication;
    2020import java.util.Collection;
    2121import java.util.Iterator;
     
    2323import org.apache.wicket.model.AbstractReadOnlyModel;
    2424import org.apache.wicket.model.IModel;
     25import org.apache.wicket.util.convert.IConverter;
    2526
    2627/**
     
    4445     */
    4546    public SolrFieldStringModel(IModel<SolrDocument> documentModel, String fieldName) {
    46         fieldModel = new SolrFieldModel<Object>(documentModel, fieldName);
     47        fieldModel = new SolrFieldModel<>(documentModel, fieldName);
    4748        field = fieldName;
    4849    }
     
    6061        final Iterator<Object> iterator = fieldValues.iterator();
    6162        if (iterator.hasNext()) {
    62             final String firstValue = postprocessValue(iterator.next().toString());
     63            final String firstValue = iterator.next().toString();
    6364            if (iterator.hasNext()) {
    6465                return getMultipleValuesString(firstValue, iterator);
     
    7273
    7374    protected String getMultipleValuesString(final String firstValue, final Iterator<Object> iterator) {
    74         final StringBuilder valuesBuilder = new StringBuilder(firstValue).append("; ");
     75        // for multiple value strings, run every individual value through the converter
     76        final StringBuilder valuesBuilder = new StringBuilder(postprocessValue(firstValue)).append("; ");
    7577        while (iterator.hasNext()) {
    76             valuesBuilder.append(iterator.next().toString());
     78            valuesBuilder.append(postprocessValue(iterator.next().toString()));
    7779            if (iterator.hasNext()) {
    7880                valuesBuilder.append("; ");
     
    9092    private String postprocessValue(String value) {
    9193        if (value != null) {
    92             if (FacetConstants.FIELD_DESCRIPTION.equals(field)) {
    93                 //remove language prefix
    94                 return value.replaceAll(FacetConstants.DESCRIPTION_LANGUAGE_PATTERN, "");
     94            final IConverter<String> converter = getFieldValueConverter();
     95            if (converter != null) {
     96                return converter.convertToString(value, null);
    9597            }
    9698        }
     
    98100    }
    99101
     102    private IConverter<String> getFieldValueConverter() {
     103        return VloWicketApplication.get().getFieldValueConverterProvider().getConverter(field);
     104    }
     105
    100106}
  • vlo/trunk/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/wicket/panels/record/FieldsTablePanel.java

    r6019 r6046  
    2626import eu.clarin.cmdi.vlo.service.PageParametersConverter;
    2727import eu.clarin.cmdi.vlo.wicket.components.FieldValueLabel;
    28 import eu.clarin.cmdi.vlo.wicket.model.DescriptionFieldModel;
     28import eu.clarin.cmdi.vlo.wicket.components.SmartLinkFieldValueLabel;
    2929import eu.clarin.cmdi.vlo.wicket.model.HandleLinkModel;
    3030import eu.clarin.cmdi.vlo.wicket.model.SolrFieldNameModel;
     
    3535import org.apache.wicket.AttributeModifier;
    3636import org.apache.wicket.Component;
    37 import org.apache.wicket.extensions.markup.html.basic.SmartLinkLabel;
    3837import org.apache.wicket.extensions.markup.html.basic.SmartLinkMultiLineLabel;
    3938import org.apache.wicket.markup.html.basic.Label;
     
    118117    }
    119118
    120     private Component createValueLabel(String id, final IModel<String> facetNameModel, final IModel<String> originalValueModel) {
     119    private Component createValueLabel(String id, final IModel<String> facetNameModel, final IModel<String> valueModel) {
    121120        final String fieldName = facetNameModel.getObject();
    122 
    123         // allow for postprocessing or wrapping of the value model depending on the field
    124         final IModel<String> valueModel = getValueModel(facetNameModel, originalValueModel);
    125121
    126122        if (FacetConstants.FIELD_LANGUAGE_CODE.equals(facetNameModel.getObject())) {
     
    128124        } else if (SMART_LINK_FIELDS.contains(fieldName)) {
    129125            // create label that generates links
    130             return new SmartLinkLabel(id, new HandleLinkModel(valueModel));
     126            return new SmartLinkFieldValueLabel(id, new HandleLinkModel(valueModel), facetNameModel);
    131127        } else {
    132128            // add a label for the facet value
     
    164160    }
    165161
    166     private IModel<String> getValueModel(IModel<String> facetNameModel, IModel<String> valueModel) {
    167         if (FacetConstants.FIELD_DESCRIPTION.equals(facetNameModel.getObject())) {
    168             //wrap in model that removes the language prefix
    169             return new DescriptionFieldModel(valueModel);
    170         }
    171         return valueModel;
    172     }
    173 
    174162}
  • vlo/trunk/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/wicket/provider/FieldValueConverterProviderImpl.java

    r6002 r6046  
    3131public class FieldValueConverterProviderImpl implements FieldValueConverterProvider {
    3232
     33    private final static Pattern LANGUAGE_CODE_PATTERN = Pattern.compile(FacetConstants.LANGUAGE_CODE_PATTERN);
    3334    private final LanguageCodeUtils languageCodeUtils;
    34     private Pattern LANGUAGE_CODE_PATTERN = Pattern.compile("(name|code):(.*)");
    3535
    3636    public FieldValueConverterProviderImpl(LanguageCodeUtils languageCodeUtils) {
     
    4343            case FacetConstants.FIELD_LANGUAGE_CODE:
    4444                return languageCodeConverter;
     45            case FacetConstants.FIELD_DESCRIPTION:
     46                return descriptionConverter;
    4547            default:
    4648                return null;
     
    4951    }
    5052
     53    /**
     54     * Abstract base class for one directional string converters
     55     */
    5156    private abstract class FieldValueConverter implements IConverter<String> {
    5257
     
    5863    }
    5964
     65    /**
     66     * Converter for language code/name strings (following the pattern of
     67     * {@link #LANGUAGE_CODE_PATTERN}) into language names
     68     *
     69     * @see LanguageCodeUtils#getLanguageNameForLanguageCode(java.lang.String)
     70     */
    6071    private final FieldValueConverter languageCodeConverter = new FieldValueConverter() {
    6172
     
    7586                }
    7687            }
    77            
     88
    7889            // does not match expected pattern, return original value
    7990            return fieldValue;
     
    8293    };
    8394
     95    /**
     96     * Converter for description field values that strips out any language code
     97     * prefix
     98     */
     99    private final FieldValueConverter descriptionConverter = new FieldValueConverter() {
     100
     101        @Override
     102        public String convertToString(String fieldValue, Locale locale) throws ConversionException {
     103            return fieldValue.replaceAll(FacetConstants.DESCRIPTION_LANGUAGE_PATTERN, "");
     104        }
     105
     106    };
     107
    84108}
Note: See TracChangeset for help on using the changeset viewer.