2 * Copyright (C) 2009 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.remote
.json
.processor
.bean
;
12 import java
.util
.Arrays
;
13 import java
.util
.List
;
15 import org
.apache
.logging
.log4j
.LogManager
;
16 import org
.apache
.logging
.log4j
.Logger
;
17 import org
.hibernate
.Hibernate
;
19 import eu
.etaxonomy
.cdm
.api
.service
.l10n
.TermRepresentation_L10n
;
20 import eu
.etaxonomy
.cdm
.model
.common
.RelationshipTermBase
;
21 import eu
.etaxonomy
.cdm
.model
.name
.Rank
;
22 import eu
.etaxonomy
.cdm
.model
.term
.OrderedTermVocabulary
;
23 import eu
.etaxonomy
.cdm
.model
.term
.TermBase
;
24 import eu
.etaxonomy
.cdm
.model
.term
.TermVocabulary
;
25 import eu
.etaxonomy
.cdm
.persistence
.dto
.ITermRepresentation_L10n
;
26 import net
.sf
.json
.JSONObject
;
27 import net
.sf
.json
.JsonConfig
;
30 * @author a.kohlbecker
32 public class TermBaseBeanProcessor
extends AbstractCdmBeanProcessor
<TermBase
> {
34 private static final Logger logger
= LogManager
.getLogger();
36 private static final List
<String
> IGNORE_LIST
= Arrays
.asList(new String
[] {
38 "inverseRepresentations",
42 "protectedTitleCache",
47 private boolean replaceRepresentations
= false;
49 public boolean isReplaceRepresentations() {
50 return replaceRepresentations
;
53 public void setReplaceRepresentations(boolean replace
) {
54 this.replaceRepresentations
= replace
;
58 public List
<String
> getIgnorePropNames() {
63 public JSONObject
processBeanSecondStep(TermBase term
, JSONObject json
, JsonConfig jsonConfig
) {
65 // handle OrderedTermVocabulary
66 if(OrderedTermVocabulary
.class.isAssignableFrom(term
.getClass())){
67 OrderedTermVocabulary
<?
> otv
= (OrderedTermVocabulary
<?
>)term
;
68 if(Hibernate
.isInitialized(otv
.getTerms())){
69 json
.element("terms", otv
.getOrderedTerms(), jsonConfig
);
71 } else if(TermVocabulary
.class.isAssignableFrom(term
.getClass())) {
72 TermVocabulary
<?
> tv
= (TermVocabulary
<?
>)term
;
73 if(Hibernate
.isInitialized(tv
.getTerms())){
74 json
.element("terms", tv
.getTerms(), jsonConfig
);
78 ITermRepresentation_L10n representation_L10n
= new TermRepresentation_L10n(term
, false);
79 handleL10nRepresentation(json
, representation_L10n
, false, term
);
80 if(!replaceRepresentations
){
81 json
.element("representations", term
.getRepresentations(), jsonConfig
);
84 // add additional representation for RelationShipBase
85 if(RelationshipTermBase
.class.isAssignableFrom(term
.getClass())){
86 RelationshipTermBase
<?
> relTerm
= (RelationshipTermBase
<?
>)term
;
87 ITermRepresentation_L10n inverseRepresentation_L10n
= new TermRepresentation_L10n(relTerm
, true);
88 handleL10nRepresentation(json
, inverseRepresentation_L10n
, true, term
);
89 if(!replaceRepresentations
){
90 json
.element("inverseRepresentations", relTerm
.getInverseRepresentations(), jsonConfig
);
94 // add additional representation for RelationShipBase
95 if(Rank
.class.isAssignableFrom(term
.getClass())){
96 Rank rank
= (Rank
)term
;
97 json
.element("isSupraGeneric", rank
.isSupraGeneric(), jsonConfig
);
98 json
.element("isFamily", rank
.getUuid().equals(Rank
.FAMILY().getUuid()), jsonConfig
);
99 json
.element("isGenus", rank
.isGenus(), jsonConfig
);
100 json
.element("isInfraGeneric", rank
.isInfraGeneric(), jsonConfig
);
101 json
.element("isSpeciesAggregate", rank
.isSpeciesAggregate(), jsonConfig
);
102 json
.element("isSpecies", rank
.isSpecies(), jsonConfig
);
103 json
.element("isInfraSpecific", rank
.isInfraSpecific(), jsonConfig
);
109 private void handleL10nRepresentation(JSONObject json
, ITermRepresentation_L10n representation_L10n
, boolean isInverse
, TermBase term
) {
110 String baseLabel
= isInverse?
"inverseRepresentation_L10n" : "representation_L10n";
111 if(representation_L10n
.getLabel() != null || representation_L10n
.getAbbreviatedLabel() != null) {
112 if (representation_L10n
.getLabel() != null) {
113 json
.element(baseLabel
,representation_L10n
.getLabel());
115 if (representation_L10n
.getAbbreviatedLabel() != null) {
116 json
.element(baseLabel
+ "_abbreviatedLabel", representation_L10n
.getAbbreviatedLabel());
118 if (representation_L10n
.getLanguageIso() != null) {
119 json
.element(baseLabel
+ "_languageIso", representation_L10n
.getLanguageIso());
121 if (representation_L10n
.getLanguageUuid() != null) {
122 json
.element(baseLabel
+ "_languageUuid", representation_L10n
.getLanguageUuid());
125 // fall back to using the titleCache produces "eu.etaxonomy.cdm.strategy.cache.term.TermDefaultCacheStrategy@..."
126 // therefore we create a better string here:
127 json
.element(baseLabel
,term
.getClass().getSimpleName() + "<" + term
.getUuid() + ">");