Project

General

Profile

Download (5.56 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.TaxonNameBase;
21
import eu.etaxonomy.cdm.model.reference.Reference;
22
import eu.etaxonomy.cdm.model.taxon.Synonym;
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<>();
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
            }else{
69
                tags.add(new TaggedText(TagEnum.fullName, "???"));
70
            }
71

    
72
            boolean isSynonym = taxonBase.isInstanceOf(Synonym.class);
73
            String secSeparator =  (isSynonym? " syn." : "") + " sec. ";
74
            //not used: we currently use a post-separator in the name tags
75
//                if (nameTags.get(nameTags.size() - 1).getType().equals(TagEnum.nomStatus)){
76
//                    secSeparator = "," + secSeparator;
77
//                }
78

    
79
            //ref.
80
            List<TaggedText> secTags = getSecundumTags(taxonBase);
81

    
82
            //sec.
83
            if (!secTags.isEmpty()){
84
                tags.add(new TaggedText(TagEnum.separator, secSeparator));
85
                tags.addAll(secTags);
86
            }
87

    
88
        }
89
        return tags;
90
    }
91

    
92
    private List<TaggedText> getNameTags(T taxonBase) {
93
        List<TaggedText> tags = new ArrayList<>();
94
        TaxonNameBase<?,INameCacheStrategy<TaxonNameBase>> name = CdmBase.deproxy(taxonBase.getName());
95

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

    
112
        return tags;
113
    }
114

    
115
    private List<TaggedText> getSecundumTags(T taxonBase) {
116
        List<TaggedText> tags = new ArrayList<>();
117

    
118
        Reference ref = taxonBase.getSec();
119
        ref = HibernateProxyHelper.deproxy(ref, Reference.class);
120
        String secRef;
121
        if (ref == null){
122
            //missing sec
123
            if (isBlank(taxonBase.getAppendedPhrase())){
124
                secRef = "???";
125
            }else{
126
                secRef = null;
127
            }
128
        }
129
        else{
130
            //existing sec
131
            if (ref.isProtectedTitleCache() == false &&
132
                    ref.getCacheStrategy() != null &&
133
                    ref.getAuthorship() != null &&
134
                    isNotBlank(ref.getAuthorship().getTitleCache()) &&
135
                    isNotBlank(ref.getYear())){
136
                secRef = ref.getCacheStrategy().getCitation(ref);
137
            }else{
138
                secRef = ref.getTitleCache();
139
            }
140
        }
141
        if (secRef != null){
142
            tags.add(new TaggedText(TagEnum.secReference, secRef));
143
        }
144
        //secMicroReference
145
        if (StringUtils.isNotBlank(taxonBase.getSecMicroReference())){
146
            tags.add(new TaggedText(TagEnum.separator, ": "));
147
            tags.add(new TaggedText(TagEnum.secReference, taxonBase.getSecMicroReference()));
148

    
149
        }
150
        return tags;
151
    }
152

    
153

    
154
    @Override
155
    public String getTitleCache(T taxonBase, HTMLTagRules htmlTagRules) {
156
        List<TaggedText> tags = getTaggedTitle(taxonBase);
157
        if (tags == null){
158
            return null;
159
        }else{
160
            String result = TaggedCacheHelper.createString(tags, htmlTagRules);
161
            return result;
162
        }
163
    }
164
}
(2-2/3)