Project

General

Profile

Download (4.87 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.taxon;
11

    
12
import java.util.ArrayList;
13
import java.util.List;
14
import java.util.UUID;
15

    
16
import org.apache.commons.lang.StringUtils;
17

    
18
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
19
import eu.etaxonomy.cdm.model.common.CdmBase;
20
import eu.etaxonomy.cdm.model.name.NonViralName;
21
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
22
import eu.etaxonomy.cdm.model.reference.Reference;
23
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
24
import eu.etaxonomy.cdm.strategy.StrategyBase;
25
import eu.etaxonomy.cdm.strategy.cache.HTMLTagRules;
26
import eu.etaxonomy.cdm.strategy.cache.TagEnum;
27
import eu.etaxonomy.cdm.strategy.cache.TaggedCacheHelper;
28
import eu.etaxonomy.cdm.strategy.cache.TaggedText;
29
import eu.etaxonomy.cdm.strategy.cache.name.INameCacheStrategy;
30

    
31
public class TaxonBaseDefaultCacheStrategy<T extends TaxonBase>
32
        extends StrategyBase
33
        implements ITaxonCacheStrategy<T> {
34

    
35
    private static final long serialVersionUID = 5769890979070021350L;
36
    final static UUID uuid = UUID.fromString("931e48f0-2033-11de-8c30-0800200c9a66");
37

    
38
	@Override
39
	protected UUID getUuid() {
40
		return uuid;
41
	}
42

    
43
   @Override
44
    public String getTitleCache(T taxonBase) {
45
        return getTitleCache(taxonBase, null);
46
    }
47

    
48
    @Override
49
    public List<TaggedText> getTaggedTitle(T taxonBase) {
50
        if (taxonBase == null){
51
            return null;
52
        }
53

    
54
        List<TaggedText> tags = new ArrayList<TaggedText>();
55

    
56
        if (taxonBase.isDoubtful()){
57
            tags.add(new TaggedText(TagEnum.separator, "?"));
58
        }
59
        if (taxonBase.isProtectedTitleCache()){
60
            //protected title cache
61
            tags.add(new TaggedText(TagEnum.name, taxonBase.getTitleCache()));
62
        }else{
63
            //name
64
            List<TaggedText> nameTags = getNameTags(taxonBase);
65

    
66
            if (nameTags.size() > 0){
67
                tags.addAll(nameTags);
68

    
69
                String secSeparator =  " sec. ";
70
                if (nameTags.get(nameTags.size() - 1).getType().equals(TagEnum.nomStatus)){
71
                    secSeparator = "," + secSeparator;
72
                }
73

    
74
                //sec.
75
                tags.add(new TaggedText(TagEnum.separator, secSeparator));
76

    
77
                //ref.
78
                List<TaggedText> secTags = getSecundumTags(taxonBase);
79
                tags.addAll(secTags);
80
            }else{
81
                tags.add(new TaggedText(TagEnum.fullName, taxonBase.toString()));
82
            }
83
        }
84
        return tags;
85
    }
86

    
87
    private List<TaggedText> getNameTags(T taxonBase) {
88
        List<TaggedText> tags = new ArrayList<TaggedText>();
89
        TaxonNameBase<?,INameCacheStrategy<TaxonNameBase>> name = CdmBase.deproxy(taxonBase.getName());
90

    
91
        if (name != null){
92
            INameCacheStrategy<TaxonNameBase> nameCacheStrategy = name.getCacheStrategy();
93
            if (taxonBase.isUseNameCache() && name.isInstanceOf(NonViralName.class)){
94
                List<TaggedText> nameCacheTags = nameCacheStrategy.getTaggedName(name);
95
                tags.addAll(nameCacheTags);
96
            }else{
97
                List<TaggedText> nameTags = nameCacheStrategy.getTaggedTitle(name);
98
                tags.addAll(nameTags);
99
                List<TaggedText> statusTags = nameCacheStrategy.getNomStatusTags(name, true, true);
100
                tags.addAll(statusTags);
101
            }
102
            if (StringUtils.isNotBlank(taxonBase.getAppendedPhrase())){
103
                tags.add(new TaggedText(TagEnum.appendedPhrase, taxonBase.getAppendedPhrase().trim()));
104
            }
105
        }
106
        return tags;
107
    }
108

    
109
    private List<TaggedText> getSecundumTags(T taxonBase) {
110
        List<TaggedText> tags = new ArrayList<TaggedText>();
111

    
112
        Reference<?> ref = taxonBase.getSec();
113
        ref = HibernateProxyHelper.deproxy(ref, Reference.class);
114
        String secRef;
115
        if (ref == null){
116
            secRef = "???";
117
        }else{
118
            if (ref.getCacheStrategy() != null &&
119
                    ref.getAuthorship() != null &&
120
                    isNotBlank(ref.getAuthorship().getTitleCache()) &&
121
                    isNotBlank(ref.getYear())){
122
                secRef = ref.getCacheStrategy().getCitation(ref);
123
            }else{
124
                secRef = ref.getTitleCache();
125
            }
126
        }
127
        tags.add(new TaggedText(TagEnum.secReference, secRef));
128
        return tags;
129
    }
130

    
131

    
132
    @Override
133
    public String getTitleCache(T taxonBase, HTMLTagRules htmlTagRules) {
134
        List<TaggedText> tags = getTaggedTitle(taxonBase);
135
        if (tags == null){
136
            return null;
137
        }else{
138
            String result = TaggedCacheHelper.createString(tags, htmlTagRules);
139
            return result;
140
        }
141
    }
142
}
(2-2/3)