Project

General

Profile

Download (5.5 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
                //not used: we currently use a post-separator in the name tags
71
//                if (nameTags.get(nameTags.size() - 1).getType().equals(TagEnum.nomStatus)){
72
//                    secSeparator = "," + secSeparator;
73
//                }
74

    
75
                //ref.
76
                List<TaggedText> secTags = getSecundumTags(taxonBase);
77

    
78
                //sec.
79
                if (!secTags.isEmpty()){
80
                    tags.add(new TaggedText(TagEnum.separator, secSeparator));
81
                    tags.addAll(secTags);
82
                }
83

    
84
            }else{
85
                tags.add(new TaggedText(TagEnum.fullName, taxonBase.toString()));
86
            }
87
        }
88
        return tags;
89
    }
90

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

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

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

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

    
146
        }
147
        return tags;
148
    }
149

    
150

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