1
|
// $Id: TaxonBaseBeanProcessor.java 5473 2009-03-25 13:42:07Z a.kohlbecker $
|
2
|
/**
|
3
|
* Copyright (C) 2009 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
|
11
|
package eu.etaxonomy.cdm.remote.json.processor;
|
12
|
|
13
|
import java.util.ArrayList;
|
14
|
import java.util.Arrays;
|
15
|
import java.util.Date;
|
16
|
import java.util.List;
|
17
|
|
18
|
import org.apache.log4j.Logger;
|
19
|
import org.hibernate.Hibernate;
|
20
|
|
21
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
22
|
import eu.etaxonomy.cdm.model.agent.Team;
|
23
|
import eu.etaxonomy.cdm.model.name.NonViralName;
|
24
|
import eu.etaxonomy.cdm.model.name.Rank;
|
25
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
26
|
import eu.etaxonomy.cdm.model.reference.INomenclaturalReference;
|
27
|
import eu.etaxonomy.cdm.model.reference.ReferenceBase;
|
28
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
29
|
import eu.etaxonomy.cdm.remote.dto.TagEnum;
|
30
|
import eu.etaxonomy.cdm.remote.dto.TaggedText;
|
31
|
import eu.etaxonomy.cdm.strategy.cache.name.INameCacheStrategy;
|
32
|
import eu.etaxonomy.cdm.strategy.cache.name.NameCacheStrategyBase;
|
33
|
|
34
|
import net.sf.json.JSONObject;
|
35
|
import net.sf.json.JsonConfig;
|
36
|
import net.sf.json.processors.JsonBeanProcessor;
|
37
|
|
38
|
/**
|
39
|
* @author a.kohlbecker
|
40
|
*
|
41
|
*/
|
42
|
public class TaxonNameBaseBeanProcessor extends AbstractCdmBeanProcessor<TaxonNameBase> {
|
43
|
|
44
|
public static final Logger logger = Logger.getLogger(TaxonNameBaseBeanProcessor.class);
|
45
|
|
46
|
/* (non-Javadoc)
|
47
|
* @see eu.etaxonomy.cdm.remote.json.processor.AbstractCdmBeanProcessor#getIgnorePropNames()
|
48
|
*/
|
49
|
@Override
|
50
|
public List<String> getIgnorePropNames() {
|
51
|
return Arrays.asList(new String[]{
|
52
|
// ignore nameRelations to avoid LazyLoadingExceptions coming
|
53
|
// from NameRelationshipBeanProcessor.secondStep() in which
|
54
|
// the transient field fromName is added to the serialization
|
55
|
"relationsFromThisName",
|
56
|
"relationsToThisName",
|
57
|
"combinationAuthorTeam",
|
58
|
"basionymAuthorTeam",
|
59
|
"exCombinationAuthorTeam",
|
60
|
"exBasionymAuthorTeam"
|
61
|
});
|
62
|
}
|
63
|
|
64
|
/* (non-Javadoc)
|
65
|
* @see eu.etaxonomy.cdm.remote.json.processor.AbstractCdmBeanProcessor#processBeanSecondStage(java.lang.Object, net.sf.json.JSONObject, net.sf.json.JsonConfig)
|
66
|
*/
|
67
|
@Override
|
68
|
public JSONObject processBeanSecondStep(TaxonNameBase bean, JSONObject json, JsonConfig jsonConfig) {
|
69
|
if(logger.isDebugEnabled()){
|
70
|
logger.debug("processing second step" + bean);
|
71
|
}
|
72
|
json.element("taggedName", getTaggedName(bean), jsonConfig);
|
73
|
return json;
|
74
|
}
|
75
|
|
76
|
/**
|
77
|
* FIXME ugly method - this functionality mainly be performed by ChachStrategies ?
|
78
|
*
|
79
|
* @param taxonNameBase
|
80
|
* @return
|
81
|
*/
|
82
|
public static List<TaggedText> getTaggedName(TaxonNameBase<TaxonNameBase<?,?>, INameCacheStrategy<TaxonNameBase<?,?>>> taxonNameBase){
|
83
|
|
84
|
List<TaggedText> tags = new ArrayList<TaggedText>();
|
85
|
|
86
|
/**
|
87
|
* taxonNameBase.getHibernateLazyInitializer().getImplementation();
|
88
|
* class eu.etaxonomy.cdm.model.name.TaxonNameBase$$EnhancerByCGLIB$$3683183d
|
89
|
* @link( CGLIBLazyInitializer.getImplementation())
|
90
|
*/
|
91
|
// if(taxonNameBase instanceof HibernateProxy) {
|
92
|
// LazyInitializer lazyInitializer = ((HibernateProxy)taxonNameBase).getHibernateLazyInitializer();
|
93
|
// taxonNameBase = (TaxonNameBase)lazyInitializer.getImplementation();
|
94
|
// }
|
95
|
taxonNameBase = HibernateProxyHelper.deproxy(taxonNameBase, TaxonNameBase.class);
|
96
|
|
97
|
//FIXME rude hack:
|
98
|
if(!(taxonNameBase instanceof NonViralName)){
|
99
|
return tags;
|
100
|
}
|
101
|
|
102
|
// --- end of rude hack
|
103
|
//FIXME infrageneric epithets are not jet handled!
|
104
|
// - infraGenericEpithet "Cicerbita"
|
105
|
// - infraSpecificEpithet null
|
106
|
|
107
|
List<Object> taggedName = taxonNameBase.getCacheStrategy().getTaggedName(taxonNameBase);
|
108
|
|
109
|
for (Object token : taggedName){
|
110
|
TaggedText tag = new TaggedText();
|
111
|
if (String.class.isInstance(token)){
|
112
|
tag.setText((String)token);
|
113
|
tag.setType(TagEnum.name);
|
114
|
}
|
115
|
else if (Rank.class.isInstance(token)){
|
116
|
Rank r = (Rank)token;
|
117
|
tag.setText(r.getAbbreviation());
|
118
|
tag.setType(TagEnum.rank);
|
119
|
}
|
120
|
else if (Date.class.isInstance(token)){
|
121
|
Date d = (Date)token;
|
122
|
tag.setText(String.valueOf(d.getYear()));
|
123
|
tag.setType(TagEnum.year);
|
124
|
}
|
125
|
else if (Team.class.isInstance(token)){
|
126
|
Team t = (Team)token;
|
127
|
tag.setText(String.valueOf(t.getTitleCache()));
|
128
|
tag.setType(TagEnum.authors);
|
129
|
}
|
130
|
|
131
|
if (tag!=null){
|
132
|
tags.add(tag);
|
133
|
}
|
134
|
}
|
135
|
return tags;
|
136
|
}
|
137
|
|
138
|
|
139
|
|
140
|
}
|