Project

General

Profile

Download (4.96 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
import eu.etaxonomy.vaadin.component.RelatedEntityListSelect;
28

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

    
37
    @Autowired
38
    @Qualifier("cdmRepository")
39
    private CdmRepository repo;
40

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

    
43
    private static List<OrderHint> orderHints = new ArrayList<>();
44

    
45
    static {
46
        orderHints.add(OrderHint.BY_ORDER_INDEX);
47
        orderHints.add(OrderHint.ORDER_BY_TITLE_CACHE);
48
    }
49

    
50
    public ListSelect createListSelect(String caption, TermType termType){
51
        BeanItemContainer<DefinedTermBase> termItemContainer = buildBeanItemContainer(termType);
52
        ListSelect select = new ListSelect(caption, termItemContainer);
53
        return select;
54
    }
55

    
56
    /**
57
     *
58
     * @param caption
59
     * @param type
60
     * @return
61
     */
62
    public <T extends CdmBase> ListSelect createListSelect(String caption, Class<T> type){
63
        return createListSelect(caption, type, null);
64
    }
65

    
66
    public <T extends CdmBase> ListSelect createListSelect(String caption, Class<T> type, List<OrderHint> orderHints){
67
        return createListSelect(caption, type, orderHints, null);
68
    }
69

    
70
    /**
71
     *
72
     * @param caption
73
     * @param type
74
     * @param orderHints
75
     * @param propertyId the property id from which to read the label
76
     * @return
77
     */
78
    public <T extends CdmBase> ListSelect createListSelect(String caption, Class<T> type, List<OrderHint> orderHints, String propertyId){
79

    
80
        if(orderHints == null){
81
            orderHints = OrderHint.defaultOrderHintsFor(type);
82
        }
83

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

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

    
99
    /**
100
    *
101
    * @param caption
102
    * @param type
103
    * @param orderHints
104
    * @param propertyId the property id from which to read the label
105
    * @return
106
    */
107
   public <T extends CdmBase> RelatedEntityListSelect<T> createListSelectEditor(String caption, Class<T> type, List<OrderHint> orderHints, String propertyId){
108

    
109
       if(orderHints == null){
110
           orderHints = OrderHint.defaultOrderHintsFor(type);
111
       }
112

    
113
       BeanItemContainer<T> termItemContainer = buildBeanItemContainer(type, orderHints);
114
       RelatedEntityListSelect<T> selectEditor = new RelatedEntityListSelect<T>(caption, type, termItemContainer);
115

    
116
    // guess property id to use for display
117
       if(propertyId == null) {
118
           if(orderHints != null && !orderHints.isEmpty()){
119
               propertyId = orderHints.get(0).getPropertyName();
120
           }
121
       }
122
       if(propertyId != null){
123
           selectEditor.getSelect().setItemCaptionPropertyId(propertyId);
124
       }
125
       return selectEditor;
126
   }
127

    
128
    /**
129
     * @param termType
130
     */
131
    private BeanItemContainer<DefinedTermBase> buildBeanItemContainer(TermType termType) {
132
        // TODO use TermCacher?
133
        List<DefinedTermBase> terms = repo.getTermService().listByTermType(termType, null, null, orderHints, INIT_STRATEGY);
134
        BeanItemContainer<DefinedTermBase> termItemContainer = new BeanItemContainer<>(DefinedTermBase.class);
135
        termItemContainer.addAll(terms);
136
        return termItemContainer;
137
    }
138

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

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

    
152
}
(6-6/6)