Project

General

Profile

Download (5.98 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 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
        String result = "";
78
        if (isNotBlank(person.getFamilyName() ) ){
79
            result = person.getFamilyName();
80
            result = addInitials(result, person);
81
            return result;
82
        }else{
83
            result = person.getNomenclaturalTitle();
84
            if (isNotBlank(result)){
85
                return result;
86
            }
87
            result = addInitials("", person);
88
            if (isNotBlank(result)){
89
                return result;
90
            }
91
        }
92
        return person.toString();
93
    }
94

    
95
    @Override
96
    public String getCollectorTitleCache(Person person){
97
        if (isNotBlank(person.getCollectorTitle())){
98
            return person.getCollectorTitle();
99
        }else{
100
            return getTitleCache(person);
101
        }
102
    }
103

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

    
115

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

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

    
147

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

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

    
188
}
(3-3/4)