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 | */ |
---|
17 | package eu.clarin.cmdi.vlo.wicket.model; |
---|
18 | |
---|
19 | import eu.clarin.cmdi.vlo.FacetConstants; |
---|
20 | import java.util.Collection; |
---|
21 | import java.util.Iterator; |
---|
22 | import org.apache.solr.common.SolrDocument; |
---|
23 | import org.apache.wicket.model.AbstractReadOnlyModel; |
---|
24 | import org.apache.wicket.model.IModel; |
---|
25 | |
---|
26 | /** |
---|
27 | * Model that provides field values as String values for a given field values |
---|
28 | * model, both for singular values and multiple values (imploding the latter |
---|
29 | * into a single string) |
---|
30 | * |
---|
31 | * @author twagoo |
---|
32 | */ |
---|
33 | public class SolrFieldStringModel extends AbstractReadOnlyModel<String> { |
---|
34 | |
---|
35 | private final IModel<Collection<Object>> fieldModel; |
---|
36 | private final String field; |
---|
37 | |
---|
38 | /** |
---|
39 | * Wraps the document model and specified field name into a |
---|
40 | * {@link SolrFieldModel} to obtain field values |
---|
41 | * |
---|
42 | * @param documentModel model of document that holds the field values |
---|
43 | * @param fieldName name of the field to take value from |
---|
44 | */ |
---|
45 | public SolrFieldStringModel(IModel<SolrDocument> documentModel, String fieldName) { |
---|
46 | fieldModel = new SolrFieldModel<Object>(documentModel, fieldName); |
---|
47 | field = fieldName; |
---|
48 | } |
---|
49 | |
---|
50 | @Override |
---|
51 | public String getObject() { |
---|
52 | final Collection<Object> fieldValues = fieldModel.getObject(); |
---|
53 | if (fieldValues != null) { |
---|
54 | return getValueString(fieldValues); |
---|
55 | } |
---|
56 | return null; |
---|
57 | } |
---|
58 | |
---|
59 | private String getValueString(final Collection<Object> fieldValues) { |
---|
60 | final Iterator<Object> iterator = fieldValues.iterator(); |
---|
61 | if (iterator.hasNext()) { |
---|
62 | final String firstValue = postprocessValue(iterator.next().toString()); |
---|
63 | if (iterator.hasNext()) { |
---|
64 | return getMultipleValuesString(firstValue, iterator); |
---|
65 | } else { |
---|
66 | return firstValue; |
---|
67 | } |
---|
68 | } else { |
---|
69 | return null; |
---|
70 | } |
---|
71 | } |
---|
72 | |
---|
73 | protected String getMultipleValuesString(final String firstValue, final Iterator<Object> iterator) { |
---|
74 | final StringBuilder valuesBuilder = new StringBuilder(firstValue).append("; "); |
---|
75 | while (iterator.hasNext()) { |
---|
76 | valuesBuilder.append(iterator.next().toString()); |
---|
77 | if (iterator.hasNext()) { |
---|
78 | valuesBuilder.append("; "); |
---|
79 | } |
---|
80 | } |
---|
81 | return valuesBuilder.toString(); |
---|
82 | } |
---|
83 | |
---|
84 | @Override |
---|
85 | public void detach() { |
---|
86 | super.detach(); |
---|
87 | fieldModel.detach(); |
---|
88 | } |
---|
89 | |
---|
90 | private String postprocessValue(String value) { |
---|
91 | if (value != null) { |
---|
92 | if (FacetConstants.FIELD_DESCRIPTION.equals(field)) { |
---|
93 | //remove language prefix |
---|
94 | return value.replaceAll(FacetConstants.DESCRIPTION_LANGUAGE_PATTERN, ""); |
---|
95 | } |
---|
96 | } |
---|
97 | return value; |
---|
98 | } |
---|
99 | |
---|
100 | } |
---|