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