Project

General

Profile

Download (5.04 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
package eu.etaxonomy.cdm.remote.l10n;
10

    
11
import java.util.List;
12
import java.util.Set;
13

    
14
import org.apache.log4j.Logger;
15
import org.hibernate.Hibernate;
16

    
17
import eu.etaxonomy.cdm.model.common.DefinedTerm;
18
import eu.etaxonomy.cdm.model.common.Language;
19
import eu.etaxonomy.cdm.model.common.RelationshipTermBase;
20
import eu.etaxonomy.cdm.model.common.Representation;
21
import eu.etaxonomy.cdm.model.common.TermBase;
22
import eu.etaxonomy.cdm.model.common.TermType;
23
import eu.etaxonomy.cdm.persistence.dto.ITermRepresentation_L10n;
24

    
25
/**
26
 * @author l.morris & a.kohlbecker
27
 * @since Feb 22, 2013
28
 *
29
 */
30
public class TermRepresentation_L10n implements ITermRepresentation_L10n {
31

    
32
    public static final Logger logger = Logger.getLogger(TermRepresentation_L10n.class);
33

    
34
    String label = null;
35
    String abbreviatedLabel = null;
36
    String text = null;
37
    String languageIso = null;
38
    String languageUuid = null;
39

    
40
    public TermRepresentation_L10n() {
41
    }
42

    
43
    public TermRepresentation_L10n(TermBase term, boolean useInverseRepresentation) {
44
        localize(term, useInverseRepresentation);
45
    }
46

    
47
    @Override
48
    public void localize(TermBase term, boolean useInverseRepresentation) {
49

    
50
        List<Language> languages = LocaleContext.getLanguages();
51

    
52
        if(useInverseRepresentation){
53
            RelationshipTermBase<?> relationshipTerm = (RelationshipTermBase<?>)term;
54
            if(Hibernate.isInitialized(relationshipTerm.getInverseRepresentations())){
55
                Representation representation = relationshipTerm.getPreferredInverseRepresentation(languages);
56
                setRepresentation(representation);
57
            } else {
58
                logger.debug("inverse representations of term not initialized  " + term.getUuid().toString());
59
            }
60

    
61
        } else {
62
            if(Hibernate.isInitialized(term.getRepresentations())){
63
                Representation representation = term.getPreferredRepresentation(languages);
64
                setRepresentation(representation);
65
            } else {
66
                logger.debug("representations of term not initialized  " + term.getUuid().toString());
67
            }
68
        }
69
    }
70

    
71
    @Override
72
    public void localize(Set<Representation> representations) {
73
        DefinedTerm tmpTerm = DefinedTerm.NewInstance(TermType.Unknown, null, null, null);
74
        tmpTerm.getRepresentations().clear(); // removes the null representation added throught the constructor
75
        tmpTerm.getRepresentations().addAll(representations);
76
        List<Language> languages = LocaleContext.getLanguages();
77
        Representation representation = tmpTerm.getPreferredRepresentation(languages);
78
        setRepresentation(representation);
79
    }
80

    
81
    /**
82
     * @param representation
83
     */
84
    private void setRepresentation(Representation representation) {
85
        if(representation != null){
86
            if(representation.getLabel() != null && representation.getLabel().length() != 0){
87
                label = representation.getLabel();
88
            } else if (representation.getText() != null && representation.getText().length() !=0) {
89
                label = representation.getText();
90
            } else {
91
                label = representation.getAbbreviatedLabel();
92
            }
93

    
94
            abbreviatedLabel = representation.getAbbreviatedLabel();
95

    
96
            text = representation.getText();
97

    
98
            Language lang = representation.getLanguage();
99
            if (lang != null){
100
                this.languageIso = lang.getIso639_2();
101
                if (this.languageIso == null){
102
                    this.languageIso = lang.getIso639_1();
103
                }
104
                this.languageUuid = lang.getUuid().toString();
105
            }
106
        }
107
    }
108

    
109
    @Override
110
    public String getLabel() {
111
        return label;
112
    }
113

    
114
    /**
115
     * @param label the label to set
116
     */
117
    public void setLabel(String label) {
118
        this.label = label;
119
    }
120

    
121
    @Override
122
    public String getAbbreviatedLabel() {
123
        return abbreviatedLabel;
124
    }
125

    
126
    /**
127
     * @param abbreviatedLabel the abbreviatedLabel to set
128
     */
129
    public void setAbbreviatedLabel(String abbreviatedLabel) {
130
        this.abbreviatedLabel = abbreviatedLabel;
131
    }
132

    
133
    @Override
134
    public String getText() {
135
        return text;
136
    }
137

    
138
    public void setText(String text) {
139
        this.text = text;
140
    }
141

    
142
    @Override
143
    public String getLanguageIso() {
144
        return languageIso;
145
    }
146

    
147
    public void setLanguageIso(String languageIso) {
148
        this.languageIso = languageIso;
149
    }
150

    
151
    @Override
152
    public String getLanguageUuid() {
153
        return languageUuid;
154
    }
155

    
156
    public void setLanguageUuid(String languageUuid) {
157
        this.languageUuid = languageUuid;
158
    }
159
}
(2-2/2)