Project

General

Profile

Download (5.04 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2019 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.model.term;
10

    
11
import javax.persistence.Column;
12
import javax.persistence.Entity;
13
import javax.persistence.FetchType;
14
import javax.persistence.Index;
15
import javax.persistence.Inheritance;
16
import javax.persistence.InheritanceType;
17
import javax.persistence.ManyToOne;
18
import javax.persistence.Table;
19
import javax.validation.constraints.NotNull;
20
import javax.xml.bind.annotation.XmlAccessType;
21
import javax.xml.bind.annotation.XmlAccessorType;
22
import javax.xml.bind.annotation.XmlAttribute;
23
import javax.xml.bind.annotation.XmlElement;
24
import javax.xml.bind.annotation.XmlIDREF;
25
import javax.xml.bind.annotation.XmlSchemaType;
26
import javax.xml.bind.annotation.XmlType;
27

    
28
import org.apache.log4j.Logger;
29
import org.hibernate.annotations.Cascade;
30
import org.hibernate.annotations.CascadeType;
31
import org.hibernate.annotations.Type;
32
import org.hibernate.envers.Audited;
33

    
34
import eu.etaxonomy.cdm.model.common.CdmBase;
35
import eu.etaxonomy.cdm.model.common.VersionableEntity;
36

    
37
/**
38
 * Common base class for {@link TermNode} and {@link TermRelation}.
39
 * @author a.mueller
40
 * @since 06.03.2019
41
 */
42
@XmlAccessorType(XmlAccessType.FIELD)
43
@XmlType(name = "TermRelationBase", propOrder = {
44
        "graph",
45
        "termType",
46
        "term"
47
})
48
@Entity
49
@Audited
50
@Table(name="TermRelation", indexes = { @Index(name = "termNodeTreeIndex", columnList = "treeIndex") })  //was feature NodeTreeIndex before
51
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
52
public abstract class TermRelationBase<TERM extends DefinedTermBase, REL extends TermRelationBase, GRAPH extends TermGraphBase>
53
        extends VersionableEntity
54
        implements IHasTermType {
55

    
56
    private static final long serialVersionUID = -7832621515891195623L;
57
    @SuppressWarnings("unused")
58
    private static final Logger logger = Logger.getLogger(TermRelationBase.class);
59

    
60
    @XmlElement(name = "TermGraph")
61
    @XmlIDREF
62
    @XmlSchemaType(name = "IDREF")
63
    @ManyToOne(fetch = FetchType.LAZY, targetEntity=TermTree.class)
64
    @Cascade({CascadeType.SAVE_UPDATE,CascadeType.MERGE}) //TODO this usage is incorrect, needed only for OneToMany, check why it is here, can it be removed??
65
     //TODO Val #3379
66
//    @NotNull
67
    private GRAPH graph;
68

    
69
    /**
70
     * The {@link TermType type} of this term relation.
71
     * Must be the same type as for the {@link TermCollection term collection}
72
     * this node belongs to and as the term type of the term this node links to.
73
     */
74
    @XmlAttribute(name ="TermType")
75
    @Column(name="termType")
76
    @NotNull
77
    @Type(type = "eu.etaxonomy.cdm.hibernate.EnumUserType",
78
        parameters = {@org.hibernate.annotations.Parameter(name  = "enumClass", value = "eu.etaxonomy.cdm.model.term.TermType")}
79
    )
80
    @Audited
81
    private TermType termType;
82

    
83
    @XmlElement(name = "Term")
84
    @XmlIDREF
85
    @XmlSchemaType(name = "IDREF")
86
    @ManyToOne(fetch = FetchType.LAZY, targetEntity=DefinedTermBase.class)
87
    private TERM term;
88

    
89
 // ******************** CONSTRUCTOR ***************************************/
90

    
91
    protected TermRelationBase(){}
92

    
93
    protected TermRelationBase(TermType termType) {
94
        this.termType = termType;
95
        IHasTermType.checkTermTypeNull(this);
96
    }
97

    
98
// ********************** *****************************/
99

    
100
    @Override
101
    public TermType getTermType() {
102
        return termType;
103
    }
104

    
105
//** ********************** Term ******************************/
106

    
107
    /**
108
     * Returns the {@link DefinedTermBase term} <i>this</i> term tree node is based on.
109
     */
110
    public TERM getTerm() {
111
        return CdmBase.deproxy(term);
112
    }
113
    public void setTerm(TERM term) {
114
        checkTermTypeKindOf(term);
115
        this.term = term;
116
    }
117

    
118
    /**
119
     * Throws {@link IllegalArgumentException} if the given
120
     * term has not the same term type as this term or if it is no sub or super type
121
     * or if term type is null.
122
     * @param term
123
     */
124
    private void checkTermTypeKindOf(IHasTermType descendant) {
125
        IHasTermType.checkTermTypeEqualOrDescendant(this, descendant);
126
    }
127

    
128
//*************************** GRAPH ************************************/
129

    
130
    public GRAPH getGraph() {
131
        return graph;
132
    }
133
    protected void setGraph(GRAPH graph) {
134
        checkTermType(graph);
135
        this.graph = graph;
136
    }
137

    
138

    
139
    /**
140
     * Throws {@link IllegalArgumentException} if the given
141
     * term has not the same term type as this term or if term type is null.
142
     * @param term
143
     */
144
    protected void checkTermType(IHasTermType term) {
145
        IHasTermType.checkTermTypes(term, this);
146
    }
147

    
148
// ********************** CLONE **************************//
149

    
150
    @Override
151
    public TermRelationBase<TERM, REL, GRAPH> clone() throws CloneNotSupportedException{
152
        @SuppressWarnings("unchecked")
153
        TermRelationBase<TERM, REL, GRAPH> result = (TermRelationBase<TERM, REL, GRAPH>)super.clone();
154
        return result;
155
    }
156
}
(27-27/33)