Project

General

Profile

Download (3.47 KB) Statistics
| Branch: | Tag: | Revision:
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

    
78
        if(orderHints == null){
79
            orderHints = OrderHint.defaultOrderHintsFor(type);
80
        }
81

    
82
        BeanItemContainer<T> termItemContainer = buildBeanItemContainer(type, orderHints);
83
        ListSelect select = new ListSelect(caption, termItemContainer);
84

    
85
        // guess property id to use for display
86
        String propertyId = null;
87
        if(orderHints != null && !orderHints.isEmpty()){
88
            propertyId = orderHints.get(0).getPropertyName();
89
            select.setItemCaptionPropertyId(propertyId);
90
        }
91
        return select;
92
    }
93

    
94
    /**
95
     * @param termType
96
     */
97
    private <T extends CdmBase> BeanItemContainer<T> buildBeanItemContainer(Class<T> type, List<OrderHint> orderHints) {
98

    
99
        List<T> terms = repo.getCommonService().list(type, (Integer)null, (Integer)null,
100
                orderHints,
101
                Arrays.asList(new String[]{"$"}));
102
        BeanItemContainer<T> termItemContainer = new BeanItemContainer<>(type);
103
        termItemContainer.addAll(terms);
104
        return termItemContainer;
105
    }
106

    
107
}
(6-6/6)