source: vlo/branches/vlo-3.1/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/wicket/model/SolrFieldStringModel.java @ 6048

Last change on this file since 6048 was 6048, checked in by Twan Goosen, 9 years ago

merged changes from trunk

File size: 3.6 KB
Line 
1/*
2 * Copyright (C) 2014 CLARIN
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17package eu.clarin.cmdi.vlo.wicket.model;
18
19import eu.clarin.cmdi.vlo.VloWicketApplication;
20import java.util.Collection;
21import java.util.Iterator;
22import org.apache.solr.common.SolrDocument;
23import org.apache.wicket.model.AbstractReadOnlyModel;
24import org.apache.wicket.model.IModel;
25import org.apache.wicket.util.convert.IConverter;
26
27/**
28 * Model that provides field values as String values for a given field values
29 * model, both for singular values and multiple values (imploding the latter
30 * into a single string)
31 *
32 * @author twagoo
33 */
34public class SolrFieldStringModel extends AbstractReadOnlyModel<String> {
35
36    private final IModel<Collection<Object>> fieldModel;
37    private final String field;
38
39    /**
40     * Wraps the document model and specified field name into a
41     * {@link SolrFieldModel} to obtain field values
42     *
43     * @param documentModel model of document that holds the field values
44     * @param fieldName name of the field to take value from
45     */
46    public SolrFieldStringModel(IModel<SolrDocument> documentModel, String fieldName) {
47        fieldModel = new SolrFieldModel<>(documentModel, fieldName);
48        field = fieldName;
49    }
50
51    @Override
52    public String getObject() {
53        final Collection<Object> fieldValues = fieldModel.getObject();
54        if (fieldValues != null) {
55            return getValueString(fieldValues);
56        }
57        return null;
58    }
59
60    private String getValueString(final Collection<Object> fieldValues) {
61        final Iterator<Object> iterator = fieldValues.iterator();
62        if (iterator.hasNext()) {
63            final String firstValue = iterator.next().toString();
64            if (iterator.hasNext()) {
65                return getMultipleValuesString(firstValue, iterator);
66            } else {
67                return firstValue;
68            }
69        } else {
70            return null;
71        }
72    }
73
74    protected String getMultipleValuesString(final String firstValue, final Iterator<Object> iterator) {
75        // for multiple value strings, run every individual value through the converter
76        final StringBuilder valuesBuilder = new StringBuilder(postprocessValue(firstValue)).append("; ");
77        while (iterator.hasNext()) {
78            valuesBuilder.append(postprocessValue(iterator.next().toString()));
79            if (iterator.hasNext()) {
80                valuesBuilder.append("; ");
81            }
82        }
83        return valuesBuilder.toString();
84    }
85
86    @Override
87    public void detach() {
88        super.detach();
89        fieldModel.detach();
90    }
91
92    private String postprocessValue(String value) {
93        if (value != null) {
94            final IConverter<String> converter = getFieldValueConverter();
95            if (converter != null) {
96                return converter.convertToString(value, null);
97            }
98        }
99        return value;
100    }
101
102    private IConverter<String> getFieldValueConverter() {
103        return VloWicketApplication.get().getFieldValueConverterProvider().getConverter(field);
104    }
105
106}
Note: See TracBrowser for help on using the repository browser.