Project

General

Profile

Download (5.32 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 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
package eu.etaxonomy.cdm.remote.dto.assembler;
10

    
11
import java.util.ArrayList;
12
import java.util.Collections;
13
import java.util.Date;
14
import java.util.Enumeration;
15
import java.util.HashSet;
16
import java.util.List;
17
import java.util.Locale;
18
import java.util.Set;
19

    
20
import org.springframework.beans.factory.annotation.Autowired;
21
import org.springframework.stereotype.Component;
22

    
23
import eu.etaxonomy.cdm.model.agent.Team;
24
import eu.etaxonomy.cdm.model.description.TaxonNameDescription;
25
import eu.etaxonomy.cdm.model.name.NomenclaturalStatus;
26
import eu.etaxonomy.cdm.model.name.NonViralName;
27
import eu.etaxonomy.cdm.model.name.Rank;
28
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
29
import eu.etaxonomy.cdm.model.reference.ReferenceBase;
30
import eu.etaxonomy.cdm.remote.dto.DescriptionTO;
31
import eu.etaxonomy.cdm.remote.dto.NameSTO;
32
import eu.etaxonomy.cdm.remote.dto.NameTO;
33
import eu.etaxonomy.cdm.remote.dto.TagEnum;
34
import eu.etaxonomy.cdm.remote.dto.TaggedText;
35
import eu.etaxonomy.cdm.remote.dto.assembler.LocalisedTermAssembler.TermType;
36
import eu.etaxonomy.cdm.strategy.cache.name.INameCacheStrategy;
37

    
38

    
39

    
40
@Component
41
public class NameAssembler extends AssemblerBase<NameSTO, NameTO, TaxonNameBase>{
42
	
43
	@Autowired
44
	private ReferenceAssembler refAssembler;
45
	@Autowired
46
	private LocalisedTermAssembler localisedTermAssembler;
47
	@Autowired
48
	private DescriptionAssembler descriptionAssembler;
49
	
50
	public NameSTO getSTO(TaxonNameBase taxonNameBase, Enumeration<Locale> locales){
51
		NameSTO name = null; 
52
		if (taxonNameBase !=null){
53
			name = new NameSTO();
54
			setVersionableEntity(taxonNameBase, name);
55
			name.setFullname(taxonNameBase.getTitleCache());
56
			name.setTaggedName(getTaggedName(taxonNameBase));
57
			ReferenceBase nomRef = (ReferenceBase)taxonNameBase.getNomenclaturalReference();
58
			if(nomRef != null) {
59
				name.setNomenclaturalReference(refAssembler.getSTO(nomRef, true, taxonNameBase.getNomenclaturalMicroReference(), locales));				
60
			}
61
			for (NomenclaturalStatus status : (Set<NomenclaturalStatus>)taxonNameBase.getStatus()) {
62
				locales = prependLocale(locales, new Locale("la"));
63
				name.addStatus(localisedTermAssembler.getSTO(status.getType(), locales, TermType.ABBREVLABEL));
64
			}
65
			name.setDescriptions(this.getDescriptions(taxonNameBase, locales));		
66
		}
67
		return name;
68
	}
69
	private Enumeration<Locale> prependLocale(Enumeration<Locale> locales,
70
			Locale firstLocale) {
71
		List<Locale> localeList = Collections.list(locales);
72
		localeList.add(0, firstLocale);
73
		locales = Collections.enumeration(localeList);
74
		return locales;
75
	}	
76
	public NameTO getTO(TaxonNameBase tnb, Enumeration<Locale> locales){		
77
		NameTO n = null;
78
		if (tnb !=null){
79
			n = new NameTO();
80
			setVersionableEntity(tnb, n);
81
			n.setFullname(tnb.getTitleCache());
82
			n.setTaggedName(getTaggedName(tnb));
83
			ReferenceBase nomRef = (ReferenceBase)tnb.getNomenclaturalReference();
84
			if(nomRef != null) {
85
				n.setNomenclaturalReference(refAssembler.getTO(nomRef, true ,tnb.getNomenclaturalMicroReference(), locales));
86
			}
87
			for (NomenclaturalStatus status : (Set<NomenclaturalStatus>)tnb.getStatus()) {
88
				locales = prependLocale(locales, new Locale("la"));
89
				n.addStatus(localisedTermAssembler.getSTO(status.getType(), locales));
90
			}
91
		}
92
		return n;
93
	}
94
	
95
	public Set<DescriptionTO> getDescriptions(TaxonNameBase<TaxonNameBase, INameCacheStrategy> taxonNameBase, Enumeration<Locale> locales){
96
		Set<DescriptionTO> descriptions = new HashSet<DescriptionTO>();
97

    
98
		for(TaxonNameDescription nameDescription : (Set<TaxonNameDescription>)taxonNameBase.getDescriptions()){
99
			descriptions.add(descriptionAssembler.getTO(nameDescription, locales));
100
		}
101
		
102
		return descriptions;
103
	}
104
	
105
	
106
	public List<TaggedText> getTaggedName(TaxonNameBase<TaxonNameBase, INameCacheStrategy> taxonNameBase){
107
		List<TaggedText> tags = new ArrayList<TaggedText>();
108
		//FIXME rude hack:
109
		if(!(taxonNameBase instanceof NonViralName)){
110
			return tags;
111
		}
112
		taxonNameBase = (NonViralName)taxonNameBase;
113
		// --- end of rude hack
114
		//FIXME infrageneric epithets are not jet handled!
115
		//   - infraGenericEpithet	"Cicerbita"	
116
        //   - infraSpecificEpithet	null	
117
		//	 - appended phrases are not handled
118

    
119
		List<Object> taggedName = taxonNameBase.getCacheStrategy().getTaggedName(taxonNameBase);
120
		
121
		for (Object token : taggedName){
122
			TaggedText tag = new TaggedText();
123
			if (String.class.isInstance(token)){
124
				tag.setText((String)token);
125
				tag.setType(TagEnum.name);
126
			}
127
			else if (Rank.class.isInstance(token)){
128
				Rank r = (Rank)token;
129
				tag.setText(r.getAbbreviation());
130
				tag.setType(TagEnum.rank);
131
			}
132
			else if (token !=null && ReferenceBase.class.isAssignableFrom(token.getClass())){
133
				ReferenceBase reference = (ReferenceBase) token;
134
				tag.setText(reference.getTitleCache());
135
				tag.setType(TagEnum.reference);
136
			}
137
			else if (Date.class.isInstance(token)){
138
				Date d = (Date)token;
139
				tag.setText(String.valueOf(d.getYear()));
140
				tag.setType(TagEnum.year);
141
			}
142
			else if (Team.class.isInstance(token)){
143
				Team t = (Team)token;
144
				tag.setText(String.valueOf(t.getTitleCache()));
145
				tag.setType(TagEnum.authors);
146
			}
147

    
148
			if (tag!=null){
149
				tags.add(tag);
150
			}
151
		}
152
		return tags;
153
	}
154
}
(5-5/10)