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 com.google.common.base.Function; |
---|
20 | import com.google.common.collect.Collections2; |
---|
21 | import java.util.Collection; |
---|
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 for a given Solr document |
---|
28 | * |
---|
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! |
---|
33 | */ |
---|
34 | public class SolrFieldModel<T> extends AbstractReadOnlyModel<Collection<T>> { |
---|
35 | |
---|
36 | private final IModel<SolrDocument> documentModel; |
---|
37 | private final String fieldName; |
---|
38 | |
---|
39 | /** |
---|
40 | * |
---|
41 | * @param documentModel model of document that holds the field values |
---|
42 | * @param fieldName name of the field to take value from |
---|
43 | */ |
---|
44 | public SolrFieldModel(IModel<SolrDocument> documentModel, String fieldName) { |
---|
45 | this.documentModel = documentModel; |
---|
46 | this.fieldName = fieldName; |
---|
47 | } |
---|
48 | |
---|
49 | @Override |
---|
50 | public Collection<T> getObject() { |
---|
51 | final Collection<Object> fieldValues = documentModel.getObject().getFieldValues(fieldName); |
---|
52 | if (fieldValues == null) { |
---|
53 | return null; |
---|
54 | } else { |
---|
55 | return transformCollectionType(fieldValues); |
---|
56 | } |
---|
57 | } |
---|
58 | |
---|
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; |
---|
72 | } |
---|
73 | }); |
---|
74 | } |
---|
75 | |
---|
76 | @Override |
---|
77 | public void detach() { |
---|
78 | super.detach(); |
---|
79 | documentModel.detach(); |
---|
80 | } |
---|
81 | |
---|
82 | } |
---|