2 * Copyright (C) 2021 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
.taxeditor
.ui
.dialog
.selection
;
11 import java
.util
.ArrayList
;
12 import java
.util
.HashSet
;
13 import java
.util
.Iterator
;
14 import java
.util
.List
;
16 import java
.util
.UUID
;
18 import org
.eclipse
.jface
.viewers
.ILabelProvider
;
19 import org
.eclipse
.jface
.viewers
.LabelProvider
;
20 import org
.eclipse
.swt
.widgets
.Shell
;
22 import eu
.etaxonomy
.cdm
.api
.service
.IAgentService
;
23 import eu
.etaxonomy
.cdm
.api
.service
.dto
.EntityDTOBase
;
24 import eu
.etaxonomy
.cdm
.api
.service
.dto
.IdentifiedEntityDTO
;
25 import eu
.etaxonomy
.cdm
.model
.agent
.AgentBase
;
26 import eu
.etaxonomy
.cdm
.model
.agent
.Person
;
27 import eu
.etaxonomy
.cdm
.model
.agent
.Team
;
28 import eu
.etaxonomy
.cdm
.model
.agent
.TeamOrPersonBase
;
29 import eu
.etaxonomy
.cdm
.model
.metadata
.PreferencePredicate
;
30 import eu
.etaxonomy
.cdm
.persistence
.dto
.TeamOrPersonUuidAndTitleCache
;
31 import eu
.etaxonomy
.cdm
.persistence
.dto
.UuidAndTitleCache
;
32 import eu
.etaxonomy
.taxeditor
.newWizard
.AbstractNewEntityWizard
;
33 import eu
.etaxonomy
.taxeditor
.newWizard
.NewPersonWizard
;
34 import eu
.etaxonomy
.taxeditor
.newWizard
.NewTeamWizard
;
35 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
36 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
42 public class CollectorSelectionDialog
extends AgentSelectionDialog
<AgentBase
<?
>> {
43 Team collectorTeam
= null;
51 * @param selectTeamMember
53 protected CollectorSelectionDialog(Shell shell
, String title
, boolean multi
, String settings
,
54 AgentBase
<?
> agent
, boolean selectTeamMember
) {
55 super(shell
, title
, multi
, settings
, agent
, selectTeamMember
);
56 if (selectTeamMember
&& agent
instanceof Team
){
57 this.collectorTeam
= (Team
)agent
;
62 public static <S
extends AgentBase
> S
select(Shell shell
,
63 S entity
, boolean selectTeamMember
) {
65 CollectorSelectionDialog dialog
= new CollectorSelectionDialog(shell
,
66 "Choose Collector", false, CollectorSelectionDialog
.class.getCanonicalName(), entity
, selectTeamMember
);
67 return (S
) getSelectionFromDialog(dialog
);
71 protected ILabelProvider
createListLabelProvider() {
72 return new CollectorLabelProvider();
76 protected AbstractNewEntityWizard
getNewEntityWizard(String parameter
) {
77 if(TEAM
.equals(parameter
)){
78 return new NewTeamWizard(false, true);
80 else if(PERSON
.equals(parameter
)){
81 return new NewPersonWizard();
84 throw new IllegalArgumentException("Could not determine the desired wizard.");
88 public class CollectorLabelProvider
extends LabelProvider
{
90 public String
getText(Object element
) {
91 if (element
== null) {
94 UuidAndTitleCache uuidAndTitleCache
= (UuidAndTitleCache
) element
;
96 String abbrevTitleCache
= uuidAndTitleCache
.getAbbrevTitleCache();
97 String titleCache
= uuidAndTitleCache
.getTitleCache();
98 String collectorTitleCache
= null;
99 if (element
instanceof TeamOrPersonUuidAndTitleCache
&& ((TeamOrPersonUuidAndTitleCache
)element
).getCollectorTitleCache() != null){
100 title
= ((TeamOrPersonUuidAndTitleCache
)element
).getCollectorTitleCache();
102 if (!title
.equals(titleCache
)){
103 title
+= " - " + titleCache
;
105 if (abbrevTitleCache
!= null && !uuidAndTitleCache
.getTitleCache().equals(abbrevTitleCache
) && !abbrevTitleCache
.equals(titleCache
)){
106 title
+= " - " + abbrevTitleCache
;
108 if(PreferencesUtil
.getBooleanValue(PreferencePredicate
.ShowIdInSelectionDialog
.getKey())){
109 title
+= " ["+uuidAndTitleCache
.getId()+"]";
111 if (element
instanceof EntityDTOBase
){
112 title
+= "(" + ((IdentifiedEntityDTO
)element
).getIdentifier().getTypeLabel() +": " + ((IdentifiedEntityDTO
)element
).getIdentifier().getIdentifier() + ")";
120 protected void callService(String pattern
) {
121 Class clazz
= AgentBase
.class;
122 if (selectTeamMember
){
123 clazz
= Person
.class;
125 Set
<UUID
> memberUuids
= new HashSet
<>();
126 if (collectorTeam
!= null && selectTeamMember
){
127 List
<Person
> teamMembers
= this.collectorTeam
.getTeamMembers();
129 teamMembers
.forEach(member
-> memberUuids
.add(member
.getUuid()));
132 model
= CdmStore
.getService(IAgentService
.class).getUuidAndTitleCacheWithCollectorTitleCache(clazz
, limitOfInitialElements
, pattern
);
133 if (collectorTeam
== null){
137 Iterator
<UuidAndTitleCache
<AgentBase
<?
>>> modelIterator
= model
.iterator();
138 List
<UuidAndTitleCache
<AgentBase
<?
>>> tempModel
= new ArrayList
<>();
139 while(modelIterator
.hasNext()){
140 UuidAndTitleCache
<AgentBase
<?
>> person
= modelIterator
.next();
141 if (memberUuids
.contains(person
.getUuid())){
142 tempModel
.add(person
);
146 model
.addAll(tempModel
);
150 protected String
getTitle(AgentBase
<?
> cdmObject
) {
151 if(cdmObject
== null){
155 if (cdmObject
instanceof TeamOrPersonBase
) {
156 return ((TeamOrPersonBase
<?
>) cdmObject
).getCollectorTitleCache();
159 throw new IllegalArgumentException("Generic method only" +
160 " supports cdmObject of type TeamOrPersonBase." +
161 " Please implement specific method in subclass.");