Project

General

Profile

Download (10.7 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.cdm.strategy.cache.taxon;
2

    
3

    
4
/**
5
* Copyright (C) 2015 EDIT
6
* European Distributed Institute of Taxonomy
7
* http://www.e-taxonomy.eu
8
*
9
* The contents of this file are subject to the Mozilla Public License Version 1.1
10
* See LICENSE.TXT at the top of this package for the full license terms.
11
*/
12

    
13

    
14

    
15
import java.util.ArrayList;
16
import java.util.List;
17
import java.util.UUID;
18

    
19
import org.apache.commons.lang.StringUtils;
20

    
21
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
22
import eu.etaxonomy.cdm.model.agent.Person;
23
import eu.etaxonomy.cdm.model.agent.Team;
24
import eu.etaxonomy.cdm.model.name.NonViralName;
25
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
26
import eu.etaxonomy.cdm.model.reference.Reference;
27
import eu.etaxonomy.cdm.model.taxon.Synonym;
28
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
29
import eu.etaxonomy.cdm.strategy.StrategyBase;
30
import eu.etaxonomy.cdm.strategy.cache.HTMLTagRules;
31
import eu.etaxonomy.cdm.strategy.cache.TagEnum;
32
import eu.etaxonomy.cdm.strategy.cache.TaggedCacheHelper;
33
import eu.etaxonomy.cdm.strategy.cache.TaggedText;
34
import eu.etaxonomy.cdm.strategy.cache.name.INameCacheStrategy;
35

    
36
public class TaxonBaseShortSecCacheStrategy<T extends TaxonBase>
37
        extends StrategyBase
38
        implements ITaxonCacheStrategy<T> {
39

    
40
    private static final long serialVersionUID = -2831618484053675222L;
41
    final static UUID uuid = UUID.fromString("931e48f0-2033-11de-8c30-0800200c9a66");
42

    
43
	@Override
44
	protected UUID getUuid() {
45
		return uuid;
46
	}
47

    
48

    
49
	@Override
50
    public String getTitleCache(T taxonBase) {
51
		String title;
52
		if (taxonBase.getName() != null && taxonBase.getName().getTitleCache() != null){
53
			title = getNamePart(taxonBase);
54
		}else{
55
		    title = "???";
56
		}
57
        boolean isSynonym = taxonBase.isInstanceOf(Synonym.class);
58
        String secSeparator =  (isSynonym? " syn." : "") + " sec. ";
59
		title += secSeparator;  //TODO check if separator is required before, e.g. for nom. status. see TaxonBaseDefaultCacheStrategy
60
		title += getSecundumPart(taxonBase);
61
		if (taxonBase.isDoubtful()){
62
			title = "?" + title;
63
		}
64
		return title;
65
	}
66

    
67
	/**
68
	 * @param taxonBase
69
	 * @param title
70
	 * @return
71
	 */
72
	private String getSecundumPart(T taxonBase) {
73
		String result = "???";
74
		Reference sec = taxonBase.getSec();
75
		sec = HibernateProxyHelper.deproxy(sec, Reference.class);
76
		if (sec != null){
77
			if (sec.isProtectedTitleCache()){
78
				return sec.getTitleCache();
79
			}
80
			if (sec.getAuthorship() != null){
81

    
82
				if (sec.getAuthorship().isInstanceOf(Team.class)){
83
					Team authorTeam = HibernateProxyHelper.deproxy(sec.getAuthorship(), Team.class);
84
					if (authorTeam.getTeamMembers().size() > 2){
85
						if (authorTeam.getTeamMembers().get(0).getLastname() != null){
86
					        result = authorTeam.getTeamMembers().get(0).getLastname() + " & al.";
87
					    } else {
88
					        result = authorTeam.getTeamMembers().get(0).getTitleCache();
89
					        result = result + " & al.";
90
					    }
91
					} else if (authorTeam.getTeamMembers().size() == 2){
92
						String firstAuthor;
93
						if (authorTeam.getTeamMembers().get(0).getLastname() != null){
94
							firstAuthor = authorTeam.getTeamMembers().get(0).getLastname();
95
						}else{
96
							firstAuthor = authorTeam.getTeamMembers().get(0).getTitleCache();
97
						}
98
						String secondAuthor;
99
						if (authorTeam.getTeamMembers().get(1).getLastname() != null){
100
							secondAuthor = authorTeam.getTeamMembers().get(1).getLastname();
101
						}else{
102
							secondAuthor = authorTeam.getTeamMembers().get(1).getTitleCache();
103
						}
104
						result = firstAuthor + " & " + secondAuthor;
105

    
106
					} else{
107
						if (authorTeam.getTeamMembers().get(0).getLastname() != null){
108
					        result = authorTeam.getTeamMembers().get(0).getLastname();
109
					    } else {
110
					        result = authorTeam.getTeamMembers().get(0).getTitleCache();
111
					    }
112
					}
113

    
114
				} else {
115
					Person author = HibernateProxyHelper.deproxy(sec.getAuthorship(), Person.class);
116
					if (author.getLastname() != null){
117
						result = author.getLastname();
118
					} else{
119
						result = author.getTitleCache();
120
					}
121
				}
122
				if (result != null){
123
					result = result.replaceAll("[A-Z]\\.", "");
124
				}
125
				if (sec.getYear() != null && result != null){
126
					result = result.concat(" (" + sec.getYear()+")");
127
				}
128
			}else{
129
				result = taxonBase.getSec().getTitleCache();
130
			}
131
		}
132
		return result;
133
	}
134

    
135
	/**
136
	 * @param name
137
	 */
138
	private String getNamePart(TaxonBase<?> taxonBase) {
139
		TaxonNameBase<?,?> nameBase = taxonBase.getName();
140
		String result = nameBase.getTitleCache();
141
		//use name cache instead of title cache if required
142
		if (taxonBase.isUseNameCache() && nameBase.isInstanceOf(NonViralName.class)){
143
			NonViralName<?> nvn = HibernateProxyHelper.deproxy(nameBase, NonViralName.class);
144
			result = nvn.getNameCache();
145
		}
146
		if (StringUtils.isNotBlank(taxonBase.getAppendedPhrase())){
147
			result = result.trim() + " " +  taxonBase.getAppendedPhrase().trim();
148
		}
149
		return result;
150
	}
151

    
152
    @Override
153
    public List<TaggedText> getTaggedTitle(T taxonBase) {
154
        if (taxonBase == null){
155
            return null;
156
        }
157

    
158
        List<TaggedText> tags = new ArrayList<TaggedText>();
159

    
160
        if (taxonBase.isProtectedTitleCache()){
161
            //protected title cache
162
            tags.add(new TaggedText(TagEnum.name, taxonBase.getTitleCache()));
163
            return tags;
164
        }else{
165
            //name
166
            TaxonNameBase<?,INameCacheStrategy<TaxonNameBase>> name = taxonBase.getName();
167
            if (name != null){
168
                //TODO
169
                List<TaggedText> nameTags = name.getCacheStrategy().getTaggedTitle(name);
170
                tags.addAll(nameTags);
171
            }
172

    
173
            //ref.
174
            List<TaggedText> secTags;
175
            Reference ref = taxonBase.getSec();
176
            ref = HibernateProxyHelper.deproxy(ref, Reference.class);
177
            if (ref != null){
178
                secTags = getSecReferenceTags(ref);
179
            }else{
180
                secTags = new ArrayList<TaggedText>();
181
                if (isBlank(taxonBase.getAppendedPhrase())){
182
                    secTags.add(new TaggedText(TagEnum.reference, "???"));
183
                }
184
            }
185
            if(! secTags.isEmpty()){
186
                //sec.
187
                boolean isSynonym = taxonBase.isInstanceOf(Synonym.class);
188
                String secSeparator =  (isSynonym? " syn." : "") + " sec. ";
189
                tags.add(new TaggedText(TagEnum.separator, secSeparator));
190
                tags.addAll(secTags);
191
            }
192
        }
193
        return tags;
194
    }
195

    
196
    /**
197
     * @param ref
198
     */
199
    private List<TaggedText> getSecReferenceTags(Reference sec) {
200
        List<TaggedText> tags = new ArrayList<TaggedText>();
201

    
202
        if (sec.isProtectedTitleCache()){
203
            tags.add(new TaggedText(TagEnum.reference, sec.getTitleCache()));
204
        }else{
205
            if (sec.getAuthorship() != null){
206
                List<TaggedText> authorTags;
207
                if (sec.getAuthorship().isInstanceOf(Team.class)){
208
                    authorTags = handleTeam(sec);
209
                } else {
210
                    authorTags = handlePerson(sec);
211
                }
212
                tags.addAll(authorTags);
213

    
214
                //FIXME why did we have this normalization? For removing first names??
215
//                if (result != null){
216
//                    result = result.replaceAll("[A-Z]\\.", "");
217
//                }
218

    
219
                //year
220
                String year = sec.getYear();
221
                if (StringUtils.isNotBlank(year) && ! authorTags.isEmpty()){
222
                    tags.add(new TaggedText(TagEnum.separator, "("));
223
                    tags.add(new TaggedText(TagEnum.year, year));
224
                    tags.add(new TaggedText(TagEnum.separator, ")"));
225
                }
226
            }else{
227

    
228
            }
229
        }
230

    
231
        return tags;
232
    }
233

    
234
    private List<TaggedText>  handlePerson(Reference sec) {
235
        List<TaggedText> tags = new ArrayList<TaggedText>();
236

    
237
        Person author = HibernateProxyHelper.deproxy(sec.getAuthorship(), Person.class);
238
        String authorStr;
239
        if (author.getLastname() != null){
240
            authorStr = author.getLastname();
241
        } else{
242
            authorStr = author.getTitleCache();
243
        }
244
        tags.add(new TaggedText(TagEnum.authors, authorStr));
245
        return tags;
246
    }
247

    
248
    private List<TaggedText> handleTeam(Reference sec) {
249
        List<TaggedText> tags = new ArrayList<TaggedText>();
250

    
251
        Team authorTeam = HibernateProxyHelper.deproxy(sec.getAuthorship(), Team.class);
252
        if (authorTeam.isProtectedTitleCache() || authorTeam.getTeamMembers().isEmpty()){
253
            String authorStr = authorTeam.getTitleCache();
254
            tags.add(new TaggedText(TagEnum.authors, authorStr));
255
        }else if (authorTeam.getTeamMembers().size() > 2){
256
            //>2 members
257
            if (authorTeam.getTeamMembers().get(0).getLastname() != null){
258
                String authorStr = authorTeam.getTeamMembers().get(0).getLastname() + " & al.";
259
                tags.add(new TaggedText(TagEnum.authors, authorStr));
260
            } else {
261
                String authorStr = authorTeam.getTeamMembers().get(0).getTitleCache();
262
                authorStr = authorStr + " & al.";
263
                tags.add(new TaggedText(TagEnum.authors, authorStr));
264
            }
265
        } else if (authorTeam.getTeamMembers().size() == 2){
266
            //2 members
267
            String firstAuthor;
268
            if (authorTeam.getTeamMembers().get(0).getLastname() != null){
269
                firstAuthor = authorTeam.getTeamMembers().get(0).getLastname();
270
            }else{
271
                firstAuthor = authorTeam.getTeamMembers().get(0).getTitleCache();
272
            }
273
            String secondAuthor;
274
            if (authorTeam.getTeamMembers().get(1).getLastname() != null){
275
                secondAuthor = authorTeam.getTeamMembers().get(1).getLastname();
276
            }else{
277
                secondAuthor = authorTeam.getTeamMembers().get(1).getTitleCache();
278
            }
279
            String authorStr = firstAuthor + " & " + secondAuthor;
280
            tags.add(new TaggedText(TagEnum.authors, authorStr));
281
        } else{
282
            //1 member
283
            String authorStr;
284
            if (authorTeam.getTeamMembers().get(0).getLastname() != null){
285
                authorStr = authorTeam.getTeamMembers().get(0).getLastname();
286
            } else {
287
                authorStr = authorTeam.getTeamMembers().get(0).getTitleCache();
288
            }
289
            tags.add(new TaggedText(TagEnum.authors, authorStr));
290
        }
291
        return tags;
292
    }
293

    
294
    @Override
295
    public String getTitleCache(T taxonBase, HTMLTagRules htmlTagRules) {
296
        List<TaggedText> tags = getTaggedTitle(taxonBase);
297
        if (tags == null){
298
            return null;
299
        }else{
300
            String result = TaggedCacheHelper.createString(tags, htmlTagRules);
301
            return result;
302
        }
303
    }
304

    
305
}
(3-3/3)