Project

General

Profile

Download (6.14 KB) Statistics
| Branch: | Tag: | Revision:
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.log4j.Logger;
17

    
18
import eu.etaxonomy.cdm.common.CdmUtils;
19
import eu.etaxonomy.cdm.model.agent.Person;
20
import eu.etaxonomy.cdm.strategy.StrategyBase;
21

    
22
/**
23
 * @author AM
24
 */
25
public class PersonDefaultCacheStrategy
26
        extends StrategyBase
27
        implements INomenclaturalAuthorCacheStrategy<Person> {
28

    
29
	private static final long serialVersionUID = -6184639515553953112L;
30
	@SuppressWarnings("unused")
31
	private static final Logger logger = Logger.getLogger(PersonDefaultCacheStrategy.class);
32

    
33
	final static UUID uuid = UUID.fromString("9abda0e1-d5cc-480f-be38-40a510a3f253");
34

    
35
	private static PersonDefaultCacheStrategy instance;
36

    
37
	private String initialsSeparator = "";
38

    
39
	public static PersonDefaultCacheStrategy NewInstance(){
40
		return new PersonDefaultCacheStrategy();
41
	}
42

    
43
	public static PersonDefaultCacheStrategy INSTANCE(){
44
	    if (instance == null){
45
	        instance = PersonDefaultCacheStrategy.NewInstance();
46
	    }
47
	    return instance;
48
	}
49

    
50
// ******************** CONSTRUCTOR **********************************/
51

    
52
	private PersonDefaultCacheStrategy() {
53
		super();
54
	}
55

    
56
	@Override
57
	protected UUID getUuid() {
58
		return uuid;
59
	}
60

    
61
	@Override
62
    public String getNomenclaturalTitleCache(Person person) {
63
		if (isNotBlank(person.getNomenclaturalTitle())){
64
            return person.getNomenclaturalTitle();
65
        }else{
66
            return getTitleCache(person);
67
        }
68
	}
69

    
70
    @Override
71
    public String getFamilyTitle(Person person) {
72
        return isNotBlank(person.getFamilyName())? person.getFamilyName() : person.getTitleCache();
73
    }
74

    
75
    @Override
76
    public String getTitleCache(Person person) {
77
        if (person.isProtectedTitleCache()){
78
            return person.getTitleCache();
79
        }
80
        String result = "";
81

    
82
        if (isNotBlank(person.getFamilyName() ) ){
83
            result = person.getFamilyName().trim();
84
            result = addInitials(result, person);
85
            return result;
86
        }else{
87
            result = person.getNomenclaturalTitle();
88
            if (isNotBlank(result)){
89
                return result.trim();
90
            }else{
91
                result = addInitials("", person);
92
                if (isNotBlank(result)){
93
                    return result.trim();
94
                }
95
            }
96
        }
97
        return person.toString();
98
    }
99

    
100
    @Override
101
    public String getCollectorTitleCache(Person person){
102
        if (isNotBlank(person.getCollectorTitle())){
103
            return person.getCollectorTitle();
104
        }else{
105
            return getTitleCache(person);
106
        }
107
    }
108

    
109
    private String addInitials(String existing, Person person) {
110
        String result = existing;
111
        String initials = person.getInitials();
112
        if (isBlank(initials)){
113
            boolean forceFirstLetter = false;
114
            initials = getInitialsFromGivenName(person.getGivenName(), forceFirstLetter);
115
        }
116
        result = CdmUtils.concat(", ", result, initials);
117
        return result;
118
    }
119

    
120

    
121
    @Override
122
    public String getFullTitle(Person person) {
123
		String result = "";
124
		result = person.getFamilyName();
125
		result = addGivenNamePrefixSuffix(result, person);
126
		if (isNotBlank(result)){
127
		    return result;
128
		}
129
	    result = person.getNomenclaturalTitle();
130
	    if (isNotBlank(result)){
131
	        return result;
132
	    }
133
	    result = addGivenNamePrefixSuffix("", person);
134
	    if (isNotBlank(result)){
135
	        return result;
136
		}
137
		return person.toString();
138
	}
139

    
140
	private String addGivenNamePrefixSuffix(String oldString, Person person) {
141
		String result = oldString;
142
		if (isNotBlank(person.getGivenName())){
143
		    result = CdmUtils.concat(" ", person.getGivenName(), result);
144
		}else{
145
		    result = CdmUtils.concat(" ", person.getInitials(), result);
146
	    }
147
		result = CdmUtils.concat(" ", person.getPrefix(), result);
148
		result = CdmUtils.concat(" ", result, person.getSuffix());
149
		return result;
150
	}
151

    
152

    
153
    public String getInitialsFromGivenName(String givenname, boolean forceOnlyFirstLetter) {
154
        if (givenname == null){
155
            return null;
156
        }else if (isBlank(givenname)){
157
            return null;
158
        }
159
        //remove brackets
160
        final String regex = "\\([^)]*\\)";
161
        final Pattern pattern = Pattern.compile(regex);
162
        final Matcher matcher = pattern.matcher(givenname);
163
        givenname = matcher.replaceAll("").replaceAll("\\s\\s", " ");
164

    
165
        String result = "";
166
        String[] splits = givenname.split("((?<=\\.)|\\s+|(?=([\\-\u2013])))+"); // [\\-\u2013]? // (?!=\\s) wasn't successful to trim
167
        for (String split : splits){
168
            split = split.trim();
169
            if (isBlank(split) || split.matches("\\(.*\\)")){  //again checking brackets not really necessary
170
                continue;
171
            }
172
            if (split.matches("^[\\-\u2013].*")){
173
                result += split.substring(0, 1);
174
                split = split.substring(1);
175
                if (isBlank(split)){
176
                    continue;
177
                }
178
            }
179
            if (split.matches("[A-Z]{2,3}")){
180
                split = split.replaceAll("(?<!^)",".");  //insert dots after each letter (each position but not at start)
181
                result = CdmUtils.concat(initialsSeparator, result, split);
182
            }else if (forceOnlyFirstLetter){
183
                result = CdmUtils.concat(initialsSeparator, result, split.substring(0, 1) + ".");
184
            }else if (split.endsWith(".")){
185
                result = CdmUtils.concat(initialsSeparator, result, split);
186
            }else{
187
                result = CdmUtils.concat(initialsSeparator, result, split.substring(0, 1) + ".");
188
            }
189
        }
190
        return CdmUtils.Ne(result);
191
    }
192

    
193
}
(3-3/4)