Project

General

Profile

Download (4.42 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2009 EDIT
4
* European Distributed Institute of Taxonomy
5
* http://www.e-taxonomy.eu
6
*
7
* The contents of this file are subject to the Mozilla Public License Version 1.1
8
* See LICENSE.TXT at the top of this package for the full license terms.
9
*/
10
package eu.etaxonomy.cdm.remote.l10n;
11

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

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

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

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

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

    
35
    String label = null;
36
    String abbreviatedLabel = null;
37

    
38
    public TermRepresentation_L10n() {
39

    
40
    }
41

    
42
    public TermRepresentation_L10n(TermBase term, boolean useInverseRepresentation) {
43

    
44
        localize(term, useInverseRepresentation);
45
    }
46

    
47
    /* (non-Javadoc)
48
     * @see eu.etaxonomy.cdm.remote.l10n.ITermRepresentation_L10n#localize(eu.etaxonomy.cdm.model.common.TermBase, boolean)
49
     */
50
    @Override
51
    public void localize(TermBase term, boolean useInverseRepresentation) {
52

    
53
        List<Language> languages = LocaleContext.getLanguages();
54

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

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

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

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

    
97
            abbreviatedLabel = representation.getAbbreviatedLabel();
98
        }
99
    }
100

    
101
    /* (non-Javadoc)
102
     * @see eu.etaxonomy.cdm.remote.l10n.ITermRepresentation_L10n#getLabel()
103
     */
104
    @Override
105
    public String getLabel() {
106
        return label;
107
    }
108

    
109
    /**
110
     * @param label the label to set
111
     */
112
    public void setLabel(String label) {
113
        this.label = label;
114
    }
115

    
116
    /* (non-Javadoc)
117
     * @see eu.etaxonomy.cdm.remote.l10n.ITermRepresentation_L10n#getAbbreviatedLabel()
118
     */
119
    @Override
120
    public String getAbbreviatedLabel() {
121
        return abbreviatedLabel;
122
    }
123

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

    
131

    
132

    
133
}
(2-2/2)