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 | */ |
---|
17 | package eu.clarin.cmdi.vlo.wicket.panels.search; |
---|
18 | |
---|
19 | import eu.clarin.cmdi.vlo.FacetConstants; |
---|
20 | import eu.clarin.cmdi.vlo.pojo.SearchContext; |
---|
21 | import eu.clarin.cmdi.vlo.service.FieldFilter; |
---|
22 | import eu.clarin.cmdi.vlo.service.ResourceStringConverter; |
---|
23 | import eu.clarin.cmdi.vlo.wicket.LazyResourceInfoUpdateBehavior; |
---|
24 | import eu.clarin.cmdi.vlo.wicket.ResourceTypeCssBehaviour; |
---|
25 | import eu.clarin.cmdi.vlo.wicket.components.RecordPageLink; |
---|
26 | import eu.clarin.cmdi.vlo.wicket.components.SmartLinkFieldValueLabel; |
---|
27 | import eu.clarin.cmdi.vlo.wicket.model.CollectionListModel; |
---|
28 | import eu.clarin.cmdi.vlo.wicket.model.HandleLinkModel; |
---|
29 | import eu.clarin.cmdi.vlo.wicket.model.NullFallbackModel; |
---|
30 | import eu.clarin.cmdi.vlo.wicket.model.ResourceInfoModel; |
---|
31 | import eu.clarin.cmdi.vlo.wicket.model.SolrFieldModel; |
---|
32 | import eu.clarin.cmdi.vlo.wicket.model.SolrFieldStringModel; |
---|
33 | import eu.clarin.cmdi.vlo.wicket.panels.record.FieldsTablePanel; |
---|
34 | import eu.clarin.cmdi.vlo.wicket.panels.record.ResourceLinkDetailsPanel; |
---|
35 | import eu.clarin.cmdi.vlo.wicket.provider.DocumentFieldsProvider; |
---|
36 | import java.util.List; |
---|
37 | import org.apache.solr.common.SolrDocument; |
---|
38 | import org.apache.wicket.ajax.AjaxRequestTarget; |
---|
39 | import org.apache.wicket.markup.html.WebMarkupContainer; |
---|
40 | import org.apache.wicket.markup.html.basic.Label; |
---|
41 | import org.apache.wicket.markup.html.link.ExternalLink; |
---|
42 | import org.apache.wicket.markup.html.list.ListItem; |
---|
43 | import org.apache.wicket.markup.html.list.PageableListView; |
---|
44 | import org.apache.wicket.markup.html.panel.GenericPanel; |
---|
45 | import org.apache.wicket.migrate.StringResourceModelMigration; |
---|
46 | import org.apache.wicket.model.AbstractReadOnlyModel; |
---|
47 | import org.apache.wicket.model.IModel; |
---|
48 | import org.apache.wicket.model.Model; |
---|
49 | import org.apache.wicket.model.PropertyModel; |
---|
50 | import org.apache.wicket.spring.injection.annot.SpringBean; |
---|
51 | |
---|
52 | /** |
---|
53 | * |
---|
54 | * @author twagoo |
---|
55 | */ |
---|
56 | public class SearchResultItemExpandedPanel extends GenericPanel<SolrDocument> { |
---|
57 | |
---|
58 | private static final int MAX_RESOURCES_TO_SHOW = 10; |
---|
59 | |
---|
60 | @SpringBean(name = "searchResultPropertiesFilter") |
---|
61 | private FieldFilter propertiesFilter; |
---|
62 | @SpringBean(name = "resourceStringConverter") |
---|
63 | ResourceStringConverter resourceStringConverter; |
---|
64 | @SpringBean(name = "resolvingResourceStringConverter") |
---|
65 | ResourceStringConverter resolvingResourceStringConverter; |
---|
66 | @SpringBean(name = "documentFieldOrder") |
---|
67 | private List<String> fieldOrder; |
---|
68 | |
---|
69 | public SearchResultItemExpandedPanel(String id, final IModel<SolrDocument> documentModel, final IModel<SearchContext> searchContextModel) { |
---|
70 | super(id, documentModel); |
---|
71 | |
---|
72 | // add untruncated description |
---|
73 | final NullFallbackModel descriptionModel = new NullFallbackModel(new SolrFieldStringModel(documentModel, FacetConstants.FIELD_DESCRIPTION), ""); |
---|
74 | add(new SmartLinkFieldValueLabel("description", descriptionModel, Model.of(FacetConstants.FIELD_DESCRIPTION))); |
---|
75 | |
---|
76 | // add link to record |
---|
77 | add(new RecordPageLink("recordLink", documentModel, searchContextModel)); |
---|
78 | |
---|
79 | // table with some basic properties |
---|
80 | add(new FieldsTablePanel("documentProperties", new DocumentFieldsProvider(documentModel, propertiesFilter, fieldOrder)) { |
---|
81 | |
---|
82 | @Override |
---|
83 | protected boolean isShowFacetSelectLinks() { |
---|
84 | // do not show the value selection links |
---|
85 | return false; |
---|
86 | } |
---|
87 | |
---|
88 | }); |
---|
89 | |
---|
90 | // add a container for the resources (only visible if there are actual resources) |
---|
91 | add(createResourcesView("resources", searchContextModel)); |
---|
92 | } |
---|
93 | |
---|
94 | private WebMarkupContainer createResourcesView(String id, final IModel<SearchContext> selectionModel) { |
---|
95 | final SolrFieldModel<String> resourceModel = new SolrFieldModel<>(getModel(), FacetConstants.FIELD_RESOURCE); |
---|
96 | // create a container for the list view that is only visible if there actually are resources |
---|
97 | final WebMarkupContainer container = new WebMarkupContainer(id) { |
---|
98 | @Override |
---|
99 | protected void onConfigure() { |
---|
100 | super.onConfigure(); |
---|
101 | setVisible(resourceModel.getObject() != null); |
---|
102 | } |
---|
103 | |
---|
104 | }; |
---|
105 | |
---|
106 | final PageableListView resourcesView = createResourcesList("resource", resourceModel); |
---|
107 | container.add(resourcesView); |
---|
108 | |
---|
109 | // create a link to the record page that is only visible when there are more resources than shown |
---|
110 | final RecordPageLink moreLink = new RecordPageLink("more", getModel(), selectionModel) { |
---|
111 | |
---|
112 | @Override |
---|
113 | protected void onConfigure() { |
---|
114 | super.onConfigure(); |
---|
115 | setVisible(resourcesView.getPageCount() > 1); |
---|
116 | } |
---|
117 | |
---|
118 | }; |
---|
119 | // add a record page link that shows the number of resources not shown |
---|
120 | moreLink.add(new Label("moreLabel", StringResourceModelMigration.of("resources.more", new AbstractReadOnlyModel<Integer>() { |
---|
121 | |
---|
122 | @Override |
---|
123 | public Integer getObject() { |
---|
124 | return resourceModel.getObject().size() - MAX_RESOURCES_TO_SHOW; |
---|
125 | } |
---|
126 | |
---|
127 | }, "more..."))); |
---|
128 | container.add(moreLink); |
---|
129 | |
---|
130 | return container; |
---|
131 | } |
---|
132 | |
---|
133 | private PageableListView createResourcesList(String id, SolrFieldModel<String> resourceModel) { |
---|
134 | // list of resources in this record |
---|
135 | final IModel<List<String>> resourceListModel = new CollectionListModel<>(resourceModel); |
---|
136 | // use a a pageable view so that the number of resources actually shown is limited |
---|
137 | return new PageableListView<String>(id, resourceListModel, MAX_RESOURCES_TO_SHOW) { |
---|
138 | |
---|
139 | @Override |
---|
140 | protected void populateItem(final ListItem<String> item) { |
---|
141 | // get resource string converted into a ResourceInfo model |
---|
142 | final ResourceInfoModel resourceInfoModel = new ResourceInfoModel(resourceStringConverter, item.getModel()); |
---|
143 | |
---|
144 | // add a link to the resource with the file name as its label |
---|
145 | final ExternalLink resourceLink = new ExternalLink("resourceLink", new HandleLinkModel(new PropertyModel(resourceInfoModel, "href"))); |
---|
146 | resourceLink.add(new Label("resourceName", new PropertyModel(resourceInfoModel, "fileName"))); |
---|
147 | resourceLink.setOutputMarkupId(true); |
---|
148 | |
---|
149 | // once loaded, make Ajax request to resolve handles and update resource link |
---|
150 | resourceLink.add(new LazyResourceInfoUpdateBehavior(resolvingResourceStringConverter, resourceInfoModel) { |
---|
151 | |
---|
152 | @Override |
---|
153 | protected void onUpdate(AjaxRequestTarget target) { |
---|
154 | // update resource link |
---|
155 | target.add(resourceLink); |
---|
156 | } |
---|
157 | }); |
---|
158 | |
---|
159 | resourceLink.add(new ResourceLinkDetailsPanel("details", resourceInfoModel)); |
---|
160 | |
---|
161 | // sets the css class depending on the resource type |
---|
162 | item.add(new ResourceTypeCssBehaviour(resourceInfoModel)); |
---|
163 | |
---|
164 | // add to list |
---|
165 | item.add(resourceLink); |
---|
166 | } |
---|
167 | }; |
---|
168 | } |
---|
169 | |
---|
170 | @Override |
---|
171 | public void detachModels() { |
---|
172 | super.detachModels(); |
---|
173 | } |
---|
174 | |
---|
175 | } |
---|