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.record; |
---|
18 | |
---|
19 | import eu.clarin.cmdi.vlo.wicket.components.LanguageInfoLink; |
---|
20 | import com.google.common.collect.ImmutableSet; |
---|
21 | import eu.clarin.cmdi.vlo.FacetConstants; |
---|
22 | import eu.clarin.cmdi.vlo.JavaScriptResources; |
---|
23 | import eu.clarin.cmdi.vlo.config.VloConfig; |
---|
24 | import eu.clarin.cmdi.vlo.pojo.DocumentField; |
---|
25 | import eu.clarin.cmdi.vlo.pojo.FacetSelection; |
---|
26 | import eu.clarin.cmdi.vlo.pojo.QueryFacetsSelection; |
---|
27 | import eu.clarin.cmdi.vlo.service.PageParametersConverter; |
---|
28 | import eu.clarin.cmdi.vlo.wicket.components.FieldValueLabel; |
---|
29 | import eu.clarin.cmdi.vlo.wicket.components.SmartLinkFieldValueLabel; |
---|
30 | import eu.clarin.cmdi.vlo.wicket.model.HandleLinkModel; |
---|
31 | import eu.clarin.cmdi.vlo.wicket.model.SolrFieldDescriptionModel; |
---|
32 | import eu.clarin.cmdi.vlo.wicket.model.SolrFieldNameModel; |
---|
33 | import eu.clarin.cmdi.vlo.wicket.pages.FacetedSearchPage; |
---|
34 | import java.util.Collection; |
---|
35 | import java.util.Collections; |
---|
36 | import java.util.List; |
---|
37 | import org.apache.wicket.AttributeModifier; |
---|
38 | import org.apache.wicket.Component; |
---|
39 | import org.apache.wicket.behavior.AttributeAppender; |
---|
40 | import org.apache.wicket.extensions.markup.html.basic.SmartLinkMultiLineLabel; |
---|
41 | import org.apache.wicket.markup.head.CssHeaderItem; |
---|
42 | import org.apache.wicket.markup.head.IHeaderResponse; |
---|
43 | import org.apache.wicket.markup.head.JavaScriptHeaderItem; |
---|
44 | import org.apache.wicket.markup.html.basic.Label; |
---|
45 | import org.apache.wicket.markup.html.link.Link; |
---|
46 | import org.apache.wicket.markup.html.list.ListItem; |
---|
47 | import org.apache.wicket.markup.html.list.ListView; |
---|
48 | import org.apache.wicket.markup.html.panel.Panel; |
---|
49 | import org.apache.wicket.markup.repeater.Item; |
---|
50 | import org.apache.wicket.markup.repeater.data.DataView; |
---|
51 | import org.apache.wicket.markup.repeater.data.IDataProvider; |
---|
52 | import org.apache.wicket.model.AbstractReadOnlyModel; |
---|
53 | import org.apache.wicket.model.IModel; |
---|
54 | import org.apache.wicket.model.PropertyModel; |
---|
55 | import org.apache.wicket.spring.injection.annot.SpringBean; |
---|
56 | |
---|
57 | /** |
---|
58 | * |
---|
59 | * @author twagoo |
---|
60 | */ |
---|
61 | public class FieldsTablePanel extends Panel { |
---|
62 | |
---|
63 | /** |
---|
64 | * List of fields that should be rendered unescaped, {@literal i.e.} HTML |
---|
65 | * contained in the field should be preserved |
---|
66 | */ |
---|
67 | private final static Collection<String> UNESCAPED_VALUE_FIELDS |
---|
68 | = Collections.emptySet(); // ImmutableSet.of(FacetConstants.FIELD_LANGUAGE_CODE); |
---|
69 | |
---|
70 | /** |
---|
71 | * List of fields that should be rendered in a |
---|
72 | * {@link SmartLinkMultiLineLabel}, which detects URLs and turns them into |
---|
73 | * links |
---|
74 | */ |
---|
75 | private final static Collection<String> SMART_LINK_FIELDS |
---|
76 | = ImmutableSet.of( |
---|
77 | FacetConstants.FIELD_DESCRIPTION, |
---|
78 | FacetConstants.FIELD_LANDINGPAGE, |
---|
79 | FacetConstants.FIELD_COMPLETE_METADATA, |
---|
80 | FacetConstants.FIELD_SELF_LINK |
---|
81 | ); |
---|
82 | |
---|
83 | @SpringBean |
---|
84 | private VloConfig vloConfig; |
---|
85 | @SpringBean(name = "queryParametersConverter") |
---|
86 | private PageParametersConverter<QueryFacetsSelection> paramsConverter; |
---|
87 | |
---|
88 | public FieldsTablePanel(String id, IDataProvider<DocumentField> fieldProvider) { |
---|
89 | super(id); |
---|
90 | add(new DataView<DocumentField>("documentField", fieldProvider) { |
---|
91 | |
---|
92 | @Override |
---|
93 | protected void populateItem(final Item<DocumentField> item) { |
---|
94 | final IModel<DocumentField> fieldModel = item.getModel(); |
---|
95 | final PropertyModel<String> fieldNameModel = new PropertyModel<String>(fieldModel, "fieldName"); |
---|
96 | final SolrFieldNameModel friendlyFieldNameModel = new SolrFieldNameModel(fieldNameModel); |
---|
97 | final Label fieldName = new Label("fieldName", friendlyFieldNameModel); |
---|
98 | item.add(fieldName); |
---|
99 | fieldName.add(new AttributeAppender("title", new SolrFieldDescriptionModel(fieldNameModel))); |
---|
100 | final PropertyModel<List> valuesModel = new PropertyModel<List>(fieldModel, "values"); |
---|
101 | item.add(new ListView("values", valuesModel) { |
---|
102 | |
---|
103 | @Override |
---|
104 | protected void populateItem(final ListItem fieldValueItem) { |
---|
105 | // add a label that holds the field value |
---|
106 | fieldValueItem.add(createValueLabel("value", fieldNameModel, fieldValueItem.getModel())); |
---|
107 | // add a link for selecting the value in the search |
---|
108 | fieldValueItem.add(createFacetSelectLink("facetSelect", fieldNameModel, fieldValueItem.getModel())); |
---|
109 | } |
---|
110 | }); |
---|
111 | |
---|
112 | // if field has multiple values, set 'multiple' class on markup element |
---|
113 | item.add(new AttributeModifier("class", new AbstractReadOnlyModel<String>() { |
---|
114 | |
---|
115 | @Override |
---|
116 | public String getObject() { |
---|
117 | if (valuesModel.getObject().size() > 1) { |
---|
118 | return "multiplevalues"; |
---|
119 | } else { |
---|
120 | return null; |
---|
121 | } |
---|
122 | } |
---|
123 | })); |
---|
124 | } |
---|
125 | }); |
---|
126 | } |
---|
127 | |
---|
128 | private Component createValueLabel(String id, final IModel<String> facetNameModel, final IModel<String> valueModel) { |
---|
129 | final String fieldName = facetNameModel.getObject(); |
---|
130 | |
---|
131 | if (FacetConstants.FIELD_LANGUAGE_CODE.equals(facetNameModel.getObject())) { |
---|
132 | return new LanguageInfoLink(id, valueModel, facetNameModel); |
---|
133 | } else if (SMART_LINK_FIELDS.contains(fieldName)) { |
---|
134 | // create label that generates links |
---|
135 | return new SmartLinkFieldValueLabel(id, new HandleLinkModel(valueModel), facetNameModel); |
---|
136 | } else { |
---|
137 | // add a label for the facet value |
---|
138 | final Label fieldLabel = new FieldValueLabel(id, valueModel, facetNameModel); |
---|
139 | |
---|
140 | // some selected fields may have HTML that needs to be preserved... |
---|
141 | fieldLabel.setEscapeModelStrings(!UNESCAPED_VALUE_FIELDS.contains(fieldName)); |
---|
142 | return fieldLabel; |
---|
143 | } |
---|
144 | } |
---|
145 | |
---|
146 | private Link createFacetSelectLink(String id, final IModel<String> facetNameModel, final IModel valueModel) { |
---|
147 | return new Link(id) { |
---|
148 | |
---|
149 | @Override |
---|
150 | public void onClick() { |
---|
151 | final FacetSelection facetSelection = new FacetSelection(Collections.singleton(valueModel.getObject().toString())); |
---|
152 | final QueryFacetsSelection selection = new QueryFacetsSelection(Collections.singletonMap(facetNameModel.getObject(), facetSelection)); |
---|
153 | setResponsePage(FacetedSearchPage.class, paramsConverter.toParameters(selection)); |
---|
154 | } |
---|
155 | |
---|
156 | @Override |
---|
157 | protected void onConfigure() { |
---|
158 | super.onConfigure(); |
---|
159 | // only show for facet fields |
---|
160 | setVisible(isShowFacetSelectLinks() |
---|
161 | && vloConfig.getAllFacetFields().contains(facetNameModel.getObject())); |
---|
162 | } |
---|
163 | |
---|
164 | }; |
---|
165 | } |
---|
166 | |
---|
167 | protected boolean isShowFacetSelectLinks() { |
---|
168 | return true; |
---|
169 | } |
---|
170 | |
---|
171 | // re-enable for 'fancy' popups for the field descriptions |
---|
172 | // @Override |
---|
173 | // public void renderHead(IHeaderResponse response) { |
---|
174 | // // JQuery UI for tooltips |
---|
175 | // response.render(CssHeaderItem.forReference(JavaScriptResources.getJQueryUICSS())); |
---|
176 | // response.render(JavaScriptHeaderItem.forReference(JavaScriptResources.getFieldsTableJS())); |
---|
177 | // } |
---|
178 | |
---|
179 | } |
---|