Project

General

Profile

Download (5.26 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 String initialsSeparator = "";
36

    
37
	static public PersonDefaultCacheStrategy NewInstance(){
38
		return new PersonDefaultCacheStrategy();
39
	}
40

    
41
// ******************** CONSTRUCTOR **********************************/
42
	private PersonDefaultCacheStrategy() {
43
		super();
44
	}
45

    
46

    
47

    
48
	@Override
49
	protected UUID getUuid() {
50
		return uuid;
51
	}
52

    
53
	@Override
54
    public String getNomenclaturalTitle(Person person) {
55
		return person.getNomenclaturalTitle();
56
	}
57

    
58
    @Override
59
    public String getTitleCache(Person person) {
60
        String result = "";
61
        if (isNotBlank(person.getFamilyName() ) ){
62
            result = person.getFamilyName();
63
            result = addInitials(result, person);
64
            return result;
65
        }else{
66
            result = person.getNomenclaturalTitle();
67
            if (isNotBlank(result)){
68
                return result;
69
            }
70
            result = addInitials("", person);
71
            if (isNotBlank(result)){
72
                return result;
73
            }
74
        }
75
        return person.toString();
76
    }
77

    
78
    /**
79
     * @param existing
80
     * @param person
81
     * @return
82
     */
83
    private String addInitials(String existing, Person person) {
84
        String result = existing;
85
        String initials = person.getInitials();
86
        if (isBlank(initials)){
87
            boolean forceFirstLetter = false;
88
            initials = getInitialsFromGivenName(person.getGivenName(), forceFirstLetter);
89
        }
90
        result = CdmUtils.concat(", ", result, initials);
91
        return result;
92
    }
93

    
94

    
95
    @Override
96
    public String getFullTitle(Person person) {
97
		String result = "";
98
		result = person.getFamilyName();
99
		result = addGivenNamePrefixSuffix(result, person);
100
		if (isNotBlank(result)){
101
		    return result;
102
		}
103
	    result = person.getNomenclaturalTitle();
104
	    if (isNotBlank(result)){
105
	        return result;
106
	    }
107
	    result = addGivenNamePrefixSuffix("", person);
108
	    if (isNotBlank(result)){
109
	        return result;
110
		}
111
		return person.toString();
112
	}
113

    
114
	private String addGivenNamePrefixSuffix(String oldString, Person person) {
115
		String result = oldString;
116
		if (isNotBlank(person.getGivenName())){
117
		    result = CdmUtils.concat(" ", person.getGivenName(), result);
118
		}else{
119
		    result = CdmUtils.concat(" ", person.getInitials(), result);
120
	    }
121
		result = CdmUtils.concat(" ", person.getPrefix(), result);
122
		result = CdmUtils.concat(" ", result, person.getSuffix());
123
		return result;
124
	}
125

    
126

    
127
    public String getInitialsFromGivenName(String givenname, boolean forceOnlyFirstLetter) {
128
        if (givenname == null){
129
            return null;
130
        }else if (isBlank(givenname)){
131
            return "";
132
        }
133
        //remove brackets
134
        final String regex = "\\([^)]*\\)";
135
        final Pattern pattern = Pattern.compile(regex);
136
        final Matcher matcher = pattern.matcher(givenname);
137
        givenname = matcher.replaceAll("").replaceAll("\\s\\s", " ");
138

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

    
167
}
(3-3/4)