Project

General

Profile

Download (4.18 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.reference;
10

    
11
import javax.persistence.Entity;
12
import javax.persistence.FetchType;
13
import javax.persistence.ManyToOne;
14
import javax.xml.bind.annotation.XmlAccessType;
15
import javax.xml.bind.annotation.XmlAccessorType;
16
import javax.xml.bind.annotation.XmlElement;
17
import javax.xml.bind.annotation.XmlIDREF;
18
import javax.xml.bind.annotation.XmlRootElement;
19
import javax.xml.bind.annotation.XmlSchemaType;
20
import javax.xml.bind.annotation.XmlType;
21

    
22
import org.hibernate.envers.Audited;
23

    
24
import eu.etaxonomy.cdm.model.common.CdmBase;
25
import eu.etaxonomy.cdm.model.common.CdmLinkBase;
26
import eu.etaxonomy.cdm.model.common.IntextReference;
27
import eu.etaxonomy.cdm.model.description.DescriptionBase;
28
import eu.etaxonomy.cdm.model.taxon.Taxon;
29

    
30
/**
31
 * Class to link to other CdmBase objects within the context of
32
 * {@link OriginalSourceBase information sources}.
33
 *
34
 * @author a.mueller
35
 * @since 09.11.2019
36
 */
37
@XmlAccessorType(XmlAccessType.FIELD)
38
@XmlType(name = "CdmLinkSource", propOrder = {
39
    "description"
40
})
41
@XmlRootElement(name = "CdmLinkSource")
42
@Entity
43
@Audited
44
public class CdmLinkSource extends CdmLinkBase {
45

    
46
    private static final long serialVersionUID = 6600576878001716986L;
47

    
48
// // ************* Source ***********************/
49
//
50
//    @XmlElement(name = "Source")
51
//    @XmlIDREF
52
//    @XmlSchemaType(name = "IDREF")
53
//    @OneToOne(fetch = FetchType.LAZY, mappedBy="cdmSource")
54
//    private OriginalSourceBase source;
55

    
56
// **************** Targets ************************/
57

    
58
    @XmlElement(name = "Description")
59
    @XmlIDREF
60
    @XmlSchemaType(name = "IDREF")
61
    @ManyToOne(fetch = FetchType.LAZY)
62
    private DescriptionBase<?> description;
63

    
64
// ************************ FACTORY *********************/
65

    
66
    public static CdmLinkSource NewInstance(ICdmTarget target) {
67
        return new CdmLinkSource(target);
68
    }
69

    
70
//******************* CONSTRUCTOR *********************/
71

    
72
    public CdmLinkSource(){}  //maybe protected is enough, needs to be tested for loading
73

    
74
    public CdmLinkSource(ICdmTarget target) {
75
//        this.source = source;
76
        setTarget(target);
77
    }
78

    
79
//***************** GETTER / SETTER *****************/
80

    
81
    /**
82
     * Returns the target object. Throws an {@link IllegalStateException} if no target
83
     * is defined.
84
     *
85
     * @see IntextReference#getTarget()
86
     */
87
    public ICdmTarget getTarget() {
88
        if (taxon != null){
89
            return CdmBase.deproxy(taxon, Taxon.class);
90
        }else if (description != null){
91
            return description;
92
        }else{
93
            throw new IllegalStateException("CdmSource has no target object defined");
94
        }
95
    }
96

    
97
    //maybe only workaround #9801
98
    public boolean hasNoTarget(){
99
        return taxon == null && description == null;
100
    }
101

    
102
    public void setTarget(ICdmTarget target) {
103
        target = CdmBase.deproxy(target);
104
        if (target == null){
105
            setToNull(); //workaround? #9801
106
        }else if (target instanceof DescriptionBase<?>){
107
            this.description = (DescriptionBase<?>)target;
108
        }else if (target instanceof Taxon){
109
            this.taxon = (Taxon)target;
110
        }else{
111
            throw new IllegalArgumentException("Target class not supported by CdmSource");
112
        }
113
    }
114

    
115
    //workaround? #9801
116
    private void setToNull() {
117
        this.description = null;
118
        this.taxon = null;
119
    }
120

    
121
// ********************************* CLONE **********************************/
122

    
123
    @Override
124
    public CdmLinkSource clone() throws CloneNotSupportedException {
125
        CdmLinkSource result = (CdmLinkSource)super.clone();
126

    
127
        return result;
128
    }
129

    
130
//    public CdmLinkSource clone(OriginalSourceBase source) {
131
//        CdmLinkSource result;
132
//        try {
133
//            result = (CdmLinkSource)super.clone();
134
////            result.source = source;
135
//            return result;
136
//        } catch (CloneNotSupportedException e) {
137
//            throw new RuntimeException(e);  //does not happen
138
//        }
139
//    }
140

    
141
}
(1-1/41)