Project

General

Profile

Download (5.25 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 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.model.common;
11

    
12
import javax.persistence.MappedSuperclass;
13
import javax.xml.bind.annotation.XmlAccessType;
14
import javax.xml.bind.annotation.XmlAccessorType;
15
import javax.xml.bind.annotation.XmlAttribute;
16
import javax.xml.bind.annotation.XmlEnum;
17
import javax.xml.bind.annotation.XmlEnumValue;
18
import javax.xml.bind.annotation.XmlRootElement;
19
import javax.xml.bind.annotation.XmlType;
20

    
21
import org.apache.log4j.Logger;
22
import org.hibernate.envers.Audited;
23

    
24
import eu.etaxonomy.cdm.model.reference.Reference;
25

    
26
/**
27
 * Concrete implementations of this abstract base class express a directed relationship between two
28
 * cdm entities ( {@link IRelated} ). The most important properties of the relationship are:
29
 * <ul>
30
 * <li>{@link #getRelatedFrom(IRelated)}</li>
31
 * <li>{@link #getRelatedTo(IRelated)}</li>
32
 * <li>The {@code <TYPE>}, a RelationshipTermBase which specifies the kind of the relationship</li>
33
 * </ul>
34
 * A relationship thus is forming a directed graph consisting of two nodes and an edge:
35
 * <pre>
36
     relatedFrom -----[TYPE]----> relatedTo
37
   </pre>
38
 * Whereas the direction of the relation can be valid for the direct (everted) and also for the inverted {@link Direction} direction.
39
 * This directional validity is defined by {@link RelationshipTermBase#isSymmetric()}
40
 *
41
 *
42
 *
43
 * @author m.doering
44
 * @author a.kohlbecker (Documentation)
45
 */
46
@XmlAccessorType(XmlAccessType.FIELD)
47
@XmlType(name = "RelationshipBase")
48
@XmlRootElement(name = "RelationshipBase")
49
@MappedSuperclass
50
@Audited
51
public abstract class RelationshipBase<FROM extends IRelated, TO extends IRelated, TYPE extends RelationshipTermBase> extends ReferencedEntityBase implements Cloneable {
52
    private static final long serialVersionUID = -5030154633820061997L;
53
    @SuppressWarnings("unused")
54
    private static final Logger logger = Logger.getLogger(RelationshipBase.class);
55

    
56
    @XmlAttribute(name = "isDoubtful")
57
    private boolean doubtful;
58

    
59

    
60
    /**
61
     * Enumeration and String representation of the <code>relatedFrom</code> (invers) and
62
     * <code>relatedTo</code> (direct, everted) bean properties. Intended to be used in the
63
     * persistence layer only.( But also used in the service layer now - a.kohlbecker )
64
     *
65
     * See also {@link RelationshipBase} for an explanation on relationships in general.
66
     */
67
    @XmlEnum
68
    public enum Direction {
69
        @XmlEnumValue("relatedFrom")
70
        relatedFrom,
71
        @XmlEnumValue("relatedTo")
72
        relatedTo
73
    }
74

    
75
    protected RelationshipBase(){
76
        super();
77
    }
78

    
79
    /**
80
     * Creates a relationship between 2 objects and adds it to the respective
81
     * relation sets of both objects.
82
     *
83
     * @param from
84
     * @param to
85
     * @param type
86
     * @param citation
87
     * @param citationMicroReference
88
     */
89
    protected RelationshipBase(FROM from, TO to, TYPE type, Reference citation, String citationMicroReference) {
90
        super(citation, citationMicroReference, null);
91
        setRelatedFrom(from);
92
        setRelatedTo(to);
93
        setType(type);
94
        from.addRelationship(this);
95
        to.addRelationship(this);
96
    }
97

    
98
    public abstract TYPE getType();
99

    
100
    public abstract void setType(TYPE type);
101

    
102
    protected abstract FROM getRelatedFrom();
103

    
104
    protected abstract void setRelatedFrom(FROM relatedFrom);
105

    
106
    protected abstract TO getRelatedTo();
107

    
108
    protected abstract void setRelatedTo(TO relatedTo);
109

    
110
    /**
111
     * A boolean flag that marks the relationship between two objects as doubtful
112
     * Please be aware that this flag should not be used to mark any status of the
113
     * objects themselves. E.g. when marking a taxon relationship as doubtful
114
     * this means that it is doubtful that the 2 taxon concept are related in
115
     * such a way. It does NOT mean that any of the taxa itself is doubtful.
116
     * @return true, if the relationship is doubtful, false otherwise
117
     */
118
    public boolean isDoubtful(){
119
        return this.doubtful;
120
    }
121

    
122
    public void setDoubtful(boolean doubtful){
123
        this.doubtful = doubtful;
124
    }
125

    
126
    public boolean isRemoved(){
127
        if ( this.getRelatedFrom() == null ^ this.getRelatedTo() == null){
128
            throw new IllegalStateException("A relationship may have only both related object as null or none. But just one is null!");
129
        }
130
        return this.getRelatedFrom() == null || this.getRelatedTo() == null;
131
    }
132

    
133

    
134
//*********************** CLONE ********************************************************/
135

    
136
    /**
137
     * Clones <i>this</i> relationship. This is a shortcut that enables to create
138
     * a new instance that differs only slightly from <i>this</i> relationship by
139
     * modifying only some of the attributes.
140
     * @throws CloneNotSupportedException
141
     *
142
     * @see eu.etaxonomy.cdm.model.common.RelationshipBase#clone()
143
     * @see java.lang.Object#clone()
144
     */
145
    @Override
146
    public Object clone() throws CloneNotSupportedException {
147
        RelationshipBase<FROM,TO,TYPE> result = (RelationshipBase<FROM,TO,TYPE>)super.clone();
148

    
149
        //no changes to: doubtful
150
        return result;
151
    }
152
}
(63-63/79)