2 * Copyright (C) 2017 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.service
;
11 import java
.util
.ArrayList
;
12 import java
.util
.Arrays
;
13 import java
.util
.List
;
14 import java
.util
.Optional
;
15 import java
.util
.UUID
;
16 import java
.util
.stream
.Collectors
;
18 import org
.springframework
.beans
.factory
.annotation
.Autowired
;
19 import org
.springframework
.beans
.factory
.annotation
.Qualifier
;
20 import org
.springframework
.stereotype
.Service
;
21 import org
.springframework
.transaction
.annotation
.Transactional
;
23 import com
.vaadin
.data
.util
.BeanItemContainer
;
25 import eu
.etaxonomy
.cdm
.api
.application
.CdmRepository
;
26 import eu
.etaxonomy
.cdm
.api
.service
.pager
.Pager
;
27 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
28 import eu
.etaxonomy
.cdm
.model
.name
.TypeDesignationStatusBase
;
29 import eu
.etaxonomy
.cdm
.model
.term
.DefinedTermBase
;
30 import eu
.etaxonomy
.cdm
.model
.term
.IEnumTerm
;
31 import eu
.etaxonomy
.cdm
.model
.term
.TermBase
;
32 import eu
.etaxonomy
.cdm
.model
.term
.TermType
;
33 import eu
.etaxonomy
.cdm
.model
.term
.TermVocabulary
;
34 import eu
.etaxonomy
.cdm
.persistence
.query
.OrderHint
;
37 * <b>Read only</b> service which can provide {@link BeanItemContainer BeanItemContainers} as source for select form
40 * @author a.kohlbecker
45 public class CdmBeanItemContainerFactory
{
48 @Qualifier("cdmRepository")
49 private CdmRepository repo
;
51 private final static List
<String
> INIT_STRATEGY
= Arrays
.asList(new String
[]{"$", "representations"});
53 private static List
<OrderHint
> orderHints
= new ArrayList
<>();
56 orderHints
.add(OrderHint
.BY_ORDER_INDEX
);
57 orderHints
.add(OrderHint
.ORDER_BY_TITLE_CACHE
);
60 @Transactional(readOnly
=true)
61 public BeanItemContainer
<DefinedTermBase
> buildTermItemContainer(TermType termType
) {
64 // TODO use TermCacher?
65 List
<DefinedTermBase
> terms
= repo
.getTermService().listByTermType(termType
, null, null, orderHints
, INIT_STRATEGY
);
66 BeanItemContainer
<DefinedTermBase
> termItemContainer
= new BeanItemContainer
<>(DefinedTermBase
.class);
67 termItemContainer
.addAll(terms
);
68 return termItemContainer
;
71 @Transactional(readOnly
=true)
72 public <T
extends DefinedTermBase
> BeanItemContainer
<T
> buildTermItemContainer(Class
<T
> type
, TermType termType
) {
75 // TODO use TermCacher?
76 List
<T
> terms
= repo
.getTermService().listByTermType(termType
, null, null, orderHints
, INIT_STRATEGY
);
77 BeanItemContainer
<T
> termItemContainer
= new BeanItemContainer
<>(DefinedTermBase
.class);
78 termItemContainer
.addAll(terms
);
79 return termItemContainer
;
83 @Transactional(readOnly
=true)
84 public BeanItemContainer
<DefinedTermBase
> buildVocabularyTermsItemContainer(UUID vocabularyUuid
) {
87 TermVocabulary vocab
= repo
.getVocabularyService().find(vocabularyUuid
);
88 Pager
<DefinedTermBase
> terms
= repo
.getVocabularyService().getTerms(vocab
, null, null, orderHints
, INIT_STRATEGY
);
89 BeanItemContainer
<DefinedTermBase
> termItemContainer
= new BeanItemContainer
<>(DefinedTermBase
.class);
90 termItemContainer
.addAll(terms
.getRecords());
91 return termItemContainer
;
94 @Transactional(readOnly
=true)
95 public <DTO
extends Object
> BeanItemContainer
<DTO
> buildVocabularyTermsItemContainer(UUID vocabularyUuid
, BeanToDTOConverter
<TermBase
, DTO
> converter
) {
98 TermVocabulary vocab
= repo
.getVocabularyService().find(vocabularyUuid
);
99 Pager
<DefinedTermBase
> terms
= repo
.getVocabularyService().getTerms(vocab
, null, null, orderHints
, INIT_STRATEGY
);
100 BeanItemContainer
<DTO
> termItemContainer
= new BeanItemContainer
<>(converter
.getDTOType());
101 termItemContainer
.addAll(terms
.getRecords().stream().map(b
-> converter
.toDTO(b
)).collect(Collectors
.toList()));
102 return termItemContainer
;
106 public BeanItemContainer
<DefinedTermBase
> buildTermItemContainer(UUID
... termUuid
) {
107 return buildTermItemContainer(Arrays
.asList(termUuid
));
110 @Transactional(readOnly
=true)
111 public BeanItemContainer
<DefinedTermBase
> buildTermItemContainer(List
<UUID
> termsUuids
) {
113 List
<DefinedTermBase
> terms
= repo
.getTermService().load(termsUuids
, INIT_STRATEGY
);
114 BeanItemContainer
<DefinedTermBase
> termItemContainer
= new BeanItemContainer
<>(DefinedTermBase
.class);
115 termItemContainer
.addAll(terms
);
116 return termItemContainer
;
119 public BeanItemContainer
<TypeDesignationStatusBase
> buildTypeDesignationStatusBaseItemContainer(List
<UUID
> termsUuids
,
120 Optional
<Boolean
> withHasDesignationSource
) {
122 List
<DefinedTermBase
> terms
= repo
.getTermService().load(termsUuids
, INIT_STRATEGY
);
123 BeanItemContainer
<TypeDesignationStatusBase
> termItemContainer
= new BeanItemContainer
<>(DefinedTermBase
.class);
124 termItemContainer
.addAll(terms
.stream()
125 .filter(t
-> t
instanceof TypeDesignationStatusBase
)
126 .map(t
-> (TypeDesignationStatusBase
)t
)
128 !withHasDesignationSource
.isPresent()
129 || withHasDesignationSource
.get().equals(false)
130 || tsb
.hasDesignationSource() == true
132 .collect(Collectors
.toList())
134 return termItemContainer
;
137 public BeanItemContainer
<TypeDesignationStatusBase
> buildTypeDesignationStatusBaseItemItemContainer(Class
<TypeDesignationStatusBase
> type
,
138 List
<OrderHint
> orderHints
, Optional
<Boolean
> withHasDesignationSource
) {
140 BeanItemContainer
<TypeDesignationStatusBase
> container
= buildBeanItemContainer(type
, null);
141 List
<TypeDesignationStatusBase
> filteredItems
= container
.getItemIds().stream().filter(tsb
->
142 !withHasDesignationSource
.isPresent()
143 || withHasDesignationSource
.get().equals(false)
144 || tsb
.hasDesignationSource() == true
146 .collect(Collectors
.toList());
147 container
.removeAllItems();
148 container
.addAll(filteredItems
);
153 @Transactional(readOnly
=true)
154 public <T
extends CdmBase
> BeanItemContainer
<T
> buildBeanItemContainer(Class
<T
> type
, List
<OrderHint
> orderHints
) {
156 if(orderHints
== null){
157 orderHints
= OrderHint
.defaultOrderHintsFor(type
);
160 List
<T
> terms
= repo
.getCommonService().list(type
, (Integer
)null, (Integer
)null,
162 Arrays
.asList(new String
[]{"$"}));
163 BeanItemContainer
<T
> termItemContainer
= new BeanItemContainer
<>(type
);
164 termItemContainer
.addAll(terms
);
165 return termItemContainer
;
168 public <T
extends CdmBase
> BeanItemContainer
<T
> buildBeanItemContainer(Class
<T
> type
) {
169 return buildBeanItemContainer(type
, null);
172 public <T
extends IEnumTerm
<T
>> BeanItemContainer
<T
> buildEnumTermItemContainer(Class
<T
> termType
, T
... enumTerms
) {
173 BeanItemContainer
<T
> termItemContainer
= new BeanItemContainer
<>(termType
);
174 List
<T
> termList
= Arrays
.asList(enumTerms
);
175 termItemContainer
.addAll(termList
);
176 return termItemContainer
;
179 public void clearSession() {