Project

General

Profile

Download (4.07 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

    
14
import org.apache.commons.lang.StringUtils;
15
import org.apache.log4j.Logger;
16

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

    
21
/**
22
 * @author AM
23
 *
24
 */
25
public class PersonDefaultCacheStrategy extends StrategyBase implements
26
		INomenclaturalAuthorCacheStrategy<Person> {
27
	private static final long serialVersionUID = -6184639515553953112L;
28

    
29
	@SuppressWarnings("unused")
30
	private static final Logger logger = Logger.getLogger(PersonDefaultCacheStrategy.class);
31

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

    
34
	private String initialsSeparator = "";
35

    
36
	static public PersonDefaultCacheStrategy NewInstance(){
37
		return new PersonDefaultCacheStrategy();
38
	}
39
	
40
	/**
41
	 * 
42
	 */
43
	private PersonDefaultCacheStrategy() {
44
		super();
45
	}
46

    
47
	/* (non-Javadoc)
48
	 * @see eu.etaxonomy.cdm.strategy.StrategyBase#getUuid()
49
	 */
50
	@Override
51
	protected UUID getUuid() {
52
		return uuid;
53
	}
54

    
55
	/* (non-Javadoc)
56
	 * @see eu.etaxonomy.cdm.strategy.INomenclaturalAuthorCacheStrategy#getNomenclaturalTitle(eu.etaxonomy.cdm.model.name.TaxonNameBase)
57
	 */
58
	public String getNomenclaturalTitle(Person person) {
59
		return person.getNomenclaturalTitle();
60
	}
61

    
62
	/* (non-Javadoc)
63
	 * @see eu.etaxonomy.cdm.strategy.INomenclaturalAuthorCacheStrategy#getTitleCache(eu.etaxonomy.cdm.model.name.TaxonNameBase)
64
	 */
65
	public String getTitleCache(Person person) {
66
		String result = "";
67
		if (StringUtils.isNotBlank(person.getLastname() ) ){
68
			result = person.getLastname();
69
			result = addFirstNamePrefixSuffix(result, person);
70
			return result;
71
		}else{
72
			result = person.getNomenclaturalTitle();
73
			if (StringUtils.isNotBlank(result)){
74
				return result;
75
			}
76
			result = addFirstNamePrefixSuffix("", person);
77
			if (StringUtils.isNotBlank(result)){
78
				return result;
79
			}
80
		}
81
		return person.toString();
82
	}
83

    
84
	/**
85
	 * 
86
	 */
87
	private String addFirstNamePrefixSuffix(String oldString, Person person) {
88
		String result = oldString;
89
		result = CdmUtils.concat(" ", person.getFirstname(), result); 
90
		result = CdmUtils.concat(" ", person.getPrefix(), result); 
91
		result = CdmUtils.concat(" ", result, person.getSuffix()); 
92
		return result;
93
	}
94

    
95

    
96
    public String getInitialsFromFirstname(String firstname, boolean forceOnlyFirstLetter) {
97
        if (firstname == null){
98
            return null;
99
        }else if (StringUtils.isBlank(firstname)){
100
            return "";
101
        }
102
        String result = "";
103
        String[] splits = firstname.split("((?<=\\.)|\\s+|(?=([\\-\u2013])))+"); // [\\-\u2013]? // (?!=\\s) wasn't successful to trim
104
        for (String split : splits){
105
            split = split.trim();
106
            if (StringUtils.isBlank(split)){
107
                continue;
108
            }
109
            if (split.matches("^[\\-\u2013].*")){
110
                result += split.substring(0, 1);
111
                split = split.substring(1);
112
                if (StringUtils.isBlank(split)){
113
                    continue;
114
                }
115
            }
116
            if (split.matches("[A-Z]{2,3}")){
117
                split = split.replaceAll("(?<!^)",".");  //insert dots after each letter (each position but not at start)
118
                result = CdmUtils.concat(initialsSeparator, result, split);
119
            }else if (forceOnlyFirstLetter){
120
                result = CdmUtils.concat(initialsSeparator, result, split.substring(0, 1) + ".");
121
            }else if (split.endsWith(".")){
122
                result = CdmUtils.concat(initialsSeparator, result, split);
123
            }else{
124
                result = CdmUtils.concat(initialsSeparator, result, split.substring(0, 1) + ".");
125
            }
126
        }
127
        return result;
128
    }
129

    
130
}
(3-3/4)