Project

General

Profile

Download (5.85 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
    @Override
92
    public String getCollectorTitleCache(Person person){
93
        if (isNotBlank(person.getCollectorTitle())){
94
            return person.getCollectorTitle();
95
        }else{
96
            return getTitleCache(person);
97
        }
98
    }
99

    
100
    private String addInitials(String existing, Person person) {
101
        String result = existing;
102
        String initials = person.getInitials();
103
        if (isBlank(initials)){
104
            boolean forceFirstLetter = false;
105
            initials = getInitialsFromGivenName(person.getGivenName(), forceFirstLetter);
106
        }
107
        result = CdmUtils.concat(", ", result, initials);
108
        return result;
109
    }
110

    
111

    
112
    @Override
113
    public String getFullTitle(Person person) {
114
		String result = "";
115
		result = person.getFamilyName();
116
		result = addGivenNamePrefixSuffix(result, person);
117
		if (isNotBlank(result)){
118
		    return result;
119
		}
120
	    result = person.getNomenclaturalTitle();
121
	    if (isNotBlank(result)){
122
	        return result;
123
	    }
124
	    result = addGivenNamePrefixSuffix("", person);
125
	    if (isNotBlank(result)){
126
	        return result;
127
		}
128
		return person.toString();
129
	}
130

    
131
	private String addGivenNamePrefixSuffix(String oldString, Person person) {
132
		String result = oldString;
133
		if (isNotBlank(person.getGivenName())){
134
		    result = CdmUtils.concat(" ", person.getGivenName(), result);
135
		}else{
136
		    result = CdmUtils.concat(" ", person.getInitials(), result);
137
	    }
138
		result = CdmUtils.concat(" ", person.getPrefix(), result);
139
		result = CdmUtils.concat(" ", result, person.getSuffix());
140
		return result;
141
	}
142

    
143

    
144
    public String getInitialsFromGivenName(String givenname, boolean forceOnlyFirstLetter) {
145
        if (givenname == null){
146
            return null;
147
        }else if (isBlank(givenname)){
148
            return null;
149
        }
150
        //remove brackets
151
        final String regex = "\\([^)]*\\)";
152
        final Pattern pattern = Pattern.compile(regex);
153
        final Matcher matcher = pattern.matcher(givenname);
154
        givenname = matcher.replaceAll("").replaceAll("\\s\\s", " ");
155

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

    
184
}
(3-3/4)