Project

General

Profile

Download (4.17 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.model.common.RelationshipTermBase;
19
import eu.etaxonomy.cdm.model.term.OrderedTermVocabulary;
20
import eu.etaxonomy.cdm.model.term.TermBase;
21
import eu.etaxonomy.cdm.model.term.TermVocabulary;
22
import eu.etaxonomy.cdm.persistence.dto.ITermRepresentation_L10n;
23
import eu.etaxonomy.cdm.remote.l10n.TermRepresentation_L10n;
24
import net.sf.json.JSONObject;
25
import net.sf.json.JsonConfig;
26

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

    
33
    public static final Logger logger = Logger.getLogger(TermBaseBeanProcessor.class);
34

    
35
    private static final List<String> IGNORE_LIST = Arrays.asList(new String[] {
36
            "representations",
37
            "inverseRepresentations",
38
            "terms"
39
            });
40

    
41
    private boolean replaceRepresentations = false;
42

    
43
    public boolean isReplaceRepresentations() {
44
        return replaceRepresentations;
45
    }
46

    
47
    public void setReplaceRepresentations(boolean replace) {
48
        this.replaceRepresentations = replace;
49
    }
50

    
51
    @Override
52
    public List<String> getIgnorePropNames() {
53
        return IGNORE_LIST;
54
    }
55

    
56
    @Override
57
    public JSONObject processBeanSecondStep(TermBase term, JSONObject json,	JsonConfig jsonConfig) {
58

    
59
        // handle OrderedTermVocabulary
60
        if(OrderedTermVocabulary.class.isAssignableFrom(term.getClass())){
61
            OrderedTermVocabulary<?> otv = (OrderedTermVocabulary<?>)term;
62
            if(Hibernate.isInitialized(otv.getTerms())){
63
                json.element("terms", otv.getOrderedTerms(), jsonConfig);
64
            }
65
        } else if(TermVocabulary.class.isAssignableFrom(term.getClass())) {
66
            TermVocabulary<?> tv = (TermVocabulary<?>)term;
67
            if(Hibernate.isInitialized(tv.getTerms())){
68
                json.element("terms", tv.getTerms(), jsonConfig);
69
            }
70
        }
71

    
72
        ITermRepresentation_L10n representation_L10n = new TermRepresentation_L10n(term, false);
73
        handleL10nRepresentation(json, representation_L10n, false);
74
        if(!replaceRepresentations){
75
            json.element("representations", term.getRepresentations(), jsonConfig);
76
        }
77

    
78
        // add additional representation for RelationShipBase
79
        if(RelationshipTermBase.class.isAssignableFrom(term.getClass())){
80
            RelationshipTermBase<?> relTerm = (RelationshipTermBase<?>)term;
81
            ITermRepresentation_L10n inverseRepresentation_L10n = new TermRepresentation_L10n(relTerm, true);
82
            handleL10nRepresentation(json, inverseRepresentation_L10n, true);
83
            if(!replaceRepresentations){
84
                json.element("inverseRepresentations", relTerm.getInverseRepresentations(), jsonConfig);
85
            }
86
        }
87
        return json;
88
    }
89

    
90
    /**
91
     * @param json
92
     * @param representation_L10n
93
     */
94
    private void handleL10nRepresentation(JSONObject json, ITermRepresentation_L10n representation_L10n, boolean isInverse) {
95
        String baseLabel = isInverse? "inverseRepresentation_L10n" : "representation_L10n";
96
        if (representation_L10n.getLabel() != null) {
97
            json.element(baseLabel,representation_L10n.getLabel());
98
        }
99
        if (representation_L10n.getAbbreviatedLabel() != null) {
100
            json.element(baseLabel + "_abbreviatedLabel", representation_L10n.getAbbreviatedLabel());
101
        }
102
        if (representation_L10n.getAbbreviatedLabel() != null) {
103
            json.element(baseLabel + "_languageIso", representation_L10n.getLanguageIso());
104
        }
105
        if (representation_L10n.getAbbreviatedLabel() != null) {
106
            json.element(baseLabel + "_languageUuid", representation_L10n.getLanguageUuid());
107
        }
108
    }
109

    
110
}
(24-24/26)