cleanup
[cdmlib.git] / cdmlib-remote / src / main / java / eu / etaxonomy / cdm / remote / json / processor / bean / TermBaseBeanProcessor.java
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.remote.json.processor.bean;
11
12 import java.util.Arrays;
13 import java.util.List;
14
15 import org.apache.logging.log4j.LogManager;
16 import org.apache.logging.log4j.Logger;
17 import org.hibernate.Hibernate;
18
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;
28
29 /**
30 * @author a.kohlbecker
31 */
32 public class TermBaseBeanProcessor extends AbstractCdmBeanProcessor<TermBase> {
33
34 private static final Logger logger = LogManager.getLogger();
35
36 private static final List<String> IGNORE_LIST = Arrays.asList(new String[] {
37 "representations",
38 "inverseRepresentations",
39 "terms",
40 "updated",
41 "created",
42 "protectedTitleCache",
43 "titleCache",
44 "uri"
45 });
46
47 private boolean replaceRepresentations = false;
48
49 public boolean isReplaceRepresentations() {
50 return replaceRepresentations;
51 }
52
53 public void setReplaceRepresentations(boolean replace) {
54 this.replaceRepresentations = replace;
55 }
56
57 @Override
58 public List<String> getIgnorePropNames() {
59 return IGNORE_LIST;
60 }
61
62 @Override
63 public JSONObject processBeanSecondStep(TermBase term, JSONObject json, JsonConfig jsonConfig) {
64
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);
70 }
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);
75 }
76 }
77
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);
82 }
83
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);
91 }
92 }
93
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);
104
105 }
106 return json;
107 }
108
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());
114 }
115 if (representation_L10n.getAbbreviatedLabel() != null) {
116 json.element(baseLabel + "_abbreviatedLabel", representation_L10n.getAbbreviatedLabel());
117 }
118 if (representation_L10n.getLanguageIso() != null) {
119 json.element(baseLabel + "_languageIso", representation_L10n.getLanguageIso());
120 }
121 if (representation_L10n.getLanguageUuid() != null) {
122 json.element(baseLabel + "_languageUuid", representation_L10n.getLanguageUuid());
123 }
124 } else {
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() + ">");
128 }
129 }
130 }