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
|
package eu.etaxonomy.cdm.strategy.cache.agent;
|
10
|
|
11
|
import java.util.UUID;
|
12
|
import java.util.regex.Matcher;
|
13
|
import java.util.regex.Pattern;
|
14
|
|
15
|
import org.apache.log4j.Logger;
|
16
|
|
17
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
18
|
import eu.etaxonomy.cdm.model.agent.Person;
|
19
|
import eu.etaxonomy.cdm.strategy.StrategyBase;
|
20
|
|
21
|
/**
|
22
|
* @author AM
|
23
|
*/
|
24
|
public class PersonDefaultCacheStrategy
|
25
|
extends StrategyBase
|
26
|
implements INomenclaturalAuthorCacheStrategy<Person> {
|
27
|
|
28
|
private static final long serialVersionUID = -6184639515553953112L;
|
29
|
@SuppressWarnings("unused")
|
30
|
private static final Logger logger = Logger.getLogger(PersonDefaultCacheStrategy.class);
|
31
|
|
32
|
final static UUID uuid = UUID.fromString("9abda0e1-d5cc-480f-be38-40a510a3f253");
|
33
|
|
34
|
private static PersonDefaultCacheStrategy instance;
|
35
|
|
36
|
private String initialsSeparator = "";
|
37
|
|
38
|
public static PersonDefaultCacheStrategy NewInstance(){
|
39
|
return new PersonDefaultCacheStrategy();
|
40
|
}
|
41
|
|
42
|
public static PersonDefaultCacheStrategy INSTANCE(){
|
43
|
if (instance == null){
|
44
|
instance = PersonDefaultCacheStrategy.NewInstance();
|
45
|
}
|
46
|
return instance;
|
47
|
}
|
48
|
|
49
|
// ******************** CONSTRUCTOR **********************************/
|
50
|
|
51
|
private PersonDefaultCacheStrategy() {
|
52
|
super();
|
53
|
}
|
54
|
|
55
|
@Override
|
56
|
protected UUID getUuid() {
|
57
|
return uuid;
|
58
|
}
|
59
|
|
60
|
@Override
|
61
|
public String getNomenclaturalTitleCache(Person person) {
|
62
|
if (isNotBlank(person.getNomenclaturalTitle())){
|
63
|
return person.getNomenclaturalTitle();
|
64
|
}else{
|
65
|
return getTitleCache(person);
|
66
|
}
|
67
|
}
|
68
|
|
69
|
@Override
|
70
|
public String getFamilyTitle(Person person) {
|
71
|
return isNotBlank(person.getFamilyName())? person.getFamilyName() : person.getTitleCache();
|
72
|
}
|
73
|
|
74
|
@Override
|
75
|
public String getTitleCache(Person person) {
|
76
|
if (person.isProtectedTitleCache()){
|
77
|
return person.getTitleCache();
|
78
|
}
|
79
|
String result = "";
|
80
|
|
81
|
if (isNotBlank(person.getFamilyName() ) ){
|
82
|
result = person.getFamilyName().trim();
|
83
|
result = addInitials(result, person);
|
84
|
return result;
|
85
|
}else{
|
86
|
result = person.getNomenclaturalTitle();
|
87
|
if (isNotBlank(result)){
|
88
|
return result.trim();
|
89
|
}
|
90
|
result = person.getCollectorTitle();
|
91
|
if (isNotBlank(result)){
|
92
|
return result.trim();
|
93
|
}
|
94
|
result = addInitials("", person);
|
95
|
if (isNotBlank(result)){
|
96
|
return result.trim();
|
97
|
}
|
98
|
}
|
99
|
return person.toString();
|
100
|
}
|
101
|
|
102
|
@Override
|
103
|
public String getCollectorTitleCache(Person person){
|
104
|
if (isNotBlank(person.getCollectorTitle())){
|
105
|
return person.getCollectorTitle();
|
106
|
}else{
|
107
|
return getTitleCache(person);
|
108
|
}
|
109
|
}
|
110
|
|
111
|
private String addInitials(String existing, Person person) {
|
112
|
String result = existing;
|
113
|
String initials = person.getInitials();
|
114
|
if (isBlank(initials)){
|
115
|
boolean forceFirstLetter = false;
|
116
|
initials = getInitialsFromGivenName(person.getGivenName(), forceFirstLetter);
|
117
|
}
|
118
|
result = CdmUtils.concat(", ", result, initials);
|
119
|
return result;
|
120
|
}
|
121
|
|
122
|
|
123
|
@Override
|
124
|
public String getFullTitle(Person person) {
|
125
|
String result = "";
|
126
|
result = person.getFamilyName();
|
127
|
result = addGivenNamePrefixSuffix(result, person);
|
128
|
if (isNotBlank(result)){
|
129
|
return result;
|
130
|
}
|
131
|
result = person.getNomenclaturalTitle();
|
132
|
if (isNotBlank(result)){
|
133
|
return result;
|
134
|
}
|
135
|
result = addGivenNamePrefixSuffix("", person);
|
136
|
if (isNotBlank(result)){
|
137
|
return result;
|
138
|
}
|
139
|
return person.toString();
|
140
|
}
|
141
|
|
142
|
private String addGivenNamePrefixSuffix(String oldString, Person person) {
|
143
|
String result = oldString;
|
144
|
if (isNotBlank(person.getGivenName())){
|
145
|
result = CdmUtils.concat(" ", person.getGivenName(), result);
|
146
|
}else{
|
147
|
result = CdmUtils.concat(" ", person.getInitials(), result);
|
148
|
}
|
149
|
result = CdmUtils.concat(" ", person.getPrefix(), result);
|
150
|
result = CdmUtils.concat(" ", result, person.getSuffix());
|
151
|
return result;
|
152
|
}
|
153
|
|
154
|
|
155
|
public String getInitialsFromGivenName(String givenname, boolean forceOnlyFirstLetter) {
|
156
|
if (givenname == null){
|
157
|
return null;
|
158
|
}else if (isBlank(givenname)){
|
159
|
return null;
|
160
|
}
|
161
|
//remove brackets
|
162
|
final String regex = "\\([^)]*\\)";
|
163
|
final Pattern pattern = Pattern.compile(regex);
|
164
|
final Matcher matcher = pattern.matcher(givenname);
|
165
|
givenname = matcher.replaceAll("").replaceAll("\\s\\s", " ");
|
166
|
|
167
|
String result = "";
|
168
|
String[] splits = givenname.split("((?<=\\.)|\\s+|(?=([\\-\u2013])))+"); // [\\-\u2013]? // (?!=\\s) wasn't successful to trim
|
169
|
for (String split : splits){
|
170
|
split = split.trim();
|
171
|
if (isBlank(split) || split.matches("\\(.*\\)")){ //again checking brackets not really necessary
|
172
|
continue;
|
173
|
}
|
174
|
if (split.matches("^[\\-\u2013].*")){
|
175
|
result += split.substring(0, 1);
|
176
|
split = split.substring(1);
|
177
|
if (isBlank(split)){
|
178
|
continue;
|
179
|
}
|
180
|
}
|
181
|
if (split.matches("[A-Z]{2,3}")){
|
182
|
split = split.replaceAll("(?<!^)","."); //insert dots after each letter (each position but not at start)
|
183
|
result = CdmUtils.concat(initialsSeparator, result, split);
|
184
|
}else if (forceOnlyFirstLetter){
|
185
|
result = CdmUtils.concat(initialsSeparator, result, split.substring(0, 1) + ".");
|
186
|
}else if (split.endsWith(".")){
|
187
|
result = CdmUtils.concat(initialsSeparator, result, split);
|
188
|
}else{
|
189
|
result = CdmUtils.concat(initialsSeparator, result, split.substring(0, 1) + ".");
|
190
|
}
|
191
|
}
|
192
|
return CdmUtils.Ne(result);
|
193
|
}
|
194
|
|
195
|
}
|