Project

General

Profile

Download (4.87 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
import java.util.UUID;
15

    
16
import org.springframework.beans.factory.annotation.Autowired;
17
import org.springframework.beans.factory.annotation.Qualifier;
18

    
19
import com.vaadin.data.util.BeanItemContainer;
20
import com.vaadin.spring.annotation.SpringComponent;
21
import com.vaadin.ui.ListSelect;
22

    
23
import eu.etaxonomy.cdm.api.application.CdmRepository;
24
import eu.etaxonomy.cdm.api.service.pager.Pager;
25
import eu.etaxonomy.cdm.model.common.CdmBase;
26
import eu.etaxonomy.cdm.model.common.DefinedTermBase;
27
import eu.etaxonomy.cdm.model.common.TermType;
28
import eu.etaxonomy.cdm.model.common.TermVocabulary;
29
import eu.etaxonomy.cdm.persistence.query.OrderHint;
30

    
31
/**
32
 * @author a.kohlbecker
33
 * @since Apr 6, 2017
34
 *
35
 */
36
@SpringComponent
37
public class SelectFieldFactory {
38

    
39
    @Autowired
40
    @Qualifier("cdmRepository")
41
    private CdmRepository repo;
42

    
43
    private final static List<String> INIT_STRATEGY = Arrays.asList(new String[]{"$", "representations"});
44

    
45
    private static List<OrderHint> orderHints = new ArrayList<>();
46

    
47
    static {
48
        orderHints.add(OrderHint.BY_ORDER_INDEX);
49
        orderHints.add(OrderHint.ORDER_BY_TITLE_CACHE);
50
    }
51

    
52
    /**
53
     * Constructor for the Spring Bean Factory
54
     */
55
    public SelectFieldFactory(){
56
        this.repo = null;
57
    }
58

    
59
    /**
60
     * Constructor to be used by presenter classes directly
61
     *
62
     * @param repo
63
     */
64
    public SelectFieldFactory(CdmRepository repo){
65
        this.repo = repo;
66
    }
67

    
68
    public ListSelect createListSelect(String caption, TermType termType){
69
        BeanItemContainer<DefinedTermBase> termItemContainer = buildBeanItemContainer(termType);
70
        ListSelect select = new ListSelect(caption, termItemContainer);
71
        return select;
72
    }
73

    
74
    /**
75
     *
76
     * @param caption
77
     * @param type
78
     * @return
79
     */
80
    public <T extends CdmBase> ListSelect createListSelect(String caption, Class<T> type){
81
        return createListSelect(caption, type, null);
82
    }
83

    
84
    public <T extends CdmBase> ListSelect createListSelect(String caption, Class<T> type, List<OrderHint> orderHints){
85
        return createListSelect(caption, type, orderHints, null);
86
    }
87

    
88
    /**
89
     *
90
     * @param caption
91
     * @param type
92
     * @param orderHints
93
     * @param propertyId the property id from which to read the label
94
     * @return
95
     */
96
    public <T extends CdmBase> ListSelect createListSelect(String caption, Class<T> type, List<OrderHint> orderHints, String propertyId){
97

    
98
        if(orderHints == null){
99
            orderHints = OrderHint.defaultOrderHintsFor(type);
100
        }
101

    
102
        BeanItemContainer<T> termItemContainer = buildBeanItemContainer(type, orderHints);
103
        ListSelect select = new ListSelect(caption, termItemContainer);
104

    
105
        // guess property id to use for display
106
        if(propertyId == null) {
107
            if(orderHints != null && !orderHints.isEmpty()){
108
                propertyId = orderHints.get(0).getPropertyName();
109
            }
110
        }
111
        if(propertyId != null){
112
            select.setItemCaptionPropertyId(propertyId);
113
        }
114
        return select;
115
    }
116

    
117

    
118
    /**
119
     * @param termType
120
     */
121
    public BeanItemContainer<DefinedTermBase> buildBeanItemContainer(TermType termType) {
122
        // TODO use TermCacher?
123
        List<DefinedTermBase> terms = repo.getTermService().listByTermType(termType, null, null, orderHints, INIT_STRATEGY);
124
        BeanItemContainer<DefinedTermBase> termItemContainer = new BeanItemContainer<>(DefinedTermBase.class);
125
        termItemContainer.addAll(terms);
126
        return termItemContainer;
127
    }
128

    
129
    /**
130
     * @param termType
131
     */
132
    public BeanItemContainer<DefinedTermBase> buildBeanItemContainer(UUID vocabularyUuid) {
133

    
134
        TermVocabulary vocab = repo.getVocabularyService().find(vocabularyUuid);
135
        Pager<DefinedTermBase> terms = repo.getVocabularyService().getTerms(vocab, null, null, orderHints, INIT_STRATEGY);
136
        BeanItemContainer<DefinedTermBase> termItemContainer = new BeanItemContainer<>(DefinedTermBase.class);
137
        termItemContainer.addAll(terms.getRecords());
138
        return termItemContainer;
139
    }
140

    
141
    /**
142
     * @param termType
143
     */
144
    public <T extends CdmBase> BeanItemContainer<T> buildBeanItemContainer(Class<T> type, List<OrderHint> orderHints) {
145

    
146
        List<T> terms = repo.getCommonService().list(type, (Integer)null, (Integer)null,
147
                orderHints,
148
                Arrays.asList(new String[]{"$"}));
149
        BeanItemContainer<T> termItemContainer = new BeanItemContainer<>(type);
150
        termItemContainer.addAll(terms);
151
        return termItemContainer;
152
    }
153

    
154
}
(6-6/6)