source: vlo/branches/vlo-3.0/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/wicket/components/SearchResultItemPanel.java @ 4622

Last change on this file since 4622 was 4622, checked in by Twan Goosen, 10 years ago

created stub for record page and links to it from search result items

File size: 7.3 KB
Line 
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 */
17package eu.clarin.cmdi.vlo.wicket.components;
18
19import eu.clarin.cmdi.vlo.wicket.provider.ResouceTypeCountDataProvider;
20import eu.clarin.cmdi.vlo.FacetConstants;
21import eu.clarin.cmdi.vlo.pojo.QueryFacetsSelection;
22import eu.clarin.cmdi.vlo.pojo.ResourceTypeCount;
23import eu.clarin.cmdi.vlo.service.ResourceTypeCountingService;
24import eu.clarin.cmdi.vlo.wicket.model.NullFallbackModel;
25import eu.clarin.cmdi.vlo.wicket.model.SolrFieldModel;
26import eu.clarin.cmdi.vlo.wicket.model.SolrFieldStringModel;
27import java.util.Locale;
28import org.apache.solr.common.SolrDocument;
29import org.apache.wicket.markup.html.basic.Label;
30import org.apache.wicket.markup.html.link.Link;
31import org.apache.wicket.markup.html.panel.Panel;
32import org.apache.wicket.markup.repeater.Item;
33import org.apache.wicket.markup.repeater.data.DataView;
34import org.apache.wicket.markup.repeater.data.IDataProvider;
35import org.apache.wicket.model.IModel;
36import org.apache.wicket.spring.injection.annot.SpringBean;
37import org.apache.wicket.util.convert.ConversionException;
38import org.apache.wicket.util.convert.IConverter;
39
40/**
41 *
42 * @author twagoo
43 */
44public class SearchResultItemPanel extends Panel {
45
46    private final static ResourceTypeCountConverter resourceTypeCountConverter = new ResourceTypeCountConverter();
47
48    @SpringBean
49    private ResourceTypeCountingService countingService;
50    private final IModel<QueryFacetsSelection> selectionModel;
51    private final IModel<SolrDocument> documentModel;
52
53    public SearchResultItemPanel(String id, IModel<SolrDocument> documentModel, IModel<QueryFacetsSelection> selectionModel) {
54        super(id, documentModel);
55        this.documentModel = documentModel;
56        this.selectionModel = selectionModel;
57
58        final Link recordLink = new RecordPageLink("recordLink", documentModel, selectionModel);
59        recordLink.add(new SolrFieldLabel("title", documentModel, FacetConstants.FIELD_NAME));
60        add(recordLink);
61
62        add(new SolrFieldLabel("description", documentModel, FacetConstants.FIELD_DESCRIPTION, "<no description>"));
63
64        // get model for resources
65        final SolrFieldModel<String> resourcesModel = new SolrFieldModel<String>(documentModel, FacetConstants.FIELD_RESOURCE);
66        // wrap with a count provider
67        final ResouceTypeCountDataProvider countProvider = new ResouceTypeCountDataProvider(resourcesModel, countingService);
68        // view that shows provided counts
69        // TODO: hide if no resources
70        add(new ResourceCountDataView("resourceCount", countProvider));
71    }
72
73    @Override
74    public void detachModels() {
75        super.detachModels();
76        // not passed to super
77        selectionModel.detach();
78    }
79
80    /**
81     * Label that shows the content of a Solr field by its string value (using
82     * {@link SolrFieldStringModel})
83     */
84    private static class SolrFieldLabel extends Label {
85
86        public SolrFieldLabel(String id, IModel<SolrDocument> documentModel, String fieldName) {
87            super(id, new SolrFieldStringModel(documentModel, fieldName));
88        }
89
90        public SolrFieldLabel(String id, IModel<SolrDocument> documentModel, String fieldName, String nullFallback) {
91            super(id,
92                    new NullFallbackModel(
93                            new SolrFieldStringModel(documentModel, fieldName), nullFallback));
94        }
95
96    }
97
98    /**
99     * Data view for resource type counts coming from a data provider for
100     * {@link ResourceTypeCount}
101     */
102    private class ResourceCountDataView extends DataView<ResourceTypeCount> {
103
104        public ResourceCountDataView(String id, IDataProvider<ResourceTypeCount> dataProvider) {
105            super(id, dataProvider);
106        }
107
108        @Override
109        protected void populateItem(Item<ResourceTypeCount> item) {
110            final Link resourceLink = new RecordPageLink("recordLink", documentModel, selectionModel);
111            final Label label = new Label("resourceCountLabel", item.getModel()) {
112
113                @Override
114                public <C> IConverter<C> getConverter(Class<C> type) {
115                    if (type == ResourceTypeCount.class) {
116                        return (IConverter<C>) resourceTypeCountConverter;
117                    } else {
118                        return super.getConverter(type);
119                    }
120                }
121
122            };
123
124            resourceLink.add(label);
125            item.add(resourceLink);
126        }
127    }
128
129    /**
130     * One-way converter for resource type counts that generates string
131     * representations like "1 video file", "2 video files" or "3 text
132     * documents"
133     */
134    private static class ResourceTypeCountConverter implements IConverter<ResourceTypeCount> {
135
136        @Override
137        public ResourceTypeCount convertToObject(String value, Locale locale) throws ConversionException {
138            throw new UnsupportedOperationException("Not supported");
139        }
140
141        @Override
142        public String convertToString(ResourceTypeCount value, Locale locale) {
143            final String resourceTypeString;
144            if (value.getCount() == 1) {
145                resourceTypeString = getSingularResourceTypeString(value);
146            } else {
147                resourceTypeString = getPluralResourceTypeString(value);
148            }
149            return String.format("%d %s", value.getCount(), resourceTypeString);
150        }
151
152        private String getSingularResourceTypeString(ResourceTypeCount value) {
153            //TODO: read from resource bundle
154            switch (value.getResourceType()) {
155                case ANNOTATION:
156                    return "annotation file";
157                case AUDIO:
158                    return "audio file";
159                case VIDEO:
160                    return "video file";
161                case IMAGE:
162                    return "image";
163                case TEXT:
164                    return "text document";
165                case OTHER:
166                    return "other";
167                default:
168                    return "unknown";
169            }
170        }
171
172        private String getPluralResourceTypeString(ResourceTypeCount value) {
173            //TODO: read from resource bundle
174            switch (value.getResourceType()) {
175                case ANNOTATION:
176                    return "annotation files";
177                case AUDIO:
178                    return "audio files";
179                case VIDEO:
180                    return "video files";
181                case IMAGE:
182                    return "images";
183                case TEXT:
184                    return "text documents";
185                case OTHER:
186                    return "other";
187                default:
188                    return "unknown";
189            }
190        }
191
192    }
193}
Note: See TracBrowser for help on using the repository browser.