Project

General

Profile

Download (5.88 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>
52
        extends ReferencedEntityBase
53
        implements Cloneable {
54

    
55
    private static final long serialVersionUID = -5030154633820061997L;
56
    @SuppressWarnings("unused")
57
    private static final Logger logger = Logger.getLogger(RelationshipBase.class);
58

    
59
    @XmlAttribute(name = "isDoubtful")
60
    private boolean doubtful;
61

    
62

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

    
77
        public Direction invers(){
78
            return (this == relatedFrom)? relatedTo: relatedFrom;
79
        }
80

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

    
97
    protected RelationshipBase(){
98
        super();
99
    }
100

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

    
120
    public abstract TYPE getType();
121

    
122
    public abstract void setType(TYPE type);
123

    
124
    protected abstract FROM getRelatedFrom();
125

    
126
    protected abstract void setRelatedFrom(FROM relatedFrom);
127

    
128
    protected abstract TO getRelatedTo();
129

    
130
    protected abstract void setRelatedTo(TO relatedTo);
131

    
132
    /**
133
     * A boolean flag that marks the relationship between two objects as doubtful
134
     * Please be aware that this flag should not be used to mark any status of the
135
     * objects themselves. E.g. when marking a taxon relationship as doubtful
136
     * this means that it is doubtful that the 2 taxon concept are related in
137
     * such a way. It does NOT mean that any of the taxa itself is doubtful.
138
     * @return true, if the relationship is doubtful, false otherwise
139
     */
140
    public boolean isDoubtful(){
141
        return this.doubtful;
142
    }
143

    
144
    public void setDoubtful(boolean doubtful){
145
        this.doubtful = doubtful;
146
    }
147

    
148
//    @Transient
149
//    public boolean isRemoved(){
150
//        if ( this.getRelatedFrom() == null ^ this.getRelatedTo() == null){
151
//            throw new IllegalStateException("A relationship may have only both related object as null or none. But just one is null!");
152
//        }
153
//        return this.getRelatedFrom() == null || this.getRelatedTo() == null;
154
//    }
155

    
156

    
157
//*********************** CLONE ********************************************************/
158

    
159
    /**
160
     * Clones <i>this</i> relationship. This is a shortcut that enables to create
161
     * a new instance that differs only slightly from <i>this</i> relationship by
162
     * modifying only some of the attributes.
163
     * @throws CloneNotSupportedException
164
     *
165
     * @see eu.etaxonomy.cdm.model.common.RelationshipBase#clone()
166
     * @see java.lang.Object#clone()
167
     */
168
    @Override
169
    public Object clone() throws CloneNotSupportedException {
170
        RelationshipBase<FROM,TO,TYPE> result = (RelationshipBase<FROM,TO,TYPE>)super.clone();
171

    
172
        //no changes to: doubtful
173
        return result;
174
    }
175
}
(63-63/80)