Project

General

Profile

Download (5.6 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
	private static final long serialVersionUID = -6184639515553953112L;
29

    
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 getNomenclaturalTitle(Person person) {
63
		return person.getNomenclaturalTitle();
64
	}
65

    
66
    @Override
67
    public String getFamilyTitle(Person person) {
68
        return isNotBlank(person.getFamilyName())? person.getFamilyName() : person.getTitleCache();
69
    }
70

    
71
    @Override
72
    public String getTitleCache(Person person) {
73
        String result = "";
74
        if (isNotBlank(person.getFamilyName() ) ){
75
            result = person.getFamilyName();
76
            result = addInitials(result, person);
77
            return result;
78
        }else{
79
            result = person.getNomenclaturalTitle();
80
            if (isNotBlank(result)){
81
                return result;
82
            }
83
            result = addInitials("", person);
84
            if (isNotBlank(result)){
85
                return result;
86
            }
87
        }
88
        return person.toString();
89
    }
90

    
91
    private String addInitials(String existing, Person person) {
92
        String result = existing;
93
        String initials = person.getInitials();
94
        if (isBlank(initials)){
95
            boolean forceFirstLetter = false;
96
            initials = getInitialsFromGivenName(person.getGivenName(), forceFirstLetter);
97
        }
98
        result = CdmUtils.concat(", ", result, initials);
99
        return result;
100
    }
101

    
102

    
103
    @Override
104
    public String getFullTitle(Person person) {
105
		String result = "";
106
		result = person.getFamilyName();
107
		result = addGivenNamePrefixSuffix(result, person);
108
		if (isNotBlank(result)){
109
		    return result;
110
		}
111
	    result = person.getNomenclaturalTitle();
112
	    if (isNotBlank(result)){
113
	        return result;
114
	    }
115
	    result = addGivenNamePrefixSuffix("", person);
116
	    if (isNotBlank(result)){
117
	        return result;
118
		}
119
		return person.toString();
120
	}
121

    
122
	private String addGivenNamePrefixSuffix(String oldString, Person person) {
123
		String result = oldString;
124
		if (isNotBlank(person.getGivenName())){
125
		    result = CdmUtils.concat(" ", person.getGivenName(), result);
126
		}else{
127
		    result = CdmUtils.concat(" ", person.getInitials(), result);
128
	    }
129
		result = CdmUtils.concat(" ", person.getPrefix(), result);
130
		result = CdmUtils.concat(" ", result, person.getSuffix());
131
		return result;
132
	}
133

    
134

    
135
    public String getInitialsFromGivenName(String givenname, boolean forceOnlyFirstLetter) {
136
        if (givenname == null){
137
            return null;
138
        }else if (isBlank(givenname)){
139
            return "";
140
        }
141
        //remove brackets
142
        final String regex = "\\([^)]*\\)";
143
        final Pattern pattern = Pattern.compile(regex);
144
        final Matcher matcher = pattern.matcher(givenname);
145
        givenname = matcher.replaceAll("").replaceAll("\\s\\s", " ");
146

    
147
        String result = "";
148
        String[] splits = givenname.split("((?<=\\.)|\\s+|(?=([\\-\u2013])))+"); // [\\-\u2013]? // (?!=\\s) wasn't successful to trim
149
        for (String split : splits){
150
            split = split.trim();
151
            if (isBlank(split) || split.matches("\\(.*\\)")){  //again checking brackets not really necessary
152
                continue;
153
            }
154
            if (split.matches("^[\\-\u2013].*")){
155
                result += split.substring(0, 1);
156
                split = split.substring(1);
157
                if (isBlank(split)){
158
                    continue;
159
                }
160
            }
161
            if (split.matches("[A-Z]{2,3}")){
162
                split = split.replaceAll("(?<!^)",".");  //insert dots after each letter (each position but not at start)
163
                result = CdmUtils.concat(initialsSeparator, result, split);
164
            }else if (forceOnlyFirstLetter){
165
                result = CdmUtils.concat(initialsSeparator, result, split.substring(0, 1) + ".");
166
            }else if (split.endsWith(".")){
167
                result = CdmUtils.concat(initialsSeparator, result, split);
168
            }else{
169
                result = CdmUtils.concat(initialsSeparator, result, split.substring(0, 1) + ".");
170
            }
171
        }
172
        return CdmUtils.Ne(result);
173
    }
174

    
175
}
(3-3/4)