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.collect.Lists; |
---|
20 | import eu.clarin.cmdi.vlo.pojo.DocumentField; |
---|
21 | import java.util.Collection; |
---|
22 | import java.util.List; |
---|
23 | import org.apache.solr.common.SolrDocument; |
---|
24 | import org.apache.wicket.model.AbstractReadOnlyModel; |
---|
25 | import org.apache.wicket.model.IModel; |
---|
26 | import org.apache.wicket.model.Model; |
---|
27 | |
---|
28 | /** |
---|
29 | * Model for {@link DocumentField} that wraps a document model and a field name |
---|
30 | * |
---|
31 | * @author twagoo |
---|
32 | * @param <T> type of the field |
---|
33 | */ |
---|
34 | public class DocumentFieldModel<T> extends AbstractReadOnlyModel<DocumentField<T>> implements DocumentField { |
---|
35 | |
---|
36 | private final IModel<String> fieldName; |
---|
37 | private final IModel<Collection<T>> valueModel; |
---|
38 | |
---|
39 | /** |
---|
40 | * Uses the document model to create a {@link SolrFieldModel} to provide the |
---|
41 | * field values |
---|
42 | * |
---|
43 | * @param documentModel model that provides the document |
---|
44 | * @param fieldName name of the field to represent |
---|
45 | */ |
---|
46 | public DocumentFieldModel(IModel<SolrDocument> documentModel, String fieldName) { |
---|
47 | this(new SolrFieldModel<T>(documentModel, fieldName), Model.of(fieldName)); |
---|
48 | } |
---|
49 | |
---|
50 | public DocumentFieldModel(IModel<Collection<T>> valueModel, IModel<String> fieldName) { |
---|
51 | this.fieldName = fieldName; |
---|
52 | this.valueModel = valueModel; |
---|
53 | } |
---|
54 | |
---|
55 | @Override |
---|
56 | public DocumentField getObject() { |
---|
57 | return this; |
---|
58 | } |
---|
59 | |
---|
60 | @Override |
---|
61 | public String getFieldName() { |
---|
62 | return fieldName.getObject(); |
---|
63 | } |
---|
64 | |
---|
65 | @Override |
---|
66 | public List<T> getValues() { |
---|
67 | return Lists.newArrayList(valueModel.getObject()); |
---|
68 | } |
---|
69 | |
---|
70 | @Override |
---|
71 | public String toString() { |
---|
72 | return String.format("%s: %s", fieldName, valueModel); |
---|
73 | } |
---|
74 | |
---|
75 | } |
---|