1
|
/**
|
2
|
* Copyright (C) 2009 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
|
|
10
|
package eu.etaxonomy.cdm.strategy.cache.agent;
|
11
|
|
12
|
import java.util.List;
|
13
|
import java.util.UUID;
|
14
|
|
15
|
import org.apache.log4j.Logger;
|
16
|
|
17
|
import eu.etaxonomy.cdm.model.agent.Person;
|
18
|
import eu.etaxonomy.cdm.model.agent.Team;
|
19
|
import eu.etaxonomy.cdm.strategy.StrategyBase;
|
20
|
|
21
|
/**
|
22
|
* @author AM
|
23
|
*/
|
24
|
public class TeamDefaultCacheStrategy extends StrategyBase implements INomenclaturalAuthorCacheStrategy<Team> {
|
25
|
|
26
|
private static final long serialVersionUID = 8375295443642690479L;
|
27
|
@SuppressWarnings("unused")
|
28
|
private static final Logger logger = Logger.getLogger(TeamDefaultCacheStrategy.class);
|
29
|
|
30
|
|
31
|
public static final String FINAL_TEAM_CONCATINATION = " & ";
|
32
|
public static final String STD_TEAM_CONCATINATION = ", ";
|
33
|
public static final String ET_AL_TEAM_CONCATINATION_FULL = " & ";
|
34
|
public static final String ET_AL_TEAM_CONCATINATION_ABBREV = " & ";
|
35
|
|
36
|
private static final int DEFAULT_ET_AL_POS = Integer.MAX_VALUE;
|
37
|
private static final int DEFAULT_NOM_ET_AL_POS = Integer.MAX_VALUE;
|
38
|
|
39
|
public static final String EMPTY_TEAM = "-empty team-";
|
40
|
|
41
|
private static TeamDefaultCacheStrategy instance;
|
42
|
private static TeamDefaultCacheStrategy instance2;
|
43
|
private static TeamDefaultCacheStrategy instance3;
|
44
|
|
45
|
final static UUID uuid = UUID.fromString("1cbda0d1-d5cc-480f-bf38-40a510a3f223");
|
46
|
|
47
|
private final int etAlPositionTitleCache; //NO_ET_AL_POS is default,
|
48
|
private final int etAlPositionNomTitleCache; //NOMCACHE_ET_AL_POS is default
|
49
|
private final int etAlPositionFamilyTitle; //NOMCACHE_ET_AL_POS is default
|
50
|
private final int etAlPositionFullTitle; //NOMCACHE_ET_AL_POS is default
|
51
|
private final int etAlPositionCollectorTitle; //NOMCACHE_ET_AL_POS is default
|
52
|
|
53
|
// ************************* FACTORY ************************/
|
54
|
|
55
|
public static TeamDefaultCacheStrategy NewInstance(){
|
56
|
return new TeamDefaultCacheStrategy(DEFAULT_ET_AL_POS, DEFAULT_NOM_ET_AL_POS, DEFAULT_ET_AL_POS,
|
57
|
DEFAULT_ET_AL_POS, DEFAULT_ET_AL_POS);
|
58
|
}
|
59
|
public static TeamDefaultCacheStrategy NewInstanceTitleEtAl(int etAlPositionForTitleCache){
|
60
|
return new TeamDefaultCacheStrategy(etAlPositionForTitleCache, DEFAULT_NOM_ET_AL_POS, DEFAULT_ET_AL_POS,
|
61
|
DEFAULT_ET_AL_POS, DEFAULT_ET_AL_POS);
|
62
|
}
|
63
|
public static TeamDefaultCacheStrategy NewInstanceNomEtAl(int etAlPositionForNomenclaturalCache){
|
64
|
return new TeamDefaultCacheStrategy(DEFAULT_ET_AL_POS, etAlPositionForNomenclaturalCache,
|
65
|
DEFAULT_ET_AL_POS, DEFAULT_ET_AL_POS, DEFAULT_ET_AL_POS);
|
66
|
}
|
67
|
public static TeamDefaultCacheStrategy NewInstance(int etAlPostionForAll){
|
68
|
return new TeamDefaultCacheStrategy(etAlPostionForAll, etAlPostionForAll,
|
69
|
etAlPostionForAll, etAlPostionForAll, etAlPostionForAll);
|
70
|
}
|
71
|
|
72
|
public static TeamDefaultCacheStrategy INSTANCE(){
|
73
|
if (instance == null){
|
74
|
instance = NewInstance();
|
75
|
}
|
76
|
return instance;
|
77
|
}
|
78
|
|
79
|
public static TeamDefaultCacheStrategy INSTANCE_ET_AL_2(){
|
80
|
if (instance2 == null){
|
81
|
instance2 = NewInstance(2);
|
82
|
}
|
83
|
return instance2;
|
84
|
}
|
85
|
|
86
|
public static TeamDefaultCacheStrategy INSTANCE_ET_AL_3(){
|
87
|
if (instance3 == null){
|
88
|
instance3 = NewInstance(3);
|
89
|
}
|
90
|
return instance3;
|
91
|
}
|
92
|
|
93
|
// ********************* CONSTRUCTOR ***************************/
|
94
|
|
95
|
private TeamDefaultCacheStrategy(int etAlPositionTitleCache, int etAlPositionForNomenclaturalCache,
|
96
|
int etAlPositionFamilyTitle, int etAlPositionFullTitle, int etAlPositionCollectorTitle) {
|
97
|
this.etAlPositionTitleCache = etAlPositionTitleCache;
|
98
|
this.etAlPositionNomTitleCache = etAlPositionForNomenclaturalCache;
|
99
|
this.etAlPositionFamilyTitle = etAlPositionFamilyTitle;
|
100
|
this.etAlPositionFullTitle = etAlPositionFullTitle;
|
101
|
this.etAlPositionCollectorTitle = etAlPositionCollectorTitle;
|
102
|
}
|
103
|
|
104
|
@Override
|
105
|
protected UUID getUuid() {
|
106
|
return uuid;
|
107
|
}
|
108
|
|
109
|
private enum CacheType{
|
110
|
TITLECACHE,
|
111
|
ABBREV,
|
112
|
FULL,
|
113
|
COLLECTOR,
|
114
|
FAMILY;
|
115
|
|
116
|
private String getCache(Person member){
|
117
|
if (this == TITLECACHE){
|
118
|
return member.getTitleCache();
|
119
|
}else if (this == ABBREV){
|
120
|
return member.getNomenclaturalTitle();
|
121
|
}else if (this == FULL){
|
122
|
return member.getFullTitle();
|
123
|
}else if (this == FAMILY){
|
124
|
return member.getCacheStrategy().getFamilyTitle(member);
|
125
|
}else if (this == COLLECTOR){
|
126
|
return member.getCollectorTitleCache();
|
127
|
}
|
128
|
throw new IllegalStateException("CacheType not supported: " + this);
|
129
|
}
|
130
|
}
|
131
|
|
132
|
// ************** GETTER *******************/
|
133
|
|
134
|
public int getEtAlPositionNomTitleCache() {
|
135
|
return etAlPositionNomTitleCache;
|
136
|
}
|
137
|
|
138
|
public int getEtAlPositionTitleCache() {
|
139
|
return etAlPositionTitleCache;
|
140
|
}
|
141
|
|
142
|
// *********************** MTEHODS ****************/
|
143
|
|
144
|
|
145
|
@Override
|
146
|
public String getTitleCache(Team team) {
|
147
|
return getCache(team, CacheType.TITLECACHE, etAlPositionTitleCache);
|
148
|
}
|
149
|
|
150
|
@Override
|
151
|
public String getNomenclaturalTitle(Team team) {
|
152
|
return getCache(team, CacheType.ABBREV, etAlPositionNomTitleCache);
|
153
|
}
|
154
|
|
155
|
@Override
|
156
|
public String getFullTitle(Team team) {
|
157
|
return getCache(team, CacheType.FULL, etAlPositionFullTitle);
|
158
|
}
|
159
|
|
160
|
@Override
|
161
|
public String getFamilyTitle(Team team) {
|
162
|
return getCache(team, CacheType.FAMILY, etAlPositionFamilyTitle);
|
163
|
}
|
164
|
|
165
|
@Override
|
166
|
public String getCollectorTitleCache(Team team) {
|
167
|
return getCache(team, CacheType.COLLECTOR, etAlPositionCollectorTitle);
|
168
|
}
|
169
|
|
170
|
private String getCache(Team team, CacheType cacheType, int etAlPosition) {
|
171
|
|
172
|
String result = "";
|
173
|
List<Person> teamMembers = team.getTeamMembers();
|
174
|
int size = teamMembers.size();
|
175
|
for (int i = 1; i <= size && (i < etAlPosition || (size == etAlPosition && !team.isHasMoreMembers())); i++){
|
176
|
Person teamMember = teamMembers.get(i-1);
|
177
|
if(teamMember == null){
|
178
|
// this can happen in UIs in the process of adding new members
|
179
|
continue;
|
180
|
}
|
181
|
String concat = teamConcatSeparator(team, i);
|
182
|
result += concat + cacheType.getCache(teamMember);
|
183
|
}
|
184
|
if (teamMembers.size() == 0){
|
185
|
if (cacheType == CacheType.TITLECACHE){
|
186
|
result = EMPTY_TEAM;
|
187
|
}else{
|
188
|
return team.getTitleCache();
|
189
|
}
|
190
|
} else if (team.isHasMoreMembers() || teamMembers.size() > etAlPosition){
|
191
|
result = addHasMoreMembers(result);
|
192
|
}
|
193
|
return result;
|
194
|
}
|
195
|
|
196
|
/**
|
197
|
* Computes the team concat separator for the member
|
198
|
* at position <code>index</code>
|
199
|
* @param team
|
200
|
* @param index
|
201
|
*/
|
202
|
public static String teamConcatSeparator(Team team, int index) {
|
203
|
List<Person> teamMembers = team.getTeamMembers();
|
204
|
if (index <= 1){
|
205
|
return "";
|
206
|
}else if (index < teamMembers.size() || ( team.isHasMoreMembers() && index == teamMembers.size())){
|
207
|
return STD_TEAM_CONCATINATION;
|
208
|
}else{
|
209
|
return FINAL_TEAM_CONCATINATION;
|
210
|
}
|
211
|
}
|
212
|
|
213
|
/**
|
214
|
* Add the et al. to the team string
|
215
|
* @param str team string without et al.
|
216
|
* @return
|
217
|
*/
|
218
|
public static String addHasMoreMembers(String str) {
|
219
|
return str + ET_AL_TEAM_CONCATINATION_ABBREV + "al.";
|
220
|
}
|
221
|
|
222
|
}
|