Project

General

Profile

Download (6.48 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
package eu.etaxonomy.cdm.model.common;
10

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

    
20
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger;
21
import org.hibernate.envers.Audited;
22

    
23
import eu.etaxonomy.cdm.model.reference.NamedSource;
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
 * @author m.doering
42
 * @author a.kohlbecker (Documentation)
43
 */
44
@XmlAccessorType(XmlAccessType.FIELD)
45
@XmlType(name = "RelationshipBase")
46
@XmlRootElement(name = "RelationshipBase")
47
@MappedSuperclass
48
@Audited
49
public abstract class RelationshipBase<FROM extends IRelated, TO extends IRelated, TYPE extends RelationshipTermBase>
50
        extends SingleSourcedEntityBase {
51

    
52
    private static final long serialVersionUID = -5030154633820061997L;
53
    @SuppressWarnings("unused")
54
    private static final Logger logger = LogManager.getLogger(RelationshipBase.class);
55

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

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

    
73
        public Direction invers(){
74
            return (this == relatedFrom)? relatedTo: relatedFrom;
75
        }
76

    
77
        /**
78
         * Is <b>this</b> the relatedTo direction?
79
         * @return <code>true</code> if <b>this</b> is {@link Direction#relatedTo}
80
         */
81
        public boolean isDirect() {
82
            return this == relatedTo;
83
        }
84
        /**
85
         * Is <b>this</b> the relatedFrom direction?
86
         * @return <code>true</code> if <b>this</b> is {@link Direction#relatedFrom}
87
         */
88
        public boolean isInvers() {
89
            return this == relatedFrom;
90
        }
91
    }
92

    
93
    protected RelationshipBase(){
94
        super();
95
    }
96

    
97
    /**
98
     * Creates a relationship between 2 objects and adds it to the respective
99
     * relation sets of both objects.
100
     *
101
     * @param from
102
     * @param to
103
     * @param type
104
     * @param citation
105
     * @param citationMicroReference
106
     */
107
    protected RelationshipBase(FROM from, TO to, TYPE type, Reference citation, String citationMicroReference) {
108
        super(citation, citationMicroReference, null);
109
        setRelatedFrom(from);
110
        setRelatedTo(to);
111
        setType(type);
112
        from.addRelationship(this);
113
        to.addRelationship(this);
114
    }
115

    
116
    /**
117
     * Creates a relationship between 2 objects and adds it to the respective
118
     * relation sets of both objects.
119
     *
120
     * @param from
121
     * @param to
122
     * @param type
123
     * @param source
124
     */
125
    protected RelationshipBase(FROM from, TO to, TYPE type, NamedSource source) {
126
        super(source);
127
        setRelatedFrom(from);
128
        setRelatedTo(to);
129
        setType(type);
130
        from.addRelationship(this);
131
        to.addRelationship(this);
132
    }
133

    
134
    public abstract TYPE getType();
135

    
136
    public abstract void setType(TYPE type);
137

    
138
    protected abstract FROM getRelatedFrom();
139

    
140
    protected abstract void setRelatedFrom(FROM relatedFrom);
141

    
142
    protected abstract TO getRelatedTo();
143

    
144
    protected abstract void setRelatedTo(TO relatedTo);
145

    
146
    /**
147
     * A boolean flag that marks the relationship between two objects as doubtful
148
     * Please be aware that this flag should not be used to mark any status of the
149
     * objects themselves. E.g. when marking a taxon relationship as doubtful
150
     * this means that it is doubtful that the 2 taxon concept are related in
151
     * such a way. It does NOT mean that any of the taxa itself is doubtful.
152
     * @return true, if the relationship is doubtful, false otherwise
153
     */
154
    public boolean isDoubtful(){
155
        return this.doubtful;
156
    }
157

    
158
    public void setDoubtful(boolean doubtful){
159
        this.doubtful = doubtful;
160
    }
161

    
162
//    @Transient
163
//    public boolean isRemoved(){
164
//        if ( this.getRelatedFrom() == null ^ this.getRelatedTo() == null){
165
//            throw new IllegalStateException("A relationship may have only both related object as null or none. But just one is null!");
166
//        }
167
//        return this.getRelatedFrom() == null || this.getRelatedTo() == null;
168
//    }
169

    
170
//*********************** CLONE ********************************************************/
171

    
172
    /**
173
     * Clones <i>this</i> relationship. This is a shortcut that enables to create
174
     * a new instance that differs only slightly from <i>this</i> relationship by
175
     * modifying only some of the attributes.
176
     * @throws CloneNotSupportedException
177
     *
178
     * @see eu.etaxonomy.cdm.model.common.RelationshipBase#clone()
179
     * @see java.lang.Object#clone()
180
     */
181
    @Override
182
    public RelationshipBase<FROM,TO,TYPE> clone() throws CloneNotSupportedException {
183

    
184
        @SuppressWarnings("unchecked")
185
        RelationshipBase<FROM,TO,TYPE> result = (RelationshipBase<FROM,TO,TYPE>)super.clone();
186

    
187
        //no changes to: doubtful
188
        return result;
189
    }
190
}
(49-49/58)