source: vlo/trunk/vlo-web-app/src/main/java/eu/clarin/cmdi/vlo/wicket/panels/record/FieldsTablePanel.java @ 6002

Last change on this file since 6002 was 6002, checked in by Twan Goosen, 9 years ago

Created a value converter for language codes. It is used by a new 'FieldValueLabel?', which is used in various panels. This way the language name is shown but the selection happens on basis of code.
TODO: generate link to language info page
Refs #553

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