Project

General

Profile

Download (5.56 KB) Statistics
| Branch: | Tag: | Revision:
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.log4j.Logger;
16
import org.hibernate.Hibernate;
17

    
18
import eu.etaxonomy.cdm.api.service.l10n.TermRepresentation_L10n;
19
import eu.etaxonomy.cdm.model.common.RelationshipTermBase;
20
import eu.etaxonomy.cdm.model.name.Rank;
21
import eu.etaxonomy.cdm.model.term.OrderedTermVocabulary;
22
import eu.etaxonomy.cdm.model.term.TermBase;
23
import eu.etaxonomy.cdm.model.term.TermVocabulary;
24
import eu.etaxonomy.cdm.persistence.dto.ITermRepresentation_L10n;
25
import net.sf.json.JSONObject;
26
import net.sf.json.JsonConfig;
27

    
28
/**
29
 * @author a.kohlbecker
30
 *
31
 */
32
public class TermBaseBeanProcessor extends AbstractCdmBeanProcessor<TermBase> {
33

    
34
    public static final Logger logger = Logger.getLogger(TermBaseBeanProcessor.class);
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
    /**
110
     * @param json
111
     * @param representation_L10n
112
     * @param term
113
     */
114
    private void handleL10nRepresentation(JSONObject json, ITermRepresentation_L10n representation_L10n, boolean isInverse, TermBase term) {
115
        String baseLabel = isInverse? "inverseRepresentation_L10n" : "representation_L10n";
116
        if(representation_L10n.getLabel() != null || representation_L10n.getAbbreviatedLabel() != null) {
117
            if (representation_L10n.getLabel() != null) {
118
                json.element(baseLabel,representation_L10n.getLabel());
119
            }
120
            if (representation_L10n.getAbbreviatedLabel() != null) {
121
                json.element(baseLabel + "_abbreviatedLabel", representation_L10n.getAbbreviatedLabel());
122
            }
123
            if (representation_L10n.getLanguageIso() != null) {
124
                json.element(baseLabel + "_languageIso", representation_L10n.getLanguageIso());
125
            }
126
            if (representation_L10n.getLanguageUuid() != null) {
127
                json.element(baseLabel + "_languageUuid", representation_L10n.getLanguageUuid());
128
            }
129
        } else {
130
            // fall back to using the titleCache produces "eu.etaxonomy.cdm.strategy.cache.term.TermDefaultCacheStrategy@..."
131
            // therefore we create a better string here:
132
            json.element(baseLabel,term.getClass().getSimpleName() +  "<" + term.getUuid() + ">");
133
        }
134
    }
135

    
136
}
(27-27/29)