1 | package eu.clarin.cmdi.vlo.pages; |
---|
2 | |
---|
3 | /** |
---|
4 | * |
---|
5 | * @author paucas |
---|
6 | */ |
---|
7 | import java.io.Serializable; |
---|
8 | import java.util.ArrayList; |
---|
9 | import java.util.Comparator; |
---|
10 | import java.util.Iterator; |
---|
11 | import java.util.LinkedList; |
---|
12 | import java.util.List; |
---|
13 | import java.util.Map.Entry; |
---|
14 | import java.util.TreeMap; |
---|
15 | |
---|
16 | import org.apache.solr.client.solrj.response.FacetField.Count; |
---|
17 | import org.apache.wicket.markup.html.WebMarkupContainer; |
---|
18 | import org.apache.wicket.markup.html.basic.Label; |
---|
19 | import org.apache.wicket.markup.html.link.ExternalLink; |
---|
20 | import org.apache.wicket.markup.html.list.ListItem; |
---|
21 | import org.apache.wicket.markup.html.list.ListView; |
---|
22 | import org.apache.wicket.markup.html.panel.Panel; |
---|
23 | import org.apache.wicket.markup.repeater.RepeatingView; |
---|
24 | import org.apache.wicket.markup.repeater.data.IDataProvider; |
---|
25 | import org.apache.wicket.model.Model; |
---|
26 | |
---|
27 | public class AlphabeticalPanel extends Panel { |
---|
28 | |
---|
29 | private static final long serialVersionUID = 1L; |
---|
30 | private final List<String> foundCharacters = new LinkedList<String>(); |
---|
31 | private final Integer facetMinOccurs; |
---|
32 | |
---|
33 | public AlphabeticalPanel(String id, IDataProvider data, final SearchPageQuery query, Integer facetMinOccurs) { |
---|
34 | super(id); |
---|
35 | this.facetMinOccurs = facetMinOccurs; |
---|
36 | |
---|
37 | RepeatingView sortedDataViewLeft = new RepeatingView("sortedDataViewLeft"); |
---|
38 | RepeatingView sortedDataViewRight = new RepeatingView("sortedDataViewRight"); |
---|
39 | TreeMap<Character, List<Count>> dataMap = transposeToMap(data); |
---|
40 | int numberOfKeys = 0; |
---|
41 | for (Entry entry : dataMap.entrySet()) { |
---|
42 | if (numberOfKeys < dataMap.size() / 2) { |
---|
43 | sortedDataViewLeft.add(categoryContainer(sortedDataViewLeft, entry, query)); |
---|
44 | numberOfKeys++; |
---|
45 | } else { |
---|
46 | sortedDataViewRight.add(categoryContainer(sortedDataViewRight, entry, query)); |
---|
47 | } |
---|
48 | } |
---|
49 | |
---|
50 | ListView<String> alphabeticalView = new ListView<String>("alphabeticalView", new Model((Serializable) foundCharacters)) { |
---|
51 | |
---|
52 | private static final long serialVersionUID = 1L; |
---|
53 | |
---|
54 | @Override |
---|
55 | protected void populateItem(ListItem<String> item) { |
---|
56 | final String character = item.getModelObject(); |
---|
57 | ExternalLink link = new ExternalLink("refLink", "#" + character); |
---|
58 | link.add(new Label("character", character)); |
---|
59 | item.add(link); |
---|
60 | } |
---|
61 | }; |
---|
62 | add(alphabeticalView); |
---|
63 | add(sortedDataViewLeft); |
---|
64 | add(sortedDataViewRight); |
---|
65 | } |
---|
66 | |
---|
67 | private WebMarkupContainer categoryContainer(RepeatingView parentView, Entry entry, SearchPageQuery query) { |
---|
68 | WebMarkupContainer container = new WebMarkupContainer(parentView.newChildId()); |
---|
69 | Character charSet = (Character) entry.getKey(); |
---|
70 | if (Character.isLetter(charSet)) { |
---|
71 | container.add(new AnchorPanel("anchorPanel", charSet.toString())); |
---|
72 | foundCharacters.add(charSet.toString()); |
---|
73 | } else if (Character.isDigit(charSet) & !foundCharacters.contains("0...9")) { |
---|
74 | container.add(new AnchorPanel("anchorPanel", "0...9")); |
---|
75 | foundCharacters.add("0...9"); |
---|
76 | } else if (!Character.isLetterOrDigit(charSet) & !foundCharacters.contains("Other")) { |
---|
77 | container.add(new AnchorPanel("anchorPanel", "Other")); |
---|
78 | foundCharacters.add("Other"); |
---|
79 | } |
---|
80 | RepeatingView charView = new RepeatingView("valueList"); |
---|
81 | List<Count> countList = (List<Count>) entry.getValue(); |
---|
82 | for (Count c : countList) { |
---|
83 | charView.add(new FacetLinkPanel(charView.newChildId(), new Model<Count>(c), query)); |
---|
84 | container.add(charView); |
---|
85 | } |
---|
86 | return container; |
---|
87 | } |
---|
88 | |
---|
89 | /** |
---|
90 | * |
---|
91 | * @param data |
---|
92 | * @return |
---|
93 | */ |
---|
94 | private TreeMap<Character, List<Count>> transposeToMap(IDataProvider data) { |
---|
95 | TreeMap<Character, List<Count>> dataMap = new TreeMap<Character, List<Count>>(new CharGroupComparator()); |
---|
96 | Iterator<? extends Count> iter = data.iterator(0, data.size()); |
---|
97 | List<Count> numericalCount = new ArrayList<Count>(); |
---|
98 | List<Count> otherCount = new ArrayList<Count>(); |
---|
99 | while (iter.hasNext()) { |
---|
100 | Count count = iter.next(); |
---|
101 | if(count.getCount() < facetMinOccurs) |
---|
102 | continue; |
---|
103 | Character key = count.getName().trim().charAt(0); |
---|
104 | if (Character.isDigit(key)) { |
---|
105 | numericalCount.add(count); |
---|
106 | } else if (!Character.isLetterOrDigit(key)) { |
---|
107 | otherCount.add(count); |
---|
108 | } else if (!dataMap.containsKey(key)) { |
---|
109 | List<Count> countList = new ArrayList<Count>(); |
---|
110 | countList.add(count); |
---|
111 | dataMap.put(Character.toUpperCase(key), countList); |
---|
112 | } else { |
---|
113 | dataMap.get(key).add(count); |
---|
114 | } |
---|
115 | } |
---|
116 | if (!numericalCount.isEmpty()) { |
---|
117 | dataMap.put('0', numericalCount); |
---|
118 | } |
---|
119 | if (!otherCount.isEmpty()) { |
---|
120 | dataMap.put('!', otherCount); |
---|
121 | } |
---|
122 | return dataMap; |
---|
123 | } |
---|
124 | |
---|
125 | /** |
---|
126 | * |
---|
127 | */ |
---|
128 | private class CharGroupComparator implements Comparator<Character> { |
---|
129 | |
---|
130 | @Override |
---|
131 | public int compare(Character a, Character b) { |
---|
132 | if (Character.isLetter(a) & !Character.isLetter(b)) { |
---|
133 | return -1; |
---|
134 | } else if (Character.isLetter(a) && Character.isLetter(b)) { |
---|
135 | return a.toString().compareToIgnoreCase(b.toString()); |
---|
136 | } else if (Character.isDigit(a) & !Character.isLetterOrDigit(b)) { |
---|
137 | return -1; |
---|
138 | } else { |
---|
139 | return 1; |
---|
140 | } |
---|
141 | } |
---|
142 | } |
---|
143 | } |
---|