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 eu.clarin.cmdi.vlo.VloWicketApplication; |
---|
21 | import eu.clarin.cmdi.vlo.service.SolrDocumentService; |
---|
22 | import org.apache.solr.common.SolrDocument; |
---|
23 | import org.apache.wicket.model.LoadableDetachableModel; |
---|
24 | |
---|
25 | /** |
---|
26 | * Detachable model for Solr documents that uses the {@link SolrDocumentService} |
---|
27 | * registered on the application to load a single document. |
---|
28 | * |
---|
29 | * @author twagoo |
---|
30 | * @see VloWicketApplication#getDocumentService() |
---|
31 | */ |
---|
32 | public class SolrDocumentModel extends LoadableDetachableModel<SolrDocument> { |
---|
33 | |
---|
34 | private final String docId; |
---|
35 | |
---|
36 | public SolrDocumentModel(SolrDocument document) { |
---|
37 | super(document); |
---|
38 | this.docId = (String) document.getFieldValue(FacetConstants.FIELD_ID); |
---|
39 | } |
---|
40 | |
---|
41 | public SolrDocumentModel(String docId) { |
---|
42 | this.docId = docId; |
---|
43 | } |
---|
44 | |
---|
45 | @Override |
---|
46 | protected SolrDocument load() { |
---|
47 | if (docId == null) { |
---|
48 | return null; |
---|
49 | } else { |
---|
50 | return VloWicketApplication.get().getDocumentService().getDocument(docId); |
---|
51 | } |
---|
52 | } |
---|
53 | |
---|
54 | @Override |
---|
55 | public String toString() { |
---|
56 | return String.format("%s docId=%s attached=%b", super.toString(), docId, isAttached()); |
---|
57 | } |
---|
58 | |
---|
59 | } |
---|