1 | package eu.clarin.cmdi.vlo.pages; |
---|
2 | |
---|
3 | import org.apache.solr.common.SolrDocument; |
---|
4 | import org.apache.solr.common.SolrDocumentList; |
---|
5 | import org.apache.wicket.markup.html.WebMarkupContainer; |
---|
6 | import org.apache.wicket.markup.html.basic.Label; |
---|
7 | import org.apache.wicket.markup.html.link.BookmarkablePageLink; |
---|
8 | import org.apache.wicket.markup.html.panel.Panel; |
---|
9 | |
---|
10 | import eu.clarin.cmdi.vlo.FacetConstants; |
---|
11 | import eu.clarin.cmdi.vlo.VloWebApplication.ThemedSession; |
---|
12 | import eu.clarin.cmdi.vlo.dao.DaoLocator; |
---|
13 | |
---|
14 | public class PrevNextHeaderPanel extends Panel { |
---|
15 | |
---|
16 | private static final long serialVersionUID = 1L; |
---|
17 | |
---|
18 | /** |
---|
19 | * Creates Prev/Next Header can take a while to generate |
---|
20 | * @param id |
---|
21 | * @param docId |
---|
22 | * @param query |
---|
23 | */ |
---|
24 | public PrevNextHeaderPanel(String id, String docId, SearchPageQuery query) { |
---|
25 | super(id); |
---|
26 | addPrevNextLabels(docId, query); |
---|
27 | } |
---|
28 | |
---|
29 | /** |
---|
30 | * Constructor that creates dummy prev/next label, which can be used to load before the real labels are calculated. |
---|
31 | * @param id |
---|
32 | */ |
---|
33 | public PrevNextHeaderPanel(String id) { |
---|
34 | super(id); |
---|
35 | addDummyPrev(); |
---|
36 | addDummyResultCount(); |
---|
37 | addDummyNext(); |
---|
38 | } |
---|
39 | |
---|
40 | private void addDummyPrev() { |
---|
41 | add(new WebMarkupContainer("prev").setVisible(false)); |
---|
42 | } |
---|
43 | |
---|
44 | private void addDummyNext() { |
---|
45 | add(new WebMarkupContainer("next").setVisible(false)); |
---|
46 | } |
---|
47 | |
---|
48 | private void addDummyResultCount() { |
---|
49 | add(new WebMarkupContainer("resultcount")); |
---|
50 | } |
---|
51 | |
---|
52 | private void addPrevNextLabels(String docId, SearchPageQuery query) { |
---|
53 | int index = -1; |
---|
54 | //Very inefficient query, possibly gets all the docId's (when no facets are selected) just to create the next/prev labels. |
---|
55 | //Cannot see another way of doing this because the result page can be bookmarked and therefor the list needs to be refetched. |
---|
56 | //If some facets are selected the list of id's is already a lot smaller and performance is not an issue. I lazy loaded the panel just to everything a bit more snappy. |
---|
57 | SolrDocumentList docIdList = DaoLocator.getSearchResultsDao().getDocIdList(query.getSolrQuery().getCopy()); |
---|
58 | |
---|
59 | for (int i = 0; i < docIdList.size(); i++) { |
---|
60 | SolrDocument doc = docIdList.get(i); |
---|
61 | if (doc.getFieldValue(FacetConstants.FIELD_ID).equals(docId)) { |
---|
62 | index = i; |
---|
63 | break; |
---|
64 | } |
---|
65 | } |
---|
66 | if (index > 0) { |
---|
67 | String prevDocId = docIdList.get(index - 1).getFieldValue(FacetConstants.FIELD_ID).toString(); |
---|
68 | BookmarkablePageLink<ShowResultPage> prev = ShowResultPage.createBookMarkableLink("prev", query, prevDocId, (ThemedSession)getSession()); |
---|
69 | add(prev); |
---|
70 | } else { |
---|
71 | addDummyPrev(); |
---|
72 | } |
---|
73 | |
---|
74 | add(new Label("resultcount", "Record "+(index+1)+" out of "+docIdList.size())); |
---|
75 | |
---|
76 | if (index < (docIdList.size() - 1) && index >= 0) { |
---|
77 | String prevDocId = docIdList.get(index + 1).getFieldValue(FacetConstants.FIELD_ID).toString(); |
---|
78 | BookmarkablePageLink<ShowResultPage> next = ShowResultPage.createBookMarkableLink("next", query, prevDocId, (ThemedSession)getSession()); |
---|
79 | add(next); |
---|
80 | } else { |
---|
81 | addDummyNext(); |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | } |
---|