1
|
/**
|
2
|
* Copyright (C) 2017 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.vaadin.component;
|
10
|
|
11
|
import java.util.ArrayList;
|
12
|
import java.util.Arrays;
|
13
|
import java.util.List;
|
14
|
|
15
|
import org.springframework.beans.factory.annotation.Autowired;
|
16
|
import org.springframework.beans.factory.annotation.Qualifier;
|
17
|
|
18
|
import com.vaadin.data.util.BeanItemContainer;
|
19
|
import com.vaadin.spring.annotation.SpringComponent;
|
20
|
import com.vaadin.ui.ListSelect;
|
21
|
|
22
|
import eu.etaxonomy.cdm.api.application.CdmRepository;
|
23
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
24
|
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
|
25
|
import eu.etaxonomy.cdm.model.common.TermType;
|
26
|
import eu.etaxonomy.cdm.persistence.query.OrderHint;
|
27
|
|
28
|
/**
|
29
|
* @author a.kohlbecker
|
30
|
* @since Apr 6, 2017
|
31
|
*
|
32
|
*/
|
33
|
@SpringComponent
|
34
|
public class SelectFieldFactory {
|
35
|
|
36
|
@Autowired
|
37
|
@Qualifier("cdmRepository")
|
38
|
private CdmRepository repo;
|
39
|
|
40
|
private final static List<String> INIT_STRATEGY = Arrays.asList(new String[]{"$", "representations"});
|
41
|
|
42
|
private static List<OrderHint> orderHints = new ArrayList<>();
|
43
|
|
44
|
static {
|
45
|
orderHints.add(OrderHint.BY_ORDER_INDEX);
|
46
|
orderHints.add(OrderHint.ORDER_BY_TITLE_CACHE);
|
47
|
}
|
48
|
|
49
|
public ListSelect createListSelect(String caption, TermType termType){
|
50
|
BeanItemContainer<DefinedTermBase> termItemContainer = buildBeanItemContainer(termType);
|
51
|
ListSelect select = new ListSelect(caption, termItemContainer);
|
52
|
return select;
|
53
|
}
|
54
|
|
55
|
/**
|
56
|
* @param termType
|
57
|
*/
|
58
|
private BeanItemContainer<DefinedTermBase> buildBeanItemContainer(TermType termType) {
|
59
|
// TODO use TermCacher?
|
60
|
List<DefinedTermBase> terms = repo.getTermService().listByTermType(termType, null, null, orderHints, INIT_STRATEGY);
|
61
|
BeanItemContainer<DefinedTermBase> termItemContainer = new BeanItemContainer<>(DefinedTermBase.class);
|
62
|
termItemContainer.addAll(terms);
|
63
|
return termItemContainer;
|
64
|
}
|
65
|
|
66
|
/**
|
67
|
*
|
68
|
* @param caption
|
69
|
* @param type
|
70
|
* @return
|
71
|
*/
|
72
|
public <T extends CdmBase> ListSelect createListSelect(String caption, Class<T> type){
|
73
|
return createListSelect(caption, type, null);
|
74
|
}
|
75
|
|
76
|
public <T extends CdmBase> ListSelect createListSelect(String caption, Class<T> type, List<OrderHint> orderHints){
|
77
|
return createListSelect(caption, type, orderHints, null);
|
78
|
}
|
79
|
|
80
|
/**
|
81
|
*
|
82
|
* @param caption
|
83
|
* @param type
|
84
|
* @param orderHints
|
85
|
* @param propertyId the property id from which to read the label
|
86
|
* @return
|
87
|
*/
|
88
|
public <T extends CdmBase> ListSelect createListSelect(String caption, Class<T> type, List<OrderHint> orderHints, String propertyId){
|
89
|
|
90
|
if(orderHints == null){
|
91
|
orderHints = OrderHint.defaultOrderHintsFor(type);
|
92
|
}
|
93
|
|
94
|
BeanItemContainer<T> termItemContainer = buildBeanItemContainer(type, orderHints);
|
95
|
ListSelect select = new ListSelect(caption, termItemContainer);
|
96
|
|
97
|
// guess property id to use for display
|
98
|
if(propertyId == null) {
|
99
|
if(orderHints != null && !orderHints.isEmpty()){
|
100
|
propertyId = orderHints.get(0).getPropertyName();
|
101
|
}
|
102
|
}
|
103
|
if(propertyId != null){
|
104
|
select.setItemCaptionPropertyId(propertyId);
|
105
|
}
|
106
|
return select;
|
107
|
}
|
108
|
|
109
|
/**
|
110
|
* @param termType
|
111
|
*/
|
112
|
private <T extends CdmBase> BeanItemContainer<T> buildBeanItemContainer(Class<T> type, List<OrderHint> orderHints) {
|
113
|
|
114
|
List<T> terms = repo.getCommonService().list(type, (Integer)null, (Integer)null,
|
115
|
orderHints,
|
116
|
Arrays.asList(new String[]{"$"}));
|
117
|
BeanItemContainer<T> termItemContainer = new BeanItemContainer<>(type);
|
118
|
termItemContainer.addAll(terms);
|
119
|
return termItemContainer;
|
120
|
}
|
121
|
|
122
|
}
|