source: vlo/branches/to-wicket-1.6/vlo_web_app/src/main/java/eu/clarin/cmdi/vlo/pages/DocumentAttributesDataProvider.java @ 4224

Last change on this file since 4224 was 4224, checked in by twagoo, 10 years ago

No longer 'recycling' document attribute list iterator

File size: 2.4 KB
Line 
1package eu.clarin.cmdi.vlo.pages;
2
3import java.util.Collection;
4import java.util.Collections;
5import java.util.HashMap;
6import java.util.Iterator;
7import java.util.Map;
8
9import org.apache.solr.common.SolrDocument;
10import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
11import org.apache.wicket.model.IModel;
12import org.apache.wicket.model.Model;
13
14import eu.clarin.cmdi.vlo.FacetConstants;
15import java.util.Arrays;
16import java.util.HashSet;
17
18public class DocumentAttributesDataProvider extends SortableDataProvider<DocumentAttribute, String> {
19
20    private static final Map<String, Collection<Object>> NOT_FOUND_MAP = Collections.singletonMap("Document not found", (Collection<Object>) null);
21    private static final Collection<String> IGNORE_FACETS = new HashSet<String>(Arrays.asList(FacetConstants.FIELD_FORMAT));
22
23    private static final long serialVersionUID = 1L;
24
25    private final Map<String, Collection<Object>> fieldMap;
26
27    public DocumentAttributesDataProvider(SolrDocument solrDocument) {
28        if (solrDocument != null) {
29            fieldMap = new HashMap<String, Collection<Object>>();
30            Map<String, Collection<Object>> fieldValuesMap = solrDocument.getFieldValuesMap();
31            for (String entry : fieldValuesMap.keySet()) {
32                if (!ignoreEntry(entry)) { //Filter out all '_' starting (internal) fields
33                    fieldMap.put(entry, fieldValuesMap.get(entry));
34                }
35            }
36
37        } else {
38            fieldMap = null;
39        }
40    }
41
42    private boolean ignoreEntry(String entry) {
43//        if(entry.equals(FacetConstants.FIELD_COMPLETE_METADATA)){
44//            return false; // Do not ignore the "complete metadata" entry, even though it starts with a _
45//        }
46        return entry.startsWith("_") || IGNORE_FACETS.contains(entry);
47    }
48
49    @Override
50    public Iterator<? extends DocumentAttribute> iterator(long first, long count) {
51        if (fieldMap != null) {
52            return new DocumentAttributeList(fieldMap);
53        } else {
54            return new DocumentAttributeList(NOT_FOUND_MAP);
55        }
56    }
57
58    @Override
59    public IModel<DocumentAttribute> model(DocumentAttribute object) {
60        return new Model<DocumentAttribute>(object);
61    }
62
63    @Override
64    public long size() {
65        if (fieldMap != null) {
66            return fieldMap.size();
67        } else {
68            return NOT_FOUND_MAP.size();
69        }
70    }
71
72}
Note: See TracBrowser for help on using the repository browser.