1 | package eu.clarin.cmdi.vlo.pages; |
---|
2 | |
---|
3 | import eu.clarin.cmdi.vlo.VloWebApplication; |
---|
4 | import eu.clarin.cmdi.vlo.VloWebApplication.ThemedSession; |
---|
5 | import java.util.ArrayList; |
---|
6 | import java.util.HashSet; |
---|
7 | import java.util.List; |
---|
8 | import java.util.Set; |
---|
9 | import org.apache.solr.client.solrj.response.FacetField; |
---|
10 | import org.apache.solr.client.solrj.response.FacetField.Count; |
---|
11 | import org.apache.wicket.PageParameters; |
---|
12 | import org.apache.wicket.behavior.SimpleAttributeModifier; |
---|
13 | import org.apache.wicket.markup.html.WebMarkupContainer; |
---|
14 | import org.apache.wicket.markup.html.basic.Label; |
---|
15 | import org.apache.wicket.markup.html.link.BookmarkablePageLink; |
---|
16 | import org.apache.wicket.markup.html.list.ListItem; |
---|
17 | import org.apache.wicket.markup.html.list.ListView; |
---|
18 | import org.apache.wicket.model.IModel; |
---|
19 | import org.apache.wicket.model.Model; |
---|
20 | |
---|
21 | public class FacetBoxPanel extends BasePanel { |
---|
22 | private static final Set<String> IGNORABLE_VALUES = new HashSet<String>(); |
---|
23 | static { |
---|
24 | IGNORABLE_VALUES.add("unknown"); |
---|
25 | IGNORABLE_VALUES.add("unspecified"); |
---|
26 | } |
---|
27 | private static final int MAX_NR_OF_FACET_VALUES = 10; |
---|
28 | private static final long serialVersionUID = 1L; |
---|
29 | private Label label; |
---|
30 | private FacetHeaderPanel facetHeaderPanel; |
---|
31 | private FacetModel facetModel; |
---|
32 | private int maxNrOfFacetValues; |
---|
33 | |
---|
34 | public FacetBoxPanel(String id, IModel<FacetField> model, String tooltipText) { |
---|
35 | super(id, model); |
---|
36 | setOutputMarkupId(true); |
---|
37 | setMaxNrOfFacetValues(MAX_NR_OF_FACET_VALUES); |
---|
38 | SimpleAttributeModifier tooltip = new SimpleAttributeModifier("title", tooltipText); |
---|
39 | add(tooltip); |
---|
40 | } |
---|
41 | |
---|
42 | @SuppressWarnings({"serial"}) |
---|
43 | public FacetBoxPanel create(final SearchPageQuery query) { |
---|
44 | final FacetField facetField = (FacetField) getDefaultModelObject(); |
---|
45 | facetModel = new FacetModel(facetField); |
---|
46 | facetModel.setSelectedValue(query.getSelectedValue(facetField)); |
---|
47 | label = new Label("headerLabel", facetField.getName()); |
---|
48 | add(label); |
---|
49 | facetHeaderPanel = new FacetHeaderPanel("facetHeaderPanel", new Model<FacetModel>(facetModel), query); |
---|
50 | if (facetModel.isSelected()) { |
---|
51 | add(facetHeaderPanel); |
---|
52 | } else { |
---|
53 | add(new WebMarkupContainer("facetHeaderPanel")); |
---|
54 | } |
---|
55 | final boolean showMore = facetField.getValueCount() > maxNrOfFacetValues + 1; |
---|
56 | List<Count> values = getFacetListForBox(facetField, showMore); |
---|
57 | ListView<Count> facetList = new ListView<Count>("facetList", values) { |
---|
58 | @Override |
---|
59 | protected void populateItem(ListItem<Count> item) { |
---|
60 | item.add(new FacetLinkPanel("facetLinks", item.getModel(), query)); |
---|
61 | } |
---|
62 | |
---|
63 | @Override |
---|
64 | public boolean isVisible() { |
---|
65 | return !facetModel.isSelected(); |
---|
66 | } |
---|
67 | }; |
---|
68 | add(facetList); |
---|
69 | PageParameters pageParameters = query.getPageParameters(); |
---|
70 | pageParameters.add(ShowAllFacetValuesPage.SELECTED_FACET_PARAM, facetField.getName()); |
---|
71 | pageParameters.add(ShowAllFacetValuesPage.FACET_MIN_OCCURS, "1"); |
---|
72 | |
---|
73 | // pageParameters = webApp.reflectPersistentParameters(pageParameters); |
---|
74 | pageParameters = ((ThemedSession)getSession()).reflectPersistentParameters(pageParameters); |
---|
75 | |
---|
76 | |
---|
77 | add(new BookmarkablePageLink("showMore", ShowAllFacetValuesPage.class, pageParameters) { |
---|
78 | |
---|
79 | @Override |
---|
80 | public boolean isVisible() { |
---|
81 | return !facetModel.isSelected() && showMore; |
---|
82 | } |
---|
83 | |
---|
84 | }); |
---|
85 | return this; |
---|
86 | } |
---|
87 | |
---|
88 | List<Count> getFacetListForBox(final FacetField facetField, final boolean showMore) { |
---|
89 | List<Count> allValues = facetField.getValues(); |
---|
90 | List<Count> values = new ArrayList<Count>(); |
---|
91 | if (showMore) { |
---|
92 | if (facetField.getValueCount() == maxNrOfFacetValues || facetField.getValueCount() == maxNrOfFacetValues + 1) { //Show all values, the "more" link can be used as the extra facet |
---|
93 | values = allValues; |
---|
94 | } else {// make a sublist |
---|
95 | //IGNORABLE_VALUES (like "unknown") are move to the back of the list and should only be shown when you click "more...", unless the list is too small then whe can just show them. |
---|
96 | List<Count> ignorables = new ArrayList<Count>(); |
---|
97 | for (int i = 0; values.size() < maxNrOfFacetValues && i < allValues.size(); i++) { |
---|
98 | Count count = allValues.get(i); |
---|
99 | if (!IGNORABLE_VALUES.contains(count.getName().toLowerCase())) { |
---|
100 | values.add(count); |
---|
101 | } else { |
---|
102 | ignorables.add(count); |
---|
103 | } |
---|
104 | } |
---|
105 | int stillToAdd = maxNrOfFacetValues - values.size(); |
---|
106 | for (int i = 0; i < stillToAdd && i < ignorables.size(); i++) { |
---|
107 | values.add(ignorables.get(i)); |
---|
108 | |
---|
109 | } |
---|
110 | } |
---|
111 | } else { //show all values |
---|
112 | values = allValues; |
---|
113 | } |
---|
114 | return values; |
---|
115 | } |
---|
116 | |
---|
117 | public void replaceHeader(boolean isSelected, String selectedValue) { |
---|
118 | facetModel.setSelectedValue(selectedValue); |
---|
119 | if (isSelected) { |
---|
120 | label.replaceWith(facetHeaderPanel); |
---|
121 | } else { |
---|
122 | facetHeaderPanel.replaceWith(label); |
---|
123 | } |
---|
124 | |
---|
125 | } |
---|
126 | |
---|
127 | void setMaxNrOfFacetValues(int maxNrOfFacetValues) { |
---|
128 | this.maxNrOfFacetValues = maxNrOfFacetValues; |
---|
129 | } |
---|
130 | |
---|
131 | } |
---|